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 |  196 +++++++++++++++++++++++++++++++++++-------------
 1 files changed, 141 insertions(+), 55 deletions(-)

diff --git a/src/socket/dgram_mod_socket.c b/src/socket/dgram_mod_socket.c
index 5a7a0b7..5efc9ca 100644
--- a/src/socket/dgram_mod_socket.c
+++ b/src/socket/dgram_mod_socket.c
@@ -13,6 +13,16 @@
 #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
@@ -22,6 +32,7 @@
 
 typedef struct dgram_mod_socket_t {
   shm_socket_t *shm_socket;
+  socket_mod_t mod;
   // pthread_t recv_thread;
   // <涓婚锛� 璁㈤槄鑰�>
 	SHMTopicSubMap *topic_sub_map;
@@ -47,12 +58,15 @@
 	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");
 		}
+		topic_sub_map->clear();
 		mem_pool_free_by_key(BUS_MAP_KEY);
 	}
-	
-
+	// printf("=============close socket\n");
 	shm_close_socket(socket->shm_socket);
 	free(_socket);
 }
@@ -69,27 +83,142 @@
 	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_start_bus(void * _socket) {
+	dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
+	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);
+	return 0;
+
+}
+
+/**
+ * @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, 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 鎬荤嚎绔彛
+ */
+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;
+	char buf[8192+content_size];
+	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, 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);
 }
 
 
@@ -103,49 +232,6 @@
 void dgram_mod_free(void *buf) {
 	free(buf);
 }
-
-int  dgram_mod_start_bus(void * _socket) {
-	dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
-printf("mem_pool_malloc_by_key before\n");
-	// void *map_ptr = mem_pool_malloc_by_key(1, sizeof(SHMTopicSubMap));
-	socket->topic_sub_map =	mem_pool_attach<SHMTopicSubMap>(BUS_MAP_KEY);
-printf("mem_pool_malloc_by_key after\n");
-
-	// socket->topic_sub_map = new(map_ptr) SHMTopicSubMap;
-
-	//socket->topic_sub_map = new SHMTopicSubMap;
-	run_pubsub_proxy(socket);
-	// pthread_t tid;
-	// pthread_create(&tid, NULL, run_accept_sub_request, _socket);
-	return 0;
-
-}
-
-/**
- * @port 鎬荤嚎绔彛
- */
-int  dgram_mod_sub(void * _socket, void *topic, int size, int port) {
-	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);
-}
-
-/**
- * @port 鎬荤嚎绔彛
- */
-int  dgram_mod_pub(void * _socket, void *topic, int topic_size, void *content, int content_size, int port) {
-
-	dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
-	int head_len;
-	char buf[8192+content_size];
-	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);
-
-}
-
 
 //==========================================================================================================================
 
@@ -189,12 +275,12 @@
 		subscripter_set = map_iter->second;
 		for(set_iter = subscripter_set->begin(); set_iter != subscripter_set->end(); set_iter++) {
 			send_port = *set_iter;
- printf("_proxy_pub 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("_proxy_pub send after: %d \n", send_port);
 			}
 
 			
@@ -220,9 +306,9 @@
 	size_t head_len;
 
 	const char *topic_delim = ",";
-printf("run_pubsub_proxy server receive before\n");
+// printf("run_pubsub_proxy server receive before\n");
 	while(shm_recvfrom(socket->shm_socket, (void **)&buf, &size, &port) == 0) {
-printf("run_pubsub_proxy 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