From 0d6000e8e75e45d83dfc5718bd249a11fcce45a3 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期五, 15 一月 2021 16:10:15 +0800
Subject: [PATCH] update

---
 src/socket/net_mod_socket.cpp |  253 ++++++++++++++++++++------------------------------
 1 files changed, 100 insertions(+), 153 deletions(-)

diff --git a/src/socket/net_mod_socket.cpp b/src/socket/net_mod_socket.cpp
index 2ac2dfa..455c0a5 100644
--- a/src/socket/net_mod_socket.cpp
+++ b/src/socket/net_mod_socket.cpp
@@ -67,15 +67,15 @@
 
 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);
+  return _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);
+  return _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);
+  return _sendandrecv_(node_arr,  arrlen, send_buf,send_size, recv_arr, recv_arr_size, 0);
 
 }
 
@@ -137,24 +137,11 @@
 }
 
 NetConnPool* NetModSocket::_get_pool() {
-  return gpool;
+  return _get_threadlocal_pool();
 }
+
 
 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 s, rv;
-  if ((s = pthread_mutex_lock(&sendMutex)) != 0)
-    err_exit(s, "NetModSocket : pthread_mutex_lock");
-
-  rv = _sendandrecv_unsafe(node_arr,  arrlen, send_buf,  send_size, recv_arr, recv_arr_size,   msec );
-
-  if ((s = pthread_mutex_unlock(&sendMutex)) != 0)
-    err_exit(s, "NetModSocket : pthread_mutex_lock");
-
-  return rv;
-}
-
-int NetModSocket::_sendandrecv_unsafe(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;
@@ -193,7 +180,16 @@
         ret_arr[n_recv_suc].content = recv_buf;
         ret_arr[n_recv_suc].content_length = recv_size;
         n_recv_suc++;
+      } else {
+        if(ret > EBUS_BASE) {
+          // bus_errno = EBUS_TIMEOUT;
+          logger->debug("NetModSocket:: %d _sendandrecv_ to key %d failed, %s", get_key(), node->key, bus_strerror(ret));
+           
+        } else {
+          logger->error(ret, "NetModSocket:: %d _sendandrecv_ to key %d failed", get_key(),  node->key);
+        }
       }
+
      
       continue;
     }
