wangzhengquan
2021-01-14 124011574583b980c1794a1de4e61b39f73f1f11
src/socket/shm_socket.cpp
@@ -49,10 +49,10 @@
  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;
  socket->key = 0;
  socket->force_bind = false;
  socket->dispatch_thread = 0;
  socket->status = SHM_CONN_CLOSED;
@@ -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);
@@ -127,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 {
@@ -163,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;
@@ -190,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_timeout(msg, &timeout) == 0) {
      client_socket->status = SHM_CONN_ESTABLISHED;
      return client_socket;
    } else {
@@ -220,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)) {
@@ -246,7 +246,7 @@
  socket->remoteQueue->push_timeout(msg, &timeout);
  //接受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;
@@ -283,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!");
@@ -300,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;
@@ -333,7 +333,7 @@
    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 {
@@ -369,7 +369,6 @@
  dest.buf = mm_malloc(size);
  memcpy(dest.buf, buf, size);
  // printf("shm_sendto push before\n");
 
  if(flags & SHM_MSG_NOWAIT != 0) {
    rv = remoteQueue->push_nowait(dest);
@@ -379,20 +378,20 @@
     rv = remoteQueue->push(dest);
  }
  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));
    if(rv == EBUS_TIMEOUT) {
      // bus_errno = EBUS_TIMEOUT;
      // logger->error("sendto key %d failed, %s", key, bus_strerror(EBUS_TIMEOUT));
      return EBUS_TIMEOUT;
    } else {
      logger->error(errno, "sendto key %d failed!", key);
      return -1;
      //logger->error(errno, "sendto key %d failed!", key);
      return rv;
    }
   
   
@@ -416,7 +415,7 @@
    err_exit(s, "shm_recvfrom : pthread_mutex_lock");
 
  if (socket->queue == NULL) {
    if (socket->key == -1) {
    if (socket->key == 0) {
      socket->key = hashtable_alloc_key(hashtable);
    } else {
@@ -433,7 +432,6 @@
    err_exit(s, "shm_recvfrom : pthread_mutex_unlock");
  shm_msg_t src;
  // printf("shm_recvfrom pop before\n");
 
   if(flags & SHM_MSG_NOWAIT != 0) {
    rv = socket->queue->pop_nowait(src);
@@ -443,7 +441,7 @@
    rv = socket->queue->pop(src);
  }
  if (rv) {
  if (rv == 0) {
    if(buf != NULL) {
      void *_buf = malloc(src.size);
      memcpy(_buf, src.buf, src.size);
@@ -457,10 +455,17 @@
      *key = src.key;
    mm_free(src.buf);
    // printf("shm_recvfrom pop after\n");
    return 0;
  } else {
    return -1;
    if(rv == EBUS_TIMEOUT) {
      // logger->error("shm_recvfrom  failed, %s", bus_strerror(EBUS_TIMEOUT));
      return EBUS_TIMEOUT;
    } else {
      // logger->error(rv, "shm_recvfrom  failed!");
      return rv;
    }
  }
}
@@ -498,8 +503,8 @@
}
int shm_sendandrecv_safe2(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_key;
@@ -547,7 +552,7 @@
  return -1;
}
int shm_sendandrecv_safe(shm_socket_t *socket, const void *send_buf,
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,  struct timespec *timeout,  int flags) {
  int recv_key;
@@ -601,7 +606,7 @@
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_safe(socket, send_buf, send_size, send_key,recv_buf, recv_size, timeout,  flags);
  return  _shm_sendandrecv_thread_local(socket, send_buf, send_size, send_key,recv_buf, recv_size, timeout,  flags);
}
@@ -644,7 +649,7 @@
  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:
@@ -687,7 +692,7 @@
  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: