From b63ce299ddacea2ad487dc635926ed52ff422c20 Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期一, 03 八月 2020 11:40:17 +0800 Subject: [PATCH] add timeout nowait --- src/socket/shm_socket.c | 38 ++++++++++++++++++++++++++------------ 1 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/socket/shm_socket.c b/src/socket/shm_socket.c index 4a705ad..f95379a 100644 --- a/src/socket/shm_socket.c +++ b/src/socket/shm_socket.c @@ -5,17 +5,19 @@ static Logger logger = LoggerFactory::getLogger(); + + void print_msg(char *head, shm_msg_t &msg) { // err_msg(0, "%s: port=%d, type=%d\n", head, msg.port, msg.type); } -void *_server_run_msg_rev(void *_socket); +static void *_server_run_msg_rev(void *_socket); -void *_client_run_msg_rev(void *_socket); +static void *_client_run_msg_rev(void *_socket); -int _shm_close_dgram_socket(shm_socket_t *socket); +static int _shm_close_dgram_socket(shm_socket_t *socket); -int _shm_close_stream_socket(shm_socket_t *socket, bool notifyRemote); +static int _shm_close_stream_socket(shm_socket_t *socket, bool notifyRemote); static inline int _shm_socket_check_key(shm_socket_t *socket) { void *tmp_ptr = mm_get_by_key(socket->port); @@ -239,9 +241,10 @@ } } + // 鐭繛鎺ユ柟寮忓彂閫� int shm_sendto(shm_socket_t *socket, const void *buf, const int size, - const int port, const struct timespec *timeout) { + const int port, const struct timespec *timeout, const int flags) { if (socket->socket_type != SHM_SOCKET_DGRAM) { err_exit(0, "Can't invoke shm_sendto method in a %d type socket which is " "not a SHM_SOCKET_DGRAM socket ", @@ -273,12 +276,14 @@ SHMQueue<shm_msg_t> *remoteQueue; if ((remoteQueue = _attach_remote_queue(port)) == NULL) { - err_msg(0, "shm_sendto failed, then other end has been closed!"); + err_msg(0, "shm_sendto failed, the other end has been closed, or has not been opened!"); return -1; } // printf("shm_sendto push before\n"); bool rv; - if(timeout != NULL) { + if(flags & SHM_MSG_NOWAIT != 0) { + rv = remoteQueue->push_nowait(dest); + } else if(timeout != NULL) { rv = remoteQueue->push_timeout(dest, timeout); } else { rv = remoteQueue->push(dest); @@ -296,7 +301,7 @@ } // 鐭繛鎺ユ柟寮忔帴鍙� -int shm_recvfrom(shm_socket_t *socket, void **buf, int *size, int *port) { +int shm_recvfrom(shm_socket_t *socket, void **buf, int *size, int *port, struct timespec *timeout, int flags) { if (socket->socket_type != SHM_SOCKET_DGRAM) { err_exit(0, "Can't invoke shm_recvfrom method in a %d type socket which " "is not a SHM_SOCKET_DGRAM socket ", @@ -316,7 +321,16 @@ shm_msg_t src; // printf("shm_recvfrom pop before\n"); - if (socket->queue->pop(src)) { + bool rv; + if(flags & SHM_MSG_NOWAIT != 0) { + rv = socket->queue->pop_nowait(src); + } else if(timeout != NULL) { + rv = socket->queue->pop_timeout(src, timeout); + } else { + rv = socket->queue->pop(src); + } + + if (rv) { void *_buf = malloc(src.size); memcpy(_buf, src.buf, src.size); *buf = _buf; @@ -332,7 +346,7 @@ int shm_sendandrecv(shm_socket_t *socket, const void *send_buf, const int send_size, const int send_port, void **recv_buf, - int *recv_size) { + int *recv_size, struct timespec *timeout, int flags) { if (socket->socket_type != SHM_SOCKET_DGRAM) { err_exit(0, "Can't invoke shm_sendandrecv method in a %d type socket " "which is not a SHM_SOCKET_DGRAM socket ", @@ -342,8 +356,8 @@ int rv; shm_socket_t *tmp_socket = shm_open_socket(SHM_SOCKET_DGRAM); - if (shm_sendto(tmp_socket, send_buf, send_size, send_port) == 0) { - rv = shm_recvfrom(tmp_socket, recv_buf, recv_size, &recv_port); + if (shm_sendto(tmp_socket, send_buf, send_size, send_port, timeout, flags) == 0) { + rv = shm_recvfrom(tmp_socket, recv_buf, recv_size, &recv_port, timeout, flags); shm_close_socket(tmp_socket); return rv; } -- Gitblit v1.8.0