@@ -340,13 +336,13 @@
   // 鏈湴鍙戦��
   if(node_arr == NULL || arrlen == 0) {
     if(msec == 0) {
-      ret = shmModSocket.pub_nowait(topic, topic_size, content, content_size, node->key);
+      ret = shmModSocket.pub_nowait(topic, topic_size, content, content_size, SHM_BUS_KEY);
     } else if(msec > 0) {
       timeout.tv_sec = msec / 1000;
       timeout.tv_nsec = (msec - timeout.tv_sec * 1000) * 10e6;
-      ret = shmModSocket.pub_timeout(topic, topic_size, content, content_size, node->key, &timeout);
+      ret = shmModSocket.pub_timeout(topic, topic_size, content, content_size, SHM_BUS_KEY, &timeout);
     } else {
-      ret = shmModSocket.pub(topic, topic_size, content, content_size, node->key);
+      ret = shmModSocket.pub(topic, topic_size, content, content_size, SHM_BUS_KEY);
     }
     if(ret == 0 ) {
       n_pub_suc++;
@@ -359,13 +355,13 @@
     if(node->host == NULL) {
       // 鏈湴鍙戦��
       if(msec == 0) {
-        ret = shmModSocket.pub_nowait(topic, topic_size, content, content_size, node->key);
+        ret = shmModSocket.pub_nowait(topic, topic_size, content, content_size, SHM_BUS_KEY);
       } else if(msec > 0) {
         timeout.tv_sec = msec / 1000;
         timeout.tv_nsec = (msec - timeout.tv_sec * 1000) * 10e6;
-        ret = shmModSocket.pub_timeout(topic, topic_size, content, content_size, node->key, &timeout);
+        ret = shmModSocket.pub_timeout(topic, topic_size, content, content_size, SHM_BUS_KEY, &timeout);
       } else {
-        ret = shmModSocket.pub(topic, topic_size, content, content_size, node->key);
+        ret = shmModSocket.pub(topic, topic_size, content, content_size, SHM_BUS_KEY);
       }
 
       if(ret == 0 ) {
@@ -456,147 +452,61 @@
   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;
- 
-   
-  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++) {
-
-    node = &node_arr[i];
-    if(node->host == NULL) {
-      // 鏈湴鍙戦��
-      shmModSocket.sendandrecv(send_buf, send_size, node->key, &recv_buf, &recv_size);
-      goto LABEL_ARR_PUSH;
-    }
-
-    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;
-
-    // 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_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_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[nsuc].host, node->host);
-    } else {
-      strcpy(ret_arr[nsuc].host, "local");
-    }
-   
-    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 = nsuc;
-  }
-
-  free(buf);
-  return nsuc;
-     
-}
-
-
-
 /**
  * 鍙戦�佷俊鎭�
  * @key 鍙戦�佺粰璋�
  * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
  */
 int NetModSocket::sendto(const void *buf, const int size, const int key){
-  return shmModSocket.sendto(buf, size, key);
+  int rv = shmModSocket.sendto(buf, size, key);
+  if(rv == 0) {
+    logger->debug("NetModSocket::sendto: %d sendto %d success.\n", get_key(), key);
+    return 0;
+  }
+
+  if(rv > EBUS_BASE) {
+    // bus_errno = EBUS_TIMEOUT;
+    logger->debug("NetModSocket::sendto: %d sendto  %d failed %s", get_key(), key, bus_strerror(rv));
+  } else {
+    logger->error(rv, "NetModSocket::sendto : %d sendto  %d failed", get_key(), key);
+  }
+  return rv;
 }
 
 // 鍙戦�佷俊鎭秴鏃惰繑鍥炪�� @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 rv = shmModSocket.sendto_timeout(buf, size, key, &timeout);
+  if(rv == 0) {
+    logger->debug("NetModSocket::sendto_timeout: %d sendto %d success.\n", get_key(), key);
+    return 0;
+  }
+
+  if(rv > EBUS_BASE) {
+    // bus_errno = EBUS_TIMEOUT;
+    logger->debug("NetModSocket::sendto_timeout : %d sendto  %d failed %s", get_key(),  key, bus_strerror(rv));
+  } else {
+    logger->error(rv, "NetModSocket::sendto_timeout:  %d sendto  %d failed", get_key(),  key);
+  }
+  return rv;
 }
 
 // 鍙戦�佷俊鎭珛鍒昏繑鍥炪��
 int NetModSocket::sendto_nowait(const void *buf, const int size, const int key){
-  return shmModSocket.sendto_nowait(buf, size, key);
+  int rv = shmModSocket.sendto_nowait(buf, size, key);
+  if(rv == 0) {
+    logger->debug("NetModSocket::sendto_nowait: %d sendto %d success.\n", get_key(), key);
+    return 0;
+  }
+
+  if(rv > EBUS_BASE) {
+    // bus_errno = EBUS_TIMEOUT;
+    logger->debug("NetModSocket::sendto_nowait %d sendto  %d failed %s", get_key(), key, bus_strerror(rv));
+     
+  } else {
+    logger->error(rv, "NetModSocket::sendto_nowait %d sendto  %d failed", get_key(), key);
+  }
+  return rv;
 }
 
 /**
@@ -605,16 +515,53 @@
  * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
 */
 int NetModSocket::recvfrom(void **buf, int *size, int *key) {
-  return shmModSocket.recvfrom(buf, size, key);
+  int rv = shmModSocket.recvfrom(buf, size, key);
+
+  if(rv == 0) {
+    logger->debug("NetModSocket::recvfrom:  %d recvfrom %d success.\n", get_key(), *key);
+    return 0;
+  }
+
+  if(rv > EBUS_BASE) {
+    logger->debug("NetModSocket::recvfrom: socket %d recvfrom failed %s", get_key(), bus_strerror(rv));
+  } else {
+    logger->error(rv, "NetModSocket::recvfrom: socket %d recvfrom failed",  get_key());
+  }
+  return rv;
 }
+
 // 鎺ュ彈淇℃伅瓒呮椂杩斿洖銆� @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 rv = shmModSocket.recvfrom_timeout(buf, size, key, &timeout);
+  if(rv == 0) {
+    logger->debug("NetModSocket::recvfrom:  %d recvfrom %d success.\n", get_key(), *key);
+    return 0;
+  }
+
+  if(rv > EBUS_BASE) {
+    // bus_errno = EBUS_TIMEOUT;
+    logger->debug("NetModSocket::recvfrom_timeout:  %d recvfrom failed %s", get_key(), bus_strerror(rv));
+  } else {
+    logger->error(rv, "NetModSocket::recvfrom_timeout:  %d recvfrom failed",  get_key());
+  }
+  return rv;
 }
 
 int NetModSocket::recvfrom_nowait(void **buf, int *size, int *key){
-  return shmModSocket.recvfrom_nowait(buf, size, key);
+  int rv = shmModSocket.recvfrom_nowait(buf, size, key);
+  if(rv == 0) {
+    logger->debug("NetModSocket::recvfrom:  %d recvfrom %d success.\n", get_key(), *key);
+    return 0;
+  }
+
+  if(rv > EBUS_BASE) {
+    // bus_errno = EBUS_TIMEOUT;
+    logger->debug("NetModSocket::recvfrom_nowait:  %d recvfrom failed %s", get_key(), bus_strerror(rv));
+  } else {
+    logger->error(rv, "NetModSocket::recvfrom_nowait:  %d recvfrom failed",  get_key());
+  }
+  return rv;
 }
 
 /**

--
Gitblit v1.8.0