From 26ed48c4e616014ee760fd13d13dbdc8539c34e3 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期二, 22 十二月 2020 19:21:55 +0800
Subject: [PATCH] 解决sendandrecv发送到一个不存在key的情况

---
 src/socket/net_mod_socket.c |  890 ++++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 762 insertions(+), 128 deletions(-)

diff --git a/src/socket/net_mod_socket.c b/src/socket/net_mod_socket.c
index 1c324d5..48b0e7c 100644
--- a/src/socket/net_mod_socket.c
+++ b/src/socket/net_mod_socket.c
@@ -1,41 +1,428 @@
 #include "net_mod_socket.h"
 #include "socket_io.h"
 #include "net_mod_socket_io.h"
+#include "net_conn_pool.h"
+#include <sys/types.h>          /* See NOTES */
+#include <sys/socket.h>
+#include <pthread.h>
+
+static Logger *logger = LoggerFactory::getLogger();
+
+static pthread_once_t once = PTHREAD_ONCE_INIT;
+static pthread_key_t poolKey;
 
 
-std::map<std::string, rio_t *> NetModSocket::connectionMap;
 
 NetModSocket::NetModSocket() 
 {
-		
+  if (Signal(SIGPIPE, SIG_IGN) == SIG_ERR)
+      logger->error(errno, "NetModSocket::NetModSocket signal");
 }
 
 
 NetModSocket::~NetModSocket() {
-  rio_t * rio;
-  for (auto map_iter = connectionMap.begin(); map_iter != connectionMap.end(); map_iter++) {
-    rio = map_iter->second;
-    Close(rio->rio_fd);
-    if(rio != NULL) {
-      free(rio);
-    }
-  }
+  
+}
+
+
+/**
+ * 缁戝畾绔彛鍒皊ocket, 濡傛灉涓嶇粦瀹氬垯绯荤粺鑷姩鍒嗛厤涓�涓�
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
+int NetModSocket::bind(int key) {
+  return shmModSocket.bind(key);
+}
+
+/**
+ * 寮哄埗缁戝畾绔彛鍒皊ocket, 閫傜敤浜庣▼搴忛潪姝e父鍏抽棴鐨勬儏鍐典笅锛岄噸鍚▼搴忕粦瀹氬師鏉ヨ繕娌¢噴鏀剧殑key
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
+int NetModSocket::force_bind( int key) {
+  return shmModSocket.force_bind(key);
 }
 
 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) {
+  _sendandrecv_(node_arr,  arrlen, send_buf,send_size, recv_arr, recv_arr_size, -1);
+}
+int NetModSocket::sendandrecv_timeout(net_node_t *node_arr, int arrlen, void *send_buf, int send_size, 
+  net_mod_recv_msg_t ** recv_arr, int *recv_arr_size, int  msec) {
+  _sendandrecv_(node_arr,  arrlen, send_buf,send_size, recv_arr, recv_arr_size, msec);
+}
+int NetModSocket::sendandrecv_nowait(net_node_t *node_arr, int arrlen, void *send_buf, int send_size, 
+  net_mod_recv_msg_t ** recv_arr, int *recv_arr_size) {
+   _sendandrecv_(node_arr,  arrlen, send_buf,send_size, recv_arr, recv_arr_size, 0);
 
-  int i, n, clientfd;
-  char portstr[32];
+}
+
+
+/* Free thread-specific data buffer */
+void NetModSocket::_destroyConnPool_(void *_pool)
+{
+
+  NetConnPool *mpool = (NetConnPool *)_pool;
+  delete mpool;
+  logger->debug("destory connPool");
+}
+
+ /* One-time key creation function */
+void NetModSocket::_createConnPoolKey_(void)
+{
+  int ret;
+
+  /* Allocate a unique thread-specific data key and save the address
+     of the destructor for thread-specific data buffers */
+
+  ret = pthread_key_create(&poolKey, _destroyConnPool_);
+  if (ret != 0) {
+    logger->error(ret, "pthread_key_create");
+    exit(1);
+  }
+}
+
+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, int  msec ) {
+
+  int i, n, recv_size, connfd;
   net_node_t *node;
-  char mapKey[256];
+  void *recv_buf = NULL;
+  
+  net_mod_request_head_t request_head = {};
+ 
+  int n_req = 0, n_recv_suc = 0, n_resp =0;
+
+   
+  net_mod_recv_msg_t *ret_arr = (net_mod_recv_msg_t *)calloc(arrlen, sizeof(net_mod_recv_msg_t));
+
+  int ret;
+  NetConnPool *mpool;
+
+  /* Make first caller allocate key for thread-specific data */
+  ret = pthread_once(&once, _createConnPoolKey_);
+  if (ret != 0) {
+    LoggerFactory::getLogger()->error(errno, "NetModSocket::_sendandrecv_ pthread_once");
+    exit(1);
+  }
+
+  mpool = (NetConnPool *)pthread_getspecific(poolKey);
+  if (mpool == NULL)
+  {
+    /* If first call from this thread, allocate buffer for thread, and save its location */
+    logger->debug("Create connPool");
+    mpool = new NetConnPool();
+    if (mpool == NULL) {
+      LoggerFactory::getLogger()->error(errno, "NetModSocket::_sendandrecv_ malloc");
+      exit(1);
+    }
+
+   
+
+    ret = pthread_setspecific(poolKey, mpool);
+    if (ret != 0) {
+      LoggerFactory::getLogger()->error(errno, "NetModSocket::_sendandrecv_ pthread_setspecific");
+      exit(1);
+    }
+  }
+  
+ 
+
+  for (i = 0; i< arrlen; i++) {
+
+    node = &node_arr[i];
+    if(node->host == NULL || strcmp(node->host, "") == 0 ) {
+      // 鏈湴鍙戦��
+      if(shmModSocket.sendandrecv(send_buf, send_size, node->key, &recv_buf, &recv_size) == 0) {
+        strcpy( ret_arr[n_recv_suc].host,"");
+        ret_arr[n_recv_suc].port = 0;
+        ret_arr[n_recv_suc].key = node->key;
+        ret_arr[n_recv_suc].content = recv_buf;
+        ret_arr[n_recv_suc].content_length = recv_size;
+        n_recv_suc++;
+      }
+     
+      continue;
+    }
+
+    if( (connfd = mpool->getConn(node->host, node->port)) < 0 ) {
+      continue;
+    }
+
+
+    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;
+    request_head.timeout = msec;
+
+ // printf("write_request %s:%d\n", request_head.host, request_head.port);
+    if(write_request(connfd, request_head, send_buf, send_size, NULL, 0) != 0) {
+      LoggerFactory::getLogger()->error("write_request failture %s:%d\n", node->host, node->port);
+      mpool->closeConn( connfd);
+    } else {
+      n_req++;
+    }
+  
+  }
+
+// printf(" mpool->maxi = %d\n",  mpool->maxi);
+// printf(" n_req = %d\n", n_req);
+
+  while(n_resp < n_req)
+  {
+    /* Wait for listening/connected descriptor(s) to become ready */
+    if( (mpool->nready = poll(mpool->conns, mpool->maxi + 1, msec) ) <= 0) {
+       // wirite_set 鍜� read_set 鍦ㄦ寚瀹氭椂闂村唴閮芥病鍑嗗濂�
+      break;
+    }
+// printf("mpool->nready =%d\n", mpool->nready);
+    for (i = 0; (i <= mpool->maxi) && (mpool->nready > 0); i++) {
+      if ( (connfd = mpool->conns[i].fd) > 0 ) {
+        /* If the descriptor is ready, echo a text line from it */
+        if (mpool->conns[i].revents & POLLIN )
+        {
+          mpool->nready--;
+// printf("POLLIN %d\n", connfd);
+          if( (n = read_response(connfd, ret_arr+n_recv_suc)) == 0) {
+            n_recv_suc++;
+            // 鎴愬姛鏀跺埌杩斿洖娑堟伅锛屾竻绌鸿鍏ヤ綅
+            mpool->conns[i].fd = -1;
+            
+          }
+          else if(n == -1)  {
+            // 缃戠粶閿欒
+            mpool->closeConn( connfd);
+            // mpool->conns[i].fd = -1;
+          } else {
+            // 瀵规柟key鏄叧闂殑
+             mpool->conns[i].fd = -1;
+          }
+
+          n_resp++;
+// printf("read response %d\n", n);
+          
+        }
+
+        if (mpool->conns[i].revents & POLLOUT ) {
+  // printf("poll POLLOUT %d\n", connfd);        
+        }
+
+        if (mpool->conns[i].revents & (POLLRDHUP | POLLHUP | POLLERR) )
+        {
+// printf("poll POLLERR %d\n", connfd);
+          mpool->nready--;
+          mpool->closeConn( connfd);
+          // mpool->conns[i].fd = -1;
+        }
+      }
+    }
+  }
+
+   //瓒呮椂鍚庯紝鍏抽棴瓒呮椂杩炴帴
+  for (i = 0; i <= mpool->maxi; i++) {
+    if ( (connfd = mpool->conns[i].fd) > 0 ) {
+      // 鍏抽棴骞舵竻闄ゅ啓鍏ユ垨璇诲彇澶辫触鐨勮繛鎺�
+      mpool->closeConn( connfd);
+      // mpool->conns[i].fd = -1;
+    }
+  }
+
+  mpool->maxi = -1;
+
+  *recv_arr = ret_arr;
+  if(recv_arr_size != NULL) {
+    *recv_arr_size = n_recv_suc;
+  }
+  return n_recv_suc;
+     
+}
+
+
+void NetModSocket::free_recv_msg_arr(net_mod_recv_msg_t * arr, size_t size) {
+
+  for(int i =0; i< size; i++) {
+    if(arr[i].content != NULL)
+      free(arr[i].content);
+  }
+  free(arr);
+}
+
+
+int NetModSocket::pub(net_node_t *node_arr, int arrlen, char *topic, int topic_size, void *content, int content_size) {
+  return _pub_(node_arr, arrlen, topic, topic_size, content,   content_size, -1);
+}
+
+int NetModSocket::pub_nowait(net_node_t *node_arr, int arrlen, char *topic, int topic_size, void *content, int content_size) {
+  return _pub_(node_arr, arrlen, topic, topic_size, content,   content_size, 0);
+}
+
+int NetModSocket::pub_timeout(net_node_t *node_arr, int arrlen, char *topic, int topic_size, void *content, int content_size, int  msec ) {
+  return _pub_(node_arr, arrlen, topic, topic_size, content, content_size, msec);
+}
+
+
+// int  pub(char *topic, int topic_size, void *content, int content_size, int port);
+
+int NetModSocket::_pub_(net_node_t *node_arr, int arrlen, char *topic, int topic_size, void *content,
+ int content_size, int  timeout) {
+  int i, connfd;
+  net_node_t *node;
+ 
+  net_mod_request_head_t request_head;
+  net_mod_recv_msg_t recv_msg;
+  char portstr[32];
+  int n_req = 0, n_pub_suc = 0, n_resp = 0;
+
+  int ret;
+  NetConnPool *mpool;
+
+  /* Make first caller allocate key for thread-specific data */
+  ret = pthread_once(&once, _createConnPoolKey_);
+  if (ret != 0) {
+    LoggerFactory::getLogger()->error(errno, "NetModSocket::_sendandrecv_ pthread_once");
+    exit(1);
+  }
+
+  mpool = (NetConnPool *)pthread_getspecific(poolKey);
+  if (mpool == NULL)
+  {
+    /* If first call from this thread, allocte buffer for thread, and save its location */
+    mpool = new NetConnPool();
+    if (mpool == NULL) {
+      LoggerFactory::getLogger()->error(errno, "NetModSocket::_sendandrecv_ malloc");
+      exit(1);
+    }
+
+    ret = pthread_setspecific(poolKey, mpool);
+    if (ret != 0) {
+      LoggerFactory::getLogger()->error(errno, "NetModSocket::_sendandrecv_ pthread_setspecific");
+      exit(1);
+    }
+  }
+
+  // 鏈湴鍙戦��
+  if(node_arr == NULL || arrlen == 0) {
+    if(shmModSocket.pub(topic, topic_size, content, content_size, node->key) == 0 ) {
+      n_pub_suc++;
+    }
+  }
+
+  for (i = 0; i < arrlen; i++) {
+
+    node = &node_arr[i];
+    if(node->host == NULL) {
+      // 鏈湴鍙戦��
+      if(shmModSocket.pub(topic, topic_size, content, content_size, node->key) == 0 ) {
+         n_pub_suc++;
+      }
+     
+    } else {
+      sprintf(portstr, "%d", node->port);
+      if( (connfd = mpool->getConn(node->host, node->port)) < 0 ) {
+        continue;
+      }
+      request_head.mod = BUS;
+      memcpy(request_head.host, node->host, sizeof(request_head.host));
+      request_head.key = node->key;
+      request_head.content_length = content_size;
+      request_head.topic_length = strlen(topic) + 1;
+      request_head.timeout = timeout;
+
+      if(write_request(connfd, request_head, content, content_size, topic, request_head.topic_length) != 0) {
+        LoggerFactory::getLogger()->error(" NetModSocket::_pub_ write_request failture %s:%d\n", node->host, node->port);
+        mpool->closeConn( connfd);
+      } else {
+        n_req++;
+      }
+      
+    }
+  }
+
+  while(n_resp < n_req)
+  {
+    /* Wait for listening/connected descriptor(s) to become ready */
+    if( (mpool->nready = poll(mpool->conns, mpool->maxi + 1, timeout) ) <= 0) {
+       // wirite_set 鍜� read_set 鍦ㄦ寚瀹氭椂闂村唴閮芥病鍑嗗濂�
+      break;
+    }
+// printf("mpool->nready =%d\n", mpool->nready);
+    for (i = 0; (i <= mpool->maxi) && (mpool->nready > 0); i++) {
+      if ( (connfd = mpool->conns[i].fd) > 0 ) {
+        if (mpool->conns[i].revents & POLLIN )
+        {
+          mpool->nready--;
+// printf("POLLIN %d\n", connfd);
+          if( (ret = read_response(connfd, &recv_msg)) == 0) {
+            
+            // 鎴愬姛鏀跺埌杩斿洖娑堟伅锛屾竻绌鸿鍏ヤ綅
+            mpool->conns[i].fd = -1;
+            n_pub_suc++;
+          } 
+          else if(ret == -1)  {
+            // 缃戠粶杩炴帴閿欒
+             mpool->closeConn( connfd);
+          } else {
+            // 瀵规柟鐨刱ey鏄叧闂殑
+            mpool->conns[i].fd = -1;
+          }
+          n_resp++;
+// printf("read response %d\n", n);
+        }
+
+        if (mpool->conns[i].revents & POLLOUT ) {
+  // printf("poll POLLOUT %d\n", connfd);        
+        }
+
+        if (mpool->conns[i].revents & (POLLRDHUP | POLLHUP | POLLERR) )
+        {
+// printf("poll POLLERR %d\n", connfd);
+          mpool->nready--;
+          
+          mpool->conns[i].fd = -1;
+          mpool->closeConn( connfd);
+        }
+      }
+    }
+  }
+
+  //瓒呮椂鍚庯紝鍏抽棴瓒呮椂杩炴帴
+  for (i = 0; i <= mpool->maxi; i++) {
+    if ( (connfd = mpool->conns[i].fd) > 0 ) {
+      // 鍏抽棴骞舵竻闄ゅ啓鍏ユ垨璇诲彇澶辫触鐨勮繛鎺�
+      mpool->closeConn( connfd);
+      // mpool->conns[i].fd = -1;
+    }
+  }
+
+  mpool->maxi = -1;
+   
+  return n_pub_suc;
+}
+
+
+int NetModSocket::sendandrecv_safe(net_node_t *node_arr, int arrlen, void *send_buf, int send_size, 
+  net_mod_recv_msg_t ** recv_arr, int *recv_arr_size) {
+
+  int i,  clientfd;
+  net_node_t *node;
   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;
-  rio_t *rio;
+ 
+   
+  char portstr[32];
+  char *buf = NULL;
+  int buf_size, max_buf_size;
+ 
+  if(buf == NULL) {
+    buf = (char *)malloc(MAXBUF);
+    max_buf_size = MAXBUF;
+    LoggerFactory::getLogger()->error(errno, "NetModSocket::sendandrecv malloc");
+  }
+
+  int nsuc = 0;
   net_mod_recv_msg_t *ret_arr = (net_mod_recv_msg_t *)calloc(arrlen, sizeof(net_mod_recv_msg_t));
   
   for (i = 0; i< arrlen; i++) {
@@ -46,171 +433,418 @@
       shmModSocket.sendandrecv(send_buf, send_size, node->key, &recv_buf, &recv_size);
       goto LABEL_ARR_PUSH;
     }
-    sprintf(mapKey, "%s:%d", node->host, node->port);
-    if( ( mapIter = connectionMap.find(mapKey)) != connectionMap.end()) {
-      rio = mapIter->second;
-    } else {
-      rio = (rio_t *)malloc(sizeof(rio_t));
-      sprintf(portstr, "%d", node->port);
-      clientfd = Open_clientfd(node-> host, portstr);
-      Rio_readinitb(rio, clientfd);
-      connectionMap.insert({mapKey, rio});
 
+    sprintf(portstr, "%d", node->port);
+    clientfd = open_clientfd(node->host, portstr);
+    if(clientfd < 0) {
+      continue;
     }
+
+    buf_size = send_size + NET_MODE_REQUEST_HEAD_LENGTH;
+    if(max_buf_size < buf_size) {
+      if((buf = (char *)realloc(buf, buf_size)) == NULL) {
+        LoggerFactory::getLogger()->error(errno, "NetModSocket::sendandrecv_safe realloc buf");
+      } else {
+        max_buf_size = buf_size;
+      }
+      
+    }
+    
 
     request_head.mod = REQ_REP;
     request_head.key = node->key;
     request_head.content_length = send_size;
     request_head.topic_length = 0;
-    if(rio_writen(rio->rio_fd, NetModSocket::encode_request_head(request_head), NET_MODE_REQUEST_HEAD_LENGTH) != NET_MODE_REQUEST_HEAD_LENGTH) {
-      LoggerFactory::getLogger()->error(errno, "NetModSocket::send head rio_writen");
-      exit(1);
 
+    // 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::sendandrecv_safe  rio_writen  buf");
+     
+      close(clientfd);
+      continue;
     }
+    // optval = 0;
+    // setsockopt(clientfd, IPPROTO_TCP, TCP_CORK, &optval, sizeof(optval));
 
-    if(rio_writen(rio->rio_fd, send_buf, send_size) != send_size ) {
-      LoggerFactory::getLogger()->error(errno, "NetModSocket::send conent rio_writen");
-      exit(1);
-    }
-
-
-    if ( rio_readnb(rio, response_head_bs, NET_MODE_RESPONSE_HEAD_LENGTH) !=  NET_MODE_RESPONSE_HEAD_LENGTH) {
-      LoggerFactory::getLogger()->error(errno, "NetModSocket::send  rio_readnb");
-      exit(1);
+    if ( rio_readn(clientfd, response_head_bs, NET_MODE_RESPONSE_HEAD_LENGTH) !=  NET_MODE_RESPONSE_HEAD_LENGTH) {
+      LoggerFactory::getLogger()->error(errno, "NetModSocket::sendandrecv_safe  rio_readnb response_head_bs");
+     
+      close(clientfd);
+      continue;
     }
 
     response_head =  NetModSocket::decode_response_head(response_head_bs);
+    if(response_head.code != 0) {
+      continue;
+    }
 
     recv_buf = malloc(response_head.content_length);
     if(recv_buf == NULL) {
       LoggerFactory::getLogger()->error(errno, "NetModSocket::send malloc");
       exit(1);
     }
-    if ( (recv_size = rio_readnb(rio, recv_buf, response_head.content_length) ) !=  response_head.content_length) {
-      LoggerFactory::getLogger()->error(errno, "NetModSocket::send  rio_readnb");
-      exit(1);
+    if ( (recv_size = rio_readn(clientfd, recv_buf, response_head.content_length) ) !=  response_head.content_length) {
+      LoggerFactory::getLogger()->error(errno, "NetModSocket::sendandrecv_safe  rio_readnb recv_buf");
+      
+      close(clientfd);
+      continue;
     }
 
 LABEL_ARR_PUSH:
     if(node->host != NULL) {
-      strcpy( ret_arr[i].host, node->host);
+      strcpy(ret_arr[nsuc].host, node->host);
     } else {
-       strcpy( ret_arr[i].host, "local");
+      strcpy(ret_arr[nsuc].host, "local");
     }
    
-    ret_arr[i].port = node->port;
-    ret_arr[i].key = node->key;
-    ret_arr[i].content = recv_buf;
-    ret_arr[i].content_length = recv_size;
+    ret_arr[nsuc].port = node->port;
+    ret_arr[nsuc].key = node->key;
+    ret_arr[nsuc].content = recv_buf;
+    ret_arr[nsuc].content_length = recv_size;
+
+    nsuc++;
   }
+
   *recv_arr = ret_arr;
   if(recv_arr_size != NULL) {
-    *recv_arr_size = i;
+    *recv_arr_size = nsuc;
   }
- 
-  return i;
+
+  free(buf);
+  return nsuc;
      
 }
 
-// int  pub(char *topic, int topic_size, void *content, int content_size, int port);
 
-int NetModSocket::pub(net_node_t *node_arr, int arrlen, char *topic, int topic_size, void *content, int content_size) {
-  int i, n, clientfd;
-  char portstr[32];
-  net_node_t *node;
-  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;
-  rio_t *rio;
-  for (i = 0; i< arrlen; i++) {
 
-    node = &node_arr[i];
-    if(node->host == NULL) {
-      // 鏈湴鍙戦��
-      shmModSocket.pub(topic, topic_size, content, content_size, node->key);
-      
+/**
+ * 鍙戦�佷俊鎭�
+ * @key 鍙戦�佺粰璋�
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+ */
+int NetModSocket::sendto(const void *buf, const int size, const int key){
+  return shmModSocket.sendto(buf, size, key);
+}
+
+// 鍙戦�佷俊鎭秴鏃惰繑鍥炪�� @sec 绉� 锛� @nsec 绾崇
+int NetModSocket::sendto_timeout(const void *buf, const int size, const int key, int sec, int nsec){
+  struct timespec timeout = {sec, nsec};
+  return shmModSocket.sendto_timeout(buf, size, key, &timeout);
+}
+
+// 鍙戦�佷俊鎭珛鍒昏繑鍥炪��
+int NetModSocket::sendto_nowait(const void *buf, const int size, const int key){
+  return shmModSocket.sendto_nowait(buf, size, key);
+}
+
+/**
+ * 鎺ユ敹淇℃伅
+ * @key 浠庤皝鍝噷鏀跺埌鐨勪俊鎭�
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
+int NetModSocket::recvfrom(void **buf, int *size, int *key) {
+  return shmModSocket.recvfrom(buf, size, key);
+}
+// 鎺ュ彈淇℃伅瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇
+int NetModSocket::recvfrom_timeout(void **buf, int *size, int *key, int sec, int nsec){
+  struct timespec timeout = {sec, nsec};
+  return shmModSocket.recvfrom_timeout(buf, size, key, &timeout);
+}
+
+int NetModSocket::recvfrom_nowait(void **buf, int *size, int *key){
+  return shmModSocket.recvfrom_nowait(buf, size, key);
+}
+
+/**
+ * 鍙戦�佽姹備俊鎭苟绛夊緟鎺ユ敹搴旂瓟
+ * @key 鍙戦�佺粰璋�
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
+int NetModSocket::sendandrecv( const void *send_buf, const int send_size, const int key, void **recv_buf, int *recv_size){
+  return shmModSocket.sendandrecv(send_buf, send_size, key, recv_buf, recv_size);
+}
+// 瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇
+int NetModSocket::sendandrecv_timeout( const void *send_buf, const int send_size, const int key, void **recv_buf, int *recv_size, int sec, int nsec){
+  struct timespec timeout = {sec, nsec};
+  return shmModSocket.sendandrecv_timeout(send_buf, send_size, key, recv_buf, recv_size, &timeout);
+}
+int NetModSocket::sendandrecv_nowait( const void *send_buf, const int send_size, const int key, void **recv_buf, int *recv_size) {
+  return shmModSocket.sendandrecv_nowait(send_buf, send_size, key, recv_buf, recv_size);
+}
+
+
+/**
+ * 璁㈤槄鎸囧畾涓婚
+ * @topic 涓婚
+ * @size 涓婚闀垮害
+ * @key 鎬荤嚎绔彛
+ */
+int  NetModSocket::sub( void *topic, int size, int key){
+  return shmModSocket.sub((char *)topic,  size,  key);
+}
+// 瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇
+int  NetModSocket::sub_timeout( void *topic, int size, int key, int sec, int nsec){
+  struct timespec timeout = {sec, nsec};
+  return shmModSocket.sub_timeout((char *)topic,  size,  key, &timeout);
+}
+int  NetModSocket::sub_nowait( void *topic, int size, int key){
+  return shmModSocket.sub_nowait((char *)topic,  size,  key);
+}
+
+
+
+/**
+ * 鍙栨秷璁㈤槄鎸囧畾涓婚
+ * @topic 涓婚
+ * @size 涓婚闀垮害
+ * @key 鎬荤嚎绔彛
+ */
+int  NetModSocket::desub( void *topic, int size, int key){
+  return shmModSocket.desub((char *)topic,  size,  key);
+}
+// 瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇
+int  NetModSocket::desub_timeout( void *topic, int size, int key, int sec, int nsec){
+  struct timespec timeout = {sec, nsec};
+  return shmModSocket.desub_timeout((char *)topic,  size,  key, &timeout);
+}
+int  NetModSocket::desub_nowait( void *topic, int size, int key){
+  return shmModSocket.desub_nowait((char *)topic,  size,  key);
+}
+
+
+
+/**
+ * 鍙戝竷涓婚
+ * @topic 涓婚
+ * @content 涓婚鍐呭
+ * @key 鎬荤嚎绔彛
+ */
+int  NetModSocket::pub( char *topic, int topic_size, void *content, int content_size, int key){
+  return shmModSocket.pub(topic, topic_size, content, content_size, key);
+}
+//  瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇
+int  NetModSocket::pub_timeout( char *topic, int topic_size, void *content, int content_size, int key, int sec, int nsec){
+  struct timespec timeout = {sec, nsec};
+  return shmModSocket.pub_timeout(topic, topic_size, content, content_size, key, &timeout);
+}
+int  NetModSocket::pub_nowait( char *topic, int topic_size, void *content, int content_size, int key){
+  return shmModSocket.pub_nowait(topic, topic_size, content, content_size, key);
+}
+
+
+/**
+ * 鑾峰彇soket绔彛鍙�
+ */
+int NetModSocket::get_key() {
+  return shmModSocket.get_key();
+}
+
+
+
+
+
+//======================================================================================
+
+int NetModSocket::write_request(int clientfd, net_mod_request_head_t &request_head, 
+  void *content_buf, int content_size, void *topic_buf, int topic_size) {
+ 
+  int buf_size;
+  char *buf;
+  int  max_buf_size;
+  if((buf = (char *)malloc(MAXBUF)) == NULL) {
+    LoggerFactory::getLogger()->error(errno, "NetModSocket::NetModSocket malloc");
+    exit(1);
+  } else {
+    max_buf_size = MAXBUF;
+  }
+
+  buf_size = NET_MODE_REQUEST_HEAD_LENGTH + content_size + topic_size  ;
+  if(max_buf_size < buf_size) {
+    
+    if((buf = (char *)realloc(buf, buf_size)) == NULL) {
+      LoggerFactory::getLogger()->error(errno, "NetModSocket::write_request  realloc buf");
+      exit(1);
     } else {
-      sprintf(mapKey, "%s:%d", node->host, node->port);
-      if( ( mapIter = connectionMap.find(mapKey)) != connectionMap.end()) {
-        rio = mapIter->second;
-      } else {
-        rio = (rio_t *)malloc(sizeof(rio_t));
-        sprintf(portstr, "%d", node->port);
-        clientfd = Open_clientfd(node-> host, portstr);
-        Rio_readinitb(rio, clientfd);
-        connectionMap.insert({mapKey, rio});
-      }
-
-      request_head.mod = BUS;
-      request_head.key = node->key;
-      request_head.content_length = content_size;
-      request_head.topic_length = strlen(topic) + 1;
-      if(rio_writen(rio->rio_fd, NetModSocket::encode_request_head(request_head), NET_MODE_REQUEST_HEAD_LENGTH) != NET_MODE_REQUEST_HEAD_LENGTH) {
-        LoggerFactory::getLogger()->error(errno, "NetModSocket::pub head rio_writen");
-        exit(1);
-
-      }
-
-      if(rio_writen(rio->rio_fd, content, content_size) != content_size ) {
-        LoggerFactory::getLogger()->error(errno, "NetModSocket::pub rio_writen conent ");
-        exit(1);
-      }
-
-      if(rio_writen(rio->rio_fd, topic, request_head.topic_length) != request_head.topic_length ) {
-        LoggerFactory::getLogger()->error(errno, "NetModSocket::pub rio_writen conent ");
-        exit(1);
-      }
+      max_buf_size = buf_size;
     }
   }
-  return i;
-}
 
-void NetModSocket::free_recv_msg_arr(net_mod_recv_msg_t * arr, size_t size) {
+  memcpy(buf, NetModSocket::encode_request_head(request_head), NET_MODE_REQUEST_HEAD_LENGTH);
+  memcpy(buf + NET_MODE_REQUEST_HEAD_LENGTH, content_buf, content_size);
+  if(topic_size != 0 ) 
+    memcpy(buf + NET_MODE_REQUEST_HEAD_LENGTH + content_size, topic_buf, topic_size);
 
-  for(int i =0; i< size; i++) {
-    free(arr[i].content);
+  if(rio_writen(clientfd, buf, buf_size) != buf_size ) {
+    LoggerFactory::getLogger()->error(errno, "NetModSocket::write_request  rio_writen");
+    free(buf);
+    return -1;
   }
-  free(arr);
+  free(buf);
+  return 0;
 }
 
-// ssize_t recv(void *buf, size_t len) {
+/**
+ * @return 0 鎴愬姛, 1 瀵规柟娌℃湁瀵瑰簲鐨刱ey, -1 缃戠粶閿欒
+ *
+ */
+int NetModSocket::read_response(int connfd, net_mod_recv_msg_t *recv_msg) {
+  int recv_size;
+  void *recv_buf;
+  char response_head_bs[NET_MODE_RESPONSE_HEAD_LENGTH];
+ 
+  net_mod_response_head_t response_head;
+  if ( rio_readn(connfd, response_head_bs, NET_MODE_RESPONSE_HEAD_LENGTH) !=  NET_MODE_RESPONSE_HEAD_LENGTH) {
+    LoggerFactory::getLogger()->error(errno, "NetModSocket::read_response  rio_readnb response_head");
+    
+    return -1;
+  }
 
-// 	return rio_readlineb(&rio, buf, MAXLINE);
+  response_head =  NetModSocket::decode_response_head(response_head_bs);
+// printf(">>>> read_response %s\n", response_head.host);
+  if(response_head.code != 0) {
+    // 浠g悊鏈嶅姟娌¤兘鎴愬姛鍙戦�佺粰瀵瑰簲鐨刱ey
+    return 1;
+  }
+
+  recv_buf = malloc(response_head.content_length);
+  if(recv_buf == NULL) {
+    LoggerFactory::getLogger()->error(errno, "NetModSocket::send malloc recv_buf");
+    exit(1);
+  }
+  if ( (recv_size = rio_readn(connfd, recv_buf, response_head.content_length) ) !=  response_head.content_length) {
+    LoggerFactory::getLogger()->error(errno, "NetModSocket::read_response  rio_readnb recv_buf");
+    
+    return -1;
+  }
+
+  strcpy( recv_msg->host, response_head.host);
+  recv_msg->port = response_head.port;
+  recv_msg->key = response_head.key;
+  recv_msg->content = recv_buf;
+  recv_msg->content_length = recv_size;
+  return 0;
+}
+
+
+
+
+
+/**
+  uint32_t mod;
+  char host[NI_MAXHOST];
+  uint32_t port;
+  uint32_t key;
+  uint32_t content_length;
+  uint32_t topic_length;
+*/
+
+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(head.mod));
+
+  tmp_ptr += 4;
+  memcpy(tmp_ptr, head.host, sizeof(head.host));
+
+  tmp_ptr += sizeof(head.host);
+  PUT(tmp_ptr, htonl(head.port));
+
+  tmp_ptr += 4;
+  PUT(tmp_ptr, htonl(head.key));
+
+  tmp_ptr += 4;
+  PUT(tmp_ptr, htonl(head.content_length));
+
+  tmp_ptr += 4;
+  PUT(tmp_ptr, htonl(head.topic_length));
+
+  tmp_ptr += 4;
+  PUT_INT32(tmp_ptr, htonl(head.timeout));
   
-// }
-
-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));
-  PUT(head + 12, htonl(request.topic_length));
-  return head;
+  
+  return headbs;
 }
 
-net_mod_request_head_t  NetModSocket::decode_request_head(void *_headbs) {
-  char *headbs = (char *)_headbs;
+net_mod_request_head_t  NetModSocket::decode_request_head(void *headbs) {
+  char *tmp_ptr = (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));
-  head.topic_length = ntohl(GET(headbs + 12));
+
+  head.mod = ntohl(GET(tmp_ptr));
+
+  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;
+  head.key = ntohl(GET(tmp_ptr));
+
+  tmp_ptr += 4;
+  head.content_length = ntohl(GET(tmp_ptr));
+
+  tmp_ptr += 4;
+  head.topic_length = ntohl(GET(tmp_ptr));
+
+  tmp_ptr += 4;
+  head.timeout = ntohl(GET_INT32(tmp_ptr));
+ 
   return head;
 }
 
+/**
+ char host[NI_MAXHOST];
+  uint32_t port;
+  uint32_t key;
+  uint32_t content_length;
+  uint32_t code;
+  */
 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));
