From 8c42a659c0cc9178d1f1305acb41dfbf4a8697ef Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期四, 22 十月 2020 16:20:26 +0800
Subject: [PATCH] update
---
src/socket/dgram_mod_socket.c | 393 +++++++++++++++++++++++++-------------------------------
1 files changed, 175 insertions(+), 218 deletions(-)
diff --git a/src/socket/dgram_mod_socket.c b/src/socket/dgram_mod_socket.c
index c5e5a28..3e0fd0c 100644
--- a/src/socket/dgram_mod_socket.c
+++ b/src/socket/dgram_mod_socket.c
@@ -1,257 +1,214 @@
-#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 "}"
-
-static int parse_pubsub_topic(char *str, size_t size, char **_action, char **_topic, size_t *head_len );
-void * run_accept_pubsub_request(void * _socket) ;
+#include "shm_mod_socket.h"
typedef struct dgram_mod_socket_t {
- socket_mod_t mod;
- shm_socket_t *shm_socket;
- // pthread_t recv_thread;
- // <涓婚锛� 璁㈤槄鑰�>
- std::map<std::string, std::set<int> *> *topic_sub_map;
+ ShmModSocket *m_socket;
+
} dgram_mod_socket_t;
+int dgram_mod_remove_keys(int keys[], int length){
+ return ShmModSocket::remove_keys(keys, length);
+}
+
+int dgram_mod_remove_key(int key){
+ int keys[] = {key};
+ return ShmModSocket::remove_keys(keys, 1);
+}
+/**
+ * 鍒涘缓socket
+ * @return 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);
+ socket->m_socket = new ShmModSocket;
return (void *)socket;
}
-
+/**
+ * 鍏抽棴socket
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
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;
-
- 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;
- }
- delete topic_sub_map;
- }
-
-
- shm_close_socket(socket->shm_socket);
+ delete socket->m_socket;
free(_socket);
+ return 0;
}
-
-int dgram_mod_bind(void * _socket, int port){
+/**
+ * 缁戝畾绔彛鍒皊ocket, 濡傛灉涓嶇粦瀹氬垯绯荤粺鑷姩鍒嗛厤涓�涓�
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
+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);
+ return socket->m_socket->bind(port);
}
-int dgram_mod_sendto(void *_socket, const void *buf, const int size, const int port) {
+/**
+ * 寮哄埗缁戝畾绔彛鍒皊ocket, 閫傜敤浜庣▼搴忛潪姝e父鍏抽棴鐨勬儏鍐典笅锛岄噸鍚▼搴忕粦瀹氬師鏉ヨ繕娌¢噴鏀剧殑key
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
+int dgram_mod_force_bind(void * _socket, int port) {
dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
- return shm_sendto(socket->shm_socket, buf, size, port);
-
+ return socket->m_socket->force_bind(port);
+}
+/**
+ * 鍙戦�佷俊鎭�
+ * @port 鍙戦�佺粰璋�
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+ */
+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 socket->m_socket->sendto(buf, size,port);
}
+// 鍙戦�佷俊鎭秴鏃惰繑鍥炪�� @sec 绉� 锛� @nsec 绾崇
+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 socket->m_socket->sendto_timeout(buf, size, port, &timeout);
+}
+
+// 鍙戦�佷俊鎭珛鍒昏繑鍥炪��
+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 socket->m_socket->sendto_nowait(buf, size,port);
+}
+
+/**
+ * 鎺ユ敹淇℃伅
+ * @port 浠庤皝鍝噷鏀跺埌鐨勪俊鎭�
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
int dgram_mod_recvfrom(void *_socket, void **buf, int *size, int *port) {
-
dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
-// printf("dgram_mod_recvfrom before\n");
- int rv = shm_recvfrom(socket->shm_socket, buf, size, port);
-// printf("dgram_mod_recvfrom after\n");
- return rv;
+ return socket->m_socket->recvfrom(buf, size, port);
}
-
-
-
-int dgram_mod_sendandrecv(void * _socket, const void *send_buf, const int send_size, const int send_port, void **recv_buf, int *recv_size) {
+// 鎺ュ彈淇℃伅瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇
+int dgram_mod_recvfrom_timeout(void *_socket, void **buf, int *size, int *port, int sec, int 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);
+ struct timespec timeout = {sec, nsec};
+ return socket->m_socket->recvfrom_timeout(buf, size, port, &timeout);
+}
+int dgram_mod_recvfrom_nowait(void *_socket, void **buf, int *size, int *port){
+ dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
+ return socket->m_socket->recvfrom_nowait(buf, size, 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){
+ dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
+ return socket->m_socket->sendandrecv(send_buf, send_size, port, recv_buf, 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){
+ dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
+ struct timespec timeout = {sec, nsec};
+ return socket->m_socket->sendandrecv_timeout(send_buf, send_size, port, recv_buf, recv_size, &timeout);
+}
+int dgram_mod_sendandrecv_nowait(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;
+ return socket->m_socket->sendandrecv_nowait(send_buf, send_size, port, recv_buf, recv_size);
+}
+
+
+/**
+ * 鍚姩bus
+ *
+ * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜
+*/
+int dgram_mod_start_bus(void * _socket) {
+ dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
+ return socket->m_socket->start_bus();
+}
+
+/**
+ * 璁㈤槄鎸囧畾涓婚
+ * @topic 涓婚
+ * @size 涓婚闀垮害
+ * @port 鎬荤嚎绔彛
+ */
+int dgram_mod_sub(void * _socket, void *topic, int size, int port){
+ dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
+ return socket->m_socket->sub((char *)topic, size, port);
+}
+// 瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇
+int dgram_mod_sub_timeout(void * _socket, void *topic, int size, int port, int sec, int nsec){
+ dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
+ struct timespec timeout = {sec, nsec};
+ return socket->m_socket->sub_timeout((char *)topic, size, port, &timeout);
+}
+int dgram_mod_sub_nowait(void * _socket, void *topic, int size, int port){
+ dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
+ return socket->m_socket->sub_nowait((char *)topic, size, port);
}
-int dgram_mod_get_socket_port(void * _socket) {
+/**
+ * 鍙栨秷璁㈤槄鎸囧畾涓婚
+ * @topic 涓婚
+ * @size 涓婚闀垮害
+ * @port 鎬荤嚎绔彛
+ */
+int dgram_mod_desub(void * _socket, void *topic, int size, int port){
dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
- return socket->shm_socket->port;
+ return socket->m_socket->desub((char *)topic, size, port);
+}
+// 瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇
+int dgram_mod_desub_timeout(void * _socket, void *topic, int size, int port, int sec, int nsec){
+ dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
+ struct timespec timeout = {sec, nsec};
+ return socket->m_socket->desub_timeout((char *)topic, size, port, &timeout);
+}
+int dgram_mod_desub_nowait(void * _socket, void *topic, int size, int port){
+ dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
+ return socket->m_socket->desub_nowait((char *)topic, size, port);
}
+
+/**
+ * 鍙戝竷涓婚
+ * @topic 涓婚
+ * @content 涓婚鍐呭
+ * @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;
+ return socket->m_socket->pub((char *)topic, topic_size, content, content_size, 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){
+ dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
+ struct timespec timeout = {sec, nsec};
+ return socket->m_socket->pub_timeout((char *)topic, topic_size, content, content_size, port, &timeout);
+}
+int dgram_mod_pub_nowait(void * _socket, void *topic, int topic_size, void *content, int content_size, int port){
+ dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
+ return socket->m_socket->pub_nowait((char *)topic, topic_size, content, content_size, port);
+}
+
+
+/**
+ * 鑾峰彇soket绔彛鍙�
+ */
+int dgram_mod_get_port(void * _socket) {
+ dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
+ return socket->m_socket->get_port();
+}
+
+
+/**
+ * 閲婃斁瀛樺偍鎺ユ敹淇℃伅鐨刡uf
+ */
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> *>;
- run_accept_pubsub_request(_socket);
- // pthread_t tid;
- // pthread_create(&tid, NULL, run_accept_sub_request, _socket);
- return 0;
-
-}
-
-/**
- * @port 鎬荤嚎绔彛
- */
-int 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 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);
-
-}
-
-
-//==========================================================================================================================
-
-
-void * run_accept_pubsub_request(void * _socket) {
- // pthread_detach(pthread_self());
- dgram_mod_socket_t * socket = (dgram_mod_socket_t *) _socket;
- int size;
- int port;
- char * action, *topic, *buf;
-
- size_t head_len;
- // void * send_buf;
- int send_port;
- struct timespec timeout = {1,0};
- 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;
- std::set<int>::iterator set_iter;
-//printf("server receive before\n");
- while(shm_recvfrom(socket->shm_socket, (void **)&buf, &size, &port) == 0) {
-//printf("server recv after: %s \n", buf);
- if(parse_pubsub_topic(buf, size, &action, &topic, &head_len)) {
- if(strcmp(action, "sub") == 0) {
- if( (map_iter = topic_sub_map->find(topic) ) != topic_sub_map->end()) {
- subscripter_set = map_iter->second;
- } else {
- subscripter_set = new std::set<int>;
- topic_sub_map->insert({topic, subscripter_set});
- }
- subscripter_set->insert(port);
-
- } else if(strcmp(action, "pub") == 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;
- // send_buf = malloc(size-head_len);
- // memcpy(send_buf, buf+head_len, );
-printf("run_accept_sub_request send before %d \n", send_port);
- if (shm_sendto(socket->shm_socket, buf+head_len, size-head_len, send_port, &timeout) !=0 ) {
-printf("erase %d \n", send_port);
- subscripter_set->erase(set_iter);
- set_iter++;
- }
-printf("run_accept_sub_request send after: %d \n", send_port);
-
- }
- }
- }
- free(buf);
- free(action);
- free(topic);
- } else {
- err_msg(0, "incorrect format msg");
- }
- }
- 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
--
Gitblit v1.8.0