From 758438289fc45829a8f6cef1b42afed0a1a8cb60 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期三, 03 二月 2021 15:59:58 +0800
Subject: [PATCH] uuid msg

---
 src/socket/shm_mod_socket.h             |    5 
 test/sole_demo.cpp                      |    4 
 src/net/net_mod_socket_wrapper.cpp      |   24 ++
 src/net/net_mod_socket_wrapper.h        |   72 ++++++-
 src/net/net_mod_socket.h                |   10 
 test_net_socket/test_net_mod_socket.cpp |   29 ++
 src/socket/shm_socket.h                 |   18 +
 src/socket/shm_mod_socket.cpp           |   13 +
 src/net/net_mod_socket.cpp              |   20 ++
 src/socket/shm_socket.cpp               |  386 +++++++++++++++++++++++++++-----------
 10 files changed, 433 insertions(+), 148 deletions(-)

diff --git a/src/net/net_mod_socket.cpp b/src/net/net_mod_socket.cpp
index 3668b14..f939c67 100644
--- a/src/net/net_mod_socket.cpp
+++ b/src/net/net_mod_socket.cpp
@@ -141,6 +141,8 @@
 }
 
 
+
+
 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 ) {
 
@@ -495,6 +497,24 @@
   return shmModSocket.recvfrom(buf, size, key, NULL, BUS_NOWAIT_FLAG);
 }
 
+
+
+int NetModSocket::recvandsend(void **recvbuf, int *recvsize, int *key, recv_callback_fn callback) {
+  return shmModSocket.recvandsend( recvbuf, recvsize, key, callback);
+
+}
+
+int NetModSocket::recvandsend_timeout(void **recvbuf, int *recvsize, int *key, recv_callback_fn callback,
+                              const struct timespec *timeout ) {
+  return shmModSocket.recvandsend( recvbuf, recvsize, key, callback, timeout, BUS_TIMEOUT_FLAG);
+
+}
+
+int NetModSocket::recvandsend_nowait(void **recvbuf, int *recvsize, int *key, recv_callback_fn callback) {
+  return shmModSocket.recvandsend( recvbuf, recvsize, key, callback, NULL,  BUS_NOWAIT_FLAG);
+
+}
+
 /**
  * 鍙戦�佽姹備俊鎭苟绛夊緟鎺ユ敹搴旂瓟
  * @key 鍙戦�佺粰璋�
diff --git a/src/net/net_mod_socket.h b/src/net/net_mod_socket.h
index 612f784..5b2e930 100644
--- a/src/net/net_mod_socket.h
+++ b/src/net/net_mod_socket.h
@@ -192,8 +192,14 @@
   int sendandrecv_timeout( const void *send_buf, const int send_size, const int send_key, void **recv_buf, int *recv_size, int sec, int nsec) ;
   int sendandrecv_nowait( const void *send_buf, const int send_size, const int send_key, void **recv_buf, int *recv_size) ;
 
- 
-   /**
+
+  int recvandsend(void **recvbuf, int *recvsize, int *key, recv_callback_fn callback) ;
+
+  int recvandsend_timeout(void **recvbuf, int *recvsize, int *key, recv_callback_fn callback,
+                                        const struct timespec *timeout );
+  int recvandsend_nowait(void **recvbuf, int *recvsize, int *key, recv_callback_fn callback);
+
+  /**
    * 鍚憂ode_arr 涓殑鎵�鏈夌綉缁滆妭鐐瑰彂甯冩秷鎭�
    * @node_arr 缃戠粶鑺傜偣缁�, @node_arr_len璇ユ暟缁勯暱搴�
    * @topic 涓婚锛孈topic_size 璇ヤ富棰樼殑闀垮害
diff --git a/src/net/net_mod_socket_wrapper.cpp b/src/net/net_mod_socket_wrapper.cpp
index 57073f0..6f244af 100644
--- a/src/net/net_mod_socket_wrapper.cpp
+++ b/src/net/net_mod_socket_wrapper.cpp
@@ -90,11 +90,34 @@
 	return sockt->recvfrom_nowait(buf, size, key);
 }
 
+
+int net_mod_socket_recvandsend(void *_socket, void **recvbuf, int *recvsize, int *key, recv_callback_fn callback) {
+  NetModSocket *sockt = (NetModSocket *)_socket;
+  return sockt->recvandsend( recvbuf, recvsize, key, callback);
+
+}
+
+int net_mod_socket_recvandsend_timeout(void *_socket, void **recvbuf, int *recvsize, int *key, recv_callback_fn callback,
+                                       int sec, int nsec ) {
+  NetModSocket *sockt = (NetModSocket *)_socket;
+  struct timespec timeout = {sec, nsec};
+  return sockt->recvandsend_timeout( recvbuf, recvsize, key, callback, &timeout);
+}
+
+int net_mod_socket_recvandsend_nowait(void *_socket, void **recvbuf, int *recvsize, int *key, recv_callback_fn callback) {
+  NetModSocket *sockt = (NetModSocket *)_socket;
+  return sockt->recvandsend_nowait( recvbuf, recvsize, key, callback);
+}
+
+
 int net_mod_socket_sendandrecv(void *_socket, net_node_t *node_arr, int arrlen, void *send_buf, int send_size, 
   net_mod_recv_msg_t ** recv_arr, int *recv_arr_size){
 	NetModSocket *sockt = (NetModSocket *)_socket;
 	return sockt->sendandrecv(node_arr,  arrlen, send_buf,  send_size, recv_arr, recv_arr_size);
 }
+
+
+
 
 /**
  * 濡傛灉寤虹珛杩炴帴鐨勮妭鐐规病鏈夋帴鍙楀埌娑堟伅绛夊緟timeout鐨勬椂闂村悗杩斿洖
@@ -113,7 +136,6 @@
 	return sockt->sendandrecv_nowait(node_arr,  arrlen, send_buf,  send_size, recv_arr, recv_arr_size);
 }
 
- 
 
  /**
  * 鍚憂ode_arr 涓殑鎵�鏈夌綉缁滆妭鐐瑰彂甯冩秷鎭�
diff --git a/src/net/net_mod_socket_wrapper.h b/src/net/net_mod_socket_wrapper.h
index b794545..2f73c7b 100644
--- a/src/net/net_mod_socket_wrapper.h
+++ b/src/net/net_mod_socket_wrapper.h
@@ -120,6 +120,54 @@
  */
 int net_mod_socket_recvfrom_nowait(void *_socket, void **buf, int *size, int *key);
 
