wangzhengquan
2020-12-24 e4130d29e80a40884310481ded90ff845c614f43
src/socket/shm_socket.c
@@ -7,9 +7,12 @@
void print_msg(char *head, shm_msg_t &msg) {
static void print_msg(char *head, shm_msg_t &msg) {
  // err_msg(0, "%s: key=%d, type=%d\n", head, msg.key, msg.type);
}
static pthread_once_t _once_ = PTHREAD_ONCE_INIT;
static pthread_key_t _tmp_recv_socket_key_;
static void *_server_run_msg_rev(void *_socket);
@@ -18,6 +21,9 @@
static int _shm_close_dgram_socket(shm_socket_t *socket);
static int _shm_close_stream_socket(shm_socket_t *socket, bool notifyRemote);
static void _destrory_tmp_recv_socket_(void *tmp_socket);
static void _create_tmp_recv_socket_key(void);
// 检查key是否已经被使用,是返回0, 否返回1
static inline int  _shm_socket_check_key(shm_socket_t *socket) {
@@ -38,6 +44,8 @@
}
shm_socket_t *shm_open_socket(shm_socket_type_t socket_type) {
  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;
@@ -45,12 +53,15 @@
  socket->dispatch_thread = 0;
  socket->status = SHM_CONN_CLOSED;
  socket->mutex = SemUtil::get(IPC_PRIVATE, 1);
  return socket;
}
int shm_close_socket(shm_socket_t *socket) {
  
  int ret;
  logger->debug("shm_close_socket\n");
  switch (socket->socket_type) {
    case SHM_SOCKET_STREAM:
      ret =  _shm_close_stream_socket(socket, true);
@@ -61,10 +72,17 @@
    default:
      break;
  }
  SemUtil::remove(socket->mutex);
  free(socket);
  SemUtil::remove(socket->mutex);
  return ret;
}
// int shm_close_socket(shm_socket_t *socket) {
//   // _destrory_tmp_recv_socket_((shm_socket_t *)pthread_getspecific(_tmp_recv_socket_key_));
//   return shm_close_socket(socket);;
// }
int shm_socket_bind(shm_socket_t *socket, int key) {
  socket->key = key;
@@ -229,11 +247,11 @@
                "SHM_SOCKET_STREAM socket");
    exit(1);
  }
  // hashtable_t *hashtable = mm_get_hashtable();
  // if(socket->remoteQueue == NULL) {
  //    err_msg(errno, "当前客户端无连接!");
  //    return -1;
  // }
  hashtable_t *hashtable = mm_get_hashtable();
  if(socket->remoteQueue == NULL) {
     err_msg(errno, "当前客户端无连接!");
     return -1;
  }
  shm_msg_t dest;
  dest.type = SHM_COMMON_MSG;
  dest.key = socket->key;
@@ -374,11 +392,18 @@
  }
  if (rv) {
    void *_buf = malloc(src.size);
    memcpy(_buf, src.buf, src.size);
    *buf = _buf;
    *size = src.size;
    *key = src.key;
    if(buf != NULL) {
      void *_buf = malloc(src.size);
      memcpy(_buf, src.buf, src.size);
      *buf = _buf;
    }
    if(size != NULL)
      *size = src.size;
    if(key != NULL)
      *key = src.key;
    mm_free(src.buf);
    // printf("shm_recvfrom pop after\n");
    return 0;
@@ -387,14 +412,21 @@
  }
}
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)
{
  int rv;
  if(tmp_socket == NULL)
    return;
  logger->debug("%d destroy tmp socket\n", pthread_self()); 
  shm_close_socket((shm_socket_t *)tmp_socket);
  rv =  pthread_setspecific(_tmp_recv_socket_key_, NULL);
  if ( rv != 0) {
    logger->error(rv, "shm_sendandrecv : pthread_setspecific");
    exit(1);
  }
}
/* One-time key creation function */
@@ -405,6 +437,7 @@
  /* 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_);
  //s = pthread_key_create(&_tmp_recv_socket_key_, NULL);
  if (s != 0) {
     logger->error(s, "pthread_key_create");
     abort(); /* dump core and terminate */ 
@@ -414,7 +447,7 @@
int shm_sendandrecv(shm_socket_t *socket, const void *send_buf,
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;
@@ -444,8 +477,8 @@
    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) {
    rv =  pthread_setspecific(_tmp_recv_socket_key_, tmp_socket);
    if ( rv != 0) {
      logger->error(rv, "shm_sendandrecv : pthread_setspecific");
      exit(1);
    }
@@ -484,6 +517,13 @@
  return -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) {
  return  shm_sendandrecv_unsafe(socket, send_buf, send_size, send_key,recv_buf, recv_size, timeout,  flags);
}
// ============================================================================================================
/**