From 53e55dbd8709a2f948cf80305545fe88ff93169a Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期五, 08 一月 2021 16:03:10 +0800 Subject: [PATCH] udpate --- src/socket/shm_socket.cpp | 35 ++++++++++++++++++++++++++++++++--- 1 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/socket/shm_socket.cpp b/src/socket/shm_socket.cpp index 0ae3027..3e2dea9 100644 --- a/src/socket/shm_socket.cpp +++ b/src/socket/shm_socket.cpp @@ -49,7 +49,7 @@ int s, type; pthread_mutexattr_t mtxAttr; - logger->debug("shm_open_socket\n"); + // 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; @@ -78,7 +78,7 @@ int ret, s; - logger->debug("shm_close_socket\n"); + // logger->debug("shm_close_socket\n"); switch (socket->socket_type) { case SHM_SOCKET_STREAM: ret = _shm_close_stream_socket(socket, true); @@ -499,7 +499,7 @@ -int shm_sendandrecv_safe(shm_socket_t *socket, const void *send_buf, +int shm_sendandrecv_safe2(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_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); + } + + /* 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) { -- Gitblit v1.8.0