+
+
+/**
+ * @brief 鎺ュ彈娑堟伅锛屽苟鎶奵allback鍑芥暟杩斿洖鐨勬暟鎹彂閫佸洖瀵规柟锛屼竴鐩寸瓑寰呭畬鎴�
+ *
+ * @param recvbuf 鎺ュ彈鍒扮殑娑堟伅瀛樻斁鐨勭紦瀛樺湴鍧�锛岃buf浣跨敤瀹屾垚鍚庨渶瑕佹墜鍔ㄩ噴鏀�
+ * @param recvsize 鎺ュ彈鍒版秷鎭殑闀垮害
+ * @param key 浠庤皝鍝噷鏀跺埌鐨勪俊鎭�
+ * @callback  void (*recv_callback_fn)(void **sendbuf, int *sendsize)
+ *            sendbuf 鍜� sendsize鏄痗allbak_fn鍥炶皟鍑芥暟鐨勮繑鍥炲��, 鍒嗗埆琛ㄧず杩斿洖鐨勬暟鎹紝鍜岃繑鍥炴暟鎹殑闀垮害銆�
+ *
+ * @return 0鏄垚鍔燂紝 鍏朵粬鍊兼槸澶辫触鐨勯敊璇爜
+ */
+int net_mod_socket_recvandsend(void *_socket, void **recvbuf, int *recvsize, int *key, recv_callback_fn callback);
+
+/**
+ * @brief 鎺ュ彈娑堟伅锛屽苟鎶奵allback鍑芥暟杩斿洖鐨勬暟鎹彂閫佸洖瀵规柟锛屽湪鎸囧畾鐨勬椂闂村唴鍗充娇娌℃湁瀹屾垚涔熻繑鍥�
+ *
+ * @param recvbuf 鎺ュ彈鍒扮殑娑堟伅瀛樻斁鐨勭紦瀛樺湴鍧�锛岃buf浣跨敤瀹屾垚鍚庨渶瑕佹墜鍔ㄩ噴鏀�
+ * @param recvsize 鎺ュ彈鍒版秷鎭殑闀垮害
+ * @param key 浠庤皝鍝噷鏀跺埌鐨勪俊鎭�
+ * @callback  void (*recv_callback_fn)(void **sendbuf, int *sendsize)
+ *            sendbuf 鍜� sendsize鏄痗allbak_fn鍥炶皟鍑芥暟鐨勮繑鍥炲��, 鍒嗗埆琛ㄧず杩斿洖鐨勬暟鎹紝鍜岃繑鍥炴暟鎹殑闀垮害銆�
+ *
+ * @param sec 绉�
+ * @param nsec 绾崇
+ *
+ * @return 0鏄垚鍔燂紝 鍏朵粬鍊兼槸澶辫触鐨勯敊璇爜
+ */
+int net_mod_socket_recvandsend_timeout(void *_socket, void **recvbuf, int *recvsize, int *key, recv_callback_fn callback,
+                                      int sec, int nsec ) ;
+
+
+/**
+ * @brief 鎺ュ彈娑堟伅锛屽苟鎶奵allback鍑芥暟杩斿洖鐨勬暟鎹彂閫佸洖瀵规柟锛屾棤璁烘垚鍔熶笌鍚︾珛鍒昏繑鍥�
+ *
+ * @param recvbuf 鎺ュ彈鍒扮殑娑堟伅瀛樻斁鐨勭紦瀛樺湴鍧�锛岃buf浣跨敤瀹屾垚鍚庨渶瑕佹墜鍔ㄩ噴鏀�
+ * @param recvsize 鎺ュ彈鍒版秷鎭殑闀垮害
+ * @param key 浠庤皝鍝噷鏀跺埌鐨勪俊鎭�
+ * @callback  void (*recv_callback_fn)(void **sendbuf, int *sendsize)
+ *            sendbuf 鍜� sendsize鏄痗allbak_fn鍥炶皟鍑芥暟鐨勮繑鍥炲��, 鍒嗗埆琛ㄧず杩斿洖鐨勬暟鎹紝鍜岃繑鍥炴暟鎹殑闀垮害銆�
+ *
+ * @return 0鏄垚鍔燂紝 鍏朵粬鍊兼槸澶辫触鐨勯敊璇爜
+ */
+int net_mod_socket_recvandsend_nowait(void *_socket, void **recvbuf, int *recvsize, int *key, recv_callback_fn callback) ;
+
+
+
 /**
  * @brief 璺ㄦ満鍣ㄥ彂閫佹秷鎭苟鎺ュ彈杩斿洖鐨勫簲绛旀秷鎭紝鐩村埌鍙戦�佸畬鎴愭墠杩斿洖
  *
@@ -134,7 +182,7 @@
  * @return 鎴愬姛鍙戦�佺殑鑺傜偣鐨勪釜鏁�
  *
  */
