From e4130d29e80a40884310481ded90ff845c614f43 Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期四, 24 十二月 2020 10:03:14 +0800 Subject: [PATCH] bus force bind --- src/socket/shm_socket.c | 164 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 134 insertions(+), 30 deletions(-) diff --git a/src/socket/shm_socket.c b/src/socket/shm_socket.c index 8fc4d94..80674c1 100644 --- a/src/socket/shm_socket.c +++ b/src/socket/shm_socket.c @@ -7,9 +7,12 @@ -void print_msg(char *head, shm_msg_t &msg) { +static void print_msg(char *head, shm_msg_t &msg) { // err_msg(0, "%s: key=%d, type=%d\n", head, msg.key, msg.type); } + +static pthread_once_t _once_ = PTHREAD_ONCE_INIT; +static pthread_key_t _tmp_recv_socket_key_; static void *_server_run_msg_rev(void *_socket); @@ -19,10 +22,14 @@ static int _shm_close_stream_socket(shm_socket_t *socket, bool notifyRemote); +static void _destrory_tmp_recv_socket_(void *tmp_socket); +static void _create_tmp_recv_socket_key(void); + +// 妫�鏌ey鏄惁宸茬粡琚娇鐢紝鏄繑鍥�0, 鍚﹁繑鍥�1 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 ) { - err_exit(0, "key %d has already been in used!", socket->key); + logger->error("key %d has already been in used!", socket->key); return 0; } return 1; @@ -37,6 +44,8 @@ } 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; @@ -44,12 +53,15 @@ socket->dispatch_thread = 0; socket->status = SHM_CONN_CLOSED; socket->mutex = SemUtil::get(IPC_PRIVATE, 1); + return 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); @@ -60,10 +72,17 @@ default: break; } - SemUtil::remove(socket->mutex); free(socket); + SemUtil::remove(socket->mutex); return ret; } + +// int shm_close_socket(shm_socket_t *socket) { + +// // _destrory_tmp_recv_socket_((shm_socket_t *)pthread_getspecific(_tmp_recv_socket_key_)); + +// return shm_close_socket(socket);; +// } int shm_socket_bind(shm_socket_t *socket, int key) { socket->key = key; @@ -91,7 +110,9 @@ socket->key = key; } else { - _shm_socket_check_key(socket); + if(!_shm_socket_check_key(socket)) { + return -1; + } } socket->queue = new SHMQueue<shm_msg_t>(socket->key, 16); @@ -161,6 +182,9 @@ } +/** + * @return 0鎴愬姛. 鍏朵粬鍊煎け璐� + */ int shm_connect(shm_socket_t *socket, int key) { if (socket->socket_type != SHM_SOCKET_STREAM) { logger->error( "can not invoke shm_connect method with a socket which is not " @@ -170,20 +194,22 @@ hashtable_t *hashtable = mm_get_hashtable(); if (hashtable_get(hashtable, key) == NULL) { logger->error("shm_connect锛歝onnect at key %d failed!", key); - exit(1); + return -1; } if (socket->key == -1) { socket->key = hashtable_alloc_key(hashtable); } else { - _shm_socket_check_key(socket); + if(!_shm_socket_check_key(socket)) { + return -1; + } } socket->queue = new SHMQueue<shm_msg_t>(socket->key, 16); if ((socket->remoteQueue = _attach_remote_queue(key)) == NULL) { logger->error("connect to %d failted", key); - exit(1); + return -1; } socket->messageQueue = new LockFreeQueue<shm_msg_t, DM_Allocator>(16); @@ -209,7 +235,7 @@ } else { logger->error( "connect failted!"); - exit(1); + return -1; } return 0; @@ -221,11 +247,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; @@ -280,17 +306,20 @@ socket->key = hashtable_alloc_key(hashtable); } else { - _shm_socket_check_key(socket); + if(!_shm_socket_check_key(socket)) { + return -1; + } + } socket->queue = new SHMQueue<shm_msg_t>(socket->key, 16); } SemUtil::inc(socket->mutex); - if (key == socket->key) { - logger->error( "can not send to your self!"); - return -1; - } + // if (key == socket->key) { + // logger->error( "can not send to your self!"); + // return -1; + // } SHMQueue<shm_msg_t> *remoteQueue; if ((remoteQueue = _attach_remote_queue(key)) == NULL) { @@ -342,7 +371,9 @@ socket->key = hashtable_alloc_key(hashtable); } else { - _shm_socket_check_key(socket); + if(!_shm_socket_check_key(socket)) { + return -1; + } } socket->queue = new SHMQueue<shm_msg_t>(socket->key, 16); @@ -361,11 +392,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; @@ -374,25 +412,84 @@ } } -int shm_sendandrecv(shm_socket_t *socket, const void *send_buf, + + /* Free thread-specific data buffer */ +static void _destrory_tmp_recv_socket_(void *tmp_socket) +{ + int rv; + if(tmp_socket == NULL) + return; + + logger->debug("%d destroy tmp socket\n", pthread_self()); + 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); + } +} + +/* One-time key creation function */ +static void _create_tmp_recv_socket_key(void) +{ + int s; + + /* Allocate a unique thread-specific data key and save the address + of the destructor for thread-specific data buffers */ + s = pthread_key_create(&_tmp_recv_socket_key_, _destrory_tmp_recv_socket_); + //s = pthread_key_create(&_tmp_recv_socket_key_, NULL); + if (s != 0) { + logger->error(s, "pthread_key_create"); + abort(); /* dump core and terminate */ + exit(1); + } +} + + + +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; + int rv; + + // 鐢╰hread local 淇濊瘉姣忎釜绾跨▼鐢ㄤ竴涓嫭鍗犵殑socket鎺ュ彈瀵规柟杩斿洖鐨勪俊鎭� + shm_socket_t *tmp_socket; + if (socket->socket_type != SHM_SOCKET_DGRAM) { logger->error( "shm_socket.shm_sendandrecv: Can't invoke shm_sendandrecv method in a %d type socket " "which is not a SHM_SOCKET_DGRAM socket ", socket->socket_type); exit(1); } - int recv_key; - int rv; + - shm_socket_t *tmp_socket = shm_open_socket(SHM_SOCKET_DGRAM); + rv = pthread_once(&_once_, _create_tmp_recv_socket_key); + if (rv != 0) { + logger->error(rv, "shm_sendandrecv pthread_once"); + exit(1); + } + + tmp_socket = (shm_socket_t *)pthread_getspecific(_tmp_recv_socket_key_); + if (tmp_socket == NULL) + { + /* If first call from this thread, allocate buffer for thread, and save its location */ + logger->debug("%d create tmp socket\n", pthread_self() ); + tmp_socket = shm_open_socket(SHM_SOCKET_DGRAM); + + rv = pthread_setspecific(_tmp_recv_socket_key_, tmp_socket); + if ( rv != 0) { + logger->error(rv, "shm_sendandrecv : pthread_setspecific"); + exit(1); + } + } + if ((rv = shm_sendto(tmp_socket, send_buf, send_size, send_key, timeout, flags)) == 0) { rv = shm_recvfrom(tmp_socket, recv_buf, recv_size, &recv_key, timeout, flags); - shm_close_socket(tmp_socket); + return rv; } else { - shm_close_socket(tmp_socket); + return rv; } return -1; @@ -420,6 +517,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); +} + + // ============================================================================================================ /** -- Gitblit v1.8.0