| | |
| | | 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 ) { |
| | | bus_errno = EBUS_KEY_INUSED; |
| | | bus_errno = ESHM_BUS_KEY_INUSED; |
| | | logger->error("%s. key = %d ", bus_strerror(bus_errno), socket->key); |
| | | return 0; |
| | | } |
| | |
| | | } |
| | | |
| | | shm_socket_t *shm_open_socket(shm_socket_type_t socket_type) { |
| | | int s, type; |
| | | pthread_mutexattr_t mtxAttr; |
| | | |
| | | logger->debug("shm_open_socket\n"); |
| | | shm_socket_t *socket = (shm_socket_t *)calloc(1, sizeof(shm_socket_t)); |
| | |
| | | socket->force_bind = false; |
| | | socket->dispatch_thread = 0; |
| | | socket->status = SHM_CONN_CLOSED; |
| | | socket->mutex = SemUtil::get(IPC_PRIVATE, 1); |
| | | |
| | | |
| | | s = pthread_mutexattr_init(&mtxAttr); |
| | | if (s != 0) |
| | | err_exit(s, "pthread_mutexattr_init"); |
| | | s = pthread_mutexattr_settype(&mtxAttr, PTHREAD_MUTEX_ERRORCHECK); |
| | | if (s != 0) |
| | | err_exit(s, "pthread_mutexattr_settype"); |
| | | s = pthread_mutex_init(&(socket->mutex), &mtxAttr); |
| | | if (s != 0) |
| | | err_exit(s, "pthread_mutex_init"); |
| | | |
| | | s = pthread_mutexattr_destroy(&mtxAttr); |
| | | if (s != 0) |
| | | err_exit(s, "pthread_mutexattr_destroy"); |
| | | |
| | | return socket; |
| | | } |
| | | |
| | | int shm_close_socket(shm_socket_t *socket) { |
| | | |
| | | int ret; |
| | | int ret, s; |
| | | |
| | | logger->debug("shm_close_socket\n"); |
| | | switch (socket->socket_type) { |
| | |
| | | default: |
| | | break; |
| | | } |
| | | |
| | | s = pthread_mutex_destroy(&(socket->mutex) ); |
| | | if(s != 0) { |
| | | err_exit(s, "shm_close_socket"); |
| | | } |
| | | |
| | | free(socket); |
| | | SemUtil::remove(socket->mutex); |
| | | return ret; |
| | | } |
| | | |
| | |
| | | } else { |
| | | |
| | | if(!_shm_socket_check_key(socket)) { |
| | | bus_errno = EBUS_KEY_INUSED; |
| | | return EBUS_KEY_INUSED; |
| | | bus_errno = ESHM_BUS_KEY_INUSED; |
| | | return ESHM_BUS_KEY_INUSED; |
| | | } |
| | | } |
| | | |
| | |
| | | socket->key = hashtable_alloc_key(hashtable); |
| | | } else { |
| | | if(!_shm_socket_check_key(socket)) { |
| | | bus_errno = EBUS_KEY_INUSED; |
| | | return EBUS_KEY_INUSED; |
| | | bus_errno = ESHM_BUS_KEY_INUSED; |
| | | return ESHM_BUS_KEY_INUSED; |
| | | } |
| | | } |
| | | |
| | |
| | | // 短连接方式发送 |
| | | int shm_sendto(shm_socket_t *socket, const void *buf, const int size, |
| | | const int key, const struct timespec *timeout, const int flags) { |
| | | |
| | | int s; |
| | | bool rv; |
| | | |
| | | if (socket->socket_type != SHM_SOCKET_DGRAM) { |
| | | logger->error( "shm_socket.shm_sendto: Can't invoke shm_sendto method in a %d type socket which is " |
| | | "not a SHM_SOCKET_DGRAM socket ", |
| | |
| | | } |
| | | hashtable_t *hashtable = mm_get_hashtable(); |
| | | |
| | | SemUtil::dec(socket->mutex); |
| | | |
| | | if ((s = pthread_mutex_lock(&(socket->mutex))) != 0) |
| | | err_exit(s, "shm_sendto : pthread_mutex_lock"); |
| | | |
| | | if (socket->queue == NULL) { |
| | | if (socket->key == -1) { |
| | | socket->key = hashtable_alloc_key(hashtable); |
| | | } else { |
| | | |
| | | if(!_shm_socket_check_key(socket)) { |
| | | bus_errno = EBUS_KEY_INUSED; |
| | | return EBUS_KEY_INUSED; |
| | | bus_errno = ESHM_BUS_KEY_INUSED; |
| | | return ESHM_BUS_KEY_INUSED; |
| | | } |
| | | |
| | | } |
| | | |
| | | socket->queue = new SHMQueue<shm_msg_t>(socket->key, 16); |
| | | } |
| | | SemUtil::inc(socket->mutex); |
| | | |
| | | if ((s = pthread_mutex_unlock(&(socket->mutex))) != 0) |
| | | err_exit(s, "shm_sendto : pthread_mutex_unlock"); |
| | | |
| | | // if (key == socket->key) { |
| | | // logger->error( "can not send to your self!"); |
| | |
| | | memcpy(dest.buf, buf, size); |
| | | |
| | | // printf("shm_sendto push before\n"); |
| | | bool rv; |
| | | |
| | | if(flags & SHM_MSG_NOWAIT != 0) { |
| | | rv = remoteQueue->push_nowait(dest); |
| | | } else if(timeout != NULL) { |
| | |
| | | |
| | | // 短连接方式接受 |
| | | int shm_recvfrom(shm_socket_t *socket, void **buf, int *size, int *key, struct timespec *timeout, int flags) { |
| | | int s; |
| | | bool rv; |
| | | |
| | | if (socket->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 ", |
| | |
| | | exit(1); |
| | | } |
| | | hashtable_t *hashtable = mm_get_hashtable(); |
| | | SemUtil::dec(socket->mutex); |
| | | |
| | | if ((s = pthread_mutex_lock(&(socket->mutex))) != 0) |
| | | err_exit(s, "shm_recvfrom : pthread_mutex_lock"); |
| | | |
| | | if (socket->queue == NULL) { |
| | | if (socket->key == -1) { |
| | | socket->key = hashtable_alloc_key(hashtable); |
| | | } else { |
| | | |
| | | if(!_shm_socket_check_key(socket)) { |
| | | bus_errno = EBUS_KEY_INUSED; |
| | | return EBUS_KEY_INUSED; |
| | | bus_errno = ESHM_BUS_KEY_INUSED; |
| | | return ESHM_BUS_KEY_INUSED; |
| | | } |
| | | } |
| | | |
| | | socket->queue = new SHMQueue<shm_msg_t>(socket->key, 16); |
| | | } |
| | | SemUtil::inc(socket->mutex); |
| | | |
| | | if ((s = pthread_mutex_unlock(&(socket->mutex))) != 0) |
| | | err_exit(s, "shm_recvfrom : pthread_mutex_unlock"); |
| | | |
| | | shm_msg_t src; |
| | | // printf("shm_recvfrom pop before\n"); |
| | | bool rv; |
| | | |
| | | if(flags & SHM_MSG_NOWAIT != 0) { |
| | | rv = socket->queue->pop_nowait(src); |
| | | } else if(timeout != NULL) { |