-extern int net_mod_socket_sendandrecv(void *_sockt, net_node_t *node_arr, int arrlen, void *send_buf, int send_size, 
+int net_mod_socket_sendandrecv(void *_sockt, net_node_t *node_arr, int arrlen, void *send_buf, int send_size, 
   net_mod_recv_msg_t ** recv_arr, int *recv_arr_size) ;
  
 
@@ -176,17 +224,17 @@
 
 
 
- /**
-  * @brief 鍚憂ode_arr涓殑鎵�鏈夌綉缁滆妭鐐瑰彂甯冩秷鎭�
-  *
-  * @param node_arr 缃戠粶鑺傜偣缁�, @node_arr_len璇ユ暟缁勯暱搴�
-  * @param topic 涓婚锛�
-  * @param topic_size 璇ヤ富棰樼殑闀垮害
-  * @param content 鍐呭锛�
-  * @param content_size 鍐呭闀垮害
-  *
-  * @return 鎴愬姛鍙戝竷鐨勮妭鐐圭殑涓暟
-  */
+  /**
+   * @brief 鍚憂ode_arr涓殑鎵�鏈夌綉缁滆妭鐐瑰彂甯冩秷鎭�
+   *
+   * @param node_arr 缃戠粶鑺傜偣缁�, @node_arr_len璇ユ暟缁勯暱搴�
+   * @param topic 涓婚锛�
+   * @param topic_size 璇ヤ富棰樼殑闀垮害
+   * @param content 鍐呭锛�
+   * @param content_size 鍐呭闀垮害
+   *
+   * @return 鎴愬姛鍙戝竷鐨勮妭鐐圭殑涓暟
+   */
 int net_mod_socket_pub(void *_sockt, net_node_t *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size);
 
 
diff --git a/src/socket/shm_mod_socket.cpp b/src/socket/shm_mod_socket.cpp
index 02b91f0..097c8fb 100644
--- a/src/socket/shm_mod_socket.cpp
+++ b/src/socket/shm_mod_socket.cpp
@@ -89,6 +89,19 @@
   logger->debug("ShmModSocket::sendandrecv : sendandrecv to %d failed %s",  send_key, bus_strerror(rv));
 	return rv;
 }
+
+
+int ShmModSocket::recvandsend(void **recvbuf, int *recvsize, int *key, recv_callback_fn callback,
+                    const struct timespec *timeout , int flag ) {
+  int rv = shm_recvandsend(shm_socket, recvbuf, recvsize, key, callback, timeout, flag);
+  if(rv == 0) {
+    logger->debug("ShmModSocket::shm_recvandsend: success. key = %d\n", *key);
+    return 0;
+  }
+
+  logger->debug("ShmModSocket::shm_recvandsend :  failed. %s", bus_strerror(rv));
+  return rv;
+}
  
 // // 瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇
 // int ShmModSocket::sendandrecv_unsafe(const void *send_buf, const int send_size, const int send_key, 
diff --git a/src/socket/shm_mod_socket.h b/src/socket/shm_mod_socket.h
index 2fa43d7..b866f0c 100644
--- a/src/socket/shm_mod_socket.h
+++ b/src/socket/shm_mod_socket.h
@@ -87,9 +87,8 @@
 	 const struct timespec *timeout = NULL, int flag = 0);
 
 
