fujuntang
2021-08-11 68d23225a38a35f1325eb39fa4ed5a005d5de473
src/socket/shm_mod_socket.cpp
@@ -3,18 +3,13 @@
static Logger *logger = LoggerFactory::getLogger();
size_t ShmModSocket::remove_keys(int keys[], size_t length) {
   BusServerSocket::remove_subscripters(keys, length);
   return shm_socket_remove_keys(keys, length);
}
ShmModSocket::ShmModSocket() {
   shm_socket = shm_open_socket(SHM_SOCKET_DGRAM);
   shm_socket = shm_socket_open(SHM_SOCKET_DGRAM);
   bus_set = new std::set<int>;
}
ShmModSocket::~ShmModSocket() {
  // logger->debug("Close 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++) {
@@ -23,8 +18,13 @@
      delete bus_set;
   }
   shm_close_socket(shm_socket);
   shm_socket_close(shm_socket);
}
int ShmModSocket::stop() {
   return shm_socket_stop(shm_socket);
}
int ShmModSocket::bind(int key) {
   return  shm_socket_bind(shm_socket, key);
@@ -89,6 +89,12 @@
  logger->debug("ShmModSocket::sendandrecv : sendandrecv to %d failed %s",  send_key, bus_strerror(rv));
   return rv;
}
int ShmModSocket::recvandsend( recvandsend_callback_fn callback,
                    const struct timespec *timeout , int flag, void * user_data ) {
  return shm_recvandsend(shm_socket, callback, timeout, flag, user_data);
}
 
// // 超时返回。 @sec 秒 , @nsec 纳秒
// int ShmModSocket::sendandrecv_unsafe(const void *send_buf, const int send_size, const int send_key, 
@@ -102,7 +108,7 @@
 * @size 主题长度
 * @key 总线端口
 */
int  ShmModSocket::sub(char *topic, int topic_size, int key,
int  ShmModSocket::sub(const char *topic, int topic_size, int key,
   const struct timespec *timeout, int flags) {
   int ret;
   bus_head_t head = {};
@@ -133,7 +139,7 @@
 * @size 主题长度
 * @key 总线端口
 */
int  ShmModSocket::desub(char *topic, int topic_size, int key, const struct timespec *timeout, int flags) {
int  ShmModSocket::desub(const char *topic, int topic_size, int key, const struct timespec *timeout, int flags) {
   // char buf[8192];
   int ret;
   if(topic == NULL) {
@@ -171,7 +177,7 @@
 * @content 主题内容
 * @key 总线端口
 */
int  ShmModSocket::pub(char *topic, int topic_size, void *content, int content_size, int key, const struct timespec *timeout, int flags) {
int  ShmModSocket::pub(const char *topic, int topic_size, const void *content, int content_size, int key, const struct timespec *timeout, int flags) {
   int ret;
   bus_head_t head = {};
   memcpy(head.action, "pub", sizeof(head.action));
@@ -204,11 +210,12 @@
// =============================================================================
int ShmModSocket::get_bus_sendbuf(bus_head_t &request_head, 
  void *topic_buf, int topic_size, void *content_buf, int content_size, void **retbuf) {
 const void *topic_buf, int topic_size, const void *content_buf, int content_size, void **retbuf) {
 
  int buf_size;
  char *buf;
  int  max_buf_size;
  void *buf_ptr;
  if((buf = (char *) malloc(MAXBUF)) == NULL) {
    LoggerFactory::getLogger()->error(errno, "ShmModSocket::get_bus_sendbuf malloc");
    exit(1);
@@ -227,13 +234,15 @@
    }
  }
  memcpy(buf, ShmModSocket::encode_bus_head(request_head), BUS_HEAD_SIZE);
  buf_ptr = ShmModSocket::encode_bus_head(request_head);
  memcpy(buf, buf_ptr, 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;
  free(buf_ptr);
  return buf_size;
}
@@ -252,7 +261,7 @@
  tmp_ptr += sizeof(head.action);
  PUT(tmp_ptr, htonl(head.topic_size));
  tmp_ptr += 4;
  tmp_ptr += sizeof(head.topic_size);
  PUT(tmp_ptr, htonl(head.content_size));
  
  return headbs;
@@ -267,7 +276,7 @@
  tmp_ptr += sizeof(head.action);
  head.topic_size = ntohl(GET(tmp_ptr));
  tmp_ptr += 4;
  tmp_ptr += sizeof(head.topic_size);
  head.content_size = ntohl(GET(tmp_ptr));
 
  return head;