From f1ae0552e2d50ce6f87a0bad2f31b8e2ec617bb7 Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期三, 03 二月 2021 09:39:54 +0800 Subject: [PATCH] update --- src/socket/shm_socket.cpp | 126 ++++++++++++++++++++++++----------------- 1 files changed, 73 insertions(+), 53 deletions(-) diff --git a/src/socket/shm_socket.cpp b/src/socket/shm_socket.cpp index a76cb4b..133190a 100644 --- a/src/socket/shm_socket.cpp +++ b/src/socket/shm_socket.cpp @@ -148,27 +148,31 @@ hashtable_t *hashtable = mm_get_hashtable(); + if( sockt->queue != NULL) + goto LABEL_PUSH; - if ((rv = pthread_mutex_lock(&(sockt->mutex))) != 0) + { + 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); + 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; + } } - 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"); + if ((rv = pthread_mutex_unlock(&(sockt->mutex))) != 0) + err_exit(rv, "shm_sendto : pthread_mutex_unlock"); - // There is some case where a sockt need to send to himeself, for example when bus server need to stop, he need to send himself - // a top message. + } + + LABEL_PUSH: if (key == sockt->key) { logger->error( "can not send to your self!"); return EBUS_SENDTO_SELF; @@ -182,7 +186,6 @@ } shm_packet_t dest; - dest.type = SHM_COMMON_MSG; dest.key = sockt->key; dest.size = size; dest.buf = mm_malloc(size); @@ -211,10 +214,14 @@ hashtable_t *hashtable = mm_get_hashtable(); - if ((rv = pthread_mutex_lock(&(sockt->mutex))) != 0) - err_exit(rv, "shm_recvfrom : pthread_mutex_lock"); + 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->queue == NULL) { + if (sockt->key == 0) { sockt->key = hashtable_alloc_key(hashtable); } @@ -223,10 +230,14 @@ 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"); + } - if ((rv = pthread_mutex_unlock(&(sockt->mutex))) != 0) - err_exit(rv, "shm_recvfrom : pthread_mutex_unlock"); +LABEL_POP: shm_packet_t src; @@ -287,7 +298,6 @@ //s = pthread_key_create(&_perthread_socket_key_, NULL); if (s != 0) { logger->error(s, "pthread_key_create"); - abort(); /* dump core and terminate */ exit(1); } } @@ -299,11 +309,13 @@ int *recv_size, const struct timespec *timeout, int flags) { int recv_key; int rv; + int tryn = 0; // 鐢╰hread local 淇濊瘉姣忎釜绾跨▼鐢ㄤ竴涓嫭鍗犵殑socket鎺ュ彈瀵规柟杩斿洖鐨勪俊鎭� shm_socket_t *tmp_socket; - + /* If first call from this thread, allocate buffer for thread, and save its location */ + // logger->debug("%d create tmp socket\n", pthread_self() ); rv = pthread_once(&_once_, _create_socket_key_perthread); if (rv != 0) { logger->error(rv, "shm_sendandrecv pthread_once"); @@ -325,26 +337,31 @@ } 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); - if(rv != 0) { - logger->error("_shm_sendandrecv_thread_local : %s\n", bus_strerror(rv)); - } - else if(rv == 0 ) { - logger->debug("======%d use tmp_socket %d, send to %d, receive from %d\n", shm_socket_get_key(sockt), shm_socket_get_key(tmp_socket), send_key, recv_key); - - if(recv_key == shm_socket_get_key(sockt)) { - logger->debug("=====鏀跺埌浜嗚嚜宸卞彂缁欒嚜宸辩殑娑堟伅\n"); + + while(tryn < 3) { + tryn++; + rv = shm_recvfrom(tmp_socket, recv_buf, recv_size, &recv_key, timeout, flags); + if(rv != 0) { + logger->error("_shm_sendandrecv_thread_local : %s\n", bus_strerror(rv)); + return rv; } - assert( send_key == recv_key); + + // 瓒呮椂瀵艰嚧鎺ュ彂閫佸璞★紝涓庤繑鍥炲璞′笉瀵瑰簲鐨勬儏鍐� if(send_key != recv_key) { - logger->error( "_shm_sendandrecv_alloc_new: send key expect to equal to recv key! send key =%d , recv key=%d", send_key, recv_key); - exit(1); + logger->debug("======%d use tmp_socket %d, send to %d, receive from %d\n", shm_socket_get_key(sockt), shm_socket_get_key(tmp_socket), send_key, recv_key); + // logger->error( "_shm_sendandrecv_alloc_new: send key expect to equal to recv key! send key =%d , recv key=%d", send_key, recv_key); + // exit(1); + continue; + // return EBUS_RECVFROM_WRONG_END; } + + return 0; } - return rv; - } else { - return rv; - } + + return EBUS_RECVFROM_WRONG_END; + } + + return rv; } int _shm_sendandrecv_alloc_new(shm_socket_t *sockt, const void *send_buf, @@ -353,31 +370,34 @@ int recv_key; int rv; - // 鐢╰hread local 淇濊瘉姣忎釜绾跨▼鐢ㄤ竴涓嫭鍗犵殑socket鎺ュ彈瀵规柟杩斿洖鐨勪俊鎭� + int tryn = 0; shm_socket_t *tmp_socket; - /* 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); 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); - if(rv != 0) { - printf("_shm_sendandrecv_alloc_new : %s\n", bus_strerror(rv)); - } - else if(rv == 0 ) { - printf("======%d use tmp_socket %d, send to %d, receive from %d\n", shm_socket_get_key(sockt), shm_socket_get_key(tmp_socket), send_key, recv_key); - - if(recv_key == shm_socket_get_key(sockt)) { - printf("=====鏀跺埌浜嗚嚜宸卞彂缁欒嚜宸辩殑娑堟伅\n"); - } - assert( send_key == recv_key); - if(send_key != recv_key) { - err_exit(0, "_shm_sendandrecv_alloc_new: send key expect to equal to recv key! send key =%d , recv key=%d", send_key, recv_key); + while(tryn < 3) { + tryn++; + rv = shm_recvfrom(tmp_socket, recv_buf, recv_size, &recv_key, timeout, flags); + if(rv != 0) { + logger->error("_shm_sendandrecv_thread_local : %s\n", bus_strerror(rv)); + return rv; } + // 瓒呮椂瀵艰嚧鎺ュ彂閫佸璞★紝涓庤繑鍥炲璞′笉瀵瑰簲鐨勬儏鍐� + if(send_key != recv_key) { + // logger->debug("======%d use tmp_socket %d, send to %d, receive from %d\n", shm_socket_get_key(sockt), shm_socket_get_key(tmp_socket), send_key, recv_key); + // logger->error( "_shm_sendandrecv_alloc_new: send key expect to equal to recv key! send key =%d , recv key=%d", send_key, recv_key); + + continue; + } + return 0; } + + return EBUS_RECVFROM_WRONG_END; } shm_close_socket(tmp_socket); -- Gitblit v1.8.0