-	// 瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇
-	int sendandrecv_unsafe(const void *send_buf, const int send_size, const int key, void **recv_buf, int *recv_size,
-	 const  struct timespec *timeout = NULL, int flag = 0) ;
+  int recvandsend(void **recvbuf, int *recvsize, int *key, recv_callback_fn callback,
+                      const struct timespec *timeout = NULL , int flag = 0);
 
 	/**
 	 * 璁㈤槄鎸囧畾涓婚
diff --git a/src/socket/shm_socket.cpp b/src/socket/shm_socket.cpp
index 690124f..bddcec0 100644
--- a/src/socket/shm_socket.cpp
+++ b/src/socket/shm_socket.cpp
@@ -4,6 +4,7 @@
 #include <map>
 #include <cassert>
 #include "bus_error.h"
+#include "sole.h"
 
 static Logger *logger = LoggerFactory::getLogger();
 
@@ -18,6 +19,12 @@
 
 static void _destrory_socket_perthread(void *tmp_socket);
 static void _create_socket_key_perthread(void);
+
+static int shm_recvpakfrom(shm_socket_t *sockt, shm_packet_t *recvpak ,  const struct timespec *timeout,  int flag);
+
+   
+static int shm_sendpakto(shm_socket_t *sockt, const shm_packet_t *sendpak,
+               const int key, const struct timespec *timeout, const int flag);
 
 //  妫�鏌ey鏄惁宸茬粡琚娇鐢紝 鏈浣跨敤鍒欑粦瀹歬ey
 static LockFreeQueue<shm_packet_t> * shm_socket_bind_queue(int key, bool force) {
@@ -78,11 +85,12 @@
   pthread_mutexattr_t mtxAttr;
 
   logger->debug("shm_open_socket\n");
-  shm_socket_t *socket = (shm_socket_t *)calloc(1, sizeof(shm_socket_t));
-  socket->socket_type = socket_type;
-  socket->key = 0;
-  socket->force_bind = false;
-  socket->queue = NULL;
+  // shm_socket_t *socket = (shm_socket_t *)calloc(1, sizeof(shm_socket_t));
+  shm_socket_t *sockt = new shm_socket_t;
+  sockt->socket_type = socket_type;
+  sockt->key = 0;
+  sockt->force_bind = false;
+  sockt->queue = NULL;
 
 
   s = pthread_mutexattr_init(&mtxAttr);
@@ -91,7 +99,7 @@
   s = pthread_mutexattr_settype(&mtxAttr, PTHREAD_MUTEX_ERRORCHECK);
   if (s != 0)
     err_exit(s, "pthread_mutexattr_settype");
-  s = pthread_mutex_init(&(socket->mutex), &mtxAttr);
+  s = pthread_mutex_init(&(sockt->mutex), &mtxAttr);
   if (s != 0)
     err_exit(s, "pthread_mutex_init");
 
@@ -99,45 +107,44 @@
   if (s != 0)
     err_exit(s, "pthread_mutexattr_destroy");
 
-  return socket;
+  return sockt;
 }
 
-int shm_close_socket(shm_socket_t *socket) {
+int shm_close_socket(shm_socket_t *sockt) {
   
   int s;
   
   logger->debug("shm_close_socket\n");
-  if(socket->queue != NULL) {
-    delete socket->queue;
-    socket->queue = NULL;
+  if(sockt->queue != NULL) {
+    delete sockt->queue;
+    sockt->queue = NULL;
   }
 
-  s =  pthread_mutex_destroy(&(socket->mutex) );
+  s =  pthread_mutex_destroy(&(sockt->mutex) );
   if(s != 0) {
     err_exit(s, "shm_close_socket");
   }
 
-  free(socket);
+  free(sockt);
   return 0;
 }
 
 
-int shm_socket_bind(shm_socket_t *socket, int key) {
-  socket->key = key;
+int shm_socket_bind(shm_socket_t *sockt, int key) {
+  sockt->key = key;
   return 0;
 }
 
-int shm_socket_force_bind(shm_socket_t *socket, int key) {
-  socket->force_bind = true;
-  socket->key = key;
+int shm_socket_force_bind(shm_socket_t *sockt, int key) {
+  sockt->force_bind = true;
+  sockt->key = key;
   return 0;
 }
 
-int shm_socket_get_key(shm_socket_t *sk){
-  return sk->key;
+int shm_socket_get_key(shm_socket_t *sockt){
+  return sockt->key;
 }
 
-  
 
 
 // 鐭繛鎺ユ柟寮忓彂閫�
@@ -145,121 +152,162 @@
                const int key, const struct timespec *timeout, const int flag) {
 
   int rv;
-
-  hashtable_t *hashtable = mm_get_hashtable();
-
-  if( sockt->queue != NULL) 
-    goto LABEL_PUSH;
  
-  {
-    if ((rv = pthread_mutex_lock(&(sockt->mutex))) != 0)
-    err_exit(rv, "shm_sendto : pthread_mutex_lock");
+  shm_packet_t sendpak;
+  sendpak.key = sockt->key;
+  sendpak.size = size;
+  sendpak.buf = mm_malloc(size);
+  memcpy(sendpak.buf, buf, size);
+  rv = shm_sendpakto(sockt, &sendpak, key, timeout, flag);
+  return rv;
+}
 
-    if (sockt->queue == NULL) {
-      if (sockt->key == 0) {
-        sockt->key = hashtable_alloc_key(hashtable);
-      }
-      sockt->queue = shm_socket_bind_queue( sockt->key, sockt->force_bind);
-      if(sockt->queue  == NULL ) {
-        logger->error("%s. key = %d", bus_strerror(EBUS_KEY_INUSED), sockt->key);
-        return EBUS_KEY_INUSED;
-      }
+ 
+
+int shm_sendandrecv(shm_socket_t *sockt, const void *send_buf,
+                    const int send_size, const int key, void **recv_buf,
+                    int *recv_size,  const struct timespec *timeout,  int flags) {
+
+  int rv, tryn = 3;
+  shm_packet_t sendpak;
+  shm_packet_t recvpak;
+  std::map<std::string, shm_packet_t>::iterator recvbufIter;
+  std::string uuid = sole::uuid4().str();
+  
+  sendpak.key = sockt->key;
+  sendpak.size = send_size;
+  sendpak.buf = mm_malloc(send_size);
+  memcpy(sendpak.buf, send_buf, send_size);
+  memcpy(sendpak.uuid, uuid.c_str(), uuid.length() + 1);
+  // uuid.copy(sendpak.uuid, sizeof sendpak.uuid);
+  rv = shm_sendpakto(sockt, &sendpak, key, timeout, flags);
+
+  if(rv != 0) {
+    return rv;
+  }
+
+  while(true) {
+    tryn--;
+    recvbufIter = sockt->recvbuf.find(uuid);
+    if(recvbufIter != sockt->recvbuf.end()) {
+      // 鍦ㄧ紦瀛橀噷鏌ュ埌浜哢UID鍖归厤鎴愬姛鐨�
+logger->debug("get from recvbuf: %s", uuid.c_str());
+      recvpak = recvbufIter->second;
+      sockt->recvbuf.erase(recvbufIter);
+      break;
     }
 
-    if ((rv = pthread_mutex_unlock(&(sockt->mutex))) != 0)
-      err_exit(rv, "shm_sendto : pthread_mutex_unlock");
-  
+    rv = shm_recvpakfrom(sockt, &recvpak, timeout, flags);
+
+    if (rv != 0) {
+
+      if(rv == ETIMEDOUT)
+        return EBUS_TIMEOUT;
+
+      logger->debug("%d shm_recvfrom failed %s", shm_socket_get_key(sockt), bus_strerror(rv));
+      return rv;
+    } 
+
+logger->debug("send uuid:%s, recv uuid: %s", uuid.c_str(), recvpak.uuid);
+    if (strncmp(uuid.c_str(), recvpak.uuid, sizeof recvpak.uuid) == 0) {
+      // 鍙戦�佷笌鎺ュ彈鐨刄UID鍖归厤鎴愬姛
+      break;
+    } else {
+      // 绛旈潪鎵�闂紝鏀惧埌缂撳瓨閲�
+      sockt->recvbuf.insert({recvpak.uuid, recvpak});
+      continue;
+    }
+
+    if(tryn == 0) {
+      // 灏濊瘯浜唗ryn娆¢兘娌℃湁鎴愬姛
+      return EBUS_RECVFROM_WRONG_END;
+    }
+
   }
-  
  
- LABEL_PUSH: 
-  if (key == sockt->key) {
-    logger->error( "can not send to your self!");
-    return EBUS_SENDTO_SELF;
+LABLE_SUC:
+ if(recv_buf != NULL) {
+    void *_buf = malloc(recvpak.size);
+    memcpy(_buf, recvpak.buf, recvpak.size);
+    *recv_buf = _buf; 
   }
+ 
+  if(recv_size != NULL)
+    *recv_size = recvpak.size;
 
-  LockFreeQueue<shm_packet_t> *remoteQueue;
-  if ((remoteQueue = shm_socket_attach_queue(key)) == NULL) {
-    bus_errno = EBUS_CLOSED;
-    logger->error("sendto key %d failed, %s", key, bus_strerror(bus_errno));
-    return EBUS_CLOSED;
-  }
+  mm_free(recvpak.buf);
 
-  shm_packet_t dest;
-  dest.key = sockt->key;
-  dest.size = size;
-  dest.buf = mm_malloc(size);
-  memcpy(dest.buf, buf, size);
+  return 0;
 
-  rv = remoteQueue->push(dest, timeout, flag);
+}
 
-  if (rv == 0) {
-    printf("%d sendto %d suc.\n", shm_socket_get_key(sockt), key);
-    return 0;
-  } else {
-    mm_free(dest.buf);
+/**
+ * @callback  void (*recv_callback_fn)(void **sendbuf, int *sendsize) 
+ *            sendbuf 鍜� sendsize鏄痗allbak_fn鍥炶皟鍑芥暟鐨勮繑鍥炲��, 鍒嗗埆琛ㄧず鍙戦�佹暟鎹紝鍜屽彂閫佹暟鎹殑澶у皬銆�
+ * 
+ */
+int shm_recvandsend(shm_socket_t *sockt, void **recvbuf, int *recvsize, int *key, recv_callback_fn callback, 
+  const struct timespec *timeout, int flag) {
+  
+  int rv;
+ 
+  void *sendbuf = NULL;
+  int sendsize = 0;
+  shm_packet_t recvpak;
+
+  rv = shm_recvpakfrom(sockt , &recvpak, timeout, flag);
+
+ 
+
+  if (rv != 0) {
     if(rv == ETIMEDOUT)
       return EBUS_TIMEOUT;
     else {
-      logger->debug("====%d sendto key %d failed %s", shm_socket_get_key(sockt), key, bus_strerror(rv));
+      logger->debug("%d shm_recvfrom failed %s", shm_socket_get_key(sockt), bus_strerror(rv));
       return rv;
     }
   }
+   
+  if(recvbuf != NULL) {
+    void *_buf = malloc(recvpak.size);
+    memcpy(_buf, recvpak.buf, recvpak.size);
+    *recvbuf = _buf; 
+  }
+ 
+  if(recvsize != NULL)
+    *recvsize = recvpak.size;
+
+  if(key != NULL)
+    *key = recvpak.key;
+
+  mm_free(recvpak.buf);
+
+  callback(&sendbuf, &sendsize);
+
+  shm_packet_t sendpak;
+  sendpak.key = sockt->key;
+  sendpak.size = sendsize;
+  memcpy(sendpak.uuid, recvpak.uuid, sizeof sendpak.uuid);
+  if(sendbuf !=NULL && sendsize > 0) {
+    sendpak.buf = mm_malloc(sendsize);
+    memcpy(sendpak.buf, sendbuf, sendsize);
+  }
+ 
+  rv = shm_sendpakto(sockt, &sendpak, recvpak.key, timeout, flag);
+
+  return 0;
+  
 }
