wangzhengquan
2021-01-25 c46be6db32872bfd7c4010b43526b5e6bc0fa6a5
src/socket/shm_socket.cpp
@@ -31,7 +31,7 @@
   void *tmp_ptr = mm_get_by_key(socket->key);
    if (tmp_ptr!= NULL && tmp_ptr != (void *)1 && !socket->force_bind ) {
      bus_errno = EBUS_KEY_INUSED;
      logger->error("%s. key = %d ", bus_strerror(bus_errno), socket->key);
      logger->error("%s. key = %d ", bus_strerror(EBUS_KEY_INUSED), socket->key);
      return 0;
    }
    return 1;
@@ -46,22 +46,37 @@
}
shm_socket_t *shm_open_socket(shm_socket_type_t socket_type) {
  int s, type;
  pthread_mutexattr_t mtxAttr;
  logger->debug("shm_open_socket\n");
  shm_socket_t *socket = (shm_socket_t *)calloc(1, sizeof(shm_socket_t));
  socket->socket_type = socket_type;
  socket->key = -1;
  socket->key = 0;
  socket->force_bind = false;
  socket->dispatch_thread = 0;
  socket->status = SHM_CONN_CLOSED;
  socket->mutex = SemUtil::get(IPC_PRIVATE, 1);
  s = pthread_mutexattr_init(&mtxAttr);
  if (s != 0)
    err_exit(s, "pthread_mutexattr_init");
  s = pthread_mutexattr_settype(&mtxAttr, PTHREAD_MUTEX_ERRORCHECK);
  if (s != 0)
    err_exit(s, "pthread_mutexattr_settype");
  s = pthread_mutex_init(&(socket->mutex), &mtxAttr);
  if (s != 0)
    err_exit(s, "pthread_mutex_init");
  s = pthread_mutexattr_destroy(&mtxAttr);
  if (s != 0)
    err_exit(s, "pthread_mutexattr_destroy");
  return socket;
}
int shm_close_socket(shm_socket_t *socket) {
  
  int ret;
  int ret, s;
  
  logger->debug("shm_close_socket\n");
  switch (socket->socket_type) {
@@ -74,8 +89,13 @@
    default:
      break;
  }
  s =  pthread_mutex_destroy(&(socket->mutex) );
  if(s != 0) {
    err_exit(s, "shm_close_socket");
  }
  free(socket);
  SemUtil::remove(socket->mutex);
  return ret;
}
@@ -107,7 +127,7 @@
  int key;
  hashtable_t *hashtable = mm_get_hashtable();
  if (socket->key == -1) {
  if (socket->key == 0) {
    key = hashtable_alloc_key(hashtable);
    socket->key = key;
  } else {
@@ -143,7 +163,7 @@
  shm_socket_t *client_socket;
  shm_msg_t src;
  if (socket->acceptQueue->pop(src)) {
  if (socket->acceptQueue->pop(src) == 0) {
    // print_msg("===accept:", src);
    client_key = src.key;
@@ -170,7 +190,7 @@
    msg.size = 0;
    msg.type = SHM_SOCKET_OPEN_REPLY;
    if (client_socket->remoteQueue->push_timeout(msg, &timeout)) {
    if (client_socket->remoteQueue->push(msg, &timeout, BUS_TIMEOUT_FLAG) == 0) {
      client_socket->status = SHM_CONN_ESTABLISHED;
      return client_socket;
    } else {
@@ -200,7 +220,7 @@
    return -1;
  }
  if (socket->key == -1) {
  if (socket->key == 0) {
    socket->key = hashtable_alloc_key(hashtable);
  } else {
    if(!_shm_socket_check_key(socket)) {
@@ -223,10 +243,10 @@
  msg.key = socket->key;
  msg.size = 0;
  msg.type = SHM_SOCKET_OPEN;
  socket->remoteQueue->push_timeout(msg, &timeout);
  socket->remoteQueue->push(msg, &timeout, BUS_TIMEOUT_FLAG);
  //接受open reply
  if (socket->queue->pop(msg)) {
  if (socket->queue->pop(msg) == 0) {
    // 在这里server端已经准备好接受客户端发送请求了,完成与服务端的连接
    if (msg.type == SHM_SOCKET_OPEN_REPLY) {
      socket->status = SHM_CONN_ESTABLISHED;
@@ -263,7 +283,7 @@
  dest.buf = mm_malloc(size);
  memcpy(dest.buf, buf, size);
  if (socket->remoteQueue->push(dest)) {
  if (socket->remoteQueue->push(dest) == 0) {
    return 0;
  } else {
    logger->error(errno, "connection has been closed!");
@@ -280,7 +300,7 @@
  }
  shm_msg_t src;
  if (socket->messageQueue->pop(src)) {
  if (socket->messageQueue->pop(src) == 0) {
    void *_buf = malloc(src.size);
    memcpy(_buf, src.buf, src.size);
    *buf = _buf;
@@ -295,7 +315,11 @@
// 短连接方式发送
int shm_sendto(shm_socket_t *socket, const void *buf, const int size,
               const int key, const struct timespec *timeout, const int flags) {
               const int key, const struct timespec *timeout, const int flag) {
  int s;
  int rv;
  if (socket->socket_type != SHM_SOCKET_DGRAM) {
    logger->error( "shm_socket.shm_sendto: Can't invoke shm_sendto method in a %d type socket  which is "
                "not a SHM_SOCKET_DGRAM socket ",
@@ -304,9 +328,12 @@
  }
  hashtable_t *hashtable = mm_get_hashtable();
  SemUtil::dec(socket->mutex);
  if ((s = pthread_mutex_lock(&(socket->mutex))) != 0)
    err_exit(s, "shm_sendto : pthread_mutex_lock");
  if (socket->queue == NULL) {
    if (socket->key == -1) {
    if (socket->key == 0) {
      socket->key = hashtable_alloc_key(hashtable);
    } else {
@@ -319,7 +346,12 @@
    socket->queue = new SHMQueue<shm_msg_t>(socket->key, 16);
  }
  SemUtil::inc(socket->mutex);
  if ((s = pthread_mutex_unlock(&(socket->mutex))) != 0)
    err_exit(s, "shm_sendto : pthread_mutex_unlock");
  // There is some case where a socket need to send to himeself, for example when bus server need to stop, he need to send himself
  // a top message.
  
  // if (key == socket->key) {
  //   logger->error( "can not send to your self!");
@@ -340,73 +372,62 @@
  dest.buf = mm_malloc(size);
  memcpy(dest.buf, buf, size);
  // printf("shm_sendto push before\n");
  bool rv;
  if(flags & SHM_MSG_NOWAIT != 0) {
    rv = remoteQueue->push_nowait(dest);
  } else if(timeout != NULL) {
     rv = remoteQueue->push_timeout(dest, timeout);
  } else {
     rv = remoteQueue->push(dest);
  }
  rv = remoteQueue->push(dest, timeout, flag);
  if (rv) {
  if (rv == 0) {
    // printf("shm_sendto push after\n");
    delete remoteQueue;
    return 0;
  } else {
    delete remoteQueue;
    mm_free(dest.buf);
    if(errno == EAGAIN) {
      bus_errno = EBUS_TIMEOUT;
      logger->error("sendto key %d failed, %s", key, bus_strerror(bus_errno));
      return EBUS_TIMEOUT;
    } else {
      logger->error(errno, "sendto key %d failed!", key);
      return -1;
    }
    logger->debug("sendto key %d failed %s", key, bus_strerror(rv));
    return rv;
  }
}
int shm_recvfrom2(shm_socket_t *socket, void **buf, int *size, int *key,  const struct timespec *timeout,  int flag) {
  return 501;
}
// 短连接方式接受
int shm_recvfrom(shm_socket_t *socket, void **buf, int *size, int *key,  struct timespec *timeout,  int flags) {
  if (socket->socket_type != SHM_SOCKET_DGRAM) {
int shm_recvfrom(shm_socket_t *sokt, void **buf, int *size, int *key,  const struct timespec *timeout,  int flag) {
  int s;
  int rv;
  if (sokt->socket_type != SHM_SOCKET_DGRAM) {
    logger->error("shm_socket.shm_recvfrom: Can't invoke shm_recvfrom method in a %d type socket  which "
                "is not a SHM_SOCKET_DGRAM socket ",
             socket->socket_type);
             sokt->socket_type);
    exit(1);
  }
  hashtable_t *hashtable = mm_get_hashtable();
  SemUtil::dec(socket->mutex);
  if (socket->queue == NULL) {
    if (socket->key == -1) {
      socket->key = hashtable_alloc_key(hashtable);
  if ((s = pthread_mutex_lock(&(sokt->mutex))) != 0)
    err_exit(s, "shm_recvfrom : pthread_mutex_lock");
  if (sokt->queue == NULL) {
    if (sokt->key == 0) {
      sokt->key = hashtable_alloc_key(hashtable);
    } else {
      if(!_shm_socket_check_key(socket)) {
      if(!_shm_socket_check_key(sokt)) {
        bus_errno = EBUS_KEY_INUSED;
        return EBUS_KEY_INUSED;
      }
    }
    socket->queue = new SHMQueue<shm_msg_t>(socket->key, 16);
    sokt->queue = new SHMQueue<shm_msg_t>(sokt->key, 16);
  }
  SemUtil::inc(socket->mutex);
  if ((s = pthread_mutex_unlock(&(sokt->mutex))) != 0)
    err_exit(s, "shm_recvfrom : pthread_mutex_unlock");
  shm_msg_t src;
  // printf("shm_recvfrom pop before\n");
  bool rv;
   if(flags & SHM_MSG_NOWAIT != 0) {
    rv = socket->queue->pop_nowait(src);
  } else if(timeout != NULL) {
    rv = socket->queue->pop_timeout(src, timeout);
  } else {
    rv = socket->queue->pop(src);
  }
printf ("====== before ======\n");
  rv = sokt->queue->pop(src, timeout, flag);
printf ("====== after ======\n %d", rv);
  if (rv) {
  if (rv == 0) {
    if(buf != NULL) {
      void *_buf = malloc(src.size);
      memcpy(_buf, src.buf, src.size);
@@ -420,10 +441,11 @@
      *key = src.key;
    mm_free(src.buf);
    // printf("shm_recvfrom pop after\n");
    return 0;
  } else {
    return -1;
    logger->debug("shm_recvfrom failed %s", bus_strerror(rv));
    return rv;
  }
}
@@ -461,10 +483,10 @@
}
int shm_sendandrecv_safe(shm_socket_t *socket, const void *send_buf,
// use thread local
int _shm_sendandrecv_thread_local(shm_socket_t *socket, const void *send_buf,
                    const int send_size, const int send_key, void **recv_buf,
                    int *recv_size,  struct timespec *timeout,  int flags) {
                    int *recv_size,  const struct timespec *timeout,  int flags) {
  int recv_key;
  int rv;
@@ -489,7 +511,7 @@
  if (tmp_socket == NULL)
  {
    /* If first call from this thread, allocate buffer for thread, and save its location */
    logger->debug("%d create tmp socket\n", pthread_self() );
    logger->debug("%ld create tmp socket\n", (long)pthread_self() );
    tmp_socket = shm_open_socket(SHM_SOCKET_DGRAM);
    rv =  pthread_setspecific(_tmp_recv_socket_key_, tmp_socket);
@@ -501,18 +523,44 @@
  if ((rv = shm_sendto(tmp_socket, send_buf, send_size, send_key, timeout, flags)) == 0) {
    rv = shm_recvfrom(tmp_socket, recv_buf, recv_size, &recv_key, timeout, flags);
    return rv;
  } else {
    return rv;
  }
  return -1;
}
int _shm_sendandrecv_alloc_new(shm_socket_t *socket, const void *send_buf,
                    const int send_size, const int send_key, void **recv_buf,
                    int *recv_size,  const struct timespec *timeout,  int flags) {
  int recv_key;
  int rv;
  // 用thread local 保证每个线程用一个独占的socket接受对方返回的信息
  shm_socket_t *tmp_socket;
  if (socket->socket_type != SHM_SOCKET_DGRAM) {
    logger->error( "shm_socket.shm_sendandrecv: Can't invoke shm_sendandrecv method in a %d type socket  "
                "which is not a SHM_SOCKET_DGRAM socket ",
             socket->socket_type);
    exit(1);
  }
  /* If first call from this thread, allocate buffer for thread, and save its location */
  // logger->debug("%d create tmp socket\n", pthread_self() );
  tmp_socket = shm_open_socket(SHM_SOCKET_DGRAM);
  if ((rv = shm_sendto(tmp_socket, send_buf, send_size, send_key, timeout, flags)) == 0) {
    rv = shm_recvfrom(tmp_socket, recv_buf, recv_size, &recv_key, timeout, flags);
  }
  shm_close_socket(tmp_socket);
  return rv;
}
int shm_sendandrecv_unsafe(shm_socket_t *socket, const void *send_buf,
                    const int send_size, const int send_key, void **recv_buf,
                    int *recv_size,  struct timespec *timeout,  int flags) {
                    int *recv_size,  const struct timespec *timeout,  int flags) {
  if (socket->socket_type != SHM_SOCKET_DGRAM) {
    logger->error( "shm_socket.shm_sendandrecv_unsafe : Can't invoke shm_sendandrecv method in a %d type socket  "
                "which is not a SHM_SOCKET_DGRAM socket ",
@@ -534,8 +582,8 @@
int shm_sendandrecv(shm_socket_t *socket, const void *send_buf,
                    const int send_size, const int send_key, void **recv_buf,
                    int *recv_size,  struct timespec *timeout,  int flags) {
  return  shm_sendandrecv_unsafe(socket, send_buf, send_size, send_key,recv_buf, recv_size, timeout,  flags);
                    int *recv_size,  const struct timespec *timeout,  int flags) {
  return  _shm_sendandrecv_thread_local(socket, send_buf, send_size, send_key,recv_buf, recv_size, timeout,  flags);
}
@@ -578,11 +626,11 @@
  shm_socket_t *client_socket;
  std::map<int, shm_socket_t *>::iterator iter;
  while (socket->queue->pop(src)) {
  while (socket->queue->pop(src) == 0) {
    switch (src.type) {
    case SHM_SOCKET_OPEN:
      socket->acceptQueue->push_timeout(src, &timeout);
      socket->acceptQueue->push(src, &timeout, BUS_TIMEOUT_FLAG);
      break;
    case SHM_SOCKET_CLOSE:
      _server_close_conn_to_client(socket, src.key);
@@ -593,7 +641,7 @@
      if (iter != socket->clientSocketMap->end()) {
        client_socket = iter->second;
        // print_msg("_server_run_msg_rev push before", src);
        client_socket->messageQueue->push_timeout(src, &timeout);
        client_socket->messageQueue->push(src, &timeout, BUS_TIMEOUT_FLAG);
        // print_msg("_server_run_msg_rev push after", src);
      }
@@ -621,14 +669,14 @@
  struct timespec timeout = {1, 0};
  shm_msg_t src;
  while (socket->queue->pop(src)) {
  while (socket->queue->pop(src) == 0) {
    switch (src.type) {
    case SHM_SOCKET_CLOSE:
      _client_close_conn_to_server(socket);
      break;
    case SHM_COMMON_MSG:
      socket->messageQueue->push_timeout(src, &timeout);
      socket->messageQueue->push(src, &timeout, BUS_TIMEOUT_FLAG);
      break;
    default:
       logger->error( "shm_socket._client_run_msg_rev: undefined message type.");
@@ -648,7 +696,7 @@
  close_msg.size = 0;
  close_msg.type = SHM_SOCKET_CLOSE;
  if (notifyRemote && socket->remoteQueue != NULL) {
    socket->remoteQueue->push_timeout(close_msg, &timeout);
    socket->remoteQueue->push(close_msg, &timeout, BUS_TIMEOUT_FLAG);
  }
  if (socket->queue != NULL) {
@@ -656,10 +704,7 @@
    socket->queue = NULL;
  }
  if (socket->remoteQueue != NULL) {
    delete socket->remoteQueue;
    socket->remoteQueue = NULL;
  }
  if (socket->messageQueue != NULL) {
    delete socket->messageQueue;
@@ -677,8 +722,7 @@
         iter != socket->clientSocketMap->end(); iter++) {
      client_socket = iter->second;
      client_socket->remoteQueue->push_timeout(close_msg, &timeout);
      delete client_socket->remoteQueue;
      client_socket->remoteQueue->push(close_msg, &timeout, BUS_TIMEOUT_FLAG);
      client_socket->remoteQueue = NULL;
      delete client_socket->messageQueue;