From 42d1c7ef91627d5ac920c8fa35573970ac1bd2d5 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期五, 15 一月 2021 16:27:55 +0800
Subject: [PATCH] Merge branch 'dev' into rdma

---
 src/socket/net_mod_socket.cpp |  218 ++++++++++++++++++++++--------------------------------
 1 files changed, 89 insertions(+), 129 deletions(-)

diff --git a/src/socket/net_mod_socket.cpp b/src/socket/net_mod_socket.cpp
index 7eaa906..455c0a5 100644
--- a/src/socket/net_mod_socket.cpp
+++ b/src/socket/net_mod_socket.cpp
@@ -180,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;
     }
@@ -443,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;
 }
 
 /**
@@ -592,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