From 7f18ccee94aa4992fc3f085132166e79556273c4 Mon Sep 17 00:00:00 2001 From: wangyu- Date: Sat, 28 Oct 2017 02:05:14 -0500 Subject: [PATCH] refactor --- main.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++ misc.cpp | 79 +++----------------------------------------------------- misc.h | 2 +- 3 files changed, 79 insertions(+), 77 deletions(-) diff --git a/main.cpp b/main.cpp index 76f6c0a..07d2339 100644 --- a/main.cpp +++ b/main.cpp @@ -13,6 +13,65 @@ using namespace std; +static void print_help() +{ + char git_version_buf[100]={0}; + strncpy(git_version_buf,gitversion,10); + + printf("UDPspeeder V2\n"); + printf("git version: %s ",git_version_buf); + printf("build date: %s %s\n",__DATE__,__TIME__); + printf("repository: https://github.com/wangyu-/UDPspeeder\n"); + printf("\n"); + printf("usage:\n"); + printf(" run as client: ./this_program -c -l local_listen_ip:local_port -r server_ip:server_port [options]\n"); + printf(" run as server: ./this_program -s -l server_listen_ip:server_port -r remote_ip:remote_port [options]\n"); + printf("\n"); + printf("common options, must be same on both sides:\n"); + printf(" -k,--key key for simple xor encryption. if not set, xor is disabled\n"); + + printf("main options:\n"); + printf(" -f,--fec x:y forward error correction, send y redundant packets for every x packets\n"); + printf(" --timeout how long could a packet be held in queue before doing fec, unit: ms, default: 8ms\n"); + printf(" --mode fec-mode,available values: 0, 1; 0 cost less bandwidth, 1 cost less latency(default)\n"); + printf(" --report turn on send/recv report, and set a period for reporting, unit: s\n"); + + printf("advanced options:\n"); + printf(" --mtu mtu. for mode 0, the program will split packet to segment smaller than mtu_value.\n"); + printf(" for mode 1, no packet will be split, the program just check if the mtu is exceed.\n"); + printf(" default value: 1250\n"); + printf(" -j,--jitter simulated jitter. randomly delay first packet for 0~ ms, default value: 0.\n"); + printf(" do not use if you dont know what it means.\n"); + printf(" -i,--interval scatter each fec group to a interval of ms, to protect burst packet loss.\n"); + printf(" default value: 0. do not use if you dont know what it means.\n"); + printf(" --random-drop simulate packet loss, unit: 0.01%%. default value: 0\n"); + printf(" --disable-obscure disable obscure, to save a bit bandwidth and cpu\n"); +// printf(" --disable-xor disable xor\n"); + + printf("developer options:\n"); + printf(" --fifo use a fifo(named pipe) for sending commands to the running program, so that you\n"); + printf(" can change fec encode parameters dynamically, check readme.md in repository for\n"); + printf(" supported commands.\n"); + printf(" -j ,--jitter jmin:jmax similiar to -j above, but create jitter randomly between jmin and jmax\n"); + printf(" -i,--interval imin:imax similiar to -i above, but scatter randomly between imin and imax\n"); + printf(" -q,--queue-len max fec queue len, only for mode 0\n"); + printf(" --decode-buf size of buffer of fec decoder,u nit: packet, default: 2000\n"); + printf(" --fix-latency try to stabilize latency, only for mode 0\n"); + printf(" --delay-capacity max number of delayed packets\n"); + printf(" --disable-fec completely disable fec, turn the program into a normal udp tunnel\n"); + printf(" --sock-buf buf size for socket, >=10 and <=10240, unit: kbyte, default: 1024\n"); + printf("log and help options:\n"); + printf(" --log-level 0: never 1: fatal 2: error 3: warn \n"); + printf(" 4: info (default) 5: debug 6: trace\n"); + printf(" --log-position enable file name, function name, line number in log\n"); + printf(" --disable-color disable log color\n"); + printf(" -h,--help print this help message\n"); + + //printf("common options,these options must be same on both side\n"); +} + + + int main(int argc, char *argv[]) { //working_mode=tunnel_mode; @@ -25,6 +84,22 @@ int main(int argc, char *argv[]) assert(sizeof(i16_t)==2); dup2(1, 2); //redirect stderr to stdout int i, j, k; + + if (argc == 1) + { + print_help(); + myexit( -1); + } + for (i = 0; i < argc; i++) + { + if(strcmp(argv[i],"-h")==0||strcmp(argv[i],"--help")==0) + { + print_help(); + myexit(0); + } + } + + process_arg(argc,argv); delay_manager.set_capacity(delay_capacity); diff --git a/misc.cpp b/misc.cpp index e098ece..2975ab0 100644 --- a/misc.cpp +++ b/misc.cpp @@ -566,15 +566,11 @@ void process_arg(int argc, char *argv[]) {"jitter", required_argument, 0,'j'}, {"fifo", required_argument, 0, 1}, {"sub-net", required_argument, 0, 1}, - {"tun-dev", required_argument, 0, 1}, + {"tun-dev", optional_argument, 0, 1}, {NULL, 0, 0, 0} }; int option_index = 0; - if (argc == 1) - { - print_help(); - myexit( -1); - } + for (i = 0; i < argc; i++) { if(strcmp(argv[i],"--unit-test")==0) @@ -583,14 +579,7 @@ void process_arg(int argc, char *argv[]) myexit(0); } } - for (i = 0; i < argc; i++) - { - if(strcmp(argv[i],"-h")==0||strcmp(argv[i],"--help")==0) - { - print_help(); - myexit(0); - } - } + for (i = 0; i < argc; i++) { if(strcmp(argv[i],"--log-level")==0) @@ -621,11 +610,6 @@ void process_arg(int argc, char *argv[]) } log_bare(log_info, "\n"); - if (argc == 1) - { - print_help(); - myexit(-1); - } int no_l = 1, no_r = 1; while ((opt = getopt_long(argc, argv, "l:r:hcsk:j:f:p:n:i:q:",long_options,&option_index)) != -1) @@ -959,60 +943,3 @@ void process_arg(int argc, char *argv[]) } -void print_help() -{ - char git_version_buf[100]={0}; - strncpy(git_version_buf,gitversion,10); - - printf("UDPspeeder V2\n"); - printf("git version: %s ",git_version_buf); - printf("build date: %s %s\n",__DATE__,__TIME__); - printf("repository: https://github.com/wangyu-/UDPspeeder\n"); - printf("\n"); - printf("usage:\n"); - printf(" run as client: ./this_program -c -l local_listen_ip:local_port -r server_ip:server_port [options]\n"); - printf(" run as server: ./this_program -s -l server_listen_ip:server_port -r remote_ip:remote_port [options]\n"); - printf("\n"); - printf("common options, must be same on both sides:\n"); - printf(" -k,--key key for simple xor encryption. if not set, xor is disabled\n"); - - printf("main options:\n"); - printf(" -f,--fec x:y forward error correction, send y redundant packets for every x packets\n"); - printf(" --timeout how long could a packet be held in queue before doing fec, unit: ms, default: 8ms\n"); - printf(" --mode fec-mode,available values: 0, 1; 0 cost less bandwidth, 1 cost less latency(default)\n"); - printf(" --report turn on send/recv report, and set a period for reporting, unit: s\n"); - - printf("advanced options:\n"); - printf(" --mtu mtu. for mode 0, the program will split packet to segment smaller than mtu_value.\n"); - printf(" for mode 1, no packet will be split, the program just check if the mtu is exceed.\n"); - printf(" default value: 1250\n"); - printf(" -j,--jitter simulated jitter. randomly delay first packet for 0~ ms, default value: 0.\n"); - printf(" do not use if you dont know what it means.\n"); - printf(" -i,--interval scatter each fec group to a interval of ms, to protect burst packet loss.\n"); - printf(" default value: 0. do not use if you dont know what it means.\n"); - printf(" --random-drop simulate packet loss, unit: 0.01%%. default value: 0\n"); - printf(" --disable-obscure disable obscure, to save a bit bandwidth and cpu\n"); -// printf(" --disable-xor disable xor\n"); - - printf("developer options:\n"); - printf(" --fifo use a fifo(named pipe) for sending commands to the running program, so that you\n"); - printf(" can change fec encode parameters dynamically, check readme.md in repository for\n"); - printf(" supported commands.\n"); - printf(" -j ,--jitter jmin:jmax similiar to -j above, but create jitter randomly between jmin and jmax\n"); - printf(" -i,--interval imin:imax similiar to -i above, but scatter randomly between imin and imax\n"); - printf(" -q,--queue-len max fec queue len, only for mode 0\n"); - printf(" --decode-buf size of buffer of fec decoder,u nit: packet, default: 2000\n"); - printf(" --fix-latency try to stabilize latency, only for mode 0\n"); - printf(" --delay-capacity max number of delayed packets\n"); - printf(" --disable-fec completely disable fec, turn the program into a normal udp tunnel\n"); - printf(" --sock-buf buf size for socket, >=10 and <=10240, unit: kbyte, default: 1024\n"); - printf("log and help options:\n"); - printf(" --log-level 0: never 1: fatal 2: error 3: warn \n"); - printf(" 4: info (default) 5: debug 6: trace\n"); - printf(" --log-position enable file name, function name, line number in log\n"); - printf(" --disable-color disable log color\n"); - printf(" -h,--help print this help message\n"); - - //printf("common options,these options must be same on both side\n"); -} - diff --git a/misc.h b/misc.h index 0c9ee24..3560c53 100644 --- a/misc.h +++ b/misc.h @@ -56,7 +56,7 @@ int handle_command(char *s); int unit_test(); -void print_help(); +//void print_help(); void process_arg(int argc, char *argv[]);