-
-
 
 // 鐭繛鎺ユ柟寮忔帴鍙�
 int shm_recvfrom(shm_socket_t *sockt, void **buf, int *size, int *key,  const struct timespec *timeout,  int flag) {
   int rv;
   
-  hashtable_t *hashtable = mm_get_hashtable();
+  shm_packet_t recvpak;
+  rv = shm_recvpakfrom(sockt , &recvpak, timeout, flag);
 
-  if( sockt->queue != NULL) 
-    goto LABEL_POP;
+  if (rv != 0) {
 
-  {
-    if ((rv = pthread_mutex_lock(&(sockt->mutex))) != 0)
-      err_exit(rv, "shm_recvfrom : pthread_mutex_lock");
- 
-   
-    if (sockt->key == 0) {
-      sockt->key = hashtable_alloc_key(hashtable);
-    }  
-    sockt->queue = shm_socket_bind_queue( sockt->key, sockt->force_bind);
-    if(sockt->queue  == NULL ) {
-      logger->error("%s. key = %d", bus_strerror(EBUS_KEY_INUSED), sockt->key);
-      return EBUS_KEY_INUSED;
-    }
-    
-    
-    if ((rv = pthread_mutex_unlock(&(sockt->mutex))) != 0)
-      err_exit(rv, "shm_recvfrom : pthread_mutex_unlock");
-
-  }
-  
-LABEL_POP:
-
-  shm_packet_t src;
- 
-  rv = sockt->queue->pop(src, timeout, flag);
-
-  if (rv == 0) {
-    if(buf != NULL) {
-      void *_buf = malloc(src.size);
-      memcpy(_buf, src.buf, src.size);
-      *buf = _buf; 
-    }
-   
-    if(size != NULL)
-      *size = src.size;
-
-    if(key != NULL)
-      *key = src.key;
-
-    mm_free(src.buf);
-    return 0;
-  } else {
     if(rv == ETIMEDOUT)
       return EBUS_TIMEOUT;
     else {
@@ -267,10 +315,27 @@
       return rv;
     }
    
+  } 
 
+
+ if(buf != NULL) {
+    void *_buf = malloc(recvpak.size);
+    memcpy(_buf, recvpak.buf, recvpak.size);
+    *buf = _buf; 
   }
+ 
+  if(size != NULL)
+    *size = recvpak.size;
+
+  if(key != NULL)
+    *key = recvpak.key;
+
+  mm_free(recvpak.buf);
+  return 0;
 }
 
+
+// =================================================================================================
 
  /* Free thread-specific data buffer */
 static void _destrory_socket_perthread(void *tmp_socket)
@@ -406,10 +471,99 @@
  
 }
  
