From f1ae0552e2d50ce6f87a0bad2f31b8e2ec617bb7 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期三, 03 二月 2021 09:39:54 +0800
Subject: [PATCH] update
---
test_net_socket/test_net_mod_socket.cpp | 4 ++--
src/socket/shm_socket.cpp | 50 +++++++++++++++++++++++++++++++-------------------
2 files changed, 33 insertions(+), 21 deletions(-)
diff --git a/src/socket/shm_socket.cpp b/src/socket/shm_socket.cpp
index 9544827..133190a 100644
--- a/src/socket/shm_socket.cpp
+++ b/src/socket/shm_socket.cpp
@@ -148,27 +148,31 @@
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;
@@ -210,10 +214,14 @@
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);
}
@@ -222,10 +230,14 @@
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;
diff --git a/test_net_socket/test_net_mod_socket.cpp b/test_net_socket/test_net_mod_socket.cpp
index 94be25b..e589808 100644
--- a/test_net_socket/test_net_mod_socket.cpp
+++ b/test_net_socket/test_net_mod_socket.cpp
@@ -272,7 +272,7 @@
sprintf(sendbuf, hello_format, net_mod_socket_get_key(client), l);
// fprintf(fp, "requst:%s\n", sendbuf);
// n = net_mod_socket_sendandrecv(client, node_arr, node_arr_size, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size);
- n = net_mod_socket_sendandrecv_timeout(client, targ->node, 1, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size, 1);
+ n = net_mod_socket_sendandrecv_timeout(client, targ->node, 1, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size, 1000);
printf("%d: send %d nodes\n", l, n);
for(j=0; j < recv_arr_size; j++) {
@@ -372,7 +372,7 @@
while(true) {
sprintf(buf, hello_format, pid, l);
n = net_mod_socket_sendandrecv_timeout(client, node_arr, node_arr_size, buf, strlen(buf)+1,
- &recv_arr, &recv_arr_size, 1);
+ &recv_arr, &recv_arr_size, 1000);
printf(" %d nodes reply\n", n);
for(j = 0; j < recv_arr_size; j++) {
--
Gitblit v1.8.0