| | |
| | | |
| | | hashtable_t *hashtable = mm_get_hashtable(); |
| | | |
| | | if( sockt->queue != NULL) |
| | | goto LABEL_PUSH; |
| | | |
| | | if ((rv = pthread_mutex_lock(&(sockt->mutex))) != 0) |
| | | { |
| | | if ((rv = pthread_mutex_lock(&(sockt->mutex))) != 0) |
| | | err_exit(rv, "shm_sendto : pthread_mutex_lock"); |
| | | |
| | | if (sockt->queue == NULL) { |
| | | if (sockt->key == 0) { |
| | | sockt->key = hashtable_alloc_key(hashtable); |
| | | if (sockt->queue == NULL) { |
| | | if (sockt->key == 0) { |
| | | sockt->key = hashtable_alloc_key(hashtable); |
| | | } |
| | | sockt->queue = shm_socket_bind_queue( sockt->key, sockt->force_bind); |
| | | if(sockt->queue == NULL ) { |
| | | logger->error("%s. key = %d", bus_strerror(EBUS_KEY_INUSED), sockt->key); |
| | | return EBUS_KEY_INUSED; |
| | | } |
| | | } |
| | | sockt->queue = shm_socket_bind_queue( sockt->key, sockt->force_bind); |
| | | if(sockt->queue == NULL ) { |
| | | logger->error("%s. key = %d", bus_strerror(EBUS_KEY_INUSED), sockt->key); |
| | | return EBUS_KEY_INUSED; |
| | | } |
| | | } |
| | | |
| | | if ((rv = pthread_mutex_unlock(&(sockt->mutex))) != 0) |
| | | err_exit(rv, "shm_sendto : pthread_mutex_unlock"); |
| | | if ((rv = pthread_mutex_unlock(&(sockt->mutex))) != 0) |
| | | err_exit(rv, "shm_sendto : pthread_mutex_unlock"); |
| | | |
| | | // There is some case where a sockt need to send to himeself, for example when bus server need to stop, he need to send himself |
| | | // a top message. |
| | | } |
| | | |
| | | |
| | | LABEL_PUSH: |
| | | if (key == sockt->key) { |
| | | logger->error( "can not send to your self!"); |
| | | return EBUS_SENDTO_SELF; |
| | |
| | | } |
| | | |
| | | shm_packet_t dest; |
| | | dest.type = SHM_COMMON_MSG; |
| | | dest.key = sockt->key; |
| | | dest.size = size; |
| | | dest.buf = mm_malloc(size); |
| | |
| | | |
| | | hashtable_t *hashtable = mm_get_hashtable(); |
| | | |
| | | if ((rv = pthread_mutex_lock(&(sockt->mutex))) != 0) |
| | | err_exit(rv, "shm_recvfrom : pthread_mutex_lock"); |
| | | if( sockt->queue != NULL) |
| | | goto LABEL_POP; |
| | | |
| | | { |
| | | if ((rv = pthread_mutex_lock(&(sockt->mutex))) != 0) |
| | | err_exit(rv, "shm_recvfrom : pthread_mutex_lock"); |
| | | |
| | | if (sockt->queue == NULL) { |
| | | |
| | | if (sockt->key == 0) { |
| | | sockt->key = hashtable_alloc_key(hashtable); |
| | | } |
| | |
| | | logger->error("%s. key = %d", bus_strerror(EBUS_KEY_INUSED), sockt->key); |
| | | return EBUS_KEY_INUSED; |
| | | } |
| | | |
| | | |
| | | if ((rv = pthread_mutex_unlock(&(sockt->mutex))) != 0) |
| | | err_exit(rv, "shm_recvfrom : pthread_mutex_unlock"); |
| | | |
| | | } |
| | | |
| | | if ((rv = pthread_mutex_unlock(&(sockt->mutex))) != 0) |
| | | err_exit(rv, "shm_recvfrom : pthread_mutex_unlock"); |
| | | LABEL_POP: |
| | | |
| | | shm_packet_t src; |
| | | |
| | |
| | | //s = pthread_key_create(&_perthread_socket_key_, NULL); |
| | | if (s != 0) { |
| | | logger->error(s, "pthread_key_create"); |
| | | abort(); /* dump core and terminate */ |
| | | exit(1); |
| | | } |
| | | } |
| | |
| | | int *recv_size, const struct timespec *timeout, int flags) { |
| | | int recv_key; |
| | | int rv; |
| | | int tryn = 0; |
| | | |
| | | // 用thread local 保证每个线程用一个独占的socket接受对方返回的信息 |
| | | shm_socket_t *tmp_socket; |
| | | |
| | | |
| | | /* If first call from this thread, allocate buffer for thread, and save its location */ |
| | | // logger->debug("%d create tmp socket\n", pthread_self() ); |
| | | rv = pthread_once(&_once_, _create_socket_key_perthread); |
| | | if (rv != 0) { |
| | | logger->error(rv, "shm_sendandrecv pthread_once"); |
| | |
| | | } |
| | | |
| | | 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); |
| | | if(rv != 0) { |
| | | logger->error("_shm_sendandrecv_thread_local : %s\n", bus_strerror(rv)); |
| | | } |
| | | else if(rv == 0 ) { |
| | | logger->debug("======%d use tmp_socket %d, send to %d, receive from %d\n", shm_socket_get_key(sockt), shm_socket_get_key(tmp_socket), send_key, recv_key); |
| | | |
| | | if(recv_key == shm_socket_get_key(sockt)) { |
| | | logger->debug("=====收到了自己发给自己的消息\n"); |
| | | |
| | | while(tryn < 3) { |
| | | tryn++; |
| | | rv = shm_recvfrom(tmp_socket, recv_buf, recv_size, &recv_key, timeout, flags); |
| | | if(rv != 0) { |
| | | logger->error("_shm_sendandrecv_thread_local : %s\n", bus_strerror(rv)); |
| | | return rv; |
| | | } |
| | | assert( send_key == recv_key); |
| | | |
| | | // 超时导致接发送对象,与返回对象不对应的情况 |
| | | if(send_key != recv_key) { |
| | | logger->error( "_shm_sendandrecv_alloc_new: send key expect to equal to recv key! send key =%d , recv key=%d", send_key, recv_key); |
| | | exit(1); |
| | | logger->debug("======%d use tmp_socket %d, send to %d, receive from %d\n", shm_socket_get_key(sockt), shm_socket_get_key(tmp_socket), send_key, recv_key); |
| | | // logger->error( "_shm_sendandrecv_alloc_new: send key expect to equal to recv key! send key =%d , recv key=%d", send_key, recv_key); |
| | | // exit(1); |
| | | continue; |
| | | // return EBUS_RECVFROM_WRONG_END; |
| | | } |
| | | |
| | | return 0; |
| | | } |
| | | return rv; |
| | | } else { |
| | | return rv; |
| | | } |
| | | |
| | | return EBUS_RECVFROM_WRONG_END; |
| | | } |
| | | |
| | | return rv; |
| | | } |
| | | |
| | | int _shm_sendandrecv_alloc_new(shm_socket_t *sockt, const void *send_buf, |
| | |
| | | int recv_key; |
| | | int rv; |
| | | |
| | | // 用thread local 保证每个线程用一个独占的socket接受对方返回的信息 |
| | | int tryn = 0; |
| | | shm_socket_t *tmp_socket; |
| | | |
| | | /* 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); |
| | | |
| | | if(rv != 0) { |
| | | printf("_shm_sendandrecv_alloc_new : %s\n", bus_strerror(rv)); |
| | | } |
| | | else if(rv == 0 ) { |
| | | printf("======%d use tmp_socket %d, send to %d, receive from %d\n", shm_socket_get_key(sockt), shm_socket_get_key(tmp_socket), send_key, recv_key); |
| | | |
| | | if(recv_key == shm_socket_get_key(sockt)) { |
| | | printf("=====收到了自己发给自己的消息\n"); |
| | | } |
| | | assert( send_key == recv_key); |
| | | if(send_key != recv_key) { |
| | | err_exit(0, "_shm_sendandrecv_alloc_new: send key expect to equal to recv key! send key =%d , recv key=%d", send_key, recv_key); |
| | | while(tryn < 3) { |
| | | tryn++; |
| | | rv = shm_recvfrom(tmp_socket, recv_buf, recv_size, &recv_key, timeout, flags); |
| | | if(rv != 0) { |
| | | logger->error("_shm_sendandrecv_thread_local : %s\n", bus_strerror(rv)); |
| | | return rv; |
| | | } |
| | | |
| | | // 超时导致接发送对象,与返回对象不对应的情况 |
| | | if(send_key != recv_key) { |
| | | // logger->debug("======%d use tmp_socket %d, send to %d, receive from %d\n", shm_socket_get_key(sockt), shm_socket_get_key(tmp_socket), send_key, recv_key); |
| | | // logger->error( "_shm_sendandrecv_alloc_new: send key expect to equal to recv key! send key =%d , recv key=%d", send_key, recv_key); |
| | | |
| | | continue; |
| | | } |
| | | return 0; |
| | | } |
| | | |
| | | return EBUS_RECVFROM_WRONG_END; |
| | | } |
| | | |
| | | shm_close_socket(tmp_socket); |