h5互动网站建设,网站开发交流吧,威县建设局网站,研究院网站建设的内容iperf3是一个非常强大的工具#xff0c;它是用C语言编写的。同时iperf3也是用C语言实现面向对象编程的典范#xff0c;他以数据结构函数指针为基础#xff0c;非常好的用C语言实现面向对象的编程的三大特征#xff1a;封装#xff0c;继承#xff0c;多态。相信通过阅读i…iperf3是一个非常强大的工具它是用C语言编写的。同时iperf3也是用C语言实现面向对象编程的典范他以数据结构函数指针为基础非常好的用C语言实现面向对象的编程的三大特征封装继承多态。相信通过阅读iperf3的源代码不仅能帮助我们更好的理解与使用iperf3也可以帮助我们去更好的理解C及其它面向对象的语言里的类-对象-实例化-多态-模板等概念的本质。
main函数代码流程
在src/main.c文件里我们可以找到main函数除了用TEST_PROC_AFFINITY宏括起来的部分外TEST_PROC_AFFINITY编译宏是一个未定义宏所以这部分代码是未启用的为了阅读方便在本文中删除了我们可以看到整个main函数分成4大步骤
第一步对象实例化创建对象test第二步根据用户在终端命令行界面输入参数初始化对象test第三步运行对象test处理测试过程 如果是客户端发起连接建立并发送数据包开始测试如果是服务端监听端口等待接收处理客户端发过来的数据包开始测试过程 第四步测试结束删除销毁对象test
int
main(int argc, char **argv)
{struct iperf_test *test;#ifdef TEST_PROC_AFFINITY//这部分代码是未实现的功能删除
#endif//第一步对象实例化创建对象test并用iperf_defaults()对test里的变量和指针赋默认值test iperf_new_test();if (!test)iperf_errexit(NULL, create new test error - %s, iperf_strerror(i_errno));iperf_defaults(test); /* sets defaults *///第二步根据用户在终端命令行界面输入参数初始化对象testif (iperf_parse_arguments(test, argc, argv) 0) {iperf_err(test, parameter error - %s, iperf_strerror(i_errno));fprintf(stderr, \n);usage_long(stdout);exit(1);}//第三步运行对象test处理测试过程if (run(test) 0)iperf_errexit(test, error - %s, iperf_strerror(i_errno));//第四步测试结束删除销毁对象testiperf_free_test(test);return 0;
}第一步测试实例的创建–创建对象test
文章开头介绍过iperf3是用C语言实现面向对象编程的典范 所以这里第一步调用iperf_new_test创建对象C语言里不支持class, 所以这里用struct里定义数据函数指针的方式来实现类的数据属性成员函数。然后通过malloc给结构指针附值的方式实现对象(全部结构指针)test的创建与分配内存(class创建对象的本质是给对象分配内存)。以及给test的settings和bitrate_limit_intervals_traffic_bytes二个struct指针进入分配内存。
struct iperf_test *
iperf_new_test()
{struct iperf_test *test;//通过malloc给test指针分配内存---------对象实例化test (struct iperf_test *) malloc(sizeof(struct iperf_test));if (!test) {i_errno IENEWTEST;return NULL;}/* initialize everything to zero */memset(test, 0, sizeof(struct iperf_test));//通过malloc给test-settings指针分配内存test-settings (struct iperf_settings *) malloc(sizeof(struct iperf_settings));if (!test-settings) {free(test);i_errno IENEWTEST;return NULL;}memset(test-settings, 0, sizeof(struct iperf_settings));//通过malloc给test-bitrate_limit_intervals_traffic_bytes 指针分配内存test-bitrate_limit_intervals_traffic_bytes (iperf_size_t *) malloc(sizeof(iperf_size_t) * MAX_INTERVAL);if (!test-bitrate_limit_intervals_traffic_bytes) {free(test-settings);free(test);i_errno IENEWTEST;return NULL;}memset(test-bitrate_limit_intervals_traffic_bytes, 0, sizeof(sizeof(iperf_size_t) * MAX_INTERVAL));/* By default all output goes to stdout */test-outfile stdout;return test;
}
第二步测试实例的初始化–根据输入参数初始化对象test
这一步本质上是调用iperf_defaults对test进行初步的初始化对test里的数据和函数附默认值比如tcp, udp, sctp分别调用什么函数去进行数据发送与接收调用什么函数进行连接的函数指针的初始化这里我们可以把依赖于配置而附不同的函数的函数指针的过程视为虚接口的初始化。这个函数的调用可以视为构造函数的调用。然后读取用户在终端命令行界面上输入的参数对test进行变量与方法函数的初始化配置各种属性以及根据输入参数的不同比如是测试UDP还是TCP是客户端还是服务端等等。
以-c和-s二个参数为例如果用户输入的命令行为iperf3 -c 则将test初始化为发送端模式以便后面第三步调用run时可以找到正确的方法与客户端、服务端TCP/UDP相应的回调函数 int
iperf_defaults(struct iperf_test *testp)
{struct protocol *tcp, *udp;
#if defined(HAVE_SCTP_H)struct protocol *sctp;
#endif /* HAVE_SCTP_H */testp-omit OMIT;testp-duration DURATION;testp-diskfile_name (char*) 0;testp-affinity -1;testp-server_affinity -1;TAILQ_INIT(testp-xbind_addrs);
#if defined(HAVE_CPUSET_SETAFFINITY)CPU_ZERO(testp-cpumask);
#endif /* HAVE_CPUSET_SETAFFINITY */testp-title NULL;testp-extra_data NULL;testp-congestion NULL;testp-congestion_used NULL;testp-remote_congestion_used NULL;testp-server_port PORT;//以代码省略
}int
iperf_parse_arguments(struct iperf_test *test, int argc, char **argv)
{static struct option longopts[] {
/*
此处省略大量代码
*/{server, no_argument, NULL, s},{client, required_argument, NULL, c},
/*
此处省略大量代码
*/{version4, no_argument, NULL, 4},{version6, no_argument, NULL, 6},
/*
此处省略大量代码
*/{NULL, 0, NULL, 0}};while ((flag getopt_long(argc, argv, p:f:i:D1VJvsc:ub:t:n:k:l:P:Rw:B:M:N46S:L:ZO:F:A:T:C:dI:hX:, longopts, NULL)) ! -1) {switch (flag) {
/*
此处省略大量代码
*/ case s:if (test-role c) {i_errno IESERVCLIENT;return -1;}iperf_set_test_role(test, s);break;case c:if (test-role s) {i_errno IESERVCLIENT;return -1;}iperf_set_test_role(test, c);}break;case 4:test-settings-domain AF_INET;break;case 6:test-settings-domain AF_INET6;break;
/*
此处省略大量代码
*/}
/*
此处省略大量代码
*/if ((test-role ! c) (test-role ! s)) {i_errno IENOROLE;return -1;}
/*
此处省略大量代码
*/return 0;
}第三步测试实例的执行–运行对象test处理测试过程
在这里程序会根据自己是运行在服务端还是客户端模式执行不同的测试流程
在服务端 程序会打开socket监听并等待连接连接成功后就不停的把客户端送过来的数据读取出来并丢弃直到连接断开继续监听并等待新的连接。在客户端程序会打开socket去连接服务端连接成功后按指定测试方式发送数据包直到测试结束。
static int
run(struct iperf_test *test)
{/*代码删除内容略*/free(test);
}
文章转载自: http://www.morning.cqyhdy.cn.gov.cn.cqyhdy.cn http://www.morning.qcztm.cn.gov.cn.qcztm.cn http://www.morning.tfpqd.cn.gov.cn.tfpqd.cn http://www.morning.jwtjf.cn.gov.cn.jwtjf.cn http://www.morning.rqwwm.cn.gov.cn.rqwwm.cn http://www.morning.zgnng.cn.gov.cn.zgnng.cn http://www.morning.yqjjn.cn.gov.cn.yqjjn.cn http://www.morning.ydxx123.cn.gov.cn.ydxx123.cn http://www.morning.mtrrf.cn.gov.cn.mtrrf.cn http://www.morning.kjtdy.cn.gov.cn.kjtdy.cn http://www.morning.ngmjn.cn.gov.cn.ngmjn.cn http://www.morning.ybgpk.cn.gov.cn.ybgpk.cn http://www.morning.rgnp.cn.gov.cn.rgnp.cn http://www.morning.fdrwk.cn.gov.cn.fdrwk.cn http://www.morning.qznkn.cn.gov.cn.qznkn.cn http://www.morning.mrlls.cn.gov.cn.mrlls.cn http://www.morning.hcxhz.cn.gov.cn.hcxhz.cn http://www.morning.jjnql.cn.gov.cn.jjnql.cn http://www.morning.gjssk.cn.gov.cn.gjssk.cn http://www.morning.qjxkx.cn.gov.cn.qjxkx.cn http://www.morning.wjjxr.cn.gov.cn.wjjxr.cn http://www.morning.npbkx.cn.gov.cn.npbkx.cn http://www.morning.xcnwf.cn.gov.cn.xcnwf.cn http://www.morning.cgntj.cn.gov.cn.cgntj.cn http://www.morning.zlbjx.cn.gov.cn.zlbjx.cn http://www.morning.youprogrammer.cn.gov.cn.youprogrammer.cn http://www.morning.qbzfp.cn.gov.cn.qbzfp.cn http://www.morning.khpgd.cn.gov.cn.khpgd.cn http://www.morning.jzxqj.cn.gov.cn.jzxqj.cn http://www.morning.owenzhi.com.gov.cn.owenzhi.com http://www.morning.lqlc.cn.gov.cn.lqlc.cn http://www.morning.mqss.cn.gov.cn.mqss.cn http://www.morning.zlsmx.cn.gov.cn.zlsmx.cn http://www.morning.slpcl.cn.gov.cn.slpcl.cn http://www.morning.jbxfm.cn.gov.cn.jbxfm.cn http://www.morning.nmngq.cn.gov.cn.nmngq.cn http://www.morning.kmwsz.cn.gov.cn.kmwsz.cn http://www.morning.ktlxk.cn.gov.cn.ktlxk.cn http://www.morning.tfpqd.cn.gov.cn.tfpqd.cn http://www.morning.wjlrw.cn.gov.cn.wjlrw.cn http://www.morning.kxymr.cn.gov.cn.kxymr.cn http://www.morning.qjxkx.cn.gov.cn.qjxkx.cn http://www.morning.qrksj.cn.gov.cn.qrksj.cn http://www.morning.tmbfz.cn.gov.cn.tmbfz.cn http://www.morning.xjmyq.com.gov.cn.xjmyq.com http://www.morning.ltdrz.cn.gov.cn.ltdrz.cn http://www.morning.mgwpy.cn.gov.cn.mgwpy.cn http://www.morning.fqtzn.cn.gov.cn.fqtzn.cn http://www.morning.jfjbl.cn.gov.cn.jfjbl.cn http://www.morning.rbtny.cn.gov.cn.rbtny.cn http://www.morning.nyqxy.cn.gov.cn.nyqxy.cn http://www.morning.kjxgc.cn.gov.cn.kjxgc.cn http://www.morning.xhhzn.cn.gov.cn.xhhzn.cn http://www.morning.mnslh.cn.gov.cn.mnslh.cn http://www.morning.lhytw.cn.gov.cn.lhytw.cn http://www.morning.zrgsg.cn.gov.cn.zrgsg.cn http://www.morning.huihuangwh.cn.gov.cn.huihuangwh.cn http://www.morning.nzqmw.cn.gov.cn.nzqmw.cn http://www.morning.hbqfh.cn.gov.cn.hbqfh.cn http://www.morning.tzkrh.cn.gov.cn.tzkrh.cn http://www.morning.fxkgp.cn.gov.cn.fxkgp.cn http://www.morning.xgjhy.cn.gov.cn.xgjhy.cn http://www.morning.gtdf.cn.gov.cn.gtdf.cn http://www.morning.wjlnz.cn.gov.cn.wjlnz.cn http://www.morning.ntgjm.cn.gov.cn.ntgjm.cn http://www.morning.bsxws.cn.gov.cn.bsxws.cn http://www.morning.hdrrk.cn.gov.cn.hdrrk.cn http://www.morning.hrypl.cn.gov.cn.hrypl.cn http://www.morning.ywzqk.cn.gov.cn.ywzqk.cn http://www.morning.fktlg.cn.gov.cn.fktlg.cn http://www.morning.sqqkr.cn.gov.cn.sqqkr.cn http://www.morning.gcqs.cn.gov.cn.gcqs.cn http://www.morning.zfkxj.cn.gov.cn.zfkxj.cn http://www.morning.nbybb.cn.gov.cn.nbybb.cn http://www.morning.wzwyz.cn.gov.cn.wzwyz.cn http://www.morning.zlzpz.cn.gov.cn.zlzpz.cn http://www.morning.bpmdh.cn.gov.cn.bpmdh.cn http://www.morning.lggng.cn.gov.cn.lggng.cn http://www.morning.jqjnl.cn.gov.cn.jqjnl.cn http://www.morning.ycwym.cn.gov.cn.ycwym.cn