-int shm_sendandrecv(shm_socket_t *socket, const void *send_buf,
-                    const int send_size, const int send_key, void **recv_buf,
-                    int *recv_size,  const struct timespec *timeout,  int flags) {
 
-  struct timespec tm = {10, 0};
-  return  _shm_sendandrecv_thread_local(socket, send_buf, send_size, send_key,recv_buf, recv_size, &tm, flags);
+   
+static int shm_sendpakto(shm_socket_t *sockt, const shm_packet_t *sendpak,
+               const int key, const struct timespec *timeout, const int flag) {
+
+  int rv;
+  hashtable_t *hashtable = mm_get_hashtable();
+
+  if( sockt->queue != NULL) 
+    goto LABEL_PUSH;
+ 
+  {
+    if ((rv = pthread_mutex_lock(&(sockt->mutex))) != 0)
+    err_exit(rv, "shm_sendto : pthread_mutex_lock");
+
+    if (sockt->queue == NULL) {
+      if (sockt->key == 0) {
+        sockt->key = hashtable_alloc_key(hashtable);
+      }
+      sockt->queue = shm_socket_bind_queue( sockt->key, sockt->force_bind);
+      if(sockt->queue  == NULL ) {
+        logger->error("%s. key = %d", bus_strerror(EBUS_KEY_INUSED), sockt->key);
+        return EBUS_KEY_INUSED;
+      }
+    }
+
+    if ((rv = pthread_mutex_unlock(&(sockt->mutex))) != 0)
+      err_exit(rv, "shm_sendto : pthread_mutex_unlock");
+  
+  }
+  
+ 
+ LABEL_PUSH: 
+  if (key == sockt->key) {
+    logger->error( "can not send to your self!");
+    return EBUS_SENDTO_SELF;
+  }
+
+  LockFreeQueue<shm_packet_t> *remoteQueue;
+  if ((remoteQueue = shm_socket_attach_queue(key)) == NULL) {
+    bus_errno = EBUS_CLOSED;
+    logger->error("sendto key %d failed, %s", key, bus_strerror(bus_errno));
+    return EBUS_CLOSED;
+  }
+
+  
+
+  rv = remoteQueue->push(*sendpak, timeout, flag);
+
+  return rv;
 }
+
+// 鐭繛鎺ユ柟寮忔帴鍙�
+static int shm_recvpakfrom(shm_socket_t *sockt, shm_packet_t *recvpak ,  const struct timespec *timeout,  int flag) {
+  int rv;
+  
+  hashtable_t *hashtable = mm_get_hashtable();
+
+  if( sockt->queue != NULL) 
+    goto LABEL_POP;
+
+  {
+    if ((rv = pthread_mutex_lock(&(sockt->mutex))) != 0)
+      err_exit(rv, "shm_recvfrom : pthread_mutex_lock");
+ 
+   
+    if (sockt->key == 0) {
+      sockt->key = hashtable_alloc_key(hashtable);
+    }  
+    sockt->queue = shm_socket_bind_queue( sockt->key, sockt->force_bind);
+    if(sockt->queue  == NULL ) {
+      logger->error("%s. key = %d", bus_strerror(EBUS_KEY_INUSED), sockt->key);
+      return EBUS_KEY_INUSED;
+    }
+    
+    
+    if ((rv = pthread_mutex_unlock(&(sockt->mutex))) != 0)
+      err_exit(rv, "shm_recvfrom : pthread_mutex_unlock");
+
+  }
+  
+LABEL_POP:
+
+ 
+ 
+  rv = sockt->queue->pop(*recvpak, timeout, flag);
+
+  return rv;
+}
+// int shm_sendandrecv(shm_socket_t *sockt, const void *send_buf,
+//                     const int send_size, const int send_key, void **recv_buf,
+//                     int *recv_size,  const struct timespec *timeout,  int flags) {
+
+//   struct timespec tm = {10, 0};
+//   return  _shm_sendandrecv_thread_local(sockt, send_buf, send_size, send_key,recv_buf, recv_size, &tm, flags);
+// }
diff --git a/src/socket/shm_socket.h b/src/socket/shm_socket.h
index 198d4da..377ba4e 100644
--- a/src/socket/shm_socket.h
+++ b/src/socket/shm_socket.h
@@ -5,6 +5,7 @@
 #include "usg_typedef.h"
 #include "shm_queue.h"
 #include "lock_free_queue.h"
+#include <functional>
 
 enum shm_socket_type_t
 {
@@ -13,11 +14,13 @@
 	
 };
  
+ 
 
 typedef struct shm_packet_t {
 	int key;
 	size_t size;
 	void * buf;
+	char uuid[64];
 
 } shm_packet_t;
 
@@ -33,11 +36,13 @@
 
 	LockFreeQueue<shm_packet_t> *queue;  //self queue
 	LockFreeQueue<shm_packet_t> *remoteQueue; // peer queue
+	std::map<std::string, shm_packet_t> recvbuf;
 	
 
 } shm_socket_t;
 
