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/dgram_mod_socket.c | 106 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 90 insertions(+), 16 deletions(-) diff --git a/src/socket/dgram_mod_socket.c b/src/socket/dgram_mod_socket.c index 4c79df2..5efc9ca 100644 --- a/src/socket/dgram_mod_socket.c +++ b/src/socket/dgram_mod_socket.c @@ -83,43 +83,78 @@ return shm_socket_force_bind(socket->shm_socket, port); } + + int dgram_mod_sendto(void *_socket, const void *buf, const int size, const int port) { dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; - return shm_sendto(socket->shm_socket, buf, size, port); + return shm_sendto(socket->shm_socket, buf, size, port, NULL, 0); } -int dgram_mod_recvfrom(void *_socket, void **buf, int *size, int *port) { +int dgram_mod_sendto_timeout(void *_socket, const void *buf, const int size, const int port, int sec, int nsec) { + dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; + struct timespec timeout = {sec, nsec}; + return shm_sendto(socket->shm_socket, buf, size, port, &timeout, 0); + +} + +int dgram_mod_sendto_nowait(void *_socket, const void *buf, const int size, const int port) { + dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; + return shm_sendto(socket->shm_socket, buf, size, port, NULL, (int)SHM_MSG_NOWAIT); + +} + +static inline int _dgram_mod_recvfrom_(void *_socket, void **buf, int *size, int *port, struct timespec *timeout, int flags) { dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; if(socket->mod == BUS) { err_exit(0, "Can not use method recvfrom in a Bus"); } // printf("dgram_mod_recvfrom before\n"); - int rv = shm_recvfrom(socket->shm_socket, buf, size, port); + int rv = shm_recvfrom(socket->shm_socket, buf, size, port, timeout, flags); // printf("dgram_mod_recvfrom after\n"); return rv; +} + +int dgram_mod_recvfrom(void *_socket, void **buf, int *size, int *port) { + return _dgram_mod_recvfrom_(_socket, buf, size, port, NULL, 0); +} + +int dgram_mod_recvfrom_timeout(void *_socket, void **buf, int *size, int *port, int sec, int nsec) { + struct timespec timeout = {sec, nsec}; + return _dgram_mod_recvfrom_(_socket, buf, size, port, &timeout, 0); +} + +int dgram_mod_recvfrom_nowait(void *_socket, void **buf, int *size, int *port) { + return _dgram_mod_recvfrom_(_socket, buf, size, port, NULL, (int)SHM_MSG_NOWAIT); } -int dgram_mod_sendandrecv(void * _socket, const void *send_buf, const int send_size, const int send_port, void **recv_buf, int *recv_size) { +int dgram_mod_sendandrecv(void * _socket, const void *send_buf, const int send_size, const int send_port, + void **recv_buf, int *recv_size) { dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; - return shm_sendandrecv(socket->shm_socket, send_buf, send_size, send_port, recv_buf, recv_size); + return shm_sendandrecv(socket->shm_socket, send_buf, send_size, send_port, recv_buf, recv_size, NULL, 0); } - - -int dgram_mod_get_port(void * _socket) { +int dgram_mod_sendandrecv_timeout(void * _socket, const void *send_buf, const int send_size, const int send_port, + void **recv_buf, int *recv_size, int sec, int nsec) { + struct timespec timeout = {sec, nsec}; dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; - return socket->shm_socket->port; + return shm_sendandrecv(socket->shm_socket, send_buf, send_size, send_port, recv_buf, recv_size, &timeout, 0); + +} + +int dgram_mod_sendandrecv_nowait(void * _socket, const void *send_buf, const int send_size, const int send_port, + void **recv_buf, int *recv_size) { + dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; + return shm_sendandrecv(socket->shm_socket, send_buf, send_size, send_port, recv_buf, recv_size, 0, (int)SHM_MSG_NOWAIT); + } -void dgram_mod_free(void *buf) { - free(buf); -} +// =================bus======================== int dgram_mod_start_bus(void * _socket) { dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; @@ -136,17 +171,32 @@ /** * @port 鎬荤嚎绔彛 */ -int dgram_mod_sub(void * _socket, void *topic, int size, int port) { +static int _dgram_mod_sub_(void * _socket, void *topic, int size, int port, + struct timespec *timeout, int flags) { dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; char buf[8192]; snprintf(buf, 8192, "%ssub%s%s%s%s", ACTION_LIDENTIFIER, ACTION_RIDENTIFIER, TOPIC_LIDENTIFIER, (char *)topic, TOPIC_RIDENTIFIER); - return shm_sendto(socket->shm_socket, buf, strlen(buf) + 1, port); + return shm_sendto(socket->shm_socket, buf, strlen(buf) + 1, port, timeout, flags); +} + +int dgram_mod_sub(void * _socket, void *topic, int size, int port ) { + return _dgram_mod_sub_(_socket, topic, size, port, NULL, 0); +} + +int dgram_mod_sub_timeout(void * _socket, void *topic, int size, int port, int sec, int nsec) { + struct timespec timeout = {sec, nsec}; + return _dgram_mod_sub_(_socket, topic, size, port, &timeout, 0); +} + +int dgram_mod_sub_nowait(void * _socket, void *topic, int size, int port) { + return _dgram_mod_sub_(_socket, topic, size, port, NULL, (int)SHM_MSG_NOWAIT); } /** * @port 鎬荤嚎绔彛 */ -int dgram_mod_pub(void * _socket, void *topic, int topic_size, void *content, int content_size, int port) { +static int _dgram_mod_pub_(void * _socket, void *topic, int topic_size, void *content, int content_size, int port, + struct timespec *timeout, int flags) { dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; int head_len; @@ -154,10 +204,34 @@ snprintf(buf, 8192, "%spub%s%s%s%s", ACTION_LIDENTIFIER, ACTION_RIDENTIFIER, TOPIC_LIDENTIFIER, (char *)topic, TOPIC_RIDENTIFIER); head_len = strlen(buf); memcpy(buf+head_len, content, content_size); - return shm_sendto(socket->shm_socket, buf, head_len+content_size, port); + return shm_sendto(socket->shm_socket, buf, head_len+content_size, port, timeout, flags); } +int dgram_mod_pub(void * _socket, void *topic, int topic_size, void *content, int content_size, int port) { + return _dgram_mod_pub_(_socket, topic, topic_size, content, content_size, port, NULL, 0); +} + +int dgram_mod_pub_timeout(void * _socket, void *topic, int topic_size, void *content, int content_size, int port, int sec, int nsec) { + struct timespec timeout = {sec, nsec}; + return _dgram_mod_pub_(_socket, topic, topic_size, content, content_size, port, &timeout, 0); +} + +int dgram_mod_pub_nowait(void * _socket, void *topic, int topic_size, void *content, int content_size, int port) { + return _dgram_mod_pub_(_socket, topic, topic_size, content, content_size, port, NULL, (int)SHM_MSG_NOWAIT); +} + + + +int dgram_mod_get_port(void * _socket) { + dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; + return socket->shm_socket->port; +} + + +void dgram_mod_free(void *buf) { + free(buf); +} //========================================================================================================================== -- Gitblit v1.8.0