From 9e64ecf008d2258cab142b10f07eca6b538d6c19 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期三, 05 八月 2020 19:39:53 +0800
Subject: [PATCH] update
---
/dev/null | 400 -------------------------
src/socket/include/shm_socket.h | 4
src/libshm_queue.a | 0
src/socket/shm_socket.c | 2
src/queue/include/shm_queue.h | 8
src/socket/include/dmod_socket.h | 138 ++++++++
src/queue/include/shm_queue_wrapper.h | 2
test_socket/dgram_mod_req_rep.c | 13
src/queue/shm_queue_wrapper.c | 3
src/socket/dmod_socket.c | 371 +++++++++++++++++++++++
10 files changed, 532 insertions(+), 409 deletions(-)
diff --git a/src/libshm_queue.a b/src/libshm_queue.a
new file mode 100644
index 0000000..5f592d6
--- /dev/null
+++ b/src/libshm_queue.a
Binary files differ
diff --git a/src/queue/include/shm_queue.h b/src/queue/include/shm_queue.h
index f47cf48..ca5036f 100644
--- a/src/queue/include/shm_queue.h
+++ b/src/queue/include/shm_queue.h
@@ -40,6 +40,7 @@
static void remove_queues_exclude(int keys[], size_t length);
static void remove_queues_include(int keys[], size_t length);
+ static void remove_queue(int key);
private:
protected:
@@ -78,6 +79,7 @@
}
+
template <typename ELEM_T>
void SHMQueue<ELEM_T>::remove_queues_include(int keys[], size_t length) {
hashtable_t *hashtable = mm_get_hashtable();
@@ -91,6 +93,12 @@
}
template <typename ELEM_T>
+void SHMQueue<ELEM_T>::remove_queue(int key) {
+ int keys[] = {key};
+ remove_queues_include(keys, 1);
+}
+
+template <typename ELEM_T>
SHMQueue<ELEM_T>::SHMQueue(int key, size_t qsize) : KEY(key) {
hashtable_t *hashtable = mm_get_hashtable();
diff --git a/src/queue/include/shm_queue_wrapper.h b/src/queue/include/shm_queue_wrapper.h
index 64e2404..9c84682 100644
--- a/src/queue/include/shm_queue_wrapper.h
+++ b/src/queue/include/shm_queue_wrapper.h
@@ -13,6 +13,8 @@
//绉婚櫎涓嶅寘鍚湪keys涓殑闃熷垪
void shm_remove_queues_exclude(void *keys, int length);
+//绉婚櫎涓嶅寘鍚湪keys涓殑闃熷垪
+void shm_remove_queue(int key);
/**
* 鍒涘缓闃熷垪
* @ shmqueue
diff --git a/src/queue/shm_queue_wrapper.c b/src/queue/shm_queue_wrapper.c
index 29a8f64..357e9e8 100644
--- a/src/queue/shm_queue_wrapper.c
+++ b/src/queue/shm_queue_wrapper.c
@@ -20,6 +20,9 @@
void shm_remove_queues_exclude(void *keys, int length) {
SHMQueue<ele_t>::remove_queues_exclude((int*)keys, (size_t)length);
}
+void shm_remove_queue(int key) {
+ SHMQueue<ele_t>::remove_queue(key);
+}
/**
* 鍒涘缓闃熷垪
diff --git a/src/socket/dgram_mod_socket.c b/src/socket/dgram_mod_socket.c
deleted file mode 100644
index 5efc9ca..0000000
--- a/src/socket/dgram_mod_socket.c
+++ /dev/null
@@ -1,400 +0,0 @@
-#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"
-#include <set>
-
-#define ACTION_LIDENTIFIER "<**"
-#define ACTION_RIDENTIFIER "**>"
-#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 {
- shm_socket_t *shm_socket;
- socket_mod_t mod;
- // pthread_t recv_thread;
- // <涓婚锛� 璁㈤槄鑰�>
- 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 );
-void * run_pubsub_proxy(dgram_mod_socket_t * socket) ;
-
-void *dgram_mod_open_socket() {
- dgram_mod_socket_t * socket = (dgram_mod_socket_t *)calloc(1, sizeof(dgram_mod_socket_t));
- // socket->mod = (socket_mod_t)mod;
- socket->shm_socket = shm_open_socket(SHM_SOCKET_DGRAM);
- return (void *)socket;
-}
-
-
-int dgram_mod_close_socket(void * _socket) {
- dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
- 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;
- 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);
-}
-
-
-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_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, NULL, 0);
-
-}
-
-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, 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) {
- 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, 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);
-}
-
-
-
-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);
-}
-
-//==========================================================================================================================
-
-/*
- * 澶勭悊璁㈤槄
-*/
-void _proxy_sub(dgram_mod_socket_t *socket, char *topic, int port) {
- SHMTopicSubMap *topic_sub_map = socket->topic_sub_map;
- SHMKeySet *subscripter_set;
-
- 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 {
- void *set_ptr = mm_malloc(sizeof(SHMKeySet));
- subscripter_set = new(set_ptr) SHMKeySet;
- topic_sub_map->insert({topic, subscripter_set});
- }
- subscripter_set->insert(port);
-}
-
-/*
- * 澶勭悊鍙戝竷锛屼唬鐞嗚浆鍙�
-*/
-void _proxy_pub(dgram_mod_socket_t * socket, char *topic, size_t head_len, void *buf, size_t size, int port) {
- SHMTopicSubMap *topic_sub_map = socket->topic_sub_map;
- SHMKeySet *subscripter_set;
-
- SHMTopicSubMap::iterator map_iter;
- SHMKeySet::iterator set_iter;
-
- std::vector<int> subscripter_to_del;
- std::vector<int>::iterator vector_iter;
-
- int send_port;
- struct timespec timeout = {1,0};
-
- if( (map_iter = topic_sub_map->find(topic) ) != topic_sub_map->end()) {
- 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);
- 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);
- }
-
-
- }
-
- // 鍒犻櫎宸插叧闂殑绔�
- for(vector_iter = subscripter_to_del.begin(); vector_iter != subscripter_to_del.end(); vector_iter++) {
- if((set_iter = subscripter_set->find(*vector_iter)) != subscripter_set->end()) {
- subscripter_set->erase(set_iter);
- printf("remove closed subscripter %d \n", send_port);
- }
- }
- subscripter_to_del.clear();
-
- }
-}
-
-void * run_pubsub_proxy(dgram_mod_socket_t * socket) {
- // pthread_detach(pthread_self());
- int size;
- int port;
- char * action, *topic, *topics, *buf;
- size_t head_len;
-
- const char *topic_delim = ",";
-// 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);
- if(parse_pubsub_topic(buf, size, &action, &topics, &head_len)) {
- if(strcmp(action, "sub") == 0) {
- // 璁㈤槄鏀寔澶氫富棰樿闃�
- topic = trim(strtok(topics, topic_delim), NULL);
- while(topic) {
- _proxy_sub(socket, topic, port);
- topic = trim(strtok(NULL, topic_delim), NULL);
- }
-
- } else if(strcmp(action, "pub") == 0) {
- _proxy_pub(socket, topics, head_len, buf, size, port);
- }
-
- free(action);
- free(topics);
- } else {
- err_msg(0, "incorrect format msg");
- }
- free(buf);
- }
- return NULL;
-}
-
-
-/**
- * @str "<**sub**>{缁忔祹}"
- */
-
-static int parse_pubsub_topic(char *str, size_t size, char **_action, char **_topic, size_t *head_len ) {
- char *ptr = str;
- char *str_end_ptr = str + size;
- char *action_start_ptr;
- char *action_end_ptr;
- size_t action_len = 0;
-
- char *topic_start_ptr;
- char *topic_end_ptr;
- size_t topic_len = 0;
-
- // if (strlen(identifier) > strlen(str)) {
- // return 0;
- // }
-
- if (strncmp(ptr, ACTION_LIDENTIFIER, strlen(ACTION_LIDENTIFIER)) == 0) {
- ptr += strlen(ACTION_LIDENTIFIER);
- action_start_ptr = ptr;
- while(strncmp(++ptr, ACTION_RIDENTIFIER, strlen(ACTION_RIDENTIFIER)) != 0) {
- if(ptr >= str_end_ptr) {
- return 0;
- }
- }
-// printf("%s\n", ptr);
- action_end_ptr = ptr;
- action_len = action_end_ptr - action_start_ptr;
- ptr += strlen(ACTION_RIDENTIFIER);
-// printf("%s\n", ptr);
-// printf("%s\n", str_end_ptr-1);
- if(strncmp(ptr, TOPIC_LIDENTIFIER, strlen(TOPIC_LIDENTIFIER)) == 0 ) {
- topic_start_ptr = ptr+strlen(TOPIC_LIDENTIFIER);
-
-
- while(strncmp(++ptr, TOPIC_RIDENTIFIER, strlen(TOPIC_RIDENTIFIER)) != 0) {
- if(ptr >= str_end_ptr) {
- return 0;
- }
- }
- topic_end_ptr = ptr;
- topic_len = topic_end_ptr - topic_start_ptr;
-
- ptr += strlen(TOPIC_RIDENTIFIER);
-
- } else {
- return 0;
- }
- } else {
- return 0;
- }
-
- char *topic = (char *)calloc(1, topic_len+1);
- strncpy(topic, topic_start_ptr, topic_len);
- *_topic = topic;
-
- char *action = (char *)calloc(1, action_len+1);
- strncpy(action, action_start_ptr, action_len);
- *_action = action;
- *head_len = ptr-str;
-
- return 1;
-}
\ No newline at end of file
diff --git a/src/socket/dmod_socket.c b/src/socket/dmod_socket.c
new file mode 100644
index 0000000..c1907ee
--- /dev/null
+++ b/src/socket/dmod_socket.c
@@ -0,0 +1,371 @@
+#include "dmod_socket.h"
+
+
+void DModSocket::foreach_subscripters(std::function<void(SHMKeySet *, SHMKeySet::iterator)> cb) {
+ SHMTopicSubMap *topic_sub_map = mem_pool_attach<SHMTopicSubMap>(BUS_MAP_KEY);
+ SHMKeySet *subscripter_set;
+ SHMKeySet::iterator set_iter;
+ 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;
+ if(subscripter_set != NULL) {
+ for(set_iter = subscripter_set->begin(); set_iter != subscripter_set->end(); set_iter++) {
+ cb(subscripter_set, set_iter);
+ }
+ }
+ }
+ }
+}
+
+
+DModSocket::DModSocket() {
+ shm_socket = shm_open_socket(SHM_SOCKET_DGRAM);
+}
+
+DModSocket::~DModSocket() {
+ 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;
+ 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(shm_socket);
+}
+
+int DModSocket::bind(int port) {
+ return shm_socket_bind(shm_socket, port);
+}
+
+
+
+/**
+ * 寮哄埗缁戝畾绔彛鍒皊ocket, 閫傜敤浜庣▼搴忛潪姝e父鍏抽棴鐨勬儏鍐典笅锛岄噸鍚▼搴忕粦瀹氬師鏉ヨ繕娌¢噴鏀剧殑key
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
+int DModSocket::force_bind(int port) {
+ return shm_socket_force_bind(shm_socket, port);
+}
+/**
+ * 鍙戦�佷俊鎭�
+ * @port 鍙戦�佺粰璋�
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+ */
+int DModSocket::sendto(const void *buf, const int size, const int port) {
+ return shm_sendto(shm_socket, buf, size, port, NULL, 0);
+}
+// 鍙戦�佷俊鎭秴鏃惰繑鍥炪�� @sec 绉� 锛� @nsec 绾崇
+int DModSocket::sendto_timeout(const void *buf, const int size, const int port, const struct timespec *timeout) {
+ return shm_sendto(shm_socket, buf, size, port, timeout, 0);
+}
+// 鍙戦�佷俊鎭珛鍒昏繑鍥炪��
+int DModSocket::sendto_nowait( const void *buf, const int size, const int port){
+ return shm_sendto(shm_socket, buf, size, port, NULL, (int)SHM_MSG_NOWAIT);
+}
+
+
+inline int DModSocket::_recvfrom_(void **buf, int *size, int *port, struct timespec *timeout, int flags) {
+
+ if(mod == BUS) {
+ err_exit(0, "Can not use method recvfrom in a Bus");
+ }
+// printf("dgram_mod_recvfrom before\n");
+ int rv = shm_recvfrom(shm_socket, buf, size, port, timeout, flags);
+// printf("dgram_mod_recvfrom after\n");
+ return rv;
+}
+/**
+ * 鎺ユ敹淇℃伅
+ * @port 浠庤皝鍝噷鏀跺埌鐨勪俊鎭�
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
+int DModSocket::recvfrom(void **buf, int *size, int *port) {
+
+ return _recvfrom_( buf, size, port, NULL, 0);
+}
+
+
+// 鎺ュ彈淇℃伅瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇
+int DModSocket::recvfrom_timeout( void **buf, int *size, int *port, struct timespec *timeout) {
+ return _recvfrom_(buf, size, port, timeout, 0);
+}
+
+int DModSocket::recvfrom_nowait( void **buf, int *size, int *port){
+ return _recvfrom_(buf, size, port, NULL, (int)SHM_MSG_NOWAIT);
+}
+
+/**
+ * 鍙戦�佽姹備俊鎭苟绛夊緟鎺ユ敹搴旂瓟
+ * @port 鍙戦�佺粰璋�
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
+int DModSocket::sendandrecv( const void *send_buf, const int send_size, const int send_port, void **recv_buf, int *recv_size){
+ return shm_sendandrecv(shm_socket, send_buf, send_size, send_port, recv_buf, recv_size, NULL, 0);
+}
+// 瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇
+int DModSocket::sendandrecv_timeout(const void *send_buf, const int send_size, const int send_port, void **recv_buf, int *recv_size, struct timespec *timeout){
+ return shm_sendandrecv(shm_socket, send_buf, send_size, send_port, recv_buf, recv_size, timeout, 0);
+}
+int DModSocket::sendandrecv_nowait(const void *send_buf, const int send_size, const int send_port, void **recv_buf, int *recv_size){
+ return shm_sendandrecv(shm_socket, send_buf, send_size, send_port, recv_buf, recv_size, 0, (int)SHM_MSG_NOWAIT);
+}
+
+
+/**
+ * 鍚姩bus
+ *
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
+int DModSocket::start_bus(){
+ mod = BUS;
+ topic_sub_map = mem_pool_attach<SHMTopicSubMap>(BUS_MAP_KEY);
+
+ run_pubsub_proxy();
+ // pthread_t tid;
+ // pthread_create(&tid, NULL, run_accept_sub_request, _socket);
+ return 0;
+}
+/**
+ * @port 鎬荤嚎绔彛
+ */
+int DModSocket::_sub_( void *topic, int size, int port,
+ struct timespec *timeout, int flags) {
+ char buf[8192];
+ snprintf(buf, 8192, "%ssub%s%s%s%s", ACTION_LIDENTIFIER, ACTION_RIDENTIFIER, TOPIC_LIDENTIFIER, (char *)topic, TOPIC_RIDENTIFIER);
+ return shm_sendto(shm_socket, buf, strlen(buf) + 1, port, timeout, flags);
+}
+/**
+ * 璁㈤槄鎸囧畾涓婚
+ * @topic 涓婚
+ * @size 涓婚闀垮害
+ * @port 鎬荤嚎绔彛
+ */
+int DModSocket::sub( void *topic, int size, int port){
+ return _sub_( topic, size, port, NULL, 0);
+}
+// 瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇
+int DModSocket::sub_timeout(void *topic, int size, int port, struct timespec *timeout){
+ return _sub_(topic, size, port, timeout, 0);
+}
+int DModSocket::sub_nowait(void *topic, int size, int port) {
+ return _sub_(topic, size, port, NULL, (int)SHM_MSG_NOWAIT);
+}
+
+
+/**
+ * @port 鎬荤嚎绔彛
+ */
+int DModSocket::_pub_( void *topic, int topic_size, void *content, int content_size, int port,
+ struct timespec *timeout, int flags) {
+ 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(shm_socket, buf, head_len+content_size, port, timeout, flags);
+
+}
+
+/**
+ * 鍙戝竷涓婚
+ * @topic 涓婚
+ * @content 涓婚鍐呭
+ * @port 鎬荤嚎绔彛
+ */
+int DModSocket::pub(void *topic, int topic_size, void *content, int content_size, int port){
+ return _pub_(topic, topic_size, content, content_size, port, NULL, 0);
+}
+// 瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇
+int DModSocket::pub_timeout(void *topic, int topic_size, void *content, int content_size, int port, struct timespec * timeout){
+ return _pub_( topic, topic_size, content, content_size, port, timeout, 0);
+}
+int DModSocket::pub_nowait(void *topic, int topic_size, void *content, int content_size, int port){
+ return _pub_(topic, topic_size, content, content_size, port, NULL, (int)SHM_MSG_NOWAIT);
+}
+
+
+/**
+ * 鑾峰彇soket绔彛鍙�
+ */
+int DModSocket::get_port(){
+ return shm_socket->port;
+}
+
+
+
+// ========================================================
+
+
+/*
+ * 澶勭悊璁㈤槄
+*/
+void DModSocket::_proxy_sub( char *topic, int port) {
+ SHMKeySet *subscripter_set;
+
+ 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 {
+ void *set_ptr = mm_malloc(sizeof(SHMKeySet));
+ subscripter_set = new(set_ptr) SHMKeySet;
+ topic_sub_map->insert({topic, subscripter_set});
+ }
+ subscripter_set->insert(port);
+}
+
+/*
+ * 澶勭悊鍙戝竷锛屼唬鐞嗚浆鍙�
+*/
+void DModSocket::_proxy_pub( char *topic, size_t head_len, void *buf, size_t size, int port) {
+ SHMKeySet *subscripter_set;
+
+ SHMTopicSubMap::iterator map_iter;
+ SHMKeySet::iterator set_iter;
+
+ std::vector<int> subscripter_to_del;
+ std::vector<int>::iterator vector_iter;
+
+ int send_port;
+ struct timespec timeout = {1,0};
+
+ if( (map_iter = topic_sub_map->find(topic) ) != topic_sub_map->end()) {
+ 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);
+ if (shm_sendto(shm_socket, buf+head_len, size-head_len, send_port, &timeout) == SHM_SOCKET_ECONNFAILED ) {
+ //瀵规柟宸插叧闂殑杩炴帴鏀惧埌寰呭垹闄ら槦鍒楅噷銆傚鏋滅洿鎺ュ垹闄や細璁﹊ter鎸囬拡鍑虹幇閿欎贡
+ subscripter_to_del.push_back(send_port);
+ } else {
+// printf("_proxy_pub send after: %d \n", send_port);
+ }
+
+
+ }
+
+ // 鍒犻櫎宸插叧闂殑绔�
+ for(vector_iter = subscripter_to_del.begin(); vector_iter != subscripter_to_del.end(); vector_iter++) {
+ if((set_iter = subscripter_set->find(*vector_iter)) != subscripter_set->end()) {
+ subscripter_set->erase(set_iter);
+ printf("remove closed subscripter %d \n", send_port);
+ }
+ }
+ subscripter_to_del.clear();
+
+ }
+}
+
+void * DModSocket::run_pubsub_proxy() {
+ // pthread_detach(pthread_self());
+ int size;
+ int port;
+ char * action, *topic, *topics, *buf;
+ size_t head_len;
+
+ const char *topic_delim = ",";
+// printf("run_pubsub_proxy server receive before\n");
+ while(shm_recvfrom(shm_socket, (void **)&buf, &size, &port) == 0) {
+// 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) {
+ // 璁㈤槄鏀寔澶氫富棰樿闃�
+ topic = trim(strtok(topics, topic_delim), NULL);
+ while(topic) {
+ _proxy_sub( topic, port);
+ topic = trim(strtok(NULL, topic_delim), NULL);
+ }
+
+ } else if(strcmp(action, "pub") == 0) {
+ _proxy_pub(topics, head_len, buf, size, port);
+ }
+
+ free(action);
+ free(topics);
+ } else {
+ err_msg(0, "incorrect format msg");
+ }
+ free(buf);
+ }
+ return NULL;
+}
+
+
+/**
+ * @str "<**sub**>{缁忔祹}"
+ */
+
+int DModSocket::parse_pubsub_topic(char *str, size_t size, char **_action, char **_topic, size_t *head_len ) {
+ char *ptr = str;
+ char *str_end_ptr = str + size;
+ char *action_start_ptr;
+ char *action_end_ptr;
+ size_t action_len = 0;
+
+ char *topic_start_ptr;
+ char *topic_end_ptr;
+ size_t topic_len = 0;
+
+ // if (strlen(identifier) > strlen(str)) {
+ // return 0;
+ // }
+
+ if (strncmp(ptr, ACTION_LIDENTIFIER, strlen(ACTION_LIDENTIFIER)) == 0) {
+ ptr += strlen(ACTION_LIDENTIFIER);
+ action_start_ptr = ptr;
+ while(strncmp(++ptr, ACTION_RIDENTIFIER, strlen(ACTION_RIDENTIFIER)) != 0) {
+ if(ptr >= str_end_ptr) {
+ return 0;
+ }
+ }
+// printf("%s\n", ptr);
+ action_end_ptr = ptr;
+ action_len = action_end_ptr - action_start_ptr;
+ ptr += strlen(ACTION_RIDENTIFIER);
+// printf("%s\n", ptr);
+// printf("%s\n", str_end_ptr-1);
+ if(strncmp(ptr, TOPIC_LIDENTIFIER, strlen(TOPIC_LIDENTIFIER)) == 0 ) {
+ topic_start_ptr = ptr+strlen(TOPIC_LIDENTIFIER);
+
+
+ while(strncmp(++ptr, TOPIC_RIDENTIFIER, strlen(TOPIC_RIDENTIFIER)) != 0) {
+ if(ptr >= str_end_ptr) {
+ return 0;
+ }
+ }
+ topic_end_ptr = ptr;
+ topic_len = topic_end_ptr - topic_start_ptr;
+
+ ptr += strlen(TOPIC_RIDENTIFIER);
+
+ } else {
+ return 0;
+ }
+ } else {
+ return 0;
+ }
+
+ char *topic = (char *)calloc(1, topic_len+1);
+ strncpy(topic, topic_start_ptr, topic_len);
+ *_topic = topic;
+
+ char *action = (char *)calloc(1, action_len+1);
+ strncpy(action, action_start_ptr, action_len);
+ *_action = action;
+ *head_len = ptr-str;
+
+ return 1;
+}
+
+
\ No newline at end of file
diff --git a/src/socket/include/dmod_socket.h b/src/socket/include/dmod_socket.h
new file mode 100644
index 0000000..5e97e2a
--- /dev/null
+++ b/src/socket/include/dmod_socket.h
@@ -0,0 +1,138 @@
+#ifndef __DMODE_SOCKET_H__
+#define __DMODE_SOCKET_H__
+#include "usg_common.h"
+#include "shm_socket.h"
+#include "shm_allocator.h"
+#include "mem_pool.h"
+#include "hashtable.h"
+#include "sem_util.h"
+#include "logger_factory.h"
+#include <set>
+
+#define ACTION_LIDENTIFIER "<**"
+#define ACTION_RIDENTIFIER "**>"
+#define TOPIC_LIDENTIFIER "{"
+#define TOPIC_RIDENTIFIER "}"
+
+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;
+
+enum socket_mod_t
+{
+ PULL_PUSH = 1,
+ REQ_REP = 2,
+ PAIR = 3,
+ PUB_SUB = 4,
+ SURVEY = 5,
+ BUS = 6
+
+};
+
+class DModSocket {
+private:
+ shm_socket_t *shm_socket;
+ socket_mod_t mod;
+ // pthread_t recv_thread;
+ // <涓婚锛� 璁㈤槄鑰�>
+ SHMTopicSubMap *topic_sub_map;
+
+private:
+ inline int _recvfrom_(void **buf, int *size, int *port, struct timespec *timeout, int flags);
+ void _proxy_sub( char *topic, int port);
+ void _proxy_pub( char *topic, size_t head_len, void *buf, size_t size, int port);
+ void *run_pubsub_proxy();
+ int parse_pubsub_topic(char *str, size_t size, char **_action, char **_topic, size_t *head_len );
+ int _sub_( void *topic, int size, int port, struct timespec *timeout, int flags);
+ int _pub_( void *topic, int topic_size, void *content, int content_size, int port, struct timespec *timeout, int flags);
+
+ static void foreach_subscripters(std::function<void(SHMKeySet *, SHMKeySet::iterator)> cb) ;
+public:
+ DModSocket();
+ ~DModSocket();
+
+
+ /**
+ * 缁戝畾绔彛鍒皊ocket, 濡傛灉涓嶇粦瀹氬垯绯荤粺鑷姩鍒嗛厤涓�涓�
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+ */
+ int bind(int port);
+
+ /**
+ * 寮哄埗缁戝畾绔彛鍒皊ocket, 閫傜敤浜庣▼搴忛潪姝e父鍏抽棴鐨勬儏鍐典笅锛岄噸鍚▼搴忕粦瀹氬師鏉ヨ繕娌¢噴鏀剧殑key
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+ */
+ int force_bind(int port);
+ /**
+ * 鍙戦�佷俊鎭�
+ * @port 鍙戦�佺粰璋�
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+ */
+ int sendto(const void *buf, const int size, const int port);
+ // 鍙戦�佷俊鎭秴鏃惰繑鍥炪�� @sec 绉� 锛� @nsec 绾崇
+ int sendto_timeout(const void *buf, const int size, const int port, const struct timespec *timeout);
+ // 鍙戦�佷俊鎭珛鍒昏繑鍥炪��
+ int sendto_nowait(const void *buf, const int size, const int port);
+
+ /**
+ * 鎺ユ敹淇℃伅
+ * @port 浠庤皝鍝噷鏀跺埌鐨勪俊鎭�
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+ */
+ int recvfrom(void **buf, int *size, int *port);
+ // 鎺ュ彈淇℃伅瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇
+ int recvfrom_timeout(void **buf, int *size, int *port, struct timespec *timeout);
+ int recvfrom_nowait(void **buf, int *size, int *port);
+
+ /**
+ * 鍙戦�佽姹備俊鎭苟绛夊緟鎺ユ敹搴旂瓟
+ * @port 鍙戦�佺粰璋�
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+ */
+ int sendandrecv(const void *send_buf, const int send_size, const int port, void **recv_buf, int *recv_size) ;
+ // 瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇
+ int sendandrecv_timeout(const void *send_buf, const int send_size, const int port, void **recv_buf, int *recv_size, struct timespec *timeout) ;
+ int sendandrecv_nowait(const void *send_buf, const int send_size, const int port, void **recv_buf, int *recv_size) ;
+
+
+ /**
+ * 鍚姩bus
+ *
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+ */
+ int start_bus();
+
+ /**
+ * 璁㈤槄鎸囧畾涓婚
+ * @topic 涓婚
+ * @size 涓婚闀垮害
+ * @port 鎬荤嚎绔彛
+ */
+ int sub(void *topic, int size, int port);
+ // 瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇
+ int sub_timeout(void *topic, int size, int port, struct timespec *timeout);
+ int sub_nowait(void *topic, int size, int port);
+
+
+
+ /**
+ * 鍙戝竷涓婚
+ * @topic 涓婚
+ * @content 涓婚鍐呭
+ * @port 鎬荤嚎绔彛
+ */
+ int pub(void *topic, int topic_size, void *content, int content_size, int port);
+ // 瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇
+ int pub_timeout(void *topic, int topic_size, void *content, int content_size, int port, struct timespec *timeout);
+ int pub_nowait(void *topic, int topic_size, void *content, int content_size, int port);
+
+
+ /**
+ * 鑾峰彇soket绔彛鍙�
+ */
+ int get_port() ;
+};
+
+#endif
\ No newline at end of file
diff --git a/src/socket/include/shm_socket.h b/src/socket/include/shm_socket.h
index dd93f75..6bb0448 100644
--- a/src/socket/include/shm_socket.h
+++ b/src/socket/include/shm_socket.h
@@ -30,8 +30,8 @@
};
enum shm_socket_error_type_t {
- SHM_SOCKET_CONN_FAILED = 1,
- SHM_SOCKET_TIMEOUT = 2
+ SHM_SOCKET_ECONNFAILED = 1,
+ SHM_SOCKET_ETIMEOUT = 2
};
enum shm_connection_status_t {
diff --git a/src/socket/shm_socket.c b/src/socket/shm_socket.c
index 61990e5..be69413 100644
--- a/src/socket/shm_socket.c
+++ b/src/socket/shm_socket.c
@@ -270,7 +270,7 @@
SHMQueue<shm_msg_t> *remoteQueue;
if ((remoteQueue = _attach_remote_queue(port)) == NULL) {
err_msg(0, "shm_sendto failed, the other end has been closed, or has not been opened!");
- return SHM_SOCKET_CONN_FAILED;
+ return SHM_SOCKET_ECONNFAILED;
}
shm_msg_t dest;
diff --git a/test_socket/dgram_mod_req_rep.c b/test_socket/dgram_mod_req_rep.c
index 746ae19..d35c6ea 100644
--- a/test_socket/dgram_mod_req_rep.c
+++ b/test_socket/dgram_mod_req_rep.c
@@ -160,13 +160,14 @@
port = atoi(argv[2]);
if (strcmp("server", argv[1]) == 0) {
- int temp = shm_alloc_key();
- printf("tmp=%d\n", temp);
- server(temp);
+ // int temp = shm_alloc_key();
+ // printf("tmp=%d\n", temp);
+ server(port);
+ } else if (strcmp("client", argv[1]) == 0) {
+ startClients(port);
+ } else {
+ printf("input invalidate arguments\n");
}
-
- if (strcmp("client", argv[1]) == 0)
- client2(port);
return 0;
--
Gitblit v1.8.0