From 350b7fe82c54117f423fc1bc174f6f65ba855fc4 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期五, 16 十月 2020 09:56:53 +0800
Subject: [PATCH] update

---
 src/socket/net_mod_socket.c |  180 ++++++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 145 insertions(+), 35 deletions(-)

diff --git a/src/socket/net_mod_socket.c b/src/socket/net_mod_socket.c
index fdce4b5..aebd15b 100644
--- a/src/socket/net_mod_socket.c
+++ b/src/socket/net_mod_socket.c
@@ -1,9 +1,11 @@
 #include "net_mod_socket.h"
 #include "socket_io.h"
 #include "net_mod_socket_io.h"
+#include <sys/types.h>          /* See NOTES */
+#include <sys/socket.h>
 
 
-std::map<std::string, rio_t *> NetModSocket::connectionMap;
+std::map<std::string, int> NetModSocket::connectionMap;
 
 NetModSocket::NetModSocket() 
 {
@@ -12,23 +14,54 @@
 
 
 NetModSocket::~NetModSocket() {
-  
+  int clientfd;
+  for (auto map_iter = connectionMap.begin(); map_iter != connectionMap.end(); map_iter++) {
+    clientfd = map_iter->second;
+    Close(clientfd);
+     
+  }
+}
+
+int NetModSocket::connect(const char *host, int port) {
+  std::map<std::string, int>::iterator mapIter;
+  int clientfd;
+  char mapKey[256];
+  char portstr[32];
+
+  sprintf(mapKey, "%s:%d", host, port);
+  if( ( mapIter = connectionMap.find(mapKey)) != connectionMap.end()) {
+    clientfd = mapIter->second;
+  } else {
+     
+    sprintf(portstr, "%d", port);
+    clientfd = open_clientfd(host, portstr);
+    connectionMap.insert({mapKey, clientfd});
+
+  }
+  return clientfd;
+}
+
+void NetModSocket::remove_connect(const char *host, int port) {
+  std::map<std::string, int>::iterator mapIter;
+  char mapKey[256];
+  // char portstr[32];
+  sprintf(mapKey, "%s:%d", host, port);
+  connectionMap.erase(mapKey);
 }
 
 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 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_request_head_t request_head = {};
   net_mod_response_head_t response_head;
-  std::map<std::string, rio_t*>::iterator mapIter;
-  rio_t *rio;
+  int optval;
+   
+  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++) {
@@ -39,59 +72,134 @@
       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});
 
+    if( (clientfd = connect(node->host, node->port)) < 0 ) {
+      continue;
     }
 
     request_head.mod = REQ_REP;
     request_head.key = node->key;
     request_head.content_length = send_size;
-    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");
+    request_head.topic_length = 0;
+    // optval = 1;
+    // setsockopt(clientfd, IPPROTO_TCP, TCP_CORK, &optval, sizeof(optval));
+    if(rio_writen(clientfd, 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");
+      remove_connect(node->host, node->port);
+      close(clientfd);
+      continue;
 
     }
 
-    if(rio_writen(rio->rio_fd, send_buf, send_size) != send_size ) {
-       err_exit(errno, "NetModSocket::send conent rio_writen");
+    if(rio_writen(clientfd, send_buf, send_size) != send_size ) {
+      LoggerFactory::getLogger()->error(errno, "NetModSocket::send conent rio_writen");
+      remove_connect(node->host, node->port);
+      close(clientfd);
+      continue;
     }
+    // optval = 0;
+    // setsockopt(clientfd, IPPROTO_TCP, TCP_CORK, &optval, sizeof(optval));
 
-
-    if ( rio_readnb(rio, response_head_bs, NET_MODE_RESPONSE_HEAD_LENGTH) !=  NET_MODE_RESPONSE_HEAD_LENGTH) {
-      err_exit(errno, "NetModSocket::send  rio_readnb");
+    if ( rio_readn(clientfd, response_head_bs, NET_MODE_RESPONSE_HEAD_LENGTH) !=  NET_MODE_RESPONSE_HEAD_LENGTH) {
+      LoggerFactory::getLogger()->error(errno, "NetModSocket::send  rio_readnb");
+      remove_connect(node->host, node->port);
+      close(clientfd);
+      continue;
     }
 
     response_head =  NetModSocket::decode_response_head(response_head_bs);
 
     recv_buf = malloc(response_head.content_length);
     if(recv_buf == NULL) {
-      err_exit(errno, "NetModSocket::send malloc");
+      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) {
-      err_exit(errno, "NetModSocket::send  rio_readnb");
+    if ( (recv_size = rio_readn(clientfd, recv_buf, response_head.content_length) ) !=  response_head.content_length) {
+      LoggerFactory::getLogger()->error(errno, "NetModSocket::send  rio_readnb");
+      remove_connect(node->host, node->port);
+      close(clientfd);
+      continue;
     }
 
 LABEL_ARR_PUSH:
-    strcpy( ret_arr[i].host, node->host);
-    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;
-  }
-  *recv_arr = ret_arr;
-  *recv_arr_size = i;
+    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;
 
-  return i;
+    nsuc++;
+  }
+
+  *recv_arr = ret_arr;
+  if(recv_arr_size != NULL) {
+    *recv_arr_size = nsuc;
+  }
+ 
+  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;
+  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, int>::iterator mapIter;
+  int nsuc = 0;
+  for (i = 0; i< arrlen; i++) {
+
+    node = &node_arr[i];
+    if(node->host == NULL) {
+      // 鏈湴鍙戦��
+      shmModSocket.pub(topic, topic_size, content, content_size, node->key);
+     
+    } else {
+      if( (clientfd = connect(node->host, node->port)) < 0 ) {
+        continue;
+      }
+
+      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(clientfd, 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");
+        remove_connect(node->host, node->port);
+        close(clientfd);
+        continue;
+
+      }
+
+      if(rio_writen(clientfd, content, content_size) != content_size ) {
+        LoggerFactory::getLogger()->error(errno, "NetModSocket::pub rio_writen conent ");
+        remove_connect(node->host, node->port);
+        close(clientfd);
+        continue;
+      }
+
+      if(rio_writen(clientfd, topic, request_head.topic_length) != request_head.topic_length ) {
+        LoggerFactory::getLogger()->error(errno, "NetModSocket::pub rio_writen conent ");
+        remove_connect(node->host, node->port);
+        close(clientfd);
+        continue;
+      }
+    }
+
+    nsuc++;
+  }
+  return nsuc;
+}
 
 void NetModSocket::free_recv_msg_arr(net_mod_recv_msg_t * arr, size_t size) {
 
@@ -112,6 +220,7 @@
   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;
 }
 
@@ -121,6 +230,7 @@
   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));
   return head;
 }
 

--
Gitblit v1.8.0