From 26ed48c4e616014ee760fd13d13dbdc8539c34e3 Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期二, 22 十二月 2020 19:21:55 +0800 Subject: [PATCH] 解决sendandrecv发送到一个不存在key的情况 --- src/socket/shm_mod_socket.c | 590 ++++++++++++++++++++-------------------------------------- 1 files changed, 204 insertions(+), 386 deletions(-) diff --git a/src/socket/shm_mod_socket.c b/src/socket/shm_mod_socket.c index e890721..4c3938e 100644 --- a/src/socket/shm_mod_socket.c +++ b/src/socket/shm_mod_socket.c @@ -1,63 +1,10 @@ #include "shm_mod_socket.h" - - -void ShmModSocket::foreach_subscripters(std::function<void(SHMKeySet *, int)> 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); - } - } - } - } -} - -bool ShmModSocket::include_in_keys(int key, int keys[], size_t length) { - if(length == 0) { - return false; - } - for(int i = 0; i < length; i++) { - if(keys[i] == key) - return true; - } - return false; -} - -size_t ShmModSocket::remove_subscripters(int keys[], size_t length) { - size_t count = 0; - int key; - for(int i = 0; i < length; i++) { - key = keys[i]; - 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 && (set_iter = subscripter_set->find(key) ) != subscripter_set->end()) { - subscripter_set->erase(set_iter); -// printf("remove_subscripter %s, %d\n", map_iter->first, key); - count++; - } - } - } - } - return count; - -} +#include "bus_server_socket.h" +static Logger *logger = LoggerFactory::getLogger(); size_t ShmModSocket::remove_keys(int keys[], size_t length) { - remove_subscripters(keys, length); + BusServerSocket::remove_subscripters(keys, length); return shm_socket_remove_keys(keys, length); } @@ -65,166 +12,127 @@ mod = (socket_mod_t)0; shm_socket = shm_open_socket(SHM_SOCKET_DGRAM); bus_set = new std::set<int>; - topic_sub_map = NULL; } ShmModSocket::~ShmModSocket() { -// printf("ShmModSocket destory 1\n"); - SHMKeySet *subscripter_set; - SHMTopicSubMap::iterator map_iter; + logger->debug("Destory ShmModSocket...\n"); struct timespec timeout = {1, 0}; if(bus_set != NULL) { for(auto bus_iter = bus_set->begin(); bus_iter != bus_set->end(); bus_iter++) { -// printf("ShmModSocket desub_timeout before"); desub_timeout(NULL, 0, *bus_iter, &timeout); -// printf("ShmModSocket desub_timeout after %d\n", *bus_iter); } delete bus_set; } -// printf("ShmModSocket destory 2\n"); - 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; -// printf("ShmModSocket destory 2-1\n"); - if(subscripter_set != NULL) { -// printf("ShmModSocket destory 2-2\n"); - subscripter_set->clear(); -// printf("ShmModSocket destory 2-3\n"); - mm_free((void *)subscripter_set); -// printf("ShmModSocket destory 2-4\n"); - } - - } - topic_sub_map->clear(); - mem_pool_free_by_key(BUS_MAP_KEY); - } -// printf("ShmModSocket destory 3\n"); - // printf("=============close socket\n"); shm_close_socket(shm_socket); // printf("ShmModSocket destory 4\n"); } -int ShmModSocket::bind(int port) { - return shm_socket_bind(shm_socket, port); +int ShmModSocket::bind(int key) { + return shm_socket_bind(shm_socket, key); } - - /** * 寮哄埗缁戝畾绔彛鍒皊ocket, 閫傜敤浜庣▼搴忛潪姝e父鍏抽棴鐨勬儏鍐典笅锛岄噸鍚▼搴忕粦瀹氬師鏉ヨ繕娌¢噴鏀剧殑key * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜 */ -int ShmModSocket::force_bind(int port) { - return shm_socket_force_bind(shm_socket, port); +int ShmModSocket::force_bind(int key) { + return shm_socket_force_bind(shm_socket, key); } /** * 鍙戦�佷俊鎭� - * @port 鍙戦�佺粰璋� + * @key 鍙戦�佺粰璋� * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜 */ -int ShmModSocket::sendto(const void *buf, const int size, const int port) { - return shm_sendto(shm_socket, buf, size, port, NULL, 0); +int ShmModSocket::sendto(const void *buf, const int size, const int key) { + return shm_sendto(shm_socket, buf, size, key, NULL, 0); } // 鍙戦�佷俊鎭秴鏃惰繑鍥炪�� @sec 绉� 锛� @nsec 绾崇 -int ShmModSocket::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 ShmModSocket::sendto_timeout(const void *buf, const int size, const int key, const struct timespec *timeout) { + return shm_sendto(shm_socket, buf, size, key, timeout, 0); } // 鍙戦�佷俊鎭珛鍒昏繑鍥炪�� -int ShmModSocket::sendto_nowait( const void *buf, const int size, const int port){ - return shm_sendto(shm_socket, buf, size, port, NULL, (int)SHM_MSG_NOWAIT); +int ShmModSocket::sendto_nowait( const void *buf, const int size, const int key){ + return shm_sendto(shm_socket, buf, size, key, NULL, (int)SHM_MSG_NOWAIT); } -inline int ShmModSocket::_recvfrom_(void **buf, int *size, int *port, struct timespec *timeout, int flags) { +inline int ShmModSocket::_recvfrom_(void **buf, int *size, int *key, struct timespec *timeout, int flags) { if(mod == BUS) { - err_exit(0, "Can not use method recvfrom in a Bus"); + logger->error("Can not use method recvfrom in a Bus"); + exit(1); } // printf("dgram_mod_recvfrom before\n"); - int rv = shm_recvfrom(shm_socket, buf, size, port, timeout, flags); + int rv = shm_recvfrom(shm_socket, buf, size, key, timeout, flags); // printf("dgram_mod_recvfrom after\n"); return rv; } /** * 鎺ユ敹淇℃伅 - * @port 浠庤皝鍝噷鏀跺埌鐨勪俊鎭� + * @key 浠庤皝鍝噷鏀跺埌鐨勪俊鎭� * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜 */ -int ShmModSocket::recvfrom(void **buf, int *size, int *port) { +int ShmModSocket::recvfrom(void **buf, int *size, int *key) { - return _recvfrom_( buf, size, port, NULL, 0); + return _recvfrom_( buf, size, key, NULL, 0); } // 鎺ュ彈淇℃伅瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇 -int ShmModSocket::recvfrom_timeout( void **buf, int *size, int *port, struct timespec *timeout) { - return _recvfrom_(buf, size, port, timeout, 0); +int ShmModSocket::recvfrom_timeout( void **buf, int *size, int *key, struct timespec *timeout) { + return _recvfrom_(buf, size, key, timeout, 0); } -int ShmModSocket::recvfrom_nowait( void **buf, int *size, int *port){ - return _recvfrom_(buf, size, port, NULL, (int)SHM_MSG_NOWAIT); +int ShmModSocket::recvfrom_nowait( void **buf, int *size, int *key){ + return _recvfrom_(buf, size, key, NULL, (int)SHM_MSG_NOWAIT); } /** * 鍙戦�佽姹備俊鎭苟绛夊緟鎺ユ敹搴旂瓟 - * @port 鍙戦�佺粰璋� + * @key 鍙戦�佺粰璋� * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜 */ -int ShmModSocket::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); +int ShmModSocket::sendandrecv( const void *send_buf, const int send_size, const int send_key, void **recv_buf, int *recv_size){ + return shm_sendandrecv(shm_socket, send_buf, send_size, send_key, recv_buf, recv_size, NULL, 0); } // 瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇 -int ShmModSocket::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 ShmModSocket::sendandrecv_timeout(const void *send_buf, const int send_size, const int send_key, void **recv_buf, int *recv_size, struct timespec *timeout){ + return shm_sendandrecv(shm_socket, send_buf, send_size, send_key, recv_buf, recv_size, timeout, 0); } -int ShmModSocket::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); +int ShmModSocket::sendandrecv_nowait(const void *send_buf, const int send_size, const int send_key, void **recv_buf, int *recv_size){ + return shm_sendandrecv(shm_socket, send_buf, send_size, send_key, recv_buf, recv_size, 0, (int)SHM_MSG_NOWAIT); } -int ShmModSocket::sendandrecv_unsafe( const void *send_buf, const int send_size, const int send_port, void **recv_buf, int *recv_size){ - return shm_sendandrecv_unsafe(shm_socket, send_buf, send_size, send_port, recv_buf, recv_size, NULL, 0); +int ShmModSocket::sendandrecv_unsafe( const void *send_buf, const int send_size, const int send_key, void **recv_buf, int *recv_size){ + return shm_sendandrecv_unsafe(shm_socket, send_buf, send_size, send_key, recv_buf, recv_size, NULL, 0); } // 瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇 -int ShmModSocket::sendandrecv_unsafe_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_unsafe(shm_socket, send_buf, send_size, send_port, recv_buf, recv_size, timeout, 0); +int ShmModSocket::sendandrecv_unsafe_timeout(const void *send_buf, const int send_size, const int send_key, void **recv_buf, int *recv_size, struct timespec *timeout){ + return shm_sendandrecv_unsafe(shm_socket, send_buf, send_size, send_key, recv_buf, recv_size, timeout, 0); } -int ShmModSocket::sendandrecv_unsafe_nowait(const void *send_buf, const int send_size, const int send_port, void **recv_buf, int *recv_size){ - return shm_sendandrecv_unsafe(shm_socket, send_buf, send_size, send_port, recv_buf, recv_size, 0, (int)SHM_MSG_NOWAIT); +int ShmModSocket::sendandrecv_unsafe_nowait(const void *send_buf, const int send_size, const int send_key, void **recv_buf, int *recv_size){ + return shm_sendandrecv_unsafe(shm_socket, send_buf, send_size, send_key, recv_buf, recv_size, 0, (int)SHM_MSG_NOWAIT); } -/** - * 鍚姩bus - * - * @return 0 鎴愬姛锛� 鍏朵粬鍊� 澶辫触鐨勯敊璇爜 -*/ -int ShmModSocket::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; -} /** * 璁㈤槄鎸囧畾涓婚 * @topic 涓婚 * @size 涓婚闀垮害 - * @port 鎬荤嚎绔彛 + * @key 鎬荤嚎绔彛 */ -int ShmModSocket::sub(char *topic, int size, int port){ - return _sub_( topic, size, port, NULL, 0); +int ShmModSocket::sub(char *topic, int size, int key){ + return _sub_( topic, size, key, NULL, 0); } // 瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇 -int ShmModSocket::sub_timeout(char *topic, int size, int port, struct timespec *timeout){ - return _sub_(topic, size, port, timeout, 0); +int ShmModSocket::sub_timeout(char *topic, int size, int key, struct timespec *timeout){ + return _sub_(topic, size, key, timeout, 0); } -int ShmModSocket::sub_nowait(char *topic, int size, int port) { - return _sub_(topic, size, port, NULL, (int)SHM_MSG_NOWAIT); +int ShmModSocket::sub_nowait(char *topic, int size, int key) { + return _sub_(topic, size, key, NULL, (int)SHM_MSG_NOWAIT); } @@ -233,17 +141,17 @@ * 鍙栨秷璁㈤槄鎸囧畾涓婚 * @topic 涓婚 * @size 涓婚闀垮害 - * @port 鎬荤嚎绔彛 + * @key 鎬荤嚎绔彛 */ -int ShmModSocket::desub(char *topic, int size, int port){ - return _desub_( topic, size, port, NULL, 0); +int ShmModSocket::desub(char *topic, int size, int key){ + return _desub_( topic, size, key, NULL, 0); } // 瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇 -int ShmModSocket::desub_timeout(char *topic, int size, int port, struct timespec *timeout){ - return _desub_(topic, size, port, timeout, 0); +int ShmModSocket::desub_timeout(char *topic, int size, int key, struct timespec *timeout){ + return _desub_(topic, size, key, timeout, 0); } -int ShmModSocket::desub_nowait(char *topic, int size, int port) { - return _desub_(topic, size, port, NULL, (int)SHM_MSG_NOWAIT); +int ShmModSocket::desub_nowait(char *topic, int size, int key) { + return _desub_(topic, size, key, NULL, (int)SHM_MSG_NOWAIT); } @@ -252,285 +160,195 @@ * 鍙戝竷涓婚 * @topic 涓婚 * @content 涓婚鍐呭 - * @port 鎬荤嚎绔彛 + * @key 鎬荤嚎绔彛 */ -int ShmModSocket::pub(char *topic, int topic_size, void *content, int content_size, int port){ - return _pub_(topic, topic_size, content, content_size, port, NULL, 0); +int ShmModSocket::pub(char *topic, int topic_size, void *content, int content_size, int key){ + return _pub_(topic, topic_size, content, content_size, key, NULL, 0); } // 瓒呮椂杩斿洖銆� @sec 绉� 锛� @nsec 绾崇 -int ShmModSocket::pub_timeout(char *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 ShmModSocket::pub_timeout(char *topic, int topic_size, void *content, int content_size, int key, struct timespec * timeout){ + return _pub_( topic, topic_size, content, content_size, key, timeout, 0); } -int ShmModSocket::pub_nowait(char *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); +int ShmModSocket::pub_nowait(char *topic, int topic_size, void *content, int content_size, int key){ + return _pub_(topic, topic_size, content, content_size, key, NULL, (int)SHM_MSG_NOWAIT); } /** - * 鑾峰彇soket绔彛鍙� + * 鑾峰彇soket key */ -int ShmModSocket::get_port(){ - return shm_socket->port; +int ShmModSocket::get_key(){ + return shm_socket->key; } // ============================================================================= /** - * @port 鎬荤嚎绔彛 + * @key 鎬荤嚎绔彛 */ -int ShmModSocket::_sub_(char *topic, int size, int port, +int ShmModSocket::_sub_(char *topic, int topic_size, int key, struct timespec *timeout, int flags) { - char buf[8192]; - int rv; - snprintf(buf, 8192, "%ssub%s%s%s%s", ACTION_LIDENTIFIER, ACTION_RIDENTIFIER, TOPIC_LIDENTIFIER, topic, TOPIC_RIDENTIFIER); - rv = shm_sendto(shm_socket, buf, strlen(buf) + 1, port, timeout, flags); - if(rv == 0) { - bus_set->insert(port); + // char buf[8192]; + // int rv; + // snprintf(buf, 8192, "%ssub%s%s%s%s", ACTION_LIDENTIFIER, ACTION_RIDENTIFIER, TOPIC_LIDENTIFIER, topic, TOPIC_RIDENTIFIER); + // rv = shm_sendto(shm_socket, buf, strlen(buf) + 1, key, timeout, flags); + // if(rv == 0) { + // bus_set->insert(key); + // } + // return rv; + + int ret; + bus_head_t head = {}; + memcpy(head.action, "sub", sizeof(head.action)); + head.topic_size = topic_size = strlen(topic) + 1; + head.content_size = 0; + + void *buf; + int size = get_bus_sendbuf(head, topic, topic_size, NULL, 0, &buf); + if(size > 0) { + ret = shm_sendto(shm_socket, buf, size, key, timeout, flags); + free(buf); + if(ret == 0) { + bus_set->insert(key); + } + return ret; + } else { + return -1; } - return rv; } /** - * @port 鎬荤嚎绔彛 + * @key 鎬荤嚎绔彛 */ -int ShmModSocket::_desub_(char *topic, int size, int port, +int ShmModSocket::_desub_(char *topic, int topic_size, int key, struct timespec *timeout, int flags) { - char buf[8192]; + // char buf[8192]; + int ret; if(topic == NULL) { topic = ""; } - snprintf(buf, 8192, "%sdesub%s%s%s%s", ACTION_LIDENTIFIER, ACTION_RIDENTIFIER, TOPIC_LIDENTIFIER, topic, TOPIC_RIDENTIFIER); - return shm_sendto(shm_socket, buf, strlen(buf) + 1, port, timeout, flags); -} + // snprintf(buf, 8192, "%sdesub%s%s%s%s", ACTION_LIDENTIFIER, ACTION_RIDENTIFIER, TOPIC_LIDENTIFIER, topic, TOPIC_RIDENTIFIER); + // return shm_sendto(shm_socket, buf, strlen(buf) + 1, key, timeout, flags); -/** - * @port 鎬荤嚎绔彛 - */ -int ShmModSocket::_pub_(char *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, 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); - -} -/* - * 澶勭悊璁㈤槄 -*/ -void ShmModSocket::_proxy_sub( char *topic, int port) { - SHMKeySet *subscripter_set; - - SHMTopicSubMap::iterator map_iter; - SHMKeySet::iterator set_iter; -//printf("_proxy_sub topic = %s\n", topic); - 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 ShmModSocket::_proxy_desub( 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; - - subscripter_set->erase(port); -printf("============ desub %d\n", port); - } -} - -/* - * 澶勭悊鍙栨秷鎵�鏈夎闃� -*/ -void ShmModSocket::_proxy_desub_all(int port) { - SHMKeySet *subscripter_set; - - SHMTopicSubMap::iterator map_iter; - // SHMKeySet::iterator set_iter; - for (auto map_iter = topic_sub_map->begin(); map_iter != topic_sub_map->end(); map_iter++) { - subscripter_set = map_iter->second; - subscripter_set->erase(port); -printf("============ desub %d\n", port); - } -} - -/* - * 澶勭悊鍙戝竷锛屼唬鐞嗚浆鍙� -*/ -void ShmModSocket::_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 * ShmModSocket::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)) { -// printf("run_pubsub_proxy %s %s \n", action, topics); - if(strcmp(action, "sub") == 0) { - // 璁㈤槄鏀寔澶氫富棰樿闃� - topic = strtok(topics, topic_delim); -//printf("run_pubsub_proxy topic = %s\n", topic); - while(topic) { - _proxy_sub(trim(topic, 0), port); - topic = strtok(NULL, topic_delim); - } - - } else if(strcmp(action, "desub") == 0) { -// printf("desub topic=%s,%s,%d\n", topics, trim(topics, 0), strcmp(trim(topics, 0), "")); - if(strcmp(trim(topics, 0), "") == 0) { - // 鍙栨秷鎵�鏈夎闃� - printf("====鍙栨秷鎵�鏈夎闃匼n"); - _proxy_desub_all(port); - } else { - - topic = strtok(topics, topic_delim); - while(topic) { - _proxy_desub(trim(topic, 0), port); - topic = strtok(NULL, topic_delim); - } - } - - - - } 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"); - } + bus_head_t head = {}; + memcpy(head.action, "desub", sizeof(head.action)); + head.topic_size = topic_size = strlen(topic) + 1; + head.content_size = 0; + void *buf; + int size = get_bus_sendbuf(head, topic, topic_size, NULL, 0, &buf); + if(size > 0) { + ret = shm_sendto(shm_socket, buf, size, key, timeout, flags); free(buf); + return ret; + } else { + return -1; } - return NULL; + +} + +/** + * @key 鎬荤嚎绔彛 + * @str "<**pub**>{缁忔祹}" + */ + +int ShmModSocket::_pub_(char *topic, int topic_size, void *content, int content_size, int key, + 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, topic, TOPIC_RIDENTIFIER); + // head_len = strlen(buf); + // memcpy(buf+head_len, content, content_size); + + int ret; + bus_head_t head = {}; + memcpy(head.action, "pub", sizeof(head.action)); + head.topic_size = topic_size = strlen(topic) + 1; + head.content_size = content_size; + + void *buf; + int size = get_bus_sendbuf(head, topic, topic_size, content, content_size, &buf); + if(size > 0) { + ret = shm_sendto(shm_socket, buf, size, key, timeout, flags); + free(buf); + return ret; + } else { + return -1; + } + + } -/** - * @str "<**sub**>{缁忔祹}" - */ - -int ShmModSocket::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); - +int ShmModSocket::get_bus_sendbuf(bus_head_t &request_head, + void *topic_buf, int topic_size, void *content_buf, int content_size, void **retbuf) { + + int buf_size; + char *buf; + int max_buf_size; + if((buf = (char *)malloc(MAXBUF)) == NULL) { + LoggerFactory::getLogger()->error(errno, "ShmModSocket::get_bus_sendbuf malloc"); + exit(1); } else { - return 0; + max_buf_size = MAXBUF; } - } else { - return 0; - } - char *topic = (char *)malloc(topic_len+1); - strncpy(topic, topic_start_ptr, topic_len); - *(topic+topic_len) = '\0'; - *_topic = topic; + buf_size = BUS_HEAD_SIZE + content_size + topic_size ; + if(max_buf_size < buf_size) { + + if((buf = (char *)realloc(buf, buf_size)) == NULL) { + LoggerFactory::getLogger()->error(errno, "ShmModSocket::get_bus_sendbuf realloc buf"); + exit(1); + } else { + max_buf_size = buf_size; + } + } - char *action = (char *)malloc(action_len+1); - strncpy(action, action_start_ptr, action_len); - *(action+action_len) = '\0'; - *_action = action; - *head_len = ptr-str; + memcpy(buf, ShmModSocket::encode_bus_head(request_head), BUS_HEAD_SIZE); + if(topic_size != 0 ) + memcpy(buf + BUS_HEAD_SIZE, topic_buf, topic_size); + if(content_size != 0) + memcpy(buf + BUS_HEAD_SIZE + topic_size, content_buf, content_size); + + *retbuf = buf; + return buf_size; +} - return 1; +/** + char action[]; + uint32_t topic_size; + uint32_t content_size; +*/ + +void * ShmModSocket::encode_bus_head(bus_head_t & head) { + void * headbs = malloc(BUS_HEAD_SIZE); + char *tmp_ptr = (char *)headbs; + + memcpy(tmp_ptr, head.action, sizeof(head.action)); + + tmp_ptr += sizeof(head.action); + PUT(tmp_ptr, htonl(head.topic_size)); + + tmp_ptr += 4; + PUT(tmp_ptr, htonl(head.content_size)); + + return headbs; +} + +bus_head_t ShmModSocket::decode_bus_head(void *headbs) { + char *tmp_ptr = (char *)headbs; + bus_head_t head; + + memcpy(head.action, tmp_ptr, sizeof(head.action)); + + tmp_ptr += sizeof(head.action); + head.topic_size = ntohl(GET(tmp_ptr)); + + tmp_ptr += 4; + head.content_size = ntohl(GET(tmp_ptr)); + + return head; } -- Gitblit v1.8.0