From 2a4e4619f34a742e36693e589e0431347a72979b Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期二, 13 十月 2020 17:36:32 +0800 Subject: [PATCH] update --- src/socket/net_mod_server_socket.c | 59 ++++++++++++++++++++++++++++++++++++++++------------------- 1 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/socket/net_mod_server_socket.c b/src/socket/net_mod_server_socket.c index 1cd3838..73a2a5a 100644 --- a/src/socket/net_mod_server_socket.c +++ b/src/socket/net_mod_server_socket.c @@ -4,7 +4,7 @@ #include "net_mod_socket_io.h" #include "net_mod_socket.h" -NetModServerSocket::NetModServerSocket(int port):max_buf(1024) +NetModServerSocket::NetModServerSocket(int port):max_buf(1024), max_topic_buf(256) { char portstr[32]; @@ -17,12 +17,18 @@ if(buf == NULL) { err_exit(errno, "process_client malloc"); } + + topic_buf = malloc(max_topic_buf); + if(topic_buf == NULL) { + err_exit(errno, "process_client malloc"); + } } NetModServerSocket::~NetModServerSocket() { Close(listenfd); - fee(buf); + free(buf); + free(topic_buf); } void NetModServerSocket::start() { @@ -72,7 +78,7 @@ { /* Add connected descriptor to the pool */ pool.clientfd[i] = connfd; //line:conc:echoservers:beginaddclient - Rio_readinitb(&pool.clientrio[i], connfd); //line:conc:echoservers:endaddclient + // Rio_readinitb(&pool.clientrio[i], connfd); //line:conc:echoservers:endaddclient /* Add the descriptor to descriptor set */ FD_SET(connfd, &pool.read_set); //line:conc:echoservers:addconnfd @@ -90,8 +96,7 @@ /* $end add_client */ -int NetModServerSocket::process_client(rio_t *rio, int connfd) { - int n; +int NetModServerSocket::process_client(int connfd) { net_mod_request_head_t request_head; net_mod_response_head_t response_head; char request_head_bs[NET_MODE_REQUEST_HEAD_LENGTH]; @@ -99,15 +104,15 @@ int recv_size; - if(buf == NULL) { - buf = malloc(max_buf); - if(buf == NULL) { - err_exit(errno, "process_client malloc"); - } - } + // if(buf == NULL) { + // buf = malloc(max_buf); + // if(buf == NULL) { + // err_exit(errno, "process_client malloc"); + // } + // } - if (rio_readnb(rio, request_head_bs, NET_MODE_REQUEST_HEAD_LENGTH) != NET_MODE_REQUEST_HEAD_LENGTH) + if (rio_readn(connfd, request_head_bs, NET_MODE_REQUEST_HEAD_LENGTH) != NET_MODE_REQUEST_HEAD_LENGTH) { return -1; } @@ -118,11 +123,12 @@ buf = realloc(buf, request_head.content_length); max_buf = request_head.content_length; if(buf == NULL) { - err_exit(errno, "process_client realloc"); + LoggerFactory::getLogger()->error(errno, "process_client realloc"); + exit(1); } } - if ((n = rio_readnb(rio, buf, request_head.content_length)) != request_head.content_length ) { + if (rio_readn(connfd, buf, request_head.content_length) != request_head.content_length ) { return -1; } @@ -131,6 +137,21 @@ response_head.content_length = recv_size; Rio_writen(connfd, NetModSocket::encode_response_head(response_head), NET_MODE_RESPONSE_HEAD_LENGTH); Rio_writen(connfd, recv_buf, recv_size); + } else if(request_head.mod == BUS) { + if(request_head.topic_length > max_topic_buf) { + topic_buf = realloc(topic_buf, request_head.topic_length); + max_topic_buf = request_head.topic_length; + if(topic_buf == NULL) { + LoggerFactory::getLogger()->error(errno, "process_client realloc"); + exit(1); + } + } + + if (rio_readn(connfd, topic_buf, request_head.topic_length) != request_head.topic_length ) { + return -1; + } + LoggerFactory::getLogger()->debug("====server pub %s===\n", buf); + shmModSocket.pub((char*)topic_buf, request_head.topic_length, buf, request_head.content_length, request_head.key); } return 0; @@ -141,22 +162,22 @@ void NetModServerSocket::check_clients() { int i, connfd; - rio_t *rio; + //rio_t *rio; for (i = 0; (i <= pool.maxi) && (pool.nready > 0); i++) { connfd = pool.clientfd[i]; - rio = &pool.clientrio[i]; + //rio = &pool.clientrio[i]; /* If the descriptor is ready, echo a text line from it */ if ((connfd > 0) && (FD_ISSET(connfd, &pool.ready_set))) { pool.nready--; - if(process_client(rio, connfd) != 0) { + if(process_client(connfd) != 0) { Close(connfd); //line:conc:echoservers:closeconnfd - FD_CLR(connfd, &pool.read_set); //line:conc:echoservers:beginremove - pool.clientfd[i] = -1; //line:conc:echoservers:endremove + FD_CLR(connfd, &pool.read_set); + pool.clientfd[i] = -1; } } -- Gitblit v1.8.0