wangzhengquan
2020-08-04 3a89a77e79407d0d638ddf983ee580410cf807c5
src/socket/dgram_mod_socket.c
@@ -58,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);
}
@@ -80,30 +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);
}
@@ -117,50 +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;
   socket->mod = BUS;
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);
}
//==========================================================================================================================
@@ -204,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 ) {
            //对方已关闭的连接放到待删除队列里。如果直接删除会让iter指针出现错乱
            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);
         }
         
@@ -235,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) {
            // 订阅支持多主题订阅