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 | 191 +++++++++++++++++++++++++++++++++++++---------- 1 files changed, 148 insertions(+), 43 deletions(-) diff --git a/src/socket/dgram_mod_socket.c b/src/socket/dgram_mod_socket.c index 4856204..5efc9ca 100644 --- a/src/socket/dgram_mod_socket.c +++ b/src/socket/dgram_mod_socket.c @@ -13,15 +13,29 @@ #define TOPIC_LIDENTIFIER "{" #define TOPIC_RIDENTIFIER "}" +enum socket_mod_t +{ + PULL_PUSH = 1, + REQ_REP = 2, + PAIR = 3, + PUB_SUB = 4, + SURVEY = 5, + BUS = 6 + +}; static Logger logger = LoggerFactory::getLogger(); +#define BUS_MAP_KEY 1 +//typedef std::basic_string<char, std::char_traits<char>, SHM_STL_Allocator<char> > SHMString; +typedef std::set<int, std::less<int>, SHM_STL_Allocator<int> > SHMKeySet; +typedef std::map<SHMString, SHMKeySet *, std::less<SHMString>, SHM_STL_Allocator<std::pair<SHMString, SHMKeySet *> > > SHMTopicSubMap; typedef struct dgram_mod_socket_t { - socket_mod_t mod; shm_socket_t *shm_socket; + socket_mod_t mod; // pthread_t recv_thread; // <涓婚锛� 璁㈤槄鑰�> - std::map<std::string, std::set<int> *> *topic_sub_map; + SHMTopicSubMap *topic_sub_map; } dgram_mod_socket_t; static int parse_pubsub_topic(char *str, size_t size, char **_action, char **_topic, size_t *head_len ); @@ -37,19 +51,22 @@ int dgram_mod_close_socket(void * _socket) { dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; - std::map<std::string, std::set<int> *> *topic_sub_map = socket->topic_sub_map; - std::set<int> *subscripter_set; - std::map<std::string, std::set<int> *>::iterator map_iter; + SHMTopicSubMap *topic_sub_map = socket->topic_sub_map; + SHMKeySet *subscripter_set; + SHMTopicSubMap::iterator map_iter; if(topic_sub_map != NULL) { for (map_iter = topic_sub_map->begin(); map_iter != topic_sub_map->end(); map_iter++) { subscripter_set = map_iter->second; - delete subscripter_set; + subscripter_set->clear(); + mm_free((void *)subscripter_set); + //delete subscripter_set; + // printf("=============delete subscripter_set\n"); } - delete topic_sub_map; + topic_sub_map->clear(); + mem_pool_free_by_key(BUS_MAP_KEY); } - - + // printf("=============close socket\n"); shm_close_socket(socket->shm_socket); free(_socket); } @@ -60,44 +77,90 @@ return shm_socket_bind(socket->shm_socket, port); } + +int dgram_mod_force_bind(void * _socket, int port) { + dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; + 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_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 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); } +// =================bus======================== -int dgram_mod_get_socket_port(void * _socket) { +int dgram_mod_start_bus(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); -} - -int start_bus(void * _socket) { - dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket; - socket->topic_sub_map = new std::map<std::string, std::set<int> *>; + socket->mod = BUS; + socket->topic_sub_map = mem_pool_attach<SHMTopicSubMap>(BUS_MAP_KEY); + run_pubsub_proxy(socket); // pthread_t tid; // pthread_create(&tid, NULL, run_accept_sub_request, _socket); @@ -108,17 +171,32 @@ /** * @port 鎬荤嚎绔彛 */ -int 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 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; @@ -126,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); +} //========================================================================================================================== @@ -137,16 +239,17 @@ * 澶勭悊璁㈤槄 */ void _proxy_sub(dgram_mod_socket_t *socket, char *topic, int port) { - std::map<std::string, std::set<int> *> *topic_sub_map = socket->topic_sub_map; - std::set<int> *subscripter_set; + SHMTopicSubMap *topic_sub_map = socket->topic_sub_map; + SHMKeySet *subscripter_set; - std::map<std::string, std::set<int> *>::iterator map_iter; - std::set<int>::iterator set_iter; + SHMTopicSubMap::iterator map_iter; + SHMKeySet::iterator set_iter; if( (map_iter = topic_sub_map->find(topic) ) != topic_sub_map->end()) { subscripter_set = map_iter->second; } else { - subscripter_set = new std::set<int>; + void *set_ptr = mm_malloc(sizeof(SHMKeySet)); + subscripter_set = new(set_ptr) SHMKeySet; topic_sub_map->insert({topic, subscripter_set}); } subscripter_set->insert(port); @@ -156,11 +259,11 @@ * 澶勭悊鍙戝竷锛屼唬鐞嗚浆鍙� */ void _proxy_pub(dgram_mod_socket_t * socket, char *topic, size_t head_len, void *buf, size_t size, int port) { - std::map<std::string, std::set<int> *> *topic_sub_map = socket->topic_sub_map; - std::set<int> *subscripter_set; + SHMTopicSubMap *topic_sub_map = socket->topic_sub_map; + SHMKeySet *subscripter_set; - std::map<std::string, std::set<int> *>::iterator map_iter; - std::set<int>::iterator set_iter; + SHMTopicSubMap::iterator map_iter; + SHMKeySet::iterator set_iter; std::vector<int> subscripter_to_del; std::vector<int>::iterator vector_iter; @@ -172,12 +275,14 @@ subscripter_set = map_iter->second; for(set_iter = subscripter_set->begin(); set_iter != subscripter_set->end(); set_iter++) { send_port = *set_iter; -// printf("run_accept_sub_request send before %d \n", send_port); + // printf("_proxy_pub send before %d \n", send_port); if (shm_sendto(socket->shm_socket, buf+head_len, size-head_len, send_port, &timeout) !=0 ) { //瀵规柟宸插叧闂殑杩炴帴鏀惧埌寰呭垹闄ら槦鍒楅噷銆傚鏋滅洿鎺ュ垹闄や細璁﹊ter鎸囬拡鍑虹幇閿欎贡 subscripter_to_del.push_back(send_port); + } else { +// printf("_proxy_pub send after: %d \n", send_port); } -// printf("run_accept_sub_request send after: %d \n", send_port); + } @@ -201,9 +306,9 @@ size_t head_len; const char *topic_delim = ","; -//printf("server receive before\n"); +// printf("run_pubsub_proxy server receive before\n"); while(shm_recvfrom(socket->shm_socket, (void **)&buf, &size, &port) == 0) { -//printf("server recv after: %s \n", buf); +// printf("run_pubsub_proxy server recv after: %s \n", buf); if(parse_pubsub_topic(buf, size, &action, &topics, &head_len)) { if(strcmp(action, "sub") == 0) { // 璁㈤槄鏀寔澶氫富棰樿闃� -- Gitblit v1.8.0