From f6efcc24490168992b80ac777188a5c798e198ba Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期二, 20 十月 2020 17:43:11 +0800
Subject: [PATCH] udpate

---
 src/socket/net_mod_socket.c        |   54 ++++++++++++++-------------
 test/Makefile                      |    2 
 src/socket/net_mod_socket.h        |    2 
 test/test.c                        |   10 +++--
 src/socket/net_mod_server_socket.c |    8 ++-
 5 files changed, 41 insertions(+), 35 deletions(-)

diff --git a/src/socket/net_mod_server_socket.c b/src/socket/net_mod_server_socket.c
index 9448a39..8dd7f48 100644
--- a/src/socket/net_mod_server_socket.c
+++ b/src/socket/net_mod_server_socket.c
@@ -43,7 +43,7 @@
 	socklen_t clientlen;
   struct sockaddr_storage clientaddr;
   char portstr[32];
-  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)    err_msg(errno, "signal");
+  if (Signal(SIGPIPE, SIG_IGN) == SIG_ERR)    err_msg(errno, "signal");
 
   //shmModSocket = new ShmModSocket;
   sprintf(portstr, "%d", port);
@@ -134,6 +134,7 @@
   }
 
   request_head = NetModSocket::decode_request_head(request_head_bs);
+printf("server received request from host = %s:%d\n", request_head.host, request_head.port);
 
   if(request_head.content_length > max_buf) {
    
@@ -150,18 +151,19 @@
   }
 
   if(request_head.mod == REQ_REP) {
-// printf("server response===========\n");
+ 
     memcpy(response_head.host, request_head.host, NI_MAXHOST);
     response_head.port = request_head.port;
     response_head.key = request_head.key;
     if(shmModSocket.sendandrecv_unsafe(buf, request_head.content_length, request_head.key, &recv_buf, &recv_size) != 0) {
-
+      // 娌℃湁瀵瑰簲鐨刱ey
       response_head.code = 1;
       response_head.content_length = 0;
       if( rio_writen(connfd, NetModSocket::encode_response_head(response_head), NET_MODE_RESPONSE_HEAD_LENGTH) != NET_MODE_RESPONSE_HEAD_LENGTH )
         return -1;
       //Rio_writen(connfd, recv_buf, recv_size);
     } else {
+      // 鍙戦�佹垚鍔�
       response_head.code = 0;
       response_head.content_length = recv_size;
 
diff --git a/src/socket/net_mod_socket.c b/src/socket/net_mod_socket.c
index 660c1f3..375b2e2 100644
--- a/src/socket/net_mod_socket.c
+++ b/src/socket/net_mod_socket.c
@@ -11,7 +11,7 @@
 {
 		init_req_rep_req_resp_pool();
    
-    if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)    err_msg(errno, "signal");
+    if (Signal(SIGPIPE, SIG_IGN) == SIG_ERR)    err_msg(errno, "signal");
 }
 
 
@@ -20,9 +20,7 @@
   for (auto map_iter = req_resp_pool.connectionMap.begin(); map_iter != req_resp_pool.connectionMap.end(); map_iter++) {
     clientfd = map_iter->second;
     Close(clientfd);
-     
   }
-  
 }
 
 
@@ -125,6 +123,7 @@
   net_node_t *node;
   void *recv_buf;
   int  timeout = 5 * 1000;
+  net_mod_request_head_t request_head = {};
  
   int n_req = 0, n_recv_suc = 0, n_resp;
    
@@ -151,8 +150,16 @@
       continue;
     }
 
