47 Commits

Author SHA1 Message Date
wangyu-
0f3a7cf3b6 Update README.zh-cn.md 2017-10-25 00:11:44 -07:00
wangyu-
a2f9afd369 Update README.zh-cn.md 2017-10-25 00:10:28 -07:00
wangyu-
7bb66e9ce7 Update README.md 2017-10-25 00:04:50 -07:00
wangyu-
8e7a8aed92 changed help page 2017-10-25 02:00:34 -05:00
wangyu-
696396cf79 change help page 2017-10-25 01:57:08 -05:00
wangyu-
e11141e036 help page 2017-10-25 01:45:35 -05:00
wangyu-
d6fc5dc072 implemented commands 2017-10-25 01:15:50 -05:00
wangyu-
4dd37700e6 add option fifo 2017-10-24 14:16:17 -05:00
wangyu-
cf08bb735d add log,changed a fatal to error 2017-10-24 04:06:37 -05:00
wangyu-
1ca82311ec qMerge branch 'master' of https://github.com/wangyu-/UDPspeeder 2017-10-24 04:04:13 -05:00
wangyu-
cdf74b780a Update README.zh-cn.md 2017-10-24 00:21:38 -07:00
wangyu-
9e54fc8a3b Update README.zh-cn.md 2017-10-24 00:18:30 -07:00
wangyu-
16efd9d4a1 Update README.zh-cn.md 2017-10-23 23:11:02 -07:00
wangyu-
597b28f05f Update README.zh-cn.md 2017-10-23 22:10:51 -07:00
wangyu-
5192438851 Update README.zh-cn.md 2017-10-23 22:07:16 -07:00
wangyu-
13084620e5 Update README.zh-cn.md 2017-10-23 22:06:33 -07:00
wangyu-
6a58f4d38c Update README.zh-cn.md 2017-10-23 22:03:43 -07:00
wangyu-
5b40129505 Update README.md 2017-10-23 22:02:39 -07:00
wangyu-
d20627f5c0 Update README.zh-cn.md 2017-10-23 21:59:40 -07:00
wangyu-
0c016e8013 Update README.zh-cn.md 2017-10-23 21:48:32 -07:00
wangyu-
0f5155051b Update README.zh-cn.md 2017-10-23 21:48:08 -07:00
wangyu-
bf7c0a5dc1 Update README.zh-cn.md 2017-10-23 21:36:51 -07:00
wangyu-
bfe3c9cb00 Update README.zh-cn.md 2017-10-23 21:34:54 -07:00
wangyu-
2ae70d49a2 Update README.zh-cn.md 2017-10-23 21:31:44 -07:00
wangyu-
67529a041b Update README.zh-cn.md 2017-10-23 21:26:39 -07:00
wangyu-
385aa66e15 Update README.zh-cn.md 2017-10-23 21:23:20 -07:00
wangyu-
20a5547b27 Update README.zh-cn.md 2017-10-23 07:09:46 -07:00
wangyu-
605787bdd6 Update README.zh-cn.md 2017-10-23 07:06:30 -07:00
wangyu-
7cf14a39c7 Update README.zh-cn.md 2017-10-23 07:04:35 -07:00
wangyu-
8b267f811b Update README.zh-cn.md 2017-10-23 07:03:41 -07:00
wangyu-
9714332658 Update udpspeeder_openvpn.md 2017-10-23 05:00:49 -07:00
wangyu-
30896c8110 Update udpspeeder_openvpn.md 2017-10-23 05:00:22 -07:00
wangyu-
9b1999ec11 Update udpspeeder_openvpn.md 2017-10-23 04:58:06 -07:00
wangyu-
e71e200f5b Update udpspeeder_openvpn.md 2017-10-23 04:57:01 -07:00
wangyu-
5391077b94 Update udpspeeder_openvpn.md 2017-10-23 04:56:28 -07:00
wangyu-
0a44043884 Update README.zh-cn.md 2017-10-22 22:18:00 -07:00
wangyu-
586eae7c34 Update udpspeeder_openvpn.md 2017-10-22 22:12:59 -07:00
wangyu-
841c387fcb Update udpspeeder_openvpn.md 2017-10-22 22:12:26 -07:00
wangyu-
592efb300a Update README.zh-cn.md 2017-10-22 21:43:59 -07:00
wangyu-
bd8cb2076d Update README.md 2017-10-22 18:22:53 -07:00
wangyu-
8ad40532bb Update README.md 2017-10-22 10:35:45 -07:00
wangyu-
ebb6cf1cea Add files via upload 2017-10-22 12:34:49 -05:00
wangyu-
3737491145 Update README.md 2017-10-22 08:41:55 -07:00
wangyu-
257f8231b7 Update udpspeeder_openvpn.md 2017-10-22 05:26:49 -07:00
wangyu-
3e7df45d24 fixed a typo 2017-10-21 20:21:16 -05:00
wangyu-
fd8deba3ed Update README.md 2017-10-21 17:30:51 -07:00
wangyu-
eda1360d56 Update README.md 2017-10-21 17:30:22 -07:00
10 changed files with 413 additions and 99 deletions

View File

