From 23a5822a2a4f874b84c2c2a9c2442ac4a5279176 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期四, 03 十二月 2020 09:51:15 +0800
Subject: [PATCH] 文档
---
/dev/null | 17 ---
src/socket/net_mod_socket_wrapper.h | 5
doc/bus_service.png | 0
test_net_socket/test_net_mod_socket.c | 63 ++++++------
src/socket/net_mod_socket.h | 2
doc/使用指南.mk | 149 +++++++++++++++++++++++++++++
test_net_socket/net_mod_socket.sh | 6
doc/network_req_rep.png | 0
8 files changed, 187 insertions(+), 55 deletions(-)
diff --git a/doc/bus_service.png b/doc/bus_service.png
new file mode 100755
index 0000000..79321d2
--- /dev/null
+++ b/doc/bus_service.png
Binary files differ
diff --git a/doc/network_req_rep.png b/doc/network_req_rep.png
new file mode 100755
index 0000000..3c571cd
--- /dev/null
+++ b/doc/network_req_rep.png
Binary files differ
diff --git "a/doc/\344\275\277\347\224\250\346\214\207\345\215\227.mk" "b/doc/\344\275\277\347\224\250\346\214\207\345\215\227.mk"
new file mode 100644
index 0000000..2b2e126
--- /dev/null
+++ "b/doc/\344\275\277\347\224\250\346\214\207\345\215\227.mk"
@@ -0,0 +1,149 @@
+# <center>BHomeBus 浣跨敤鎸囧崡</center>
+
+
+## 1. BHomeBus鍖呭惈鐨勮鑹�
+- NetProxyService锛堢綉缁滀唬鐞嗘湇鍔★級锛� 璐熻矗璺ㄦ満鍣ㄨ浆鍙戝彂甯冩垨璇锋眰娑堟伅
+- BusService锛� 鎬荤嚎鏈嶅姟锛�: 鍙戝竷璁㈤槄鐨勮矾鐢憋紝璐熻矗璁板綍璁㈤槄鑰呯殑娑堟伅骞舵妸鍙戝竷鑰呭彂甯冪殑娑堟伅杞彂缁欐劅鍏磋叮鐨勮闃呰�呫��
+- Pub/Sub Client锛堝彂甯冭闃呭鎴风锛夛細 鍚態usService鍙戝竷娑堟伅鎴栬�呭湪BusService涓婅闃呮劅鍏磋叮鐨勬秷鎭�
+- Request/Reply Client 锛堣姹傚簲绛斿鎴风锛夛細鍚戝叾浠栨満鍣�/杩涚▼鍙戦�佽姹傛秷鎭垨鑰呬粠鍏朵粬鏈哄櫒/杩涚▼鎺ュ彈搴旂瓟娑堟伅
+
+鍏朵腑BusService鍜孨etProxyService鏄湇鍔¤繘绋嬶紝浣跨敤鑰呮妸瀹冭捣鏉ュ悗灏变笉闇�瑕佸叧蹇冧簡銆備娇鐢ㄨ�呬富瑕佸叧娉ㄧ殑鏄疨ub/Sub Client 鍜� Request/Reply Client
+
+## 2. 浣跨敤璇存槑
+鍏蜂綋姣忎釜鏂规硶鐨勫強鍏跺弬鏁扮殑璇存槑鍙互鍙傜湅瀹冧滑瀵瑰簲鐨勫ご鏂囦欢銆�
+
+浣跨敤鏃讹紝姣忓彴涓绘満閮介渶瑕佸厛鎶夿usService鍜孨etProxyService杩欎袱涓湇鍔″惎鍔ㄨ捣鏉ャ�傚洜涓哄悗闈㈢殑鍔熻兘閮戒緷璧栬繖涓や釜鏈嶅姟銆�
+
+### 2.1 鍚姩NetProxyService
+NetProxyService鐨勫ご鏂囦欢鏄� net_mod_server_socket_wrapper.h. 涓嬮潰鐨勪唬鐮佸惎鍔ㄧ鍙f槸5000鐨勭綉缁滀唬鐞嗘湇鍔�
+
+```
+void *serverSocket = net_mod_server_socket_open(5000);
+if(net_mod_server_socket_start(serverSocket) != 0) {
+ err_exit(errno, "net_mod_server_socket_start");
+}
+```
+
+### 2.2 鍚姩BusService
+BusService锛� Pub/Sub Client 鍜� Request/Reply Client鐨勫ご鏂囦欢閮芥槸 net_mod_socket_wrapper.h锛� 涓嬮潰鍚姩key鏄�8鐨凚us鏈嶅姟.
+
+```
+
+void * server_socket = net_mod_socket_open();
+
+net_mod_socket_bind(server_socket, 8);
+
+net_mod_socket_start_bus(server_socket);
+
+```
+
+### 2.3 Request/Reply 鐢ㄤ緥璇存槑
+鐜板湪妯℃嫙涓�涓満鏅紝A鍚態鍜孋鍙戦�佷竴涓姹傦紝B鍜孋鏀跺埌璇锋眰鍚庡垎鍒繑鍥炰竴涓搷搴旂粰A 銆傚亣璁続鐨処P鏄�192.168.20.101锛孊鐨処P鏄�192.168.20.102, C鐨処P鏄�192.168.20.103, 瀹冧滑鐨刱ey閮芥槸100锛� 浠g悊server鐨勭鍙f槸5000銆�
+
+A 鐨勪唬鐮佸涓嬶細
+
+```
+int recv_arr_size, n;
+net_mod_recv_msg_t *recv_arr;
+const char* content = "HELLO WORLD!":
+net_mod_socket_bind(client, 100);
+net_node_t node_arr = {
+ {"192.168.20.102", 5000, 100},
+ {"192.168.20.103", 5000, 100}
+};
+int node_arr_size = 2;
+
+void *client = net_mod_socket_open();
+n = net_mod_socket_sendandrecv(client, node_arr, node_arr_size, content, strlen(content), &recv_arr, &recv_arr_size);
+printf(">>> %d nodes reply\n", n);
+for(i=0; i<recv_arr_size; i++) {
+ printf("host:%s, port: %d, key:%d, content: %s\n",
+ recv_arr[i].host,
+ recv_arr[i].port,
+ recv_arr[i].key,
+ recv_arr[i].content
+ );
+}
+
+// 浣跨敤瀹屽悗锛屼笉瑕佸繕璁伴噴鏀炬帀
+net_mod_socket_free_recv_msg_arr(recv_arr, recv_arr_size);
+
+```
+
+B 鍜� C鐨勪唬鐮佸涓嬶細
+
+```
+void *client = net_mod_socket_open();
+net_mod_socket_bind(client, 100);
+int size;
+void *recvbuf;
+char sendbuf[512];
+int rv;
+int remote_port;
+while ( (rv = net_mod_socket_recvfrom(client, &recvbuf, &size, &remote_port) ) == 0) {
+ // printf( "server: RECEIVED REQUEST FROM PORT %d NAME %s\n", remote_port, recvbuf);
+ sprintf(sendbuf, "RECEIVED PORT %d NAME %s", remote_port, recvbuf);
+ net_mod_socket_sendto(client, sendbuf, strlen(sendbuf) + 1, remote_port);
+ free(recvbuf);
+}
+```
+
+
+### 2.3 Pub/Sub 鐢ㄤ緥璇存槑
+
+鐜板湪妯℃嫙涓�涓満鏅紝B 鍜� C璁㈤槄浜嗕富棰榥ews, A 鍙戝竷浜嗚涓婚鐩稿叧鐨勫唴瀹� 銆傚亣璁続鐨処P鏄�192.168.20.101锛孊鐨処P鏄�192.168.20.102, C鐨処P鏄�192.168.20.103, 瀹冧滑鐨刱ey閮芥槸200锛� Bus鐨刱ey鏄�8锛� 浠g悊server鐨勭鍙f槸5000.
+
+A鐨勪唬鐮佸涓嬶細
+
+```
+int n;
+const char* topic = "news":
+const char* content = "HELLO WORLD!":
+void *client = net_mod_socket_open();
+net_mod_socket_bind(client, 200);
+net_node_t node_arr = {
+ {"192.168.20.102", 5000, 8},
+ {"192.168.20.103", 5000, 8}
+};
+int node_arr_size = 2;
+
+void *client = net_mod_socket_open();
+net_mod_socket_bind(client, 200);
+n = net_mod_socket_pub(client, pub_node_arr, pub_node_arr_size, topic, strlen(topic)+1, content, strlen(content)+1);
+printf("pub %d nodes\n", n);
+```
+
+B 鍜� C 鐨勪唬鐮佸涓�
+```
+const char* topic = "news":
+
+void *client = net_mod_socket_open();
+net_mod_socket_bind(client, 200);
+while (net_mod_socket_sub(client, topic, strlen(topic), 8) == 0) {
+ printf("%d Sub success!\n", net_mod_socket_get_key(client));
+}
+```
+
+鏇村叿浣撶殑瀹炰緥浠g爜璇峰弬鐪媊test_net_mod_socket.c`
+
+## 3 璁捐璇存槑
+
+浣跨敤鐨勬椂鍊欙紝澶у浼氬BusService鍜孨etProxyService杩欎袱涓渶瑕侀澶栧惎鍔ㄦ湇鍔℃劅鍒扮枒鎯戙�備笅闈㈤噸鐐归槦杩欎袱涓湇鍔″仛鍋氫竴浜涜鏄庛��
+
+### 3.1 Bus璁捐
+
+
+![Bus绀烘剰鍥綸(./bus_service.png)
+
+涓婇潰杩欏紶绀烘剰鍥炬槸璇磋闃卬ews鐨凜lient鏈堿鍜孊锛岃闃卻ports鐨勬湁B鍜孋锛岃繖浜涢兘鍦ㄦ�荤嚎閲岃褰曠潃銆傚綋A鍚戞�荤嚎pub鍜屼富棰榮ports鐩稿叧鐨勫唴瀹规椂锛孊鍜孋浼氶�氳繃鎬荤嚎鏀跺埌杩欎釜涓婚鐨勬秷鎭��
+
+### 3.2 NetProxyService璁捐
+
+
+![NetProxyService绀烘剰鍥綸(./network_req_rep.png)
+
+涓婇潰杩欏紶鍥捐法鏈哄櫒璇锋眰搴旂瓟鐨勭ず鎰忓浘銆傝繖寮犵ず鎰忓浘鏃惰褰撹妭鐐笰鍚戣妭鐐笲鐨刱ey 1001闃熷垪鍜宬ey 1002闃熷垪鍙戦�佹秷鎭椂锛屽畠浼氶鍏堝彂閫佸埌鑺傜偣B鐨勭綉缁滀唬鐞唖erver涓娿�傜綉缁滀唬鐞唖erver浼氭妸璇锋眰娑堟伅杞彂鍒扮浉搴旂殑闃熷垪涓婏紝骞舵帴鍙楀簲绛旇繑鍥炵粰鑺傜偣A銆�
+
+鍚岀悊璺ㄦ満鍣ㄧ殑鍙戝竷璁㈤槄涔熸槸閫氳繃杩欐牱鐨勬柟寮忎氦浜掔殑銆�
+
+
diff --git "a/doc/\344\275\277\347\224\250\350\257\264\346\230\216.mk" "b/doc/\344\275\277\347\224\250\350\257\264\346\230\216.mk"
deleted file mode 100644
index 098bb21..0000000
--- "a/doc/\344\275\277\347\224\250\350\257\264\346\230\216.mk"
+++ /dev/null
@@ -1,17 +0,0 @@
-## BHomeBus鍖呭惈鐨勮鑹�
-- NetProxyService锛堢綉缁滀唬鐞嗘湇鍔★級锛� 璐熻矗璺ㄦ満鍣ㄨ浆鍙戝彂甯冩垨璇锋眰娑堟伅
-- BusService锛� 鎬荤嚎鏈嶅姟锛�: 鍙戝竷璁㈤槄鐨勮矾鐢憋紝璐熻矗璁板綍璁㈤槄鑰呯殑娑堟伅骞舵妸鍙戝竷鑰呭彂甯冪殑娑堟伅杞彂缁欐劅鍏磋叮鐨勮闃呰�呫��
-- Pub/Sub Client锛堝彂甯冭闃呭鎴风锛夛細 鍚態usService鍙戝竷娑堟伅鎴栬�呭湪BusService涓婅闃呮劅鍏磋叮鐨勬秷鎭�
-- Request/Reply Client 锛堣姹傚簲绛斿鎴风锛夛細鍚戝叾浠栨満鍣�/杩涚▼鍙戦�佽姹傛秷鎭垨鑰呬粠鍏朵粬鏈哄櫒/杩涚▼鎺ュ彈搴旂瓟娑堟伅
-
-鍏朵腑BusService鍜孨etProxyService鏄湇鍔¤繘绋嬶紝浣跨敤鑰呮妸瀹冭捣鏉ュ悗灏变笉闇�瑕佸叧蹇冧簡銆備娇鐢ㄨ�呬富瑕佸叧娉ㄧ殑鏄疨ub/Sub Client 鍜� Request/Reply Client
-
-## 鎺ュ彛璇存槑
-
-浣跨敤鏃讹紝BusService鍜孨etProxyService杩欎袱涓湇鍔¢渶瑕佸厛鍚姩璧锋潵銆傚洜涓哄悗闈㈢殑鍔熻兘閮戒緷璧栬繖涓や釜鏈嶅姟銆�
-
-### NetProxyService
-
-### BusService
-net_mod_socket_wrapper.h
-
diff --git a/src/socket/net_mod_socket.h b/src/socket/net_mod_socket.h
index 83f5735..55739d2 100644
--- a/src/socket/net_mod_socket.h
+++ b/src/socket/net_mod_socket.h
@@ -115,7 +115,7 @@
/**
* 濡傛灉寤虹珛杩炴帴鐨勮妭鐐规病鏈夋帴鍙楀埌娑堟伅浼氫竴鐩寸瓑寰�
* 鍚憂ode_arr 涓殑鎵�鏈夌綉缁滆妭鐐瑰彂閫佽姹傛秷鎭紝鑺傜偣鐨勮繑鍥炰俊鎭眹鎬诲苟瀛樺偍鍦╮ecv_arr涓�
- * @node_arr 缃戠粶鑺傜偣缁�, @node_arr_len璇ユ暟缁勯暱搴�
+ * @node_arr 缃戠粶鑺傜偣缁�, @node_arr_len璇ユ暟缁勯暱搴�.濡傛灉IP涓虹┖鍒欎负鏈湴鍙戦�併��
* @send_buf 鍙戦�佺殑娑堟伅锛孈send_size 璇ユ秷鎭綋鐨勯暱搴�
* @recv_arr 杩斿洖鐨勫簲绛旀秷鎭粍锛孈recv_arr_size 璇ユ暟缁勯暱搴�
* @return 鎴愬姛鍙戦�佺殑鑺傜偣鐨勪釜鏁�
diff --git a/src/socket/net_mod_socket_wrapper.h b/src/socket/net_mod_socket_wrapper.h
index 7ed96bf..421733f 100644
--- a/src/socket/net_mod_socket_wrapper.h
+++ b/src/socket/net_mod_socket_wrapper.h
@@ -59,15 +59,14 @@
int net_mod_socket_recvfrom_timeout(void *_socket, void **buf, int *size, int *key, int sec, int nsec);
int net_mod_socket_recvfrom_nowait(void *_socket, void **buf, int *size, int *key);
-
/**
* 濡傛灉寤虹珛杩炴帴鐨勮妭鐐规病鏈夋帴鍙楀埌娑堟伅浼氫竴鐩寸瓑寰�
* 鍚憂ode_arr 涓殑鎵�鏈夌綉缁滆妭鐐瑰彂閫佽姹傛秷鎭紝鑺傜偣鐨勮繑鍥炰俊鎭眹鎬诲苟瀛樺偍鍦╮ecv_arr涓�
- * @node_arr 缃戠粶鑺傜偣缁�, @node_arr_len璇ユ暟缁勯暱搴�
+ * @node_arr 缃戠粶鑺傜偣缁�, @node_arr_len璇ユ暟缁勯暱搴�.濡傛灉IP涓虹┖鍒欎负鏈湴鍙戦�併��
* @send_buf 鍙戦�佺殑娑堟伅锛孈send_size 璇ユ秷鎭綋鐨勯暱搴�
* @recv_arr 杩斿洖鐨勫簲绛旀秷鎭粍锛孈recv_arr_size 璇ユ暟缁勯暱搴�
* @return 鎴愬姛鍙戦�佺殑鑺傜偣鐨勪釜鏁�
- * 浼樼偣锛�1鏌愪釜鑺傜偣鐨勬晠闅滀笉浼氶樆濉炲叾浠栬妭鐐广��2鎬ц兘濂�
+ * 浼樼偣锛�1鏌愪釜鑺傜偣鐨勬晠闅滀笉浼氶樆濉炲叾浠栬妭鐐广��2 鎬ц兘濂姐�� 3 閲囩敤thread local鎶�鏈嵆淇濊瘉浜嗙嚎绋嬪畨鍏紝鍙堝彲浠ヤ娇鐢ㄨ繛鎺ユ睜缂撳瓨杩炴帴
*/
int net_mod_socket_sendandrecv(void *_sockt, net_node_t *node_arr, int arrlen, void *send_buf, int send_size,
net_mod_recv_msg_t ** recv_arr, int *recv_arr_size) ;
diff --git a/test_net_socket/net_mod_socket.sh b/test_net_socket/net_mod_socket.sh
index 38bfcaa..a54fca4 100755
--- a/test_net_socket/net_mod_socket.sh
+++ b/test_net_socket/net_mod_socket.sh
@@ -6,7 +6,7 @@
./test_net_mod_socket --fun="start_net_proxy" --port=5000 & server_pid=$! && echo "pid: ${server_pid}"
# 鎵撳紑璇锋眰搴旂瓟娴嬭瘯鐨勬帴鍙楃
- ./test_net_mod_socket --fun="start_reply" --key=11 & server_pid=$! && echo "pid: ${server_pid}"
+ ./test_net_mod_socket --fun="start_reply" --key=100 & server_pid=$! && echo "pid: ${server_pid}"
}
function client() {
@@ -17,7 +17,7 @@
./test_net_mod_socket --fun="start_net_client" \
- --sendlist="localhost:5000:11" \
+ --sendlist="localhost:5000:100" \
--publist="localhost:5000:8"
@@ -25,7 +25,7 @@
function msend() {
./test_net_mod_socket --fun="test_net_sendandrecv_threads" \
- --sendlist="localhost:5000:11, localhost:5000:11"
+ --sendlist="localhost:5000:100, localhost:5000:100"
}
diff --git a/test_net_socket/test_net_mod_socket.c b/test_net_socket/test_net_mod_socket.c
index ab16c93..d8dcd0b 100644
--- a/test_net_socket/test_net_mod_socket.c
+++ b/test_net_socket/test_net_mod_socket.c
@@ -52,6 +52,36 @@
}
+
+void start_bus_server(int key) {
+ printf("Start bus server\n");
+ void * server_socket = net_mod_socket_open();
+
+ net_mod_socket_bind(server_socket, key);
+
+ net_mod_socket_start_bus(server_socket);
+}
+
+
+
+void start_reply(int key) {
+ printf("start reply\n");
+ void *client = net_mod_socket_open();
+ net_mod_socket_bind(client, key);
+ int size;
+ void *recvbuf;
+ char sendbuf[512];
+ int rv;
+ int remote_port;
+ while ( (rv = net_mod_socket_recvfrom(client, &recvbuf, &size, &remote_port) ) == 0) {
+ // printf( "server: RECEIVED REQUEST FROM PORT %d NAME %s\n", remote_port, recvbuf);
+ sprintf(sendbuf, "RECEIVED PORT %d NAME %s", remote_port, recvbuf);
+ net_mod_socket_sendto(client, sendbuf, strlen(sendbuf) + 1, remote_port);
+ free(recvbuf);
+ }
+}
+
+
void start_net_client(char *sendlist, char*publist ){
client = net_mod_socket_open();
char content[MAXLINE];
@@ -93,9 +123,8 @@
if (fgets(content, MAXLINE, stdin) != NULL) {
// 鏀跺埌娑堟伅鐨勮妭鐐瑰嵆浣挎病鏈夊搴旂殑淇℃伅锛� 涔熻鍥炲涓�涓〃绀烘棤鐨勬秷鎭�,鍚﹀垯浼氫竴鐩寸瓑寰�
- n = net_mod_socket_sendandrecv(client, node_arr, node_arr_size, content,
- strlen(content), &recv_arr, &recv_arr_size);
- printf("send %d nodes\n", n);
+ n = net_mod_socket_sendandrecv(client, node_arr, node_arr_size, content, strlen(content), &recv_arr, &recv_arr_size);
+ printf(" %d nodes reply\n", n);
for(i=0; i<recv_arr_size; i++) {
printf("host:%s, port: %d, key:%d, content: %s\n",
recv_arr[i].host,
@@ -147,34 +176,6 @@
}
-
-void start_bus_server(int key) {
- printf("Start bus server\n");
- void * server_socket = net_mod_socket_open();
-
- net_mod_socket_bind(server_socket, key);
-
- net_mod_socket_start_bus(server_socket);
-}
-
-
-
-void start_reply(int key) {
- printf("start reply\n");
- void *socket = net_mod_socket_open();
- net_mod_socket_bind(socket, key);
- int size;
- void *recvbuf;
- char sendbuf[512];
- int rv;
- int remote_port;
- while ( (rv = net_mod_socket_recvfrom(socket, &recvbuf, &size, &remote_port) ) == 0) {
- // printf( "server: RECEIVED REQUEST FROM PORT %d NAME %s\n", remote_port, recvbuf);
- sprintf(sendbuf, "RECEIVED PORT %d NAME %s", remote_port, recvbuf);
- net_mod_socket_sendto(socket, sendbuf, strlen(sendbuf) + 1, remote_port);
- free(recvbuf);
- }
-}
--
Gitblit v1.8.0