wangzhengquan
2021-01-08 53e55dbd8709a2f948cf80305545fe88ff93169a
udpate
1个文件已修改
35 ■■■■■ 已修改文件
src/socket/shm_socket.cpp 35 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/socket/shm_socket.cpp
@@ -49,7 +49,7 @@
  int s, type;
  pthread_mutexattr_t mtxAttr;
  logger->debug("shm_open_socket\n");
  // 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;
@@ -78,7 +78,7 @@
  
  int ret, s;
  
  logger->debug("shm_close_socket\n");
  // logger->debug("shm_close_socket\n");
  switch (socket->socket_type) {
    case SHM_SOCKET_STREAM:
      ret =  _shm_close_stream_socket(socket, true);
@@ -499,7 +499,7 @@
int shm_sendandrecv_safe(shm_socket_t *socket, const void *send_buf,
int shm_sendandrecv_safe2(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;
@@ -547,6 +547,35 @@
  return -1;
}
int shm_sendandrecv_safe(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);
  }
  /* 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) {