From 509cf5db80c1d8919e19e1e061f2014e1ff3caf7 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期三, 05 八月 2020 19:44:03 +0800
Subject: [PATCH] udpate

---
 src/socket/dgram_mod_socket.c |  214 +++++++++++++++++++++-------------------------------
 1 files changed, 87 insertions(+), 127 deletions(-)

diff --git a/src/socket/dgram_mod_socket.c b/src/socket/dgram_mod_socket.c
index f9857bc..4d58458 100644
--- a/src/socket/dgram_mod_socket.c
+++ b/src/socket/dgram_mod_socket.c
@@ -1,139 +1,99 @@
-#include "usg_common.h"
 #include "dgram_mod_socket.h"
-#include "shm_socket.h"
-#include "shm_allocator.h"
-#include "mem_pool.h"
-#include "hashtable.h"
-#include "sem_util.h"
-#include "logger_factory.h"
 
-typedef struct dgram_mod_socket_t {
-	socket_mod_t mod;
-  shm_socket_t *shm_socket;
-  pthread_t recv_thread;
-	std::map<int, LockFreeQueue<shm_msg_t, DM_Allocator> * > *recv_queue_map;
-} dgram_mod_socket_t;
+/**
+ * 鍒涘缓socket
+ * @return socket鍦板潃
+*/
+void *dgram_mod_open_socket();
+
+/**
+ * 鍏抽棴socket
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
+int dgram_mod_close_socket(void * _socket);
+
+/**
+ * 缁戝畾绔彛鍒皊ocket, 濡傛灉涓嶇粦瀹氬垯绯荤粺鑷姩鍒嗛厤涓�涓�
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
+int dgram_mod_bind(void * _socket, int port);
+
+/**
+ * 寮哄埗缁戝畾绔彛鍒皊ocket, 閫傜敤浜庣▼搴忛潪姝e父鍏抽棴鐨勬儏鍐典笅锛岄噸鍚▼搴忕粦瀹氬師鏉ヨ繕娌¢噴鏀剧殑key
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
+int dgram_mod_force_bind(void * _socket, int port);
+/**
+ * 鍙戦�佷俊鎭�
+ * @port 鍙戦�佺粰璋�
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+ */
+int dgram_mod_sendto(void *_socket, const void *buf, const int size, const int port);
+// 鍙戦�佷俊鎭秴鏃惰繑鍥炪�� @sec 绉� 锛� @nsec 绾崇
+int dgram_mod_sendto_timeout(void *_socket, const void *buf, const int size, const int port, int sec, int nsec);
+// 鍙戦�佷俊鎭珛鍒昏繑鍥炪��
+int dgram_mod_sendto_nowait(void *_socket, const void *buf, const int size, const int port);
+
+/**
+ * 鎺ユ敹淇℃伅
+ * @port 浠庤皝鍝噷鏀跺埌鐨勪俊鎭�
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
+int dgram_mod_recvfrom(void *_socket, void **buf, int *size, int *port);
+// 鎺ュ彈淇℃伅瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇
+int dgram_mod_recvfrom_timeout(void *_socket, void **buf, int *size, int *port, int sec, int nsec);
+int dgram_mod_recvfrom_nowait(void *_socket, void **buf, int *size, int *port);
+
+/**
+ * 鍙戦�佽姹備俊鎭苟绛夊緟鎺ユ敹搴旂瓟
+ * @port 鍙戦�佺粰璋�
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
+int dgram_mod_sendandrecv(void * _socket, const void *send_buf, const int send_size, const int port, void **recv_buf, int *recv_size) ;
+// 瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇
+int dgram_mod_sendandrecv_timeout(void * _socket, const void *send_buf, const int send_size, const int port, void **recv_buf, int *recv_size, int sec, int nsec) ;
+int dgram_mod_sendandrecv_nowait(void * _socket, const void *send_buf, const int send_size, const int port, void **recv_buf, int *recv_size) ;
 
 
-void *dgram_mod_open_socket(int mod) {
-	dgram_mod_socket_t * socket = (dgram_mod_socket_t *)calloc(1, sizeof(dgram_mod_socket_t));
-	socket->mod = (socket_mod_t)mod;
-	socket->recv_thread = 0;
-	socket->recv_queue_map = NULL;
-	socket->shm_socket = shm_open_socket(SHM_SOCKET_DGRAM);
+/**
+ * 鍚姩bus
+ * 
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
+int  dgram_mod_start_bus(void * _socket);
 
-	printf("socket->shm_socket = %p \n" , socket->shm_socket);
-	return (void *)socket;
-}
-
-
-int dgram_mod_close_socket(void * _socket) {
-	dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
-	shm_close_socket(socket->shm_socket);
-	if(socket->recv_queue_map != NULL) {
-		for(auto iter = socket->recv_queue_map->begin(); iter != socket->recv_queue_map->end(); iter++) {
-			delete iter->second;
-			socket->recv_queue_map->erase(iter);
-			
-		}
-		delete socket->recv_queue_map;
-	}
-
-
-	if(socket->recv_thread != 0)
-		pthread_cancel(socket->recv_thread);
-	free(_socket);
-}
-
-
-int dgram_mod_bind(void * _socket, int port){
-	dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
-	return  shm_socket_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);
-
-}
-
-int dgram_mod_recvfrom(void *_socket, void **buf, int *size, int *port) {
-
-	dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
-	if(socket->mod == REQ_REP && socket->recv_thread != 0) {
-		err_exit(0, "you have used sendandrecv method os you can not use recvfrom method any more. these two method can not be used at the same time.");
-		return -1;
-	}
-	return shm_recvfrom(socket->shm_socket, buf, size, port);
-}
-
-void *_dgram_mod_run_recv(void * _socket) {
-	pthread_detach(pthread_self());
-	dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
-	void *buf;
-	int size;
-	int port;
-	shm_msg_t msg;
-	LockFreeQueue<shm_msg_t, DM_Allocator> *queue;
-	std::map<int, LockFreeQueue<shm_msg_t, DM_Allocator> * >::iterator iter;
-// printf("==============_dgram_mod_run_recv recv before\n");
-	while(shm_recvfrom(socket->shm_socket, &buf, &size, &port) == 0) {
-		if( (iter = socket->recv_queue_map->find(port) ) != socket->recv_queue_map->end()) {
-			queue = iter->second;
-		} else {
-			queue = new LockFreeQueue<shm_msg_t, DM_Allocator>(16);
-			socket->recv_queue_map->insert({port, queue});
-		}
-
-		msg.buf = buf;
-		msg.size = size;
-		msg.port = port;
-// printf("==============_dgram_mod_run_recv push before\n");
-		queue->push(msg);
-// printf("==============_dgram_mod_run_recv push after\n");
-	 
-	}
-	return NULL;
+/**
+ * 璁㈤槄鎸囧畾涓婚
+ * @topic 涓婚
+ * @size 涓婚闀垮害
+ * @port 鎬荤嚎绔彛
+ */
+int  dgram_mod_sub(void * _socket, void *topic, int size, int port);
+// 瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇
+int  dgram_mod_sub_timeout(void * _socket, void *topic, int size, int port, int sec, int nsec);
+int  dgram_mod_sub_nowait(void * _socket, void *topic, int size, int port);
 
 
 
