From f0732bbcbdca67ef8f9444f2aafaf44da81c9e5a Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期六, 01 八月 2020 16:44:00 +0800 Subject: [PATCH] update --- src/socket/shm_socket.c | 36 +++++++++++++++++++++++------------- 1 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/socket/shm_socket.c b/src/socket/shm_socket.c index 52251e6..4a705ad 100644 --- a/src/socket/shm_socket.c +++ b/src/socket/shm_socket.c @@ -17,12 +17,22 @@ 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) { shm_socket_t *socket = (shm_socket_t *)calloc(1, sizeof(shm_socket_t)); socket->socket_type = socket_type; socket->port = -1; + socket->force_bind = false; socket->dispatch_thread = 0; socket->status = SHM_CONN_CLOSED; @@ -46,6 +56,12 @@ return 0; } +int shm_socket_force_bind(shm_socket_t *socket, int port) { + socket->force_bind = true; + socket->port = port; + return 0; +} + int shm_listen(shm_socket_t *socket) { if (socket->socket_type != SHM_SOCKET_STREAM) { @@ -60,9 +76,7 @@ socket->port = port; } else { - if (hashtable_get(hashtable, socket->port) != NULL) { - 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); @@ -130,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 " @@ -143,10 +158,7 @@ if (socket->port == -1) { socket->port = hashtable_alloc_key(hashtable); } else { - - if (hashtable_get(hashtable, socket->port) != NULL) { - 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); @@ -242,9 +254,7 @@ socket->port = hashtable_alloc_key(hashtable); } else { - if (hashtable_get(hashtable, socket->port) != NULL) { - 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); @@ -298,9 +308,7 @@ socket->port = hashtable_alloc_key(hashtable); } else { - if (hashtable_get(hashtable, socket->port) != NULL) { - 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); @@ -342,6 +350,8 @@ return -1; } +// ============================================================================================================ + /** * 缁戝畾key鍒伴槦鍒楋紝浣嗘槸骞朵笉浼氬垱寤洪槦鍒椼�傚鏋滄病鏈夊搴旀寚瀹歬ey鐨勯槦鍒楁彁绀洪敊璇苟閫�鍑� */ -- Gitblit v1.8.0