-
+// typedef void (*recv_callback_fn)(void **sendbuf, int *sendsize);
+typedef std::function<void(void **sendbuf, int *sendsize)> recv_callback_fn;
 
 size_t shm_socket_remove_keys(int keys[], size_t length);
 
@@ -59,13 +64,16 @@
 int shm_recvfrom(shm_socket_t *socket, void **buf, int *size, int *key,  const struct timespec * timeout = NULL,  int flags=0);
 
 int shm_sendandrecv(shm_socket_t *socket, const void *send_buf, const int send_size, const int send_key, void **recv_buf, int *recv_size,  
-	const struct timespec * timeout = NULL,  int flags=0);
+	const struct timespec * timeout = NULL,  int flags = 0);
 
 /**
- * 鍔熻兘鍚宻hm_sendandrecv, 浣嗘槸涓嶆槸绾跨▼瀹夊叏鐨�
+ * @callback  void (*recv_callback_fn)(void **sendbuf, int *sendsize)
+ *            sendbuf 鍜� sendsize鏄痗allbak_fn鍥炶皟鍑芥暟鐨勮繑鍥炲��, 鍒嗗埆琛ㄧず鍙戦�佹暟鎹紝鍜屽彂閫佹暟鎹殑澶у皬銆�
+ *
  */