-}
+/**
+ * 鍙戝竷涓婚
+ * @topic 涓婚
+ * @content 涓婚鍐呭
+ * @port 鎬荤嚎绔彛
+ */
+int  dgram_mod_pub(void * _socket, void *topic, int topic_size, void *content, int content_size, int port);
+//  瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇
+int  dgram_mod_pub_timeout(void * _socket, void *topic, int topic_size, void *content, int content_size, int port, int sec, int nsec);
+int  dgram_mod_pub_nowait(void * _socket, void *topic, int topic_size, void *content, int content_size, int port);
 
-int dgram_mod_sendandrecv(void * _socket, const void *send_buf, const int send_size, const int port, void **recv_buf, int *recv_size) {
-	
-	dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
-	if(socket->mod != REQ_REP) {
-		err_exit(0, "you can't use this method other than REQ_REP mod!");
-	}
-	if(socket->recv_queue_map == NULL) {
-		socket->recv_queue_map = new std::map<int, LockFreeQueue<shm_msg_t, DM_Allocator> * >;
-	}
 
-	std::map<int, LockFreeQueue<shm_msg_t, DM_Allocator> * >::iterator iter;
-	LockFreeQueue<shm_msg_t, DM_Allocator> *queue;
-	if( (iter = socket->recv_queue_map->find(port) ) != socket->recv_queue_map->end()) {
-		queue = iter->second;
-	} else {
-		queue = new LockFreeQueue<shm_msg_t, DM_Allocator>(16);
-		socket->recv_queue_map->insert({port, queue});
-	}
+/**
+ * 鑾峰彇soket绔彛鍙�
+ */
+int dgram_mod_get_port(void * _socket) ;
 
-	if (socket->recv_thread == 0) {
-		
-		pthread_create(&(socket->recv_thread ), NULL, _dgram_mod_run_recv , _socket);
-		
-	}
 
-	shm_sendto(socket->shm_socket, send_buf, send_size, port);	
-	shm_msg_t msg;
-// printf("==============dgram_mod_sendandrecv pop before\n");
-	if(queue->pop(msg)) {
-		*recv_buf = msg.buf;
-		*recv_size = msg.size;
-// printf("==============dgram_mod_sendandrecv pop after\n");
-		return 0;
-	}
-	return -1;
-
-}
\ No newline at end of file
+/**
+ * 閲婃斁瀛樺偍鎺ユ敹淇℃伅鐨刡uf
+ */
+void dgram_mod_free(void *buf) ;
\ No newline at end of file

--
Gitblit v1.8.0