From d7d05fb334db91e3ecbf009ca2650d88f9ed7b46 Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期一, 07 十二月 2020 14:21:08 +0800 Subject: [PATCH] update doc --- test_net_socket/test_net_mod_socket.c | 5 +- doc/使用指南.mk | 87 +++++++++++++++++++++++++++++-------------- 2 files changed, 61 insertions(+), 31 deletions(-) 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" index 2b2e126..4514d41 100644 --- "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" @@ -25,19 +25,21 @@ ``` ### 2.2 鍚姩BusService -BusService锛� Pub/Sub Client 鍜� Request/Reply Client鐨勫ご鏂囦欢閮芥槸 net_mod_socket_wrapper.h锛� 涓嬮潰鍚姩key鏄�8鐨凚us鏈嶅姟. +BusService鐨勫ご鏂囦欢鏄痓us_server_socket_wrapper.h锛� 涓嬮潰鏄惎鍔˙us鏈嶅姟鐨勪唬鐮�. ``` - -void * server_socket = net_mod_socket_open(); - -net_mod_socket_bind(server_socket, 8); - -net_mod_socket_start_bus(server_socket); +void * server_socket = bus_server_socket_wrapper_open(); +if(bus_server_socket_wrapper_start_bus(server_socket) != 0) { + printf("start bus failed\n"); + exit(1); +} ``` ### 2.3 Request/Reply 鐢ㄤ緥璇存槑 + +Pub/Sub Client 鍜� Request/Reply Client鐨勫ご鏂囦欢閮芥槸 net_mod_socket_wrapper.h + 鐜板湪妯℃嫙涓�涓満鏅紝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 鐨勪唬鐮佸涓嬶細 @@ -54,7 +56,9 @@ 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); +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", @@ -91,37 +95,62 @@ ### 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. +鐜板湪妯℃嫙涓�涓満鏅紝B 鍜� C璁㈤槄浜嗕富棰榥ews, A 鍙戝竷浜嗚涓婚鐩稿叧鐨勫唴瀹� 銆傚亣璁続鐨処P鏄�192.168.20.101锛孊鐨処P鏄�192.168.20.102, C鐨処P鏄�192.168.20.103, 瀹冧滑鐨刱ey閮芥槸200锛� 浠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; +int main() { + 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}, + {"192.168.20.103", 5000} + }; + 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); + 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)); +// 鎵撳嵃鎺ュ彈鍒扮殑璁㈤槄娑堟伅 +void *print_sub_msg(void *sockt) { + pthread_detach(pthread_self()); + void *recvbuf; + int size; + int key; + while (net_mod_socket_recvfrom( sockt, &recvbuf, &size, &key) == 0) { + printf("鏀跺埌璁㈤槄娑堟伅:%s\n", recvbuf); + free(recvbuf); + } + } + +int main() { + pthread_t tid; + // 鍒涘缓涓�涓嚎绋嬫帴鍙楄闃呮秷鎭� + pthread_create(&tid, NULL, print_sub_msg, client); + const char* topic = "news": + + void *client = net_mod_socket_open(); + net_mod_socket_bind(client, 200); + // 璁㈤槄鎰熷叴瓒g殑涓婚 + if (net_mod_socket_sub(client, topic, strlen(topic)) == 0) { + printf("%d Sub success!\n", net_mod_socket_get_key(client)); + } +} + ``` 鏇村叿浣撶殑瀹炰緥浠g爜璇峰弬鐪媊test_net_mod_socket.c` diff --git a/test_net_socket/test_net_mod_socket.c b/test_net_socket/test_net_mod_socket.c index 08f3589..dbebba5 100644 --- a/test_net_socket/test_net_mod_socket.c +++ b/test_net_socket/test_net_mod_socket.c @@ -41,7 +41,7 @@ } } - +// 鎵撳嵃鎺ュ彈鍒扮殑璁㈤槄娑堟伅 void *print_sub_msg(void *sockt) { pthread_detach(pthread_self()); void *recvbuf; @@ -83,7 +83,7 @@ } } - +// 浜や簰寮忓鎴风 void start_net_client(char *sendlist, char*publist ){ client = net_mod_socket_open(); char content[MAXLINE]; @@ -95,6 +95,7 @@ net_mod_recv_msg_t *recv_arr; pthread_t tid; + // 鍒涘缓涓�涓嚎绋嬫帴鍙楄闃呮秷鎭� pthread_create(&tid, NULL, print_sub_msg, client); //192.168.5.10:5000:11, 192.168.5.22:5000:11, 192.168.5.104:5000:11 -- Gitblit v1.8.0