-int shm_sendandrecv_unsafe(shm_socket_t *socket, const void *send_buf, const int send_size, const int send_key, void **recv_buf, int *recv_size,  
-	const struct timespec * timeout = NULL,  int flags=0);
+int shm_recvandsend(shm_socket_t *sockt, void **recvbuf, int *recvsize, int *key, recv_callback_fn callback,
+                    const struct timespec *timeout = NULL, int flag = 0);
+
 
 
 
diff --git a/test/sole_demo.cpp b/test/sole_demo.cpp
index 5adaa8b..e5a4e63 100644
--- a/test/sole_demo.cpp
+++ b/test/sole_demo.cpp
@@ -23,6 +23,10 @@
     std::cout << "uuid v4 base62 : " << u4.base62() << std::endl;
     std::cout << "uuid v4 pretty : " << u4.pretty() << std::endl << std::endl;
 
+    std::cout << "uuid v4 length : " << u4.str().length() << std::endl;
+
+    std::string test("123");
+    std::cout << "test length : " << test.length() << std::endl;
     u1 = sole::rebuild("F81D4FAE-7DEC-11D0-A765-00A0C91E6BF6");
     u4 = sole::rebuild("GITheR4tLlg-BagIW20DGja");
 
diff --git a/test_net_socket/test_net_mod_socket.cpp b/test_net_socket/test_net_mod_socket.cpp
index e589808..568392d 100644
--- a/test_net_socket/test_net_mod_socket.cpp
+++ b/test_net_socket/test_net_mod_socket.cpp
@@ -133,21 +133,32 @@
 
  
 
-void start_reply(int key) {
+void start_reply(int mkey) {
   printf("start reply\n");
   void *ser = net_mod_socket_open();
-  net_mod_socket_bind(ser, key);
-  int size;
+  net_mod_socket_bind(ser, mkey);
+  int recvsize;
   void *recvbuf;
   char sendbuf[512];
   int rv;
-  int remote_port;
-  while ( (rv = net_mod_socket_recvfrom(ser, &recvbuf, &size, &remote_port) ) == 0) {
-   // printf( "server: RECEIVED REQUEST FROM PORT %d NAME %s\n", remote_port, recvbuf);
+  int key;
+  while(true) {
+    rv = net_mod_socket_recvandsend_timeout(ser, &recvbuf, &recvsize, &key, [&](void ** buf, int *size){
+    printf( "server: RECEIVED REQUEST FROM  %d : %s\n", key, recvbuf);
     sprintf(sendbuf, "%d RECEIVED %s", net_mod_socket_get_key(ser), (char *)recvbuf);
-    net_mod_socket_sendto(ser, sendbuf, strlen(sendbuf) + 1, remote_port);
+    // buf 鍜� size鏄繑鍥炲��
+    *buf = sendbuf;
+    *size = strlen(sendbuf) + 1;
     free(recvbuf);
+    return;
+    }, 0, 2000000 );
   }
+  // while ( (rv = net_mod_socket_recvfrom(ser, &recvbuf, &size, &key) ) == 0) {
+  //  // printf( "server: RECEIVED REQUEST FROM  %d NAME %s\n", key, recvbuf);
+  //   sprintf(sendbuf, "%d RECEIVED %s", net_mod_socket_get_key(ser), (char *)recvbuf);
+  //   net_mod_socket_sendto(ser, sendbuf, strlen(sendbuf) + 1, key);
+  //   free(recvbuf);
+  // }
 }
 
 // 浜や簰寮忓鎴风
@@ -194,7 +205,7 @@
 		  if (fgets(content, MAXLINE, stdin) != NULL) {
 		  	// 鏀跺埌娑堟伅鐨勮妭鐐瑰嵆浣挎病鏈夊搴旂殑淇℃伅锛� 涔熻鍥炲涓�涓〃绀烘棤鐨勬秷鎭�,鍚﹀垯浼氫竴鐩寸瓑寰�
 		    // n = net_mod_socket_sendandrecv(client, node_arr, node_arr_size, content, strlen(content), &recv_arr, &recv_arr_size);
-        n = net_mod_socket_sendandrecv_timeout(client, node_arr, node_arr_size, content, strlen(content), &recv_arr, &recv_arr_size, 1000);
+        n = net_mod_socket_sendandrecv_timeout(client, node_arr, node_arr_size, content, strlen(content), &recv_arr, &recv_arr_size, 1);
 		    printf(" %d nodes reply\n", n);
 		    for(i=0; i<recv_arr_size; i++) {
 		    	printf("reply from (host:%s, port: %d, key:%d) >> %s\n", 
@@ -272,7 +283,7 @@
     sprintf(sendbuf, hello_format, net_mod_socket_get_key(client), l);
     // fprintf(fp, "requst:%s\n", sendbuf);
     // n = net_mod_socket_sendandrecv(client, node_arr, node_arr_size, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size);
-    n = net_mod_socket_sendandrecv_timeout(client, targ->node, 1, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size, 1000);
+    n = net_mod_socket_sendandrecv_timeout(client, targ->node, 1, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size, 1);
     printf("%d: send %d nodes\n", l, n);
     for(j=0; j < recv_arr_size; j++) {
 

--
Gitblit v1.8.0