-// printf("write_request %s:%d\n", node->host, node->port);
-    if(write_request(connfd, node->key, send_buf, send_size) != 0) {
+
+    request_head.mod = REQ_REP;
+    memcpy(request_head.host, node->host, sizeof(request_head.host));
+    request_head.port = node->port;
+    request_head.key = node->key;
+    request_head.content_length = send_size;
+
+
+ printf("write_request %s:%d\n", request_head.host, request_head.port);
+    if(write_request(connfd, request_head, send_buf, send_size) != 0) {
       LoggerFactory::getLogger()->error("write_request failture %s:%d\n", node->host, node->port);
       close_connect(connfd);
       // req_resp_pool.conns[i].fd = -1;
@@ -233,8 +240,8 @@
      
 }
 
-int NetModSocket::write_request(int clientfd, int key, void *send_buf, int send_size) {
-  net_mod_request_head_t request_head = {};
+int NetModSocket::write_request(int clientfd, net_mod_request_head_t &request_head, void *send_buf, int send_size) {
+ 
   int buf_size;
   char *buf;
   int  max_buf_size;
@@ -256,16 +263,8 @@
     }
   }
 
-  request_head.mod = REQ_REP;
-  request_head.key = key;
-  request_head.content_length = send_size;
-  request_head.topic_length = 0;
-
-  // optval = 1;
-  // setsockopt(clientfd, IPPROTO_TCP, TCP_CORK, &optval, sizeof(optval));
   memcpy(buf, NetModSocket::encode_request_head(request_head), NET_MODE_REQUEST_HEAD_LENGTH);
   memcpy(buf + NET_MODE_REQUEST_HEAD_LENGTH, send_buf, send_size);
-
 
   if(rio_writen(clientfd, buf, buf_size) != buf_size ) {
     LoggerFactory::getLogger()->error(errno, "NetModSocket::write_request  rio_writen");
@@ -293,6 +292,7 @@
   }
 
   response_head =  NetModSocket::decode_response_head(response_head_bs);
+printf(">>>> read_response %s\n", response_head.host);
   if(response_head.code != 0) {
     // 瀵规柟娌℃湁瀵瑰簲鐨刱ey
     return 1;
@@ -525,25 +525,26 @@
   uint32_t topic_length;
 */
 
-void * NetModSocket::encode_request_head(net_mod_request_head_t & request) {
+void * NetModSocket::encode_request_head(net_mod_request_head_t & head) {
   void * headbs = malloc(NET_MODE_REQUEST_HEAD_LENGTH);
   char *tmp_ptr = (char *)headbs;
-  PUT(tmp_ptr, htonl(request.mod));
+
+  PUT(tmp_ptr, htonl(head.mod));
 
   tmp_ptr += 4;
-  memcpy(tmp_ptr, request.host, NI_MAXHOST);
+  memcpy(tmp_ptr, head.host, sizeof(head.host));
 
-  tmp_ptr += NI_MAXHOST;
-  PUT(tmp_ptr, htonl(request.port));
+  tmp_ptr += sizeof(head.host);
+  PUT(tmp_ptr, htonl(head.port));
 
   tmp_ptr += 4;
-  PUT(tmp_ptr, htonl(request.key));
+  PUT(tmp_ptr, htonl(head.key));
 
   tmp_ptr += 4;
-  PUT(tmp_ptr, htonl(request.content_length));
+  PUT(tmp_ptr, htonl(head.content_length));
 
   tmp_ptr += 4;
-  PUT(tmp_ptr, htonl(request.topic_length));
+  PUT(tmp_ptr, htonl(head.topic_length));
   
   
   return headbs;
@@ -555,10 +556,11 @@
 
   head.mod = ntohl(GET(tmp_ptr));
 
-  tmp_ptr += NI_MAXHOST;
-  memcpy(head.host, tmp_ptr, NI_MAXHOST);
-
   tmp_ptr += 4;
+  memcpy(head.host, tmp_ptr, sizeof(head.host));
+
+ 
+  tmp_ptr += sizeof(head.host);
   head.port = ntohl(GET(tmp_ptr));
 
   tmp_ptr += 4;
diff --git a/src/socket/net_mod_socket.h b/src/socket/net_mod_socket.h
index 4e4394c..0988f1b 100644
--- a/src/socket/net_mod_socket.h
+++ b/src/socket/net_mod_socket.h
@@ -85,7 +85,7 @@
   int connect( net_node_t*);
   void close_connect(int connfd);
   int read_response(int clientfd, net_mod_recv_msg_t *recv_msg);
-  int write_request(int clientfd, int key, void *send_buf, int send_size);
+  int write_request(int clientfd, net_mod_request_head_t &request_head, void *send_buf, int send_size);
 
 
 public:
diff --git a/test/Makefile b/test/Makefile
index a4b59f1..f719401 100755
--- a/test/Makefile
+++ b/test/Makefile
@@ -13,7 +13,7 @@
 INCLUDES += -I${DEST}/include/shmqueue -I$(ROOT)/include/usgcommon
 
 
-PROGS = ${DEST}/is_little_endian
+PROGS = ${DEST}/test
 
 DEPENDENCES = $(patsubst %, %.d, $(PROGS)) 
 
diff --git a/test/test.c b/test/test.c
index dcb5d74..1e27d2f 100644
--- a/test/test.c
+++ b/test/test.c
@@ -5,9 +5,11 @@
 	count++;
 	return count;
 }
-int main() {
 
- 
-	printf("%d\n", test());
-	printf("%d\n", test());
+
+int main() {
+	char host[128];
+	const char * src = "192.168.20.21";
+ 	memcpy( host, src, sizeof(host));
+	printf("%s\n", host);
 }
\ No newline at end of file

--
Gitblit v1.8.0