wangzhengquan
2020-12-04 aa2f3b2a9968bb4928463bdae05fb026d16b60bb
src/socket/shm_socket.c
@@ -19,10 +19,11 @@
static int _shm_close_stream_socket(shm_socket_t *socket, bool notifyRemote);
// 检查key是否已经被使用,是返回0, 否返回1
static inline int  _shm_socket_check_key(shm_socket_t *socket) {
   void *tmp_ptr = mm_get_by_key(socket->key);
    if (tmp_ptr!= NULL && tmp_ptr != (void *)1 && !socket->force_bind ) {
      err_exit(0, "key %d has already been in used!", socket->key);
      logger->error("key %d has already been in used!", socket->key);
      return 0;
    }
    return 1;
@@ -91,7 +92,9 @@
    socket->key = key;
  } else {
   _shm_socket_check_key(socket);
   if(!_shm_socket_check_key(socket)) {
     return -1;
   }
  }
  socket->queue = new SHMQueue<shm_msg_t>(socket->key, 16);
@@ -161,6 +164,9 @@
}
/**
 * @return 0成功. 其他值失败
 */
int shm_connect(shm_socket_t *socket, int key) {
  if (socket->socket_type != SHM_SOCKET_STREAM) {
    logger->error( "can not invoke shm_connect method with a socket which is not "
@@ -170,20 +176,22 @@
  hashtable_t *hashtable = mm_get_hashtable();
  if (hashtable_get(hashtable, key) == NULL) {
    logger->error("shm_connect:connect at key %d  failed!", key);
    exit(1);
    return -1;
  }
  if (socket->key == -1) {
    socket->key = hashtable_alloc_key(hashtable);
  } else {
    _shm_socket_check_key(socket);
    if(!_shm_socket_check_key(socket)) {
      return -1;
    }
  }
  socket->queue = new SHMQueue<shm_msg_t>(socket->key, 16);
  if ((socket->remoteQueue = _attach_remote_queue(key)) == NULL) {
    logger->error("connect to %d failted", key);
    exit(1);
    return -1;
  }
  socket->messageQueue = new LockFreeQueue<shm_msg_t, DM_Allocator>(16);
@@ -209,7 +217,7 @@
  } else {
    logger->error( "connect failted!");
    exit(1);
    return -1;
  }
  return 0;
@@ -280,7 +288,10 @@
      socket->key = hashtable_alloc_key(hashtable);
    } else {
     _shm_socket_check_key(socket);
     if(!_shm_socket_check_key(socket)) {
        return -1;
     }
    }
    socket->queue = new SHMQueue<shm_msg_t>(socket->key, 16);
@@ -342,7 +353,9 @@
      socket->key = hashtable_alloc_key(hashtable);
    } else {
      _shm_socket_check_key(socket);
      if(!_shm_socket_check_key(socket)) {
        return -1;
      }
    }
    socket->queue = new SHMQueue<shm_msg_t>(socket->key, 16);
@@ -374,25 +387,76 @@
  }
}
static pthread_once_t _once_ = PTHREAD_ONCE_INIT;
static pthread_key_t _tmp_recv_socket_key_;
 /* Free thread-specific data buffer */
static void _destrory_tmp_recv_socket_(void *tmp_socket)
{
  logger->debug("%d destroy tmp socket\n", pthread_self());
  shm_close_socket((shm_socket_t *)tmp_socket);
}
/* One-time key creation function */
static void _create_tmp_recv_socket_key(void)
{
  int s;
  /* Allocate a unique thread-specific data key and save the address
     of the destructor for thread-specific data buffers */
  s = pthread_key_create(&_tmp_recv_socket_key_, _destrory_tmp_recv_socket_);
  if (s != 0) {
     logger->error(s, "pthread_key_create");
     abort(); /* dump core and terminate */
     exit(1);
  }
}
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) {
  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);
  }
  int recv_key;
  int rv;
  shm_socket_t *tmp_socket = shm_open_socket(SHM_SOCKET_DGRAM);
  rv = pthread_once(&_once_, _create_tmp_recv_socket_key);
  if (rv != 0) {
    logger->error(rv, "shm_sendandrecv pthread_once");
    exit(1);
  }
  tmp_socket = (shm_socket_t *)pthread_getspecific(_tmp_recv_socket_key_);
  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() );
    tmp_socket = shm_open_socket(SHM_SOCKET_DGRAM);
    rv = pthread_setspecific(_tmp_recv_socket_key_, tmp_socket);
    if (rv != 0) {
      logger->error(rv, "shm_sendandrecv : pthread_setspecific");
      exit(1);
    }
  }
  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;
  } else {
    shm_close_socket(tmp_socket);
    return rv;
  }
  return -1;