From ed1f486e2c2d8c02d675363d848a3460edfe80e3 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期二, 29 十二月 2020 16:22:34 +0800
Subject: [PATCH] update

---
 src/socket/shm_socket.c |   95 +++++++++++++++++++++++++++++++----------------
 1 files changed, 63 insertions(+), 32 deletions(-)

diff --git a/src/socket/shm_socket.c b/src/socket/shm_socket.c
index efb3ef7..ddc26fa 100644
--- a/src/socket/shm_socket.c
+++ b/src/socket/shm_socket.c
@@ -2,6 +2,7 @@
 #include "hashtable.h"
 #include "logger_factory.h"
 #include <map>
+#include "bus_error.h"
 
 static Logger *logger = LoggerFactory::getLogger();
 
@@ -29,7 +30,8 @@
 static inline int  _shm_socket_check_key(shm_socket_t *socket) {
    void *tmp_ptr = mm_get_by_key(socket->key);
     if (tmp_ptr!= NULL && tmp_ptr != (void *)1 && !socket->force_bind ) {
-      logger->error("key %d has already been in used!", socket->key);
+      bus_errno = EBUS_KEY_INUSED;
+      logger->error("%s. key = %d ", bus_strerror(bus_errno), socket->key);
       return 0;
     }
     return 1;
@@ -45,6 +47,7 @@
 
 shm_socket_t *shm_open_socket(shm_socket_type_t socket_type) {
 
+  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 = -1;
@@ -52,14 +55,15 @@
   socket->dispatch_thread = 0;
   socket->status = SHM_CONN_CLOSED;
   socket->mutex = SemUtil::get(IPC_PRIVATE, 1);
-  logger->debug("shm_open_socket\n");
+  
   return socket;
 }
 
-static int _shm_close_socket(shm_socket_t *socket) {
+int shm_close_socket(shm_socket_t *socket) {
   
   int ret;
-
+  
+  logger->debug("shm_close_socket\n");
   switch (socket->socket_type) {
     case SHM_SOCKET_STREAM:
       ret =  _shm_close_stream_socket(socket, true);
@@ -72,16 +76,15 @@
   }
   free(socket);
   SemUtil::remove(socket->mutex);
-  logger->debug("shm_close_socket\n");
   return ret;
 }
 
-int shm_close_socket(shm_socket_t *socket) {
+// int shm_close_socket(shm_socket_t *socket) {
   
-  // _destrory_tmp_recv_socket_((shm_socket_t *)pthread_getspecific(_tmp_recv_socket_key_));
+//   // _destrory_tmp_recv_socket_((shm_socket_t *)pthread_getspecific(_tmp_recv_socket_key_));
  
-  return _shm_close_socket(socket);;
-}
+//   return shm_close_socket(socket);;
+// }
 
 int shm_socket_bind(shm_socket_t *socket, int key) {
   socket->key = key;
@@ -110,7 +113,8 @@
   } else {
 
    if(!_shm_socket_check_key(socket)) {
-     return -1;
+     bus_errno = EBUS_KEY_INUSED;
+     return EBUS_KEY_INUSED;
    }
   }
 
@@ -200,7 +204,8 @@
     socket->key = hashtable_alloc_key(hashtable);
   } else {
     if(!_shm_socket_check_key(socket)) {
-      return -1;
+      bus_errno = EBUS_KEY_INUSED;
+      return EBUS_KEY_INUSED;
     }
   }
 
@@ -246,11 +251,11 @@
                 "SHM_SOCKET_STREAM socket");
     exit(1);
   }
-  // hashtable_t *hashtable = mm_get_hashtable();
-  // if(socket->remoteQueue == NULL) {
-  // 	err_msg(errno, "褰撳墠瀹㈡埛绔棤杩炴帴!");
-  // 	return -1;
-  // }
+  hashtable_t *hashtable = mm_get_hashtable();
+  if(socket->remoteQueue == NULL) {
+  	err_msg(errno, "褰撳墠瀹㈡埛绔棤杩炴帴!");
+  	return -1;
+  }
   shm_msg_t dest;
   dest.type = SHM_COMMON_MSG;
   dest.key = socket->key;
