Added and Fixed.

This commit is contained in:
Saturneic 2019-02-21 19:02:31 +08:00
parent e1453765a4
commit 9b8587c001
5 changed files with 93 additions and 22 deletions

View File

@ -329,7 +329,7 @@ class Client{
// 请求监听列表
list<request_listener *> req_lst;
list<raw_data *> rwd_lst;
list<encrypt_post *> ecryp_lst;
// 回复处理列表
list<respond *> res_lst;
// 请求监听端口

View File

@ -26,7 +26,8 @@
#include <sys/socket.h>
#include<sys/wait.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <sys/shm.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

View File

@ -217,7 +217,7 @@ int server(string instruct, vector<string> &configs, vector<string> &lconfigs, v
setServerClockForSquare(&nsvr, 3);
}
}
while(1) sleep(1);
while(1) usleep(1000000);
return 0;
}
@ -297,6 +297,14 @@ int client(string instruct, vector<string> &configs, vector<string> &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<string> &configs, vector<string> &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<string> &configs, vector<string> &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));
}
}
}
}

View File

@ -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);
}

View File

@ -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;