diff --git a/include/server.h b/include/server.h index 92cf14a..42e4bca 100644 --- a/include/server.h +++ b/include/server.h @@ -329,7 +329,7 @@ class Client{ // 请求监听列表 list req_lst; list rwd_lst; - + list ecryp_lst; // 回复处理列表 list res_lst; // 请求监听端口 diff --git a/include/type.h b/include/type.h index 86770a1..16352fc 100644 --- a/include/type.h +++ b/include/type.h @@ -26,7 +26,8 @@ #include #include #include -#include +#include +#include #include #include #include diff --git a/src/controller.cpp b/src/controller.cpp index c7ba3e2..8f49d13 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -217,7 +217,7 @@ int server(string instruct, vector &configs, vector &lconfigs, v setServerClockForSquare(&nsvr, 3); } } - while(1) sleep(1); + while(1) usleep(1000000); return 0; } @@ -297,6 +297,14 @@ int client(string instruct, vector &configs, vector &lconfigs, v // 建立客户端 Client nclt(9050); + bool if_setip = false; + string set_ip; + + if(config_search(configs, "-p")){ + set_ip = targets[0]; + printf("Set IP: %s\n",set_ip.data()); + if_setip = true; + } setClientClock(&nclt, 3); request *preq; @@ -366,7 +374,10 @@ int client(string instruct, vector &configs, vector &lconfigs, v reqdata["tag"].SetString(nclt.tag.data(),(uint32_t)nclt.name.size()); reqdata["sqe_key"].SetString(nclt.sqe_key.data(), (uint32_t)nclt.sqe_key.size()); reqdata["listen_port"].SetInt(9053); - string ip = inet_ntoa(nclt.server_cnt->GetAddr().Obj()->sin_addr); + + string ip; + if(if_setip) ip = set_ip; + else ip = inet_ntoa(nclt.server_cnt->GetAddr().Obj()->sin_addr); reqdata["listen_ip"].SetString(ip.data(),(uint32_t)ip.size()); @@ -386,18 +397,57 @@ int client(string instruct, vector &configs, vector &lconfigs, v if (!if_wait) { // 成功注册 printf("Wait for server to connect\n"); - nclt.server_cnt = new SocketTCPCServer(9053); - nclt.server_cnt->Listen(); +// 创建守护进程 - while (1) { - nclt.server_cnt->Accept(); - printf("Get connection request from server.\n"); - connection_listener *pncl = new connection_listener(); - pncl->client_addr = nclt.server_cnt->GetClientAddr(); - pncl->data_sfd = nclt.server_cnt->GetDataSFD(); - pncl->key = nclt.post_key; - pthread_create(&pncl->pid, NULL, connectionDeamon, pncl); - + int shmid = shmget((key_t)9058, 1024, 0666|IPC_CREAT); + if(shmid == -1){ + printf("SHMAT Failed.\n"); + } + pid_t fpid = fork(); + if(fpid == 0){ + printf("Client Register Deamon Has Been Created."); + nclt.server_cnt = new SocketTCPCServer(9053); + nclt.server_cnt->Listen(); +// 获得共享内存地址 + Byte *buff = (Byte *)shmat(shmid, NULL, 0); + if(shmid == -1){ + printf("SHMAT Failed.\n"); + } + while (1) { + if(!memcmp(buff, "SEND", sizeof(uint32_t))){ + printf("Get Sending Raw Data\n"); + memset(buff, 0, sizeof(uint32_t)); + } + nclt.server_cnt->Accept(); + //printf("Get connection request from server.\n"); + connection_listener *pncl = new connection_listener(); + pncl->client_addr = nclt.server_cnt->GetClientAddr(); + pncl->data_sfd = nclt.server_cnt->GetDataSFD(); + pncl->key = nclt.post_key; + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + pthread_create(&pncl->pid, &attr, connectionDeamon, pncl); + pthread_attr_destroy(&attr); + usleep(1000); + } + } + else{ +// 父进程 + int shmid = shmget((key_t)9058, 1024, 0666|IPC_CREAT); + Byte *buff = (Byte *)shmat(shmid, 0, 0); + printf("Net Command line: \n"); + while (1) { + char cmd[1024]; + printf(">"); + gets(cmd); + string cmdstr = cmd; + + if(cmdstr == "send"){ + memcpy(buff, "SEND", sizeof(uint32_t)); + } + + } } } diff --git a/src/model.cpp b/src/model.cpp index c9ad2fa..9507309 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -66,10 +66,10 @@ void registerSQECallback(respond *pres,void *args){ } } -void* connectionDeamon(void *args){ +void *connectionDeamon(void *args){ connection_listener * pcntl = (connection_listener *)args; string first_data; - printf("Start Listen Connection From Server.\n"); + //printf("Start Listen Connection From Server.\n"); char *buff = nullptr; Addr t_addr; ssize_t size = 0; @@ -91,11 +91,19 @@ void* connectionDeamon(void *args){ if_sm = true; printf("Short Connection From Server\n"); } + else if(!memcmp(&pnrwd->info, "CNTL", sizeof(uint32_t))){ + if_sm = true; + //printf("Listen Connection From Server\n"); + + ntcps.CloseConnection(); + pthread_exit(NULL); + } else{ printf("Connection illegal\n"); delete pnrwd; pthread_exit(NULL); } + } else{ printf("Connection illegal\n"); @@ -126,13 +134,17 @@ void* connectionDeamon(void *args){ } } else if(!memcmp(&pnrwd->info, "BEAT", sizeof(uint32_t))){ - printf("Connection Beated.\n"); + //printf("Connection Beated.\n"); + } + else if(!memcmp(&pnrwd->info, "SCMD", sizeof(uint32_t))){ +// 来自管理员的命令 + } Server::freeRawdataServer(*pnrwd); Server::freePcaketServer(*nppkt); } else if(size < 0){ - printf("Lost Connection From Server.\n"); + //printf("Lost Connection From Server.\n"); delete pnrwd; delete pncryp; delete nppkt; @@ -144,8 +156,8 @@ void* connectionDeamon(void *args){ delete pnrwd; delete pncryp; delete nppkt; - + usleep(10000); } + pthread_exit(NULL); } - diff --git a/src/server.cpp b/src/server.cpp index eb26020..d2890c5 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1052,9 +1052,17 @@ void *clientListener(void *args){ client_listen *pcltl = (client_listen *)args; char *buff; Addr taddr; + printf("Start listening to client.\n"); while(1){ // 如果连接断开 if(pcltl->if_connected == false) break; +// 建立新的监听连接 + pcltl->ptcps->Reconnect(); +// 说明连接类型 + raw_data nsrwd; + SQEServer::BuildSmallRawData(nsrwd, "CNTL"); + pcltl->ptcps->SendRAW(nsrwd.msg, nsrwd.msg_size); + Server::freeRawdataServer(nsrwd); ssize_t size = pcltl->ptcps->RecvRAW(&buff, taddr); if(size > 0){ if(Server::CheckRawMsg(buff, size)){ @@ -1110,7 +1118,7 @@ void *clientWaitDeamon(void *pvclt){ } printf("Get Register: %s[%s]\n",pclr->name.data(),pclr->tag.data()); -// 第一报文 +// 注册信息报文 string res_type = "{\"status\":\"ok\",\"passwd\":null}"; Document ndoc;