From 01e202b60cc8aceb65fd6c82949e2a2b08142f8c Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期一, 18 一月 2021 10:44:50 +0800 Subject: [PATCH] ADD EBUS_NET --- src/socket/shm_socket.cpp | 118 +++++++++++++++++++++++++++++++++++++--------------------- 1 files changed, 75 insertions(+), 43 deletions(-) diff --git a/src/socket/shm_socket.cpp b/src/socket/shm_socket.cpp index 857e7e6..534202d 100644 --- a/src/socket/shm_socket.cpp +++ b/src/socket/shm_socket.cpp @@ -30,8 +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 ) { - bus_errno = ESHM_BUS_KEY_INUSED; - logger->error("%s. key = %d ", bus_strerror(bus_errno), socket->key); + bus_errno = EBUS_KEY_INUSED; + logger->error("%s. key = %d ", bus_strerror(EBUS_KEY_INUSED), socket->key); return 0; } return 1; @@ -52,7 +52,7 @@ 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; + socket->key = 0; socket->force_bind = false; socket->dispatch_thread = 0; socket->status = SHM_CONN_CLOSED; @@ -127,14 +127,14 @@ int key; hashtable_t *hashtable = mm_get_hashtable(); - if (socket->key == -1) { + if (socket->key == 0) { key = hashtable_alloc_key(hashtable); socket->key = key; } else { if(!_shm_socket_check_key(socket)) { - bus_errno = ESHM_BUS_KEY_INUSED; - return ESHM_BUS_KEY_INUSED; + bus_errno = EBUS_KEY_INUSED; + return EBUS_KEY_INUSED; } } @@ -163,7 +163,7 @@ shm_socket_t *client_socket; shm_msg_t src; - if (socket->acceptQueue->pop(src)) { + if (socket->acceptQueue->pop(src) == 0) { // print_msg("===accept:", src); client_key = src.key; @@ -190,7 +190,7 @@ msg.size = 0; msg.type = SHM_SOCKET_OPEN_REPLY; - if (client_socket->remoteQueue->push_timeout(msg, &timeout)) { + if (client_socket->remoteQueue->push_timeout(msg, &timeout) == 0) { client_socket->status = SHM_CONN_ESTABLISHED; return client_socket; } else { @@ -220,12 +220,12 @@ return -1; } - if (socket->key == -1) { + if (socket->key == 0) { socket->key = hashtable_alloc_key(hashtable); } else { if(!_shm_socket_check_key(socket)) { - bus_errno = ESHM_BUS_KEY_INUSED; - return ESHM_BUS_KEY_INUSED; + bus_errno = EBUS_KEY_INUSED; + return EBUS_KEY_INUSED; } } @@ -246,7 +246,7 @@ socket->remoteQueue->push_timeout(msg, &timeout); //鎺ュ彈open reply - if (socket->queue->pop(msg)) { + if (socket->queue->pop(msg) == 0) { // 鍦ㄨ繖閲宻erver绔凡缁忓噯澶囧ソ鎺ュ彈瀹㈡埛绔彂閫佽姹備簡,瀹屾垚涓庢湇鍔$鐨勮繛鎺� if (msg.type == SHM_SOCKET_OPEN_REPLY) { socket->status = SHM_CONN_ESTABLISHED; @@ -283,7 +283,7 @@ dest.buf = mm_malloc(size); memcpy(dest.buf, buf, size); - if (socket->remoteQueue->push(dest)) { + if (socket->remoteQueue->push(dest) == 0) { return 0; } else { logger->error(errno, "connection has been closed!"); @@ -300,7 +300,7 @@ } shm_msg_t src; - if (socket->messageQueue->pop(src)) { + if (socket->messageQueue->pop(src) == 0) { void *_buf = malloc(src.size); memcpy(_buf, src.buf, src.size); *buf = _buf; @@ -318,7 +318,7 @@ const int key, const struct timespec *timeout, const int flags) { int s; - bool rv; + int rv; if (socket->socket_type != SHM_SOCKET_DGRAM) { logger->error( "shm_socket.shm_sendto: Can't invoke shm_sendto method in a %d type socket which is " @@ -333,13 +333,13 @@ err_exit(s, "shm_sendto : pthread_mutex_lock"); if (socket->queue == NULL) { - if (socket->key == -1) { + if (socket->key == 0) { socket->key = hashtable_alloc_key(hashtable); } else { if(!_shm_socket_check_key(socket)) { - bus_errno = ESHM_BUS_KEY_INUSED; - return ESHM_BUS_KEY_INUSED; + bus_errno = EBUS_KEY_INUSED; + return EBUS_KEY_INUSED; } } @@ -349,6 +349,9 @@ if ((s = pthread_mutex_unlock(&(socket->mutex))) != 0) err_exit(s, "shm_sendto : pthread_mutex_unlock"); + + // There is some case where a socket need to send to himeself, for example when bus server need to stop, he need to send himself + // a top message. // if (key == socket->key) { // logger->error( "can not send to your self!"); @@ -369,7 +372,6 @@ dest.buf = mm_malloc(size); memcpy(dest.buf, buf, size); - // printf("shm_sendto push before\n"); if(flags & SHM_MSG_NOWAIT != 0) { rv = remoteQueue->push_nowait(dest); @@ -379,21 +381,20 @@ rv = remoteQueue->push(dest); } - if (rv) { + if (rv == 0) { // printf("shm_sendto push after\n"); delete remoteQueue; return 0; } else { delete remoteQueue; mm_free(dest.buf); - if(errno == EAGAIN) { - bus_errno = EBUS_TIMEOUT; - logger->error("sendto key %d failed, %s", key, bus_strerror(bus_errno)); - return EBUS_TIMEOUT; + if(rv > EBUS_BASE) { + // bus_errno = EBUS_TIMEOUT; + logger->debug("sendto key %d failed %s", key, bus_strerror(rv)); } else { - logger->error(errno, "sendto key %d failed!", key); - return -1; + logger->error(rv, "sendto key %d failed", key); } + return rv; } @@ -402,7 +403,7 @@ // 鐭繛鎺ユ柟寮忔帴鍙� int shm_recvfrom(shm_socket_t *socket, void **buf, int *size, int *key, struct timespec *timeout, int flags) { int s; - bool rv; + int rv; if (socket->socket_type != SHM_SOCKET_DGRAM) { logger->error("shm_socket.shm_recvfrom: Can't invoke shm_recvfrom method in a %d type socket which " @@ -416,13 +417,13 @@ err_exit(s, "shm_recvfrom : pthread_mutex_lock"); if (socket->queue == NULL) { - if (socket->key == -1) { + if (socket->key == 0) { socket->key = hashtable_alloc_key(hashtable); } else { if(!_shm_socket_check_key(socket)) { - bus_errno = ESHM_BUS_KEY_INUSED; - return ESHM_BUS_KEY_INUSED; + bus_errno = EBUS_KEY_INUSED; + return EBUS_KEY_INUSED; } } @@ -433,17 +434,17 @@ err_exit(s, "shm_recvfrom : pthread_mutex_unlock"); shm_msg_t src; - // printf("shm_recvfrom pop before\n"); if(flags & SHM_MSG_NOWAIT != 0) { rv = socket->queue->pop_nowait(src); } else if(timeout != NULL) { rv = socket->queue->pop_timeout(src, timeout); +// printf("0 shm_recvfrom====%d\n", rv); } else { rv = socket->queue->pop(src); } - if (rv) { + if (rv == 0) { if(buf != NULL) { void *_buf = malloc(src.size); memcpy(_buf, src.buf, src.size); @@ -457,10 +458,15 @@ *key = src.key; mm_free(src.buf); - // printf("shm_recvfrom pop after\n"); return 0; } else { - return -1; + if(rv > EBUS_BASE) { + logger->debug("shm_recvfrom failed %s", bus_strerror(rv)); + } else { + logger->error(rv, "shm_recvfrom failed"); + } + return rv; + } } @@ -498,8 +504,8 @@ } - -int shm_sendandrecv_safe(shm_socket_t *socket, const void *send_buf, +// use thread local +int _shm_sendandrecv_thread_local(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; @@ -526,7 +532,7 @@ 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() ); + logger->debug("%ld create tmp socket\n", (long)pthread_self() ); tmp_socket = shm_open_socket(SHM_SOCKET_DGRAM); rv = pthread_setspecific(_tmp_recv_socket_key_, tmp_socket); @@ -538,13 +544,39 @@ 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); - return rv; } else { - return rv; } - return -1; +} + +int _shm_sendandrecv_alloc_new(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); + } + + /* 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); + } + + shm_close_socket(tmp_socket); + return rv; + } int shm_sendandrecv_unsafe(shm_socket_t *socket, const void *send_buf, @@ -572,7 +604,7 @@ 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); + return _shm_sendandrecv_thread_local(socket, send_buf, send_size, send_key,recv_buf, recv_size, timeout, flags); } @@ -615,7 +647,7 @@ shm_socket_t *client_socket; std::map<int, shm_socket_t *>::iterator iter; - while (socket->queue->pop(src)) { + while (socket->queue->pop(src) == 0) { switch (src.type) { case SHM_SOCKET_OPEN: @@ -658,7 +690,7 @@ struct timespec timeout = {1, 0}; shm_msg_t src; - while (socket->queue->pop(src)) { + while (socket->queue->pop(src) == 0) { switch (src.type) { case SHM_SOCKET_CLOSE: -- Gitblit v1.8.0