+  void * headbs = malloc(NET_MODE_RESPONSE_HEAD_LENGTH);
+  char *tmp_ptr = (char *)headbs;
+
+  memcpy(tmp_ptr, response.host, NI_MAXHOST);
+
+  tmp_ptr += NI_MAXHOST;
+  PUT(tmp_ptr, htonl(response.port));
+
+  tmp_ptr += 4;
+  PUT(tmp_ptr , htonl(response.key));
+
+  tmp_ptr += 4;
+  PUT(tmp_ptr, htonl(response.content_length));
+
+  tmp_ptr += 4;
+  PUT(tmp_ptr, htonl(response.code));
+
+  return headbs;
+}
+
+net_mod_response_head_t  NetModSocket::decode_response_head(void *headbs) {
+  char *tmp_ptr = (char *)headbs;
+  net_mod_response_head_t head;
+
+  memcpy(head.host, tmp_ptr, NI_MAXHOST);
+
+  tmp_ptr += NI_MAXHOST;
+  head.port = ntohl(GET(tmp_ptr));
+
+  tmp_ptr += 4;
+  head.key = ntohl(GET(tmp_ptr));
+
+  tmp_ptr += 4;
+  head.content_length = ntohl(GET(tmp_ptr));
+
+  tmp_ptr += 4;
+  head.code = ntohl(GET(tmp_ptr));
+
   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