From 76bd3ff05443dc703c0fa1f8907301199b2c09c0 Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期三, 13 一月 2021 13:01:21 +0800 Subject: [PATCH] Merge branch 'master' of https://github.com/wangzhengquan/shmqueue --- src/socket/shm_socket.cpp | 39 ++++++++++++++++++++++++++++++++++----- 1 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/socket/shm_socket.cpp b/src/socket/shm_socket.cpp index 0ae3027..e2d3fab 100644 --- a/src/socket/shm_socket.cpp +++ b/src/socket/shm_socket.cpp @@ -386,9 +386,9 @@ } else { delete remoteQueue; mm_free(dest.buf); - if(errno == EAGAIN) { + if(errno == ETIMEDOUT) { bus_errno = EBUS_TIMEOUT; - logger->error("sendto key %d failed, %s", key, bus_strerror(bus_errno)); + logger->error(errno, "sendto key %d failed, %s", key, bus_strerror(EBUS_TIMEOUT)); return EBUS_TIMEOUT; } else { logger->error(errno, "sendto key %d failed!", key); @@ -498,8 +498,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; @@ -547,6 +547,35 @@ 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, const int send_size, const int send_key, void **recv_buf, int *recv_size, struct timespec *timeout, int flags) { @@ -572,7 +601,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_safe(socket, send_buf, send_size, send_key,recv_buf, recv_size, timeout, flags); + return shm_sendandrecv_unsafe(socket, send_buf, send_size, send_key,recv_buf, recv_size, timeout, flags); } -- Gitblit v1.8.0