From 5f7b0d34f54fee057814c3f146c871c474d53b75 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期六, 01 八月 2020 16:30:39 +0800
Subject: [PATCH] fix alloc_key

---
 src/socket/shm_socket.c |   31 ++++++++++++++++---------------
 1 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/src/socket/shm_socket.c b/src/socket/shm_socket.c
index 168eb65..4a705ad 100644
--- a/src/socket/shm_socket.c
+++ b/src/socket/shm_socket.c
@@ -17,6 +17,15 @@
 
 int _shm_close_stream_socket(shm_socket_t *socket, bool notifyRemote);
 
+static inline int  _shm_socket_check_key(shm_socket_t *socket) {
+   void *tmp_ptr = mm_get_by_key(socket->port);
+    if (tmp_ptr!= NULL && tmp_ptr != (void *)1 && !socket->force_bind ) {
+      err_exit(0, "key %d has already been in used!", socket->port);
+      return 0;
+    }
+    return 1;
+}
+
 SHMQueue<shm_msg_t> *_attach_remote_queue(int port);
 
 shm_socket_t *shm_open_socket(shm_socket_type_t socket_type) {
@@ -67,9 +76,7 @@
     socket->port = port;
   } else {
 
-    if (hashtable_get(hashtable, socket->port) != NULL && !socket->force_bind) {
-      err_exit(0, "key %d has already been in used!", socket->port);
-    }
+   _shm_socket_check_key(socket);
   }
 
   socket->queue = new SHMQueue<shm_msg_t>(socket->port, 16);
@@ -137,6 +144,7 @@
   return NULL;
 }
 
+
 int shm_connect(shm_socket_t *socket, int port) {
   if (socket->socket_type != SHM_SOCKET_STREAM) {
     err_exit(0, "can not invoke shm_connect method with a socket which is not "
@@ -150,10 +158,7 @@
   if (socket->port == -1) {
     socket->port = hashtable_alloc_key(hashtable);
   } else {
-
-    if (hashtable_get(hashtable, socket->port) != NULL && !socket->force_bind ) {
-      err_exit(0, "key %d has already been in used!", socket->port);
-    }
+    _shm_socket_check_key(socket);
   }
 
   socket->queue = new SHMQueue<shm_msg_t>(socket->port, 16);
@@ -249,10 +254,7 @@
       socket->port = hashtable_alloc_key(hashtable);
     } else {
 
-      if (hashtable_get(hashtable, socket->port) != NULL) {
-        if(!socket->force_bind)
-          err_exit(0, "key %d has already been in used!", socket->port);
-      }
+     _shm_socket_check_key(socket);
     }
 
     socket->queue = new SHMQueue<shm_msg_t>(socket->port, 16);
@@ -306,10 +308,7 @@
       socket->port = hashtable_alloc_key(hashtable);
     } else {
 
-      if (hashtable_get(hashtable, socket->port) != NULL) {
-        if(!socket->force_bind)
-          err_exit(0, "key %d has already been in used!", socket->port);
-      }
+      _shm_socket_check_key(socket);
     }
 
     socket->queue = new SHMQueue<shm_msg_t>(socket->port, 16);
@@ -351,6 +350,8 @@
   return -1;
 }
 
+// ============================================================================================================
+
 /**
  * 缁戝畾key鍒伴槦鍒楋紝浣嗘槸骞朵笉浼氬垱寤洪槦鍒椼�傚鏋滄病鏈夊搴旀寚瀹歬ey鐨勯槦鍒楁彁绀洪敊璇苟閫�鍑�
  */

--
Gitblit v1.8.0