@@ -9,13 +9,18 @@ or
![image_vpn](/images/en/udpspeeder+openvpn3.PNG)
[简体中文](/doc/README.zh-cn.md)
[简体中文](/doc/README.zh-cn.md)(内容更丰富)
###### Note
You can use udp2raw with UDPspeeder together to bypass UDP firewalls.
udp2rawhttps://github.com/wangyu-/udp2raw-tunnel
# Efficacy
tested on a link with 100ms latency and 10% packet loss at both direction
### Ping Packet Loss
![](/images/en/ping_compare3.PNG)
![](/images/en/ping_compare_mode1.png)
### SCP Copy Speed
![](/images/en/scp_compare2.PNG)
@@ -27,7 +32,7 @@ For Windows and MacOS You can run UDPspeeder inside [this](https://github.com/wa
# How does it work
UDPspeeder uses FEC(Forward Error Correction) to improve your connection's quality,at the cost of addtional bandwidth.The algorithm for FEC is called Reed-Solomon.
UDPspeeder uses FEC(Forward Error Correction) to reduce packet loss rate,at the cost of addtional bandwidth.The algorithm for FEC is called Reed-Solomon.
![image0](/images/en/fec.PNG)
@@ -68,11 +73,14 @@ Now connecting to UDP port 3333 at the client side is equivalent to connecting t
`-f20:10` means sending 10 redundant packets for every 20 original packets.
`-k` enables simple XOR encryption to confuse DPI(Deep Packet Inspection)
To run stably,pay attention to MTU.
# Advanced Topic
### Full Options
```
UDPspeeder V2
git version:f479ca2779 build date:Oct 19 2017 01:37:08
git version:8e7a8aed92 build date:Oct 25 2017 02:00:54
repository: https://github.com/wangyu-/UDPspeeder
usage:
@@ -83,13 +91,13 @@ common option,must be same on both sides:
-k,--key <string> key for simple xor encryption. if not set,xor is disabled
main options:
-f,--fec x:y forward error correction,send y redundant packets for every x packets
--timeout <number> how long could a packet be held in queue before doing fec,unit: ms
--mode <number> fec-mode,available values: 0,1 ; 0 cost less bandwidth,1 cost less latency
--timeout <number> how long could a packet be held in queue before doing fec,unit: ms,default :8ms
--mode <number> fec-mode,available values: 0,1 ; 0 cost less bandwidth,1 cost less latency(default)
--report <number> turn on send/recv report,and set a period for reporting,unit:s
advanced options:
--mtu <number> mtu. for mode 0,the program will split packet to segment smaller than mtu_value.
for mode 1,no packet will be split,the program just check if the mtu is exceed.
default value:1250
default value:1250
-j,--jitter <number> simulated jitter.randomly delay first packet for 0~<number> ms,default value:0.
do not use if you dont know what it means.
-i,--interval <number> scatter each fec group to a interval of <number> ms,to protect burst packet loss.
@@ -97,6 +105,9 @@ advanced options:
--random-drop <number> simulate packet loss ,unit:0.01%. default value: 0
--disable-obscure <number> disable obscure,to save a bit bandwidth and cpu
developer options:
--fifo <string> use a fifo(named pipe) for sending commands to the running program,so that you
can change fec encode parameters dynamically,check readme.md in repository for
supported commands.
-j ,--jitter jmin:jmax similiar to -j above,but create jitter randomly between jmin and jmax
-i,--interval imin:imax similiar to -i above,but scatter randomly between imin and imax
-q,--queue-len <number> max fec queue len,only for mode 0
@@ -106,11 +117,21 @@ developer options:
--disable-fec <number> completely disable fec,turn the program into a normal udp tunnel
--sock-buf <number> buf size for socket,>=10 and <=10240,unit:kbyte,default:1024
log and help options:
--log-level <number> 0:never 1:fatal 2:error 3:warn
--log-level <number> 0:never 1:fatal 2:error 3:warn
4:info (default) 5:debug 6:trace
--log-position enable file name,function name,line number in log
--disable-color disable log color
-h,--help print this help message
```
#### `--fifo` option
Use a fifo(named pipe) for sending commands to the running program. For example `--fifo fifo.file`,you can use following commands to change parameters dynamically:
```
echo fec 19:9 >fifo.file
echo mtu 1100 >fifo.file
echo timeout 5 >fifo.file
echo queue-len 100 >fifo.file
echo mode 0 >fifo.file
```
### Speed-Up any traffic with OpenVPN+UDPspeeder

View File

@@ -488,3 +488,41 @@ int round_up_div(int a,int b)
{
return (a+b-1)/b;
}
int create_fifo(char * file)
{
if(mkfifo (file, 0666)!=0)
{
if(errno==EEXIST)
{
mylog(log_warn,"warning fifo file %s exist\n",file);
}
else
{
mylog(log_fatal,"create fifo file %s failed\n",file);
myexit(-1);
}
}
int fifo_fd=open (file, O_RDWR);
if(fifo_fd<0)
{
mylog(log_fatal,"create fifo file %s failed\n",file);
myexit(-1);
}
struct stat st;
if (fstat(fifo_fd, &st)!=0)
{
mylog(log_fatal,"fstat failed for fifo file %s\n",file);
myexit(-1);
}
if(!S_ISFIFO(st.st_mode))
{
mylog(log_fatal,"%s is not a fifo\n",file);
myexit(-1);
}
setnonblocking(fifo_fd);
return fifo_fd;
}

View File

@@ -21,6 +21,7 @@
#include <sys/wait.h>
#include <sys/socket.h> //for socket ofcourse
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h> //for exit(0);
#include <errno.h> //For errno - the error number
#include <netinet/tcp.h> //Provides declarations for tcp header
@@ -211,6 +212,7 @@ int set_timer_ms(int epollfd,int &timer_fd,u32_t timer_interval);
int round_up_div(int a,int b);
int create_fifo(char * file);
/*
int create_new_udp(int &new_udp_fd,int remote_address_uint32,int remote_port);
*/

View File

@@ -19,7 +19,7 @@
#### 原理简介
主要原理是通过冗余数据来对抗网络的丢包发送冗余数据的方式支持FEC(Forward Error Correction)和多倍发包,其中FEC算法是Reed-Solomon。
对于FEC方式的原理图:
FEC方式的原理图:
![image0](/images/en/fec.PNG)
@@ -43,6 +43,9 @@ Linux主机可以是桌面版可以是android手机/平板可以是open
android版需要通过terminal运行。
###### 注意
在使用虚拟机时,建议手动指定桥接到哪个网卡,不要设置成自动。否则可能会桥接到错误的网卡。
### 安装
下载编译好的二进制文件,解压到本地和服务器的任意目录。
@@ -51,11 +54,11 @@ https://github.com/wangyu-/UDPspeeder/releases
### 运行
假设你有一个serverip为44.55.66.77有一个服务监听在udp 7777端口。 假设你需要加速本地到44.55.66.77:7777的流量。
```
在client端运行:
./speederv2 -c -l0.0.0.0:3333 -r44.55.66.77:4096 -f20:10 -k "passwd"
在server端运行:
./speederv2 -s -l0.0.0.0:4096 -r127.0.0.1:7777 -f20:10 -k "passwd"
在client端运行:
./speederv2 -c -l0.0.0.0:3333 -r44.55.66.77:4096 -f20:10 -k "passwd"
```
现在client和server之间建立起了tunnel。想要连接44.55.66.77:7777只需要连接 127.0.0.1:3333。来回的所有的udp流量会被加速。
@@ -77,7 +80,7 @@ https://github.com/wangyu-/UDPspeeder/releases
### 命令选项
```
UDPspeeder V2
git version:99f6099e86 build date:Oct 19 2017 13:35:38
git version:8e7a8aed92 build date:Oct 25 2017 02:00:54
repository: https://github.com/wangyu-/UDPspeeder
usage:
@@ -102,6 +105,9 @@ advanced options:
--random-drop <number> simulate packet loss ,unit:0.01%. default value: 0
--disable-obscure <number> disable obscure,to save a bit bandwidth and cpu
developer options:
--fifo <string> use a fifo(named pipe) for sending commands to the running program,so that you
can change fec encode parameters dynamically,check readme.md in repository for
supported commands.
-j ,--jitter jmin:jmax similiar to -j above,but create jitter randomly between jmin and jmax
-i,--interval imin:imax similiar to -i above,but scatter randomly between imin and imax
-q,--queue-len <number> max fec queue len,only for mode 0
@@ -119,40 +125,58 @@ log and help options:
```
### 包发送选项,两端设置可以不同。 只影响本地包发送。
##### -f 选项
##### `-f` 选项
设置fec参数影响数据的冗余度。
##### --timeout 选项
##### `--timeout` 选项
指定fec编码器在编码时候最多可以引入多大的延迟。越高fec越有效率加速游戏时调低可以降低延迟。
##### --mode 选项
##### `--mode` 选项 和 `--mtu`选项
fec编码器的工作模式。对于mode 0编码器会积攒一定数量的packet然后把他们合并再切成等长的片段切分长度由--mtu指定。对于mode 1编码器不会做任何切分,而是会把packet按最大长度对齐fec冗余包的长度为对齐后的长度最大长度
mode 0更省流量在丢包率正常的情况下效果和mode 1是一样的mode 1延迟更低在极高丢包的情况下表现更好。
mode 0使用起来可以不用关注mtu因为fec编码器会帮你把包切分到合理的大小用mode 1时必须合理设置上层应用的mtu。
mode 0使用起来可以不用关注mtu因为fec编码器会帮你把包切分到合理的大小用mode 1时必须合理设置上层应用的mtu。mode 0模式中--mtu选项决定切分的片段的长度mode 1模式中--mtu选项只起检查作用如果超过了--mtu指定的值数据包会被丢弃。
mode 0模式的流量消耗基本完全透明。mode 1因为涉及到数据按最大长度对齐所以流量消耗不是完全可预期。不过就实际使用来看mode 1消耗的额外流量不多。
##### --report  选项
mode 0模式数据包一般不会乱序除非网络本身有严重乱序mode 1模式被恢复的数据包可能会乱序不过UDP本来就允许乱序对绝大多数应用没有影响。mode 0模式反而可以纠正一些乱序情况。
##### `--report`  选项
数据发送和接受报告。开启后可以根据此数据推测出包速和丢包率等特征。
##### -j 选项
为原始数据发送增加一个延迟抖动值。这样上层应用计算出来的RTT方差会更大以等待后续冗余包的到达不至于发生在冗余包到达之前就触发重传的尴尬。配合-t选项使用。正常情况下跨国网络本身的延迟抖动就很大。可以不用设-j
##### `-i` 选项
指定一个时间窗口长度为n毫秒。同一个fec分组的数据发送时候会被均匀分散到这n毫秒中。可以对抗突发性的丢包。默认值是0因为这个功能需要用到时钟在某些虚拟机里时钟不稳定可能会导致个别包出现非常大的延迟所以默认关掉了。这个功能很有用默认参数效果不理想时可以尝试打开。
##### -i 选项
指定一个时间窗口长度为n毫秒。同一个fec分组的数据发送时候会被均匀分散到这n毫秒中。可以对抗突发性的丢包
##### `-j` 选项
为原始数据发送增加一个延迟抖动值。这样上层应用计算出来的RTT方差会更大以等待后续冗余包的到达不至于发生在冗余包到达之前就触发重传的尴尬。配合-t选项使用。正常情况下跨国网络本身的延迟抖动就很大可以不用设-j。这个功能也需要时钟默认关掉了不过一般情况应该不需要这个功能
##### --random-drop 选项
随机丢包。模拟恶劣的网络环境时使用。
##### `--random-drop` 选项
随机丢包。模拟恶劣的网络环境时使用。如果你的网络现在没有多大丢包但是你想测试一下高丢包环境下各种FEC参数的表现可以开这个选项。
##### -k选项
##### `-k`选项
指定一个字符串server/client间所有收发的包都会被异或改变协议特征防止UDPspeeder的协议被运营商针对。
##### `--disable-obscure`
UDPspeeder默认情况下会对每个发出的数据包随机填充和异或一些字节(4~32字节)这样通过抓包难以发现你发了冗余数据防止VPS被封。这个功能只是为了小心谨慎即使你关掉这个功能基本上也没问题关掉可以省一些带宽和CPU。
##### `-q,--queue-len`
编码器在做FEC前最多积攒多少个数据包只对mode 0有效。除非是使用下文`V2版如何多倍发包`里面提到的用法,不建议改动。
#### `--fifo` option
用fifo(命名管道)向运行中的程序发送command. 例如`--fifo fifo.file`,可用的command有
```
echo fec 19:9 >fifo.file
echo mtu 1100 >fifo.file
echo timeout 5 >fifo.file
echo queue-len 100 >fifo.file
echo mode 0 >fifo.file
```
可以动态改变fec编码器参数。
# 使用经验
### 在FEC和多倍发包之间如何选择
对于游戏,游戏的流量本身不大,延迟很重要,多倍发包是最有效的解决方案,多倍发包不会引入额外的延迟。
对于游戏,游戏的流量本身不大,延迟很重要,多倍发包是最解决方案,多倍发包不会引入额外的延迟。FEC编码器需要先积攒一些数据才可以做FEC,延迟无法避免;对于多倍发包,没有这个问题,所以没有延迟。
对于其他日常应用延迟要求一般在合理配置的情况下FEC的效果肯定好过多倍发包。不过需要根据网络的最大丢包来配置FEC参数才能有稳定的效果。如果配置不当对于--mode 1可能会完全没有效果对于--mode 0可能效果会比不用UDPspeeder还差。
@@ -169,6 +193,8 @@ mode 0模式的流量消耗基本完全透明。mode 1因为涉及到数据按
./speederv2 -c -l0.0.0.0:3333 -r44.55.66.77:4096 -f1:1 -k "passwd" --mode 0 -q1
```
使用了`--mode 0 -q1`以后,`--timeout`选项不起作用,所以不用调。
如果你只需要多倍发包可以直接用回V1版V1版配置更简单占用内存更小而且经过了几个月的考验很稳定。
### 根据网络丢包合理设置FEC参数
@@ -189,6 +215,35 @@ FEC算法很吃CPU,初次使用建议关注UDPspeeder的CPU占用。如果CPU被
另外fec分组大小不宜过大否则不但很耗CPU,还有其他副作用建议x+y<50
### 改变FEC参数而不断线
`--fifo`选项可以在运行时无缝改变FEC参数无需重启程序也不会断线
### 为什么使用之后效果反而变差了?
有可能是你用了mode 0参数而又没调好参数
如果你没有使用mode 0而确实效果变差了那很可能是因为你的运营商对UDP有限制一般看视频和下载都是TCP流量而用UDPspeeder中转后流量变成了UDP流量如果运营商对UDP做了限制就可能导致效果比不用还差用udp2raw可以解决udp2raw: https://github.com/wangyu-/udp2raw-tunnel
### UDPspeeder和BBR/锐速配合
UDPspeeder和BBR/锐速可以配合使用UDPspeeder工作在IP层负责降低丢包率BBR/锐速工作在TCP层负责优化拥塞和重传这种情况下可以调低UDPspeeder的冗余度能把丢包率降低到5%以内就可以了剩下的交给BBR/锐速解决这样预计可以节省一些流量如果是UDPspeeder跟Linux默认的Cubic一起用最少也要把丢包率降低到1%以下才能流畅使用TCP
对下文的`UDPspeeder + openvpn``UDPspeeder + openvpn + $***`方法有效不过有一点区别具体见下文
### UDPspeeder和Kcptun配合
UDPspeeder和Kcptun配合,UDPspeeder和Kcptun可以并联也可以串联
并联的情况下让kcptun负责加速TCP,UDPspeeder负责加速UDP见下文的`UDPspeeder + kcptun + $*** 同时加速tcp和udp流量`
串联的情况UDPspeeder的FEC跟Kcptun自带的相比可以对两个方向设置不同的FEC参数有一个更省流量的mode 0模式可以动态改变FEC参数但是UDPspeeder本身不优化拥塞和重传算法所以UDPspeeder和Kcptun也可以配合使用结合两者的优点
串联时可以关掉Kcptun的FEC,让UDPspeeder接管FEC功能这样UDPspeeder工作在UDP层负责降低丢包率Kcptun工作在应用层用kcp算法负责优化拥塞和重传能起到和`UDPspeeder+BBR/锐速`类似的效果
如果发Issue问Kcptun+UDPspeeder相关的问题一定要说明是并联还是串联
# 应用
#### UDPspeeder + openvpn加速任何流量
@@ -196,19 +251,23 @@ FEC算法很吃CPU,初次使用建议关注UDPspeeder的CPU占用。如果CPU被
具体配置见[UDPspeeder + openvpn config guide](/doc/udpspeeder_openvpn.md).
可以和BBR/锐速叠加不过BBR/锐速部署在VPS上只对从本地到VPS的流量有效对从本地到第三方服务器的流量无效
需要在服务端开启ipforward和NAT在客户端改路由表可以手动修改也可以由OpenVPN的redirect-gateway选项自动加好)。
#### UDPspeeder + kcptun/finalspeed + $*** 同时加速tcp和udp流量
如果你需要用加速的tcp看视频和下载文件这样效果比UDPspeeder+vpn方案更好在没有BBR的情况下)。
![image0](/images/cn/speeder_kcptun.PNG)
#### UDPspeeder + openvpn + $*** 混合方案
也是我正在用的方案优点是可以随时在vpn和$\*\*\*方案间快速切换
实际部署起来比图中看起来的还要简单不需要改路由表需要做的只是用openvpn的ip访问$*** server
实际部署起来比图中看起来的还要简单不需要改路由表不需要写iptables规则和开启NAT需要做的只是用openvpn分配的ip访问$*** server
![image0](/images/cn/speeder_vpn_s.PNG)
(也可以把图中的$*** server换成其他的socks5 server这样$*** client也不需要)
(也可以把图中的$*** server换成其他的socks5 server这样就不需要$*** client了)
另外这种方案加速TCP时效果可以和BBR叠加UDPspeeder用来改善丢包率BBR负责重传是不错的组合
可以和BBR/锐速叠加BBR/锐速只要部署在VPS上就有效
# 编译教程
暂时先参考udp2raw的这篇教程几乎一样的过程

View File

@@ -1,12 +1,14 @@
# UDPspeeder + openvpn config guide
![image_vpn](/images/en/udpspeeder+openvpn.PNG)
![image_vpn](/images/en/udpspeeder+openvpn3.PNG)
# UDPspeeder command
#### run at server side
```
./speederv2 -s -l0.0.0.0:8855 -r 127.0.0.1:7777 -f20:10
```
#### run at client side
assume server ip is 45.66.77.88
```
@@ -84,3 +86,17 @@ sndbuf 2000000 ##### important
rcvbuf 2000000 ##### important
txqueuelen 4000 ##### suggested
```
##### Note:
If you use the `redirect-gateway` option of OpenVPN,you may need to add a route exception for your remote server ip at client side.Otherwise OpenVPN may hijack UDPspeeder 's traffic.
For example,depend on your network environment,the command may looks like:
```
ip route add 44.55.66.77 via 44.55.66.1
```
or
```
ip route add 44.55.66.77 dev XXX
```
(run at client side)

View File

@@ -11,6 +11,15 @@
#include "lib/rs.h"
#include "fd_manager.h"
int g_fec_data_num=20;
int g_fec_redundant_num=10;
int g_fec_mtu=1250;
int g_fec_queue_len=200;
int g_fec_timeout=8*1000; //8ms
int g_fec_mode=1;
int dynamic_update_fec=1;
const int encode_fast_send=1;
const int decode_fast_send=1;
@@ -118,6 +127,15 @@ int blob_decode_t::output(int &n,char ** &s_arr,int *&len_arr)
return 0;
}
fec_encode_manager_t::~fec_encode_manager_t()
{
fd_manager.fd64_close(timer_fd64);
}
u64_t fec_encode_manager_t::get_timer_fd64()
{
return timer_fd64;
}
fec_encode_manager_t::fec_encode_manager_t()
{
//int timer_fd;
@@ -128,25 +146,18 @@ fec_encode_manager_t::fec_encode_manager_t()
}
timer_fd64=fd_manager.create(timer_fd);
re_init(4,2,1200,100,10000,0);
re_init(fec_data_num,fec_redundant_num,fec_mtu,fec_queue_len,fec_timeout,fec_mode);
seq=(u32_t)get_true_random_number(); //TODO temp solution for a bug.
}
fec_encode_manager_t::~fec_encode_manager_t()
{
fd_manager.fd64_close(timer_fd64);
}
u64_t fec_encode_manager_t::get_timer_fd64()
{
return timer_fd64;
}
int fec_encode_manager_t::re_init(int data_num,int redundant_num,int mtu,int pending_num,int pending_time,int type)
int fec_encode_manager_t::re_init(int data_num,int redundant_num,int mtu,int queue_len,int timeout,int mode)
{
fec_data_num=data_num;
fec_redundant_num=redundant_num;
fec_mtu=mtu;
fec_pending_num=pending_num;
fec_pending_time=pending_time;
this->type=type;
fec_queue_len=queue_len;
fec_timeout=timeout;
fec_mode=mode;
assert(data_num+redundant_num<max_fec_packet_num);
counter=0;
@@ -168,16 +179,16 @@ int fec_encode_manager_t::append(char *s,int len/*,int &is_first_packet*/)
itimerspec its;
memset(&its.it_interval,0,sizeof(its.it_interval));
first_packet_time=get_current_time_us();
my_time_t tmp_time=fec_pending_time+first_packet_time;
my_time_t tmp_time=fec_timeout+first_packet_time;
its.it_value.tv_sec=tmp_time/1000000llu;
its.it_value.tv_nsec=(tmp_time%1000000llu)*1000llu;
timerfd_settime(timer_fd,TFD_TIMER_ABSTIME,&its,0);
}
if(type==0)//for type 0 use blob
if(fec_mode==0)//for type 0 use blob
{
assert(blob_encode.input(s,len)==0);
}
else if(type==1)//for tpe 1 use input_buf and counter
else if(fec_mode==1)//for tpe 1 use input_buf and counter
{
mylog(log_trace,"counter=%d\n",counter);
assert(len<=65535&&len>=0);
@@ -199,17 +210,27 @@ int fec_encode_manager_t::append(char *s,int len/*,int &is_first_packet*/)
}
int fec_encode_manager_t::input(char *s,int len/*,int &is_first_packet*/)
{
if(counter==0&&dynamic_update_fec)
{
fec_data_num=g_fec_data_num;
fec_redundant_num=g_fec_redundant_num;
fec_mtu=g_fec_mtu;
fec_queue_len=g_fec_queue_len;
fec_timeout=g_fec_timeout;
fec_mode=g_fec_mode;
}
int about_to_fec=0;
int delayed_append=0;
//int counter_back=counter;
assert(type==0||type==1);
assert(fec_mode==0||fec_mode==1);
if(type==0&& s!=0 &&counter==0&&blob_encode.get_shard_len(fec_data_num,len)>=fec_mtu)
if(fec_mode==0&& s!=0 &&counter==0&&blob_encode.get_shard_len(fec_data_num,len)>=fec_mtu)
{
mylog(log_warn,"message too long len=%d,ignored\n",len);
return -1;
}
if(type==1&&s!=0&&len>=fec_mtu)
if(fec_mode==1&&s!=0&&len>=fec_mtu)
{
mylog(log_warn,"message too long len=%d fec_mtu=%d,ignored\n",len,fec_mtu);
return -1;
@@ -221,10 +242,10 @@ int fec_encode_manager_t::input(char *s,int len/*,int &is_first_packet*/)
}
if(s==0) about_to_fec=1;//now
if(type==0&& blob_encode.get_shard_len(fec_data_num,len)>=fec_mtu) {about_to_fec=1; delayed_append=1;}//fec then add packet
if(fec_mode==0&& blob_encode.get_shard_len(fec_data_num,len)>=fec_mtu) {about_to_fec=1; delayed_append=1;}//fec then add packet
if(type==0) assert(counter<fec_pending_num);//counter will never equal fec_pending_num,if that happens fec should already been done.
if(type==1) assert(counter<fec_data_num);
if(fec_mode==0) assert(counter<fec_queue_len);//counter will never equal fec_pending_num,if that happens fec should already been done.
if(fec_mode==1) assert(counter<fec_data_num);
if(s!=0&&!delayed_append)
@@ -232,9 +253,9 @@ int fec_encode_manager_t::input(char *s,int len/*,int &is_first_packet*/)
append(s,len);
}
if(type==0&& counter==fec_pending_num) about_to_fec=1;
if(fec_mode==0&& counter==fec_queue_len) about_to_fec=1;
if(type==1&& counter==fec_data_num) about_to_fec=1;
if(fec_mode==1&& counter==fec_data_num) about_to_fec=1;
if(about_to_fec)
@@ -252,7 +273,7 @@ int fec_encode_manager_t::input(char *s,int len/*,int &is_first_packet*/)
int actual_data_num;
int actual_redundant_num;
if(type==0)
if(fec_mode==0)
{
actual_data_num=fec_data_num;
@@ -299,8 +320,8 @@ int fec_encode_manager_t::input(char *s,int len/*,int &is_first_packet*/)
write_u32(input_buf[i] + tmp_idx, seq);
tmp_idx += sizeof(u32_t);
input_buf[i][tmp_idx++] = (unsigned char) type;
if (type == 1 && i < actual_data_num)
input_buf[i][tmp_idx++] = (unsigned char) fec_mode;
if (fec_mode == 1 && i < actual_data_num)
{
input_buf[i][tmp_idx++] = (unsigned char) 0;
input_buf[i][tmp_idx++] = (unsigned char) 0;
@@ -313,7 +334,7 @@ int fec_encode_manager_t::input(char *s,int len/*,int &is_first_packet*/)
tmp_output_buf[i]=input_buf[i]+tmp_idx; //////caution ,trick here.
if(type==0)
if(fec_mode==0)
{
output_len[i]=tmp_idx+fec_len;
if(i<actual_data_num)
@@ -383,7 +404,7 @@ int fec_encode_manager_t::input(char *s,int len/*,int &is_first_packet*/)
memset(&its,0,sizeof(its));
timerfd_settime(timer_fd,TFD_TIMER_ABSTIME,&its,0);
if(encode_fast_send&&type==1)
if(encode_fast_send&&fec_mode==1)
{
int packet_to_send[max_fec_packet_num+5]={0};
int packet_to_send_counter=0;
@@ -407,7 +428,7 @@ int fec_encode_manager_t::input(char *s,int len/*,int &is_first_packet*/)
}
else
{
if(encode_fast_send&&s!=0&&type==1)
if(encode_fast_send&&s!=0&&fec_mode==1)
{
assert(counter>=1);
assert(counter<=255);
@@ -422,7 +443,7 @@ int fec_encode_manager_t::input(char *s,int len/*,int &is_first_packet*/)
write_u32(input_buf[input_buf_idx]+tmp_idx,seq);
tmp_idx+=sizeof(u32_t);
input_buf[input_buf_idx][tmp_idx++]=(unsigned char)type;
input_buf[input_buf_idx][tmp_idx++]=(unsigned char)fec_mode;
input_buf[input_buf_idx][tmp_idx++]=(unsigned char)0;
input_buf[input_buf_idx][tmp_idx++]=(unsigned char)0;
input_buf[input_buf_idx][tmp_idx++]=(unsigned char)((u32_t)input_buf_idx);
@@ -444,7 +465,7 @@ int fec_encode_manager_t::input(char *s,int len/*,int &is_first_packet*/)
if(s!=0&&delayed_append)
{
assert(type!=1);
assert(fec_mode!=1);
append(s,len);
}

View File

@@ -19,6 +19,17 @@ const int max_fec_packet_num=255;// this is the limitation of the rs lib
extern u32_t fec_buff_num;
/*begin for first time init or dynamic update*/
extern int g_fec_data_num;
extern int g_fec_redundant_num;
extern int g_fec_mtu;
extern int g_fec_queue_len;
extern int g_fec_timeout; //8ms
extern int g_fec_mode;
extern int dynamic_update_fec;
/*end for first time init or dynamic update*/
struct anti_replay_t
{
@@ -98,11 +109,11 @@ class fec_encode_manager_t
private:
u32_t seq;
int type;
int fec_mode;
int fec_data_num,fec_redundant_num;
int fec_mtu;
int fec_pending_num;
int fec_pending_time;
int fec_queue_len;
int fec_timeout;
my_time_t first_packet_time;
my_time_t first_packet_time_for_output;
@@ -134,9 +145,14 @@ public:
return first_packet_time_for_output;
}
int get_pending_time()
{
return fec_timeout;
}
int get_type()
{
return type;
return fec_mode;
}
u64_t get_timer_fd64();
int re_init(int data_num,int redundant_num,int mtu,int pending_num,int pending_time,int type);

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

View File

@@ -51,6 +51,6 @@ void fec_encode(void *code, void *src[], void *dst, int index, int sz) ;
int fec_decode(void *code, void *pkt[], int index[], int sz) ;
int get_k(void *code);
int get_n(void *codev);
int get_n(void *code);
/* end of file */

205
main.cpp
View File

@@ -24,6 +24,8 @@ typedef int i32_t;
//int random_number_fd=-1;
char fifo_file[1000]="";
int mtu_warn=1350;
int disable_mtu_warn=1;
@@ -31,12 +33,6 @@ int disable_fec=0;
int debug_force_flush_fec=0;
int fec_data_num=20;
int fec_redundant_num=10;
int fec_mtu=1250;
int fec_pending_num=200;
int fec_pending_time=8*1000; //8ms
int fec_type=1;
int jitter_min=0*1000;
int jitter_max=0*1000;
@@ -196,9 +192,9 @@ int from_normal_to_fec(conn_info_t & conn_info,char *data,int len,int & out_n,ch
my_time_t tmp;
assert(first_packet_time!=0);
//mylog(log_info,"current_time=%llu first_packlet_time=%llu fec_pending_time=%llu\n",current_time,first_packet_time,(my_time_t)fec_pending_time);
if((my_time_t)fec_pending_time >=(current_time - first_packet_time))
if((my_time_t)conn_info.fec_encode_manager.get_pending_time() >=(current_time - first_packet_time))
{
tmp=(my_time_t)fec_pending_time-(current_time - first_packet_time);
tmp=(my_time_t)conn_info.fec_encode_manager.get_pending_time()-(current_time - first_packet_time);
//mylog(log_info,"tmp=%llu\n",tmp);
}
else
@@ -306,6 +302,84 @@ int from_fec_to_normal(conn_info_t & conn_info,char *data,int len,int & out_n,ch
//my_send(dest,data,len);
return 0;
}
int print_parameter()
{
mylog(log_info,"jitter_min=%d jitter_max=%d output_interval_min=%d output_interval_max=%d fec_timeout=%d fec_data_num=%d fec_redundant_num=%d fec_mtu=%d fec_queue_len=%d fec_mode=%d\n",
jitter_min/1000,jitter_max/1000,output_interval_min/1000,output_interval_max/1000,g_fec_timeout/1000,
g_fec_data_num,g_fec_redundant_num,g_fec_mtu,g_fec_queue_len,g_fec_mode);
return 0;
}
int handle_command(char *s)
{
int len=strlen(s);
while(len>=1&&s[len-1]=='\n')
s[len-1]=0;
mylog(log_info,"got data from fifo,len=%d,s=[%s]\n",len,s);
int a=-1,b=-1;
if(strncmp(s,"fec",strlen("fec"))==0)
{
mylog(log_info,"got command [fec]\n");
sscanf(s,"fec %d:%d",&a,&b);
if(a<1||b<0||a+b>254)
{
mylog(log_warn,"invaild value\n");
return -1;
}
g_fec_data_num=a;
g_fec_redundant_num=b;
}
else if(strncmp(s,"mtu",strlen("mtu"))==0)
{
mylog(log_info,"got command [mtu]\n");
sscanf(s,"mtu %d",&a);
if(a<100||a>2000)
{
mylog(log_warn,"invaild value\n");
return -1;
}
g_fec_mtu=a;
}
else if(strncmp(s,"queue-len",strlen("queue-len"))==0)
{
mylog(log_info,"got command [queue-len]\n");
sscanf(s,"queue-len %d",&a);
if(a<1||a>10000)
{
mylog(log_warn,"invaild value\n");
return -1;
}
g_fec_queue_len=a;
}
else if(strncmp(s,"mode",strlen("mode"))==0)
{
mylog(log_info,"got command [mode]\n");
sscanf(s,"mode %d",&a);
if(a!=0&&a!=1)
{
mylog(log_warn,"invaild value\n");
return -1;
}
g_fec_mode=a;
}
else if(strncmp(s,"timeout",strlen("timeout"))==0)
{
mylog(log_info,"got command [timeout]\n");
sscanf(s,"timeout %d",&a);
if(a<0||a>1000)
{
mylog(log_warn,"invaild value\n");
return -1;
}
g_fec_timeout=a*1000;
}
else
{
mylog(log_info,"unknown command\n");
}
print_parameter();
return 0;
}
int client_event_loop()
{
//char buf[buf_len];
@@ -318,7 +392,7 @@ int client_event_loop()
conn_info_t *conn_info_p=new conn_info_t;
conn_info_t &conn_info=*conn_info_p; //huge size of conn_info,do not allocate on stack
//conn_info.conv_manager.reserve();
conn_info.fec_encode_manager.re_init(fec_data_num,fec_redundant_num,fec_mtu,fec_pending_num,fec_pending_time,fec_type);
//conn_info.fec_encode_manager.re_init(fec_data_num,fec_redundant_num,fec_mtu,fec_pending_num,fec_pending_time,fec_type);
init_listen_socket();
@@ -383,6 +457,22 @@ int client_event_loop()
int fifo_fd=-1;
if(fifo_file[0]!=0)
{
fifo_fd=create_fifo(fifo_file);
ev.events = EPOLLIN;
ev.data.u64 = fifo_fd;
ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fifo_fd, &ev);
if (ret!= 0) {
mylog(log_fatal,"add fifo_fd to epoll error %s\n",strerror(errno));
myexit(-1);
}
mylog(log_info,"fifo_file=%s\n",fifo_file);
}
while(1)////////////////////////
{
if(about_to_exit) myexit(0);
@@ -425,6 +515,18 @@ int client_event_loop()
}
}
}
else if (events[idx].data.u64 == (u64_t)fifo_fd)
{
char buf[buf_len];
int len=read (fifo_fd, buf, sizeof (buf));
if(len<0)
{
mylog(log_warn,"fifo read failed len=%d,errno=%s\n",len,strerror(errno));
continue;
}
buf[len]=0;
handle_command(buf);
}
else if (events[idx].data.u64 == (u64_t)local_listen_fd||events[idx].data.u64 == conn_info.fec_encode_manager.get_timer_fd64())
{
char data[buf_len];
@@ -469,8 +571,10 @@ int client_event_loop()
socklen_t udp_new_addr_len = sizeof(sockaddr_in);
if ((data_len = recvfrom(local_listen_fd, data, max_data_len, 0,
(struct sockaddr *) &udp_new_addr_in, &udp_new_addr_len)) == -1) {
mylog(log_error,"recv_from error,this shouldnt happen at client\n");
myexit(1);
mylog(log_error,"recv_from error,this shouldnt happen,err=%s\n",strerror(errno));
continue;
//mylog(log_error,"recv_from error,this shouldnt happen at client\n");
//myexit(1);
};
if(!disable_mtu_warn&&data_len>=mtu_warn)
@@ -649,10 +753,24 @@ int server_event_loop()
timer.add_fd_to_epoll(epoll_fd);
timer.set_timer_repeat_us(timer_interval*1000);
mylog(log_debug," timer.get_timer_fd() =%d\n",timer.get_timer_fd());
int fifo_fd=-1;
if(fifo_file[0]!=0)
{
fifo_fd=create_fifo(fifo_file);
ev.events = EPOLLIN;
ev.data.u64 = fifo_fd;
ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fifo_fd, &ev);
if (ret!= 0) {
mylog(log_fatal,"add fifo_fd to epoll error %s\n",strerror(errno));
myexit(-1);
}
mylog(log_info,"fifo_file=%s\n",fifo_file);
}
while(1)////////////////////////
{
@@ -691,6 +809,20 @@ int server_event_loop()
mylog(log_trace,"events[idx].data.u64==(u64_t)timer.get_timer_fd()\n");
//conn_info.conv_manager.clear_inactive();
}
else if (events[idx].data.u64 == (u64_t)fifo_fd)
{
char buf[buf_len];
int len=read (fifo_fd, buf, sizeof (buf));
if(len<0)
{
mylog(log_warn,"fifo read failed len=%d,errno=%s\n",len,strerror(errno));
continue;
}
buf[len]=0;
handle_command(buf);
}
else if (events[idx].data.u64 == (u64_t)local_listen_fd)
{
@@ -702,8 +834,9 @@ int server_event_loop()
socklen_t udp_new_addr_len = sizeof(sockaddr_in);
if ((data_len = recvfrom(local_listen_fd, data, max_data_len, 0,
(struct sockaddr *) &udp_new_addr_in, &udp_new_addr_len)) == -1) {
mylog(log_error,"recv_from error,this shouldnt happen at client\n");
myexit(1);
mylog(log_error,"recv_from error,this shouldnt happen,err=%s\n",strerror(errno));
continue;
//myexit(1);
};
mylog(log_trace,"Received packet from %s:%d,len: %d\n", inet_ntoa(udp_new_addr_in.sin_addr),
ntohs(udp_new_addr_in.sin_port),data_len);
@@ -735,7 +868,7 @@ int server_event_loop()
conn_manager.insert(ip_port);
conn_info_t &conn_info=conn_manager.find(ip_port);
conn_info.fec_encode_manager.re_init(fec_data_num,fec_redundant_num,fec_mtu,fec_pending_num,fec_pending_time,fec_type);
//conn_info.fec_encode_manager.re_init(fec_data_num,fec_redundant_num,fec_mtu,fec_pending_num,fec_pending_time,fec_type);
//conn_info.conv_manager.reserve(); //already reserved in constructor
u64_t fec_fd64=conn_info.fec_encode_manager.get_timer_fd64();
@@ -1098,7 +1231,7 @@ int unit_test()
int * len;
fec_decode_manager.output(n,s_arr,len);
fec_encode_manager.re_init(3,2,fec_mtu,fec_pending_num,fec_pending_time,1);
fec_encode_manager.re_init(3,2,g_fec_mtu,g_fec_queue_len,g_fec_timeout,1);
fec_encode_manager.input((char *) a.c_str(), a.length());
fec_encode_manager.output(n,s_arr,len);
@@ -1187,6 +1320,9 @@ void print_help()
// printf(" --disable-xor <number> disable xor\n");
printf("developer options:\n");
printf(" --fifo <string> 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 <number> max fec queue len,only for mode 0\n");
@@ -1230,7 +1366,7 @@ void process_arg(int argc, char *argv[])
{"queue-len", required_argument, 0,'q'},
{"fec", required_argument, 0,'f'},
{"jitter", required_argument, 0,'j'},
{"fifo", required_argument, 0, 1},
{NULL, 0, 0, 0}
};
int option_index = 0;
@@ -1366,8 +1502,8 @@ void process_arg(int argc, char *argv[])
}
else
{
sscanf(optarg,"%d:%d\n",&fec_data_num,&fec_redundant_num);
if(fec_data_num<1 ||fec_redundant_num<0||fec_data_num+fec_redundant_num>254)
sscanf(optarg,"%d:%d\n",&g_fec_data_num,&g_fec_redundant_num);
if(g_fec_data_num<1 ||g_fec_redundant_num<0||g_fec_data_num+g_fec_redundant_num>254)
{
mylog(log_fatal,"fec_data_num<1 ||fec_redundant_num<0||fec_data_num+fec_redundant_num>254\n");
myexit(-1);
@@ -1375,8 +1511,8 @@ void process_arg(int argc, char *argv[])
}
break;
case 'q':
sscanf(optarg,"%d",&fec_pending_num);
if(fec_pending_num<1||fec_pending_num>10000)
sscanf(optarg,"%d",&g_fec_queue_len);
if(g_fec_queue_len<1||g_fec_queue_len>10000)
{
mylog(log_fatal,"fec_pending_num should be between 1 and 10000\n");
@@ -1515,8 +1651,8 @@ void process_arg(int argc, char *argv[])
}
else if(strcmp(long_options[option_index].name,"mode")==0)
{
sscanf(optarg,"%d",&fec_type);
if(fec_type!=0&&fec_type!=1)
sscanf(optarg,"%d",&g_fec_mode);
if(g_fec_mode!=0&&g_fec_mode!=1)
{
mylog(log_fatal,"mode should be 0 or 1\n");
myexit(-1);
@@ -1524,8 +1660,8 @@ void process_arg(int argc, char *argv[])
}
else if(strcmp(long_options[option_index].name,"mtu")==0)
{
sscanf(optarg,"%d",&fec_mtu);
if(fec_mtu<100||fec_mtu>2000)
sscanf(optarg,"%d",&g_fec_mtu);
if(g_fec_mtu<100||g_fec_mtu>2000)
{
mylog(log_fatal,"fec_mtu should be between 100 and 2000\n");
myexit(-1);
@@ -1533,14 +1669,20 @@ void process_arg(int argc, char *argv[])
}
else if(strcmp(long_options[option_index].name,"timeout")==0)
{
sscanf(optarg,"%d",&fec_pending_time);
if(fec_pending_time<0||fec_pending_time>1000)
sscanf(optarg,"%d",&g_fec_timeout);
if(g_fec_timeout<0||g_fec_timeout>1000)
{
mylog(log_fatal,"fec_pending_time should be between 0 and 1000(1s)\n");
myexit(-1);
}
fec_pending_time*=1000;
g_fec_timeout*=1000;
}
else if(strcmp(long_options[option_index].name,"fifo")==0)
{
sscanf(optarg,"%s",fifo_file);
mylog(log_info,"fifo_file =%s \n",fifo_file);
}
else
{
@@ -1579,9 +1721,8 @@ void process_arg(int argc, char *argv[])
program_mode=server_mode;
}
mylog(log_info,"jitter_min=%d jitter_max=%d output_interval_min=%d output_interval_max=%d fec_pending_time=%d fec_data_num=%d fec_redundant_num=%d fec_mtu=%d fec_pending_num=%d fec_type=%d\n",
jitter_min/1000,jitter_max/1000,output_interval_min/1000,output_interval_max/1000,fec_pending_time/1000,
fec_data_num,fec_redundant_num,fec_mtu,fec_pending_num,fec_type);
print_parameter();
}
int main(int argc, char *argv[])