From 34ddc11240baa01d15b24f09e39d09cb4dd15c75 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期二, 13 十月 2020 10:41:20 +0800
Subject: [PATCH] update

---
 src/socket/net_mod_socket.c |   43 ++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 38 insertions(+), 5 deletions(-)

diff --git a/src/socket/net_mod_socket.c b/src/socket/net_mod_socket.c
index 9152697..bb50c59 100644
--- a/src/socket/net_mod_socket.c
+++ b/src/socket/net_mod_socket.c
@@ -11,6 +11,10 @@
 }
 
 
+NetModSocket::~NetModSocket() {
+  
+}
+
 int NetModSocket::sendandrecv(net_node_t *node_arr, int arrlen, void *send_buf, int send_size, 
   net_mod_recv_msg_t ** recv_arr, int *recv_arr_size) {
 
@@ -20,6 +24,7 @@
   char mapKey[256];
   void *recv_buf;
   int recv_size;
+  char response_head_bs[NET_MODE_RESPONSE_HEAD_LENGTH];
   net_mod_request_head_t request_head;
   net_mod_response_head_t response_head;
   std::map<std::string, rio_t*>::iterator mapIter;
@@ -49,18 +54,21 @@
     request_head.mod = REQ_REP;
     request_head.key = node->key;
     request_head.content_length = send_size;
-    if( (n = rio_writen(rio->rio_fd, &request_head, sizeof(request_head))) != sizeof(request_head)) {
+    if(rio_writen(rio->rio_fd, NetModSocket::encode_request_head(request_head), NET_MODE_REQUEST_HEAD_LENGTH) != NET_MODE_REQUEST_HEAD_LENGTH) {
       err_exit(errno, "NetModSocket::send head rio_writen");
 
     }
 
-    if( (n = rio_writen(rio->rio_fd, send_buf, send_size)) != send_size ) {
+    if(rio_writen(rio->rio_fd, send_buf, send_size) != send_size ) {
        err_exit(errno, "NetModSocket::send conent rio_writen");
     }
 
-    if ((n = rio_readnb(rio, &response_head, sizeof(response_head))) !=  sizeof(response_head)) {
+
+    if ( rio_readnb(rio, response_head_bs, NET_MODE_RESPONSE_HEAD_LENGTH) !=  NET_MODE_RESPONSE_HEAD_LENGTH) {
       err_exit(errno, "NetModSocket::send  rio_readnb");
     }
+
+    response_head =  NetModSocket::decode_response_head(response_head_bs);
 
     recv_buf = malloc(response_head.content_length);
     if(recv_buf == NULL) {
@@ -99,7 +107,32 @@
   
 // }
 
-NetModSocket::~NetModSocket() {
-  
+void * NetModSocket::encode_request_head(net_mod_request_head_t & request) {
+  char * head = (char *)malloc(NET_MODE_REQUEST_HEAD_LENGTH);
+  PUT(head, htonl(request.mod));
+  PUT(head + 4, htonl(request.key));
+  PUT(head + 8, htonl(request.content_length));
+  return head;
 }
 
+net_mod_request_head_t  NetModSocket::decode_request_head(void *_headbs) {
+  char *headbs = (char *)_headbs;
+  net_mod_request_head_t head;
+  head.mod = ntohl(GET(headbs));
+  head.key = ntohl(GET(headbs + 4));
+  head.content_length = ntohl(GET(headbs + 8));
+  return head;
+}
+
+void * NetModSocket::encode_response_head(net_mod_response_head_t & response) {
+  char * head = (char *)malloc(NET_MODE_RESPONSE_HEAD_LENGTH);
+  PUT(head, htonl(response.content_length));
+  return head;
+}
+
+net_mod_response_head_t  NetModSocket::decode_response_head(void *_headbs) {
+  char *headbs = (char *)_headbs;
+  net_mod_response_head_t head;
+  head.content_length = ntohl(GET(headbs));
+  return head;
+}

--
Gitblit v1.8.0