| | |
| | | msg.size = 0; |
| | | msg.type = SHM_SOCKET_OPEN_REPLY; |
| | | |
| | | if (client_socket->remoteQueue->push_timeout(msg, &timeout) == 0) { |
| | | if (client_socket->remoteQueue->push(msg, &timeout, BUS_TIMEOUT_FLAG) == 0) { |
| | | client_socket->status = SHM_CONN_ESTABLISHED; |
| | | return client_socket; |
| | | } else { |
| | |
| | | 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) == 0) { |
| | |
| | | |
| | | // 短连接方式发送 |
| | | 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; |
| | |
| | | dest.buf = mm_malloc(size); |
| | | memcpy(dest.buf, buf, size); |
| | | |
| | | |
| | | if( (flags & BUS_NOWAIT_FLAG) == BUS_NOWAIT_FLAG) { |
| | | rv = remoteQueue->push_nowait(dest); |
| | | } else if((flags & BUS_TIMEOUT_FLAG) == BUS_TIMEOUT_FLAG && timeout != NULL) { |
| | | rv = remoteQueue->push_timeout(dest, timeout); |
| | | } else { |
| | | rv = remoteQueue->push(dest); |
| | | } |
| | | rv = remoteQueue->push(dest, timeout, flag); |
| | | |
| | | if (rv == 0) { |
| | | // printf("shm_sendto push after\n"); |
| | | return 0; |
| | | } else { |
| | | mm_free(dest.buf); |
| | | if(rv > EBUS_BASE) { |
| | | // bus_errno = EBUS_TIMEOUT; |
| | | logger->debug("sendto key %d failed %s", key, bus_strerror(rv)); |
| | | } else { |
| | | logger->error(rv, "sendto key %d failed", key); |
| | | } |
| | | 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) { |
| | | int shm_recvfrom(shm_socket_t *sokt, void **buf, int *size, int *key, const struct timespec *timeout, int flag) { |
| | | int s; |
| | | int rv; |
| | | |
| | | if (socket->socket_type != SHM_SOCKET_DGRAM) { |
| | | 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(); |
| | | |
| | | if ((s = pthread_mutex_lock(&(socket->mutex))) != 0) |
| | | if ((s = pthread_mutex_lock(&(sokt->mutex))) != 0) |
| | | err_exit(s, "shm_recvfrom : pthread_mutex_lock"); |
| | | |
| | | if (socket->queue == NULL) { |
| | | if (socket->key == 0) { |
| | | socket->key = hashtable_alloc_key(hashtable); |
| | | 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); |
| | | } |
| | | |
| | | if ((s = pthread_mutex_unlock(&(socket->mutex))) != 0) |
| | | if ((s = pthread_mutex_unlock(&(sokt->mutex))) != 0) |
| | | err_exit(s, "shm_recvfrom : pthread_mutex_unlock"); |
| | | |
| | | shm_msg_t src; |
| | | |
| | | if((flags & BUS_NOWAIT_FLAG) == BUS_NOWAIT_FLAG) { |
| | | rv = socket->queue->pop_nowait(src); |
| | | } else if((flags & BUS_TIMEOUT_FLAG) == BUS_TIMEOUT_FLAG && timeout != NULL) { |
| | | rv = socket->queue->pop_timeout(src, timeout); |
| | | // printf("0 shm_recvfrom====%d\n", rv); |
| | | } else { |
| | | rv = socket->queue->pop(src); |
| | | } |
| | | printf ("====== before ======\n"); |
| | | rv = sokt->queue->pop(src, timeout, flag); |
| | | printf ("====== after ======\n %d", rv); |
| | | |
| | | if (rv == 0) { |
| | | if(buf != NULL) { |
| | |
| | | mm_free(src.buf); |
| | | return 0; |
| | | } else { |
| | | if(rv > EBUS_BASE) { |
| | | logger->debug("shm_recvfrom failed %s", bus_strerror(rv)); |
| | | } else { |
| | | logger->error(rv, "shm_recvfrom failed"); |
| | | } |
| | | logger->debug("shm_recvfrom failed %s", bus_strerror(rv)); |
| | | return rv; |
| | | |
| | | } |
| | |
| | | // 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; |
| | | |
| | |
| | | |
| | | 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_size, const struct timespec *timeout, int flags) { |
| | | int recv_key; |
| | | int 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 ", |
| | |
| | | |
| | | 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_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); |
| | | } |
| | | |
| | |
| | | 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) { |
| | |
| | | iter != socket->clientSocketMap->end(); iter++) { |
| | | client_socket = iter->second; |
| | | |
| | | client_socket->remoteQueue->push_timeout(close_msg, &timeout); |
| | | client_socket->remoteQueue->push(close_msg, &timeout, BUS_TIMEOUT_FLAG); |
| | | client_socket->remoteQueue = NULL; |
| | | |
| | | delete client_socket->messageQueue; |