From 68d23225a38a35f1325eb39fa4ed5a005d5de473 Mon Sep 17 00:00:00 2001 From: fujuntang <fujuntang@aiot.com> Date: 星期三, 11 八月 2021 09:50:20 +0800 Subject: [PATCH] fix from 3.1 first commit --- test_net_socket/shm_util.cpp | 96 ++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 77 insertions(+), 19 deletions(-) diff --git a/test_net_socket/shm_util.cpp b/test_net_socket/shm_util.cpp index 34efbc4..4eb03f8 100644 --- a/test_net_socket/shm_util.cpp +++ b/test_net_socket/shm_util.cpp @@ -21,6 +21,8 @@ struct argument_t { bool interactive; + bool force; + int bind; int port; int key; char *sendlist; @@ -42,7 +44,7 @@ pthread_detach(pthread_self()); char action[512]; - while ( true) { + while ( true ) { printf("Input action: Close?\n"); if(scanf("%s",action) < 1) { printf("Invalide action\n"); @@ -147,13 +149,18 @@ } } -void start_reply(int mkey) { +void start_recvfrom(int mkey, bool force) { logger->debug("start reply\n"); signal(SIGINT, stop_replyserver_handler); signal(SIGTERM, stop_replyserver_handler); serverSockt = net_mod_socket_open(); - net_mod_socket_bind(serverSockt, mkey); + if(force) { + net_mod_socket_force_bind(serverSockt, mkey); + } else { + net_mod_socket_bind(serverSockt, mkey); + } + int rv = 0 ; while( true) { @@ -163,6 +170,9 @@ if(rv == EBUS_STOPED) { logger->debug("Stopping\n"); break; + } else if(rv == EBUS_KEY_INUSED){ + printf("key宸茬粡琚崰鐢╘n"); + exit(1); } logger->debug("net_mod_socket_recvandsend error.%s\n", bus_strerror(rv)); @@ -526,10 +536,34 @@ } void list () { + LockFreeQueue<shm_packet_t> * mqueue; hashtable_t *hashtable = mm_get_hashtable(); + printf("%10s \t %-10s \t %10s\n", "KEY", "LENGTH", "STATUS"); hashtable_foreach(hashtable, [&](int key, void * value){ - printf("%d\n", key); + if(key >= 100 ) { + mqueue = (LockFreeQueue<shm_packet_t> *)hashtable_get(hashtable, key); + if((long)mqueue == 0x1) { + printf("%10d \t %-10s\n", key, "Not In Used"); + } else { + printf("%10d \t %-10d\n", key, mqueue->size()); + } + + } else { + printf("%10d\n", key); + } + }); +} + +void info(int key) { + LockFreeQueue<shm_packet_t> * mqueue; + hashtable_t *hashtable = mm_get_hashtable(); + mqueue = (LockFreeQueue<shm_packet_t> *) hashtable_get(hashtable, key); + printf("%10s: %-10p\n", "PTR", mqueue); + printf("%10s: %-10d\n", "KEY", key); + printf("%10s: %-10d\n", "LENGTH", mqueue->size()); + + } @@ -551,7 +585,7 @@ net_node_t node_arr[] = {NULL, 0, key}; void * client = net_mod_socket_open(); - n = net_mod_socket_sendandrecv_timeout(client, node_arr, 1, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size, 5); + n = net_mod_socket_sendandrecv_timeout(client, node_arr, 1, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size, 5000); if(n == 0) { printf("send failed\n"); return; @@ -596,6 +630,8 @@ fpe("./shm_util list\n"); fpe("# remove key 1001\n"); fpe("./shm_util rm 1001\n"); + fpe("./shm_util info 1002\n"); + fpe("./shm_util recvfrom --bind 1002 [--force]\n") fpe("\n"); } @@ -627,6 +663,8 @@ {"key", required_argument, 0, 'k'}, {"port", required_argument, 0, 'p'}, {"interactive", no_argument, 0, 'i'}, + {"force", no_argument, 0, 'f'}, + {"bind", required_argument, (int *)mopt.bind, 0}, {"sendlist", required_argument, (int *)mopt.sendlist, 0}, {"publist", required_argument, (int *)mopt.publist, 0}, {0, 0, 0, 0} @@ -637,7 +675,7 @@ { - c = getopt_long (argc, argv, "+f:k:p:i", long_options, &option_index); + c = getopt_long (argc, argv, "+fk:p:i", long_options, &option_index); /* Detect the end of the options. */ if (c == -1) @@ -656,6 +694,9 @@ else if(strcmp(long_options[option_index].name, "publist") == 0) { mopt.publist = optarg; } + else if(strcmp(long_options[option_index].name, "bind") == 0) { + mopt.bind = atoi(optarg); + } else { printf ("option %s", long_options[option_index].name); if (optarg) @@ -671,6 +712,10 @@ case 'i': mopt.interactive = true; + break; + + case 'f': + mopt.force = true; break; case 'p': @@ -760,7 +805,7 @@ int i; char *prog; char * fun; - argument_t opt; + argument_t opt = {}; shm_mm_wrapper_init(512); @@ -780,15 +825,29 @@ else if (strcmp("list", fun) == 0 ) { list(); } + else if (strcmp("info", fun) == 0 ) { + if(argc < 2) { + + usage(prog); + + } else { + for(i = 1; i < argc; i++) { + int key = atoi(argv[i]); + info(key); + } + } + } else if (strcmp("rm", fun) == 0 ) { if(argc < 2) { usage(prog); - exit(1); + + } else { + for(i = 1; i < argc; i++) { + int key = atoi(argv[i]); + remove(key); + } } - for(i = 1; i < argc; i++) { - int key = atoi(argv[i]); - remove(key); - } + } else if (strcmp("sendandrecv", fun) == 0 ) { if(argc < 3) { @@ -818,14 +877,14 @@ } - else if (strcmp("start_reply", fun) == 0) { + else if (strcmp("recvfrom", fun) == 0) { opt = parse_args(argc, argv); - opt = parse_args(argc, argv); - if(opt.key == 0) { + if(opt.bind == 0) { usage(argv[0]); - exit(1); + } else { + start_recvfrom(opt.bind, opt.force); } - start_reply(opt.key); + } else if (strcmp("start_net_client", fun) == 0) { opt = parse_args(argc, argv); @@ -889,7 +948,6 @@ } - printf("==========end========\n"); - // shm_mm_wrapper_destroy(); + shm_mm_wrapper_destroy(); } -- Gitblit v1.8.0