@@ -306,7 +311,8 @@
     } else {
 
      if(!_shm_socket_check_key(socket)) {
-        return -1;
+        bus_errno = EBUS_KEY_INUSED;
+        return EBUS_KEY_INUSED;
      }
 
     }
@@ -322,8 +328,9 @@
 
   SHMQueue<shm_msg_t> *remoteQueue;
   if ((remoteQueue = _attach_remote_queue(key)) == NULL) {
-  	logger->error( "shm_sendto failed, the other end has been closed, or has not been opened!");
-    return SHM_SOCKET_ECONNFAILED;
+    bus_errno = EBUS_CLOSED;
+    logger->error("sendto key %d failed, %s", key, bus_strerror(bus_errno));
+    return EBUS_CLOSED;
   }
 
   shm_msg_t dest;
@@ -350,8 +357,16 @@
   } else {
     delete remoteQueue;
     mm_free(dest.buf);
-    logger->error(errno, "sendto key %d failed!", key);
-    return -1;
+    if(errno == EAGAIN) {
+      bus_errno = EBUS_TIMEOUT;
+      logger->error("sendto key %d failed, %s", key, bus_strerror(bus_errno));
+      return EBUS_TIMEOUT;
+    } else {
+      logger->error(errno, "sendto key %d failed!", key);
+      return -1;
+    }
+   
+   
   }
 }
 
@@ -371,7 +386,8 @@
     } else {
 
       if(!_shm_socket_check_key(socket)) {
-        return -1;
+        bus_errno = EBUS_KEY_INUSED;
+        return EBUS_KEY_INUSED;
       }
     }
 
@@ -391,11 +407,18 @@
   }
 
   if (rv) {
-    void *_buf = malloc(src.size);
-    memcpy(_buf, src.buf, src.size);
-    *buf = _buf;
-    *size = src.size;
-    *key = src.key;
+    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);
     // printf("shm_recvfrom pop after\n");
     return 0;
@@ -411,12 +434,13 @@
   int rv;
   if(tmp_socket == NULL)
     return;
+
   logger->debug("%d destroy tmp socket\n", pthread_self()); 
-  _shm_close_socket((shm_socket_t *)tmp_socket);
+  shm_close_socket((shm_socket_t *)tmp_socket);
   rv =  pthread_setspecific(_tmp_recv_socket_key_, NULL);
   if ( rv != 0) {
-      logger->error(rv, "shm_sendandrecv : pthread_setspecific");
-      exit(1);
+    logger->error(rv, "shm_sendandrecv : pthread_setspecific");
+    exit(1);
   }
 }
 
@@ -438,7 +462,7 @@
 
 
 
-int shm_sendandrecv(shm_socket_t *socket, const void *send_buf,
+int shm_sendandrecv_safe(shm_socket_t *socket, const void *send_buf,
                     const int send_size, const int send_key, void **recv_buf,
                     int *recv_size,  struct timespec *timeout,  int flags) {
   int recv_key;
@@ -508,6 +532,13 @@
   return -1;
 }
 
+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,  struct timespec *timeout,  int flags) {
+  return  shm_sendandrecv_unsafe(socket, send_buf, send_size, send_key,recv_buf, recv_size, timeout,  flags);
+}
+
+
 // ============================================================================================================
 
 /**
@@ -517,7 +548,7 @@
 
   hashtable_t *hashtable = mm_get_hashtable();
   if (hashtable_get(hashtable, key) == NULL) {
-    logger->error("shm_socket._remote_queue_attach锛歝onnet at key %d  failed!", key);
+    //logger->error("shm_socket._remote_queue_attach锛歝onnet at key %d  failed!", key);
     return NULL;
   }
 

--
Gitblit v1.8.0