98 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
wangyu-
39f2ead0af Merge branch 'master' of https://github.com/wangyu-/UDPspeeder 2017-10-21 11:09:51 -05:00
wangyu-
77b908a663 fix -q option 2017-10-21 10:59:49 -05:00
wangyu-
6d8904d144 Update README.zh-cn.md 2017-10-21 06:49:52 -07:00
wangyu-
be9d0270d1 Update README.zh-cn.md 2017-10-21 06:48:41 -07:00
wangyu-
a5612072ea Update README.zh-cn.md 2017-10-20 23:48:02 -07:00
wangyu-
aec423012c Update README.zh-cn.md 2017-10-20 23:45:27 -07:00
wangyu-
a8120dde23 Update README.zh-cn.md 2017-10-20 23:41:37 -07:00
wangyu-
0009531a6f Update README.zh-cn.md 2017-10-20 23:40:29 -07:00
wangyu-
06510d2e21 Update README.md 2017-10-20 22:15:44 -07:00
wangyu-
c4a84b5d66 Update README.md 2017-10-20 21:52:09 -07:00
wangyu-
1f366d56f8 Update README.md 2017-10-20 21:47:26 -07:00
wangyu-
43c6afbd16 Merge pull request #32 from heart4lor/patch-1
Update main.cpp
2017-10-20 22:42:14 -05:00
Sun Yongfei
5d6d904ff7 Update main.cpp 2017-10-21 11:27:39 +08:00
wangyu-
bbf202a438 Update README.zh-cn.md 2017-10-20 20:04:40 -07:00
wangyu-
27801fe6c0 Add files via upload 2017-10-20 06:34:36 -05:00
wangyu-
de173447ba Update README.zh-cn.md 2017-10-20 04:20:27 -07:00
wangyu-
cb352c911c Update README.zh-cn.md 2017-10-20 04:19:29 -07:00
wangyu-
0351bd0e59 Update README.zh-cn.md 2017-10-20 01:57:38 -07:00
wangyu-
15f77da7d9 Update README.zh-cn.md 2017-10-20 01:47:25 -07:00
wangyu-
bc92925b30 Update README.zh-cn.md 2017-10-20 01:46:30 -07:00
wangyu-
d9b8664709 Update README.zh-cn.md 2017-10-20 01:35:55 -07:00
wangyu-
e5e7c302d6 Update README.zh-cn.md 2017-10-20 00:49:24 -07:00
wangyu-
dc5c7c4e05 Update README.zh-cn.md 2017-10-20 00:47:04 -07:00
wangyu-
3db1b7c068 Update README.zh-cn.md 2017-10-19 23:35:23 -07:00
wangyu-
a0d23221a6 Update README.zh-cn.md 2017-10-19 23:17:09 -07:00
wangyu-
7dbd5b2575 Update README.zh-cn.md 2017-10-19 23:16:22 -07:00
wangyu-
a48e9a08d9 Update README.zh-cn.md 2017-10-19 23:11:34 -07:00
wangyu-
919a6022d0 Add files via upload 2017-10-20 01:08:28 -05:00
wangyu-
e158c3922e Update README.zh-cn.md 2017-10-19 21:54:41 -07:00
wangyu-
3ad201ba56 Update README.zh-cn.md 2017-10-19 21:52:37 -07:00
wangyu-
7adf9d7cde Update README.zh-cn.md 2017-10-19 21:32:58 -07:00
wangyu-
2d08c6f7ec Add files via upload 2017-10-19 23:32:19 -05:00
wangyu-
8b2957818a Update README.zh-cn.md 2017-10-19 21:30:06 -07:00
wangyu-
55516c16d5 Update README.zh-cn.md 2017-10-19 21:28:25 -07:00
wangyu-
2e52b163ec Add files via upload 2017-10-19 23:24:45 -05:00
wangyu-
34523fee11 Update README.md 2017-10-19 21:03:27 -07:00
wangyu-
e8fe62300d Update README.zh-cn.v1.md 2017-10-19 20:56:28 -07:00
wangyu-
dbed5c3e6b Add files via upload 2017-10-19 22:53:31 -05:00
wangyu-
5228a3c44d Update README.zh-cn.v1.md 2017-10-19 20:46:24 -07:00
wangyu-
0d4c59552f Update README.zh-cn.v1.md 2017-10-19 20:45:24 -07:00
wangyu-
43f8bb2367 Update README.zh-cn.v1.md 2017-10-19 20:42:35 -07:00
wangyu-
60aa420a99 Update README.zh-cn.md 2017-10-19 20:38:08 -07:00
wangyu-
9bd3707301 Update README.zh-cn.md 2017-10-19 20:31:02 -07:00
wangyu-
f99a84a5a0 Add files via upload 2017-10-19 22:25:40 -05:00
wangyu-
87972b8801 Create 11 2017-10-19 20:25:06 -07:00
wangyu-
adb3a8890f Update README.md 2017-10-19 20:17:26 -07:00
wangyu-
2b0e64d1ae just commit 2017-10-19 22:15:52 -05:00
wangyu-
d407af5bd1 just commit 2017-10-19 22:14:39 -05:00
wangyu-
df34c723bc Update README.md 2017-10-19 11:48:12 -07:00
wangyu-
f83075390b Add files via upload 2017-10-19 13:47:52 -05:00
wangyu-
bb88b2e6b6 Add files via upload 2017-10-19 13:44:34 -05:00
20 changed files with 631 additions and 93 deletions

View File

@@ -1,21 +1,26 @@
# UDPspeeder
Network Speed-Up Tool. Boost your Connection on a High Lantency High Packet-Loss Link by using Forward Error Correction.
When used alone,UDPspeeder speeds-up only udp connection.Nevertheless,if you used UDPspeeder + any UDP-based VPN together,you can speed-up any traffic(include tcp/udp/icmp)
When used alone,UDPspeeder speeds-up only UDP connection.Nevertheless,if you used UDPspeeder + any UDP-based VPN together,you can speed-up any traffic(include TCP/UDP/ICMP),currently OpenVPN/L2TP/ShadowVPN are confirmed to be supported
![](/images/en/udpspeeder.PNG)
or
![image_vpn](/images/en/udpspeeder+openvpn.PNG)
![image_vpn](/images/en/udpspeeder+openvpn3.PNG)
[简体中文](/doc/README.zh-cn.md)(内容更丰富)
###### Note
You can use udp2raw with UDPspeeder together to bypass UDP firewalls.
udp2rawhttps://github.com/wangyu-/udp2raw-tunnel
[简体中文](/doc/README.md)
# 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,9 @@ 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)
### Reed-Solomon
@@ -53,10 +60,10 @@ Assume your server ip is 44.55.66.77, you have a service listening on udp port 7
```bash
# Run at server side:
./speederv2 -s -l0.0.0.0:4096 -r 127.0.0.1:7777 -f20:10
./speederv2 -s -l0.0.0.0:4096 -r 127.0.0.1:7777 -f20:10 -k "passwd"
# Run at client side
./speederv2 -c -l0.0.0.0:3333 -r44.55.66.77:4096 -f20:10
./speederv2 -c -l0.0.0.0:3333 -r44.55.66.77:4096 -f20:10 -k "passwd"
```
Now connecting to UDP port 3333 at the client side is equivalent to connecting to port 7777 at the server side,and the connection is boosted by UDPspeeder.
@@ -65,11 +72,15 @@ 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:
@@ -80,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.
@@ -94,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
@@ -103,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);
*/

275
doc/README.zh-cn.md Normal file
View File

@@ -0,0 +1,275 @@
# UDPspeeder
![image0](/images/cn/speedercn.PNG)
双边网络加速工具软件本身的功能是加速UDP不过配合vpn可以加速全流量(包括TCP/UDP/ICMP)。通过合理配置可以加速游戏降低游戏的丢包和延迟也可以加速下载和看视频这种大流量的应用。用1.5倍的流量就可以把10%的丢包率降低到万分之一以下。跟 kcptun/finalspeed/BBR 等现有方案比,主要优势是可以加速 UDP 和 ICMP现有方案几乎都只能加速 TCP。
我自己稳定用了几个月用来加速美服的Brawl Stars和亚服的Mobile Legend效果不错加速前卡得几乎没法玩加速后就没怎么卡过了。用来看视频也基本满速。
最新的版本是v2版在v1版的基础上增加了FEC功能更省流量。如果你用的是v1版路由器固件里自带的集成版很可能是v1版的请看[v1版主页](/doc/README.zh-cn.v1.md)
配合vpn加速全流量的原理图(已测试支持VPN的有OpenVPN、L2TP、$\*\*\*VPN):
![image0](/images/Capture2.PNG)
另外可以和udp2raw串联使用在加速的同时把UDP伪装成TCP防止UDP被运营商QOS或屏蔽。udp2raw: https://github.com/wangyu-/udp2raw-tunnel
#### 效果
![image0](/images/Capture8.PNG)
![image0](/images/cn/scp_compare.PNG)
#### 原理简介
主要原理是通过冗余数据来对抗网络的丢包发送冗余数据的方式支持FEC(Forward Error Correction)和多倍发包,其中FEC算法是Reed-Solomon。
FEC方式的原理图:
![image0](/images/en/fec.PNG)
#### 其他功能
对包的内容和长度做随机化可以理解为混淆从抓包看不出你发送了冗余数据不用担心vps被封。
在多个冗余包之间引入延迟时间可配来对抗突发性的丢包避开中间路由器因为瞬时buffer长度过长而连续丢掉所有副本。
模拟一定的延迟抖动(时间可配),这样上层应用计算出来的RTT方差会更大以等待后续冗余包的到达不至于发生在冗余包到达之前就触发重传的尴尬。
输出UDP收发情况报告可以看出丢包率。
模拟丢包模拟延迟模拟jitter。便于通过实验找出应用卡顿的原因。
client支持多个udp连接server也支持多个client
# 简明操作说明
### 环境要求
Linux主机可以是桌面版可以是android手机/平板可以是openwrt路由器也可以是树莓派。在windows和mac上配合虚拟机可以稳定使用speeder跑在Linux里其他应用照常跑在window里桥接模式测试可用可以使用[这个](https://github.com/wangyu-/udp2raw-tunnel/releases/download/20170918.0/lede-17.01.2-x86_virtual_machine_image_with_udp2raw_pre_installed.zip)虚拟机镜像大小只有7.5mb,免去在虚拟机里装系统的麻烦。
android版需要通过terminal运行。
###### 注意
在使用虚拟机时,建议手动指定桥接到哪个网卡,不要设置成自动。否则可能会桥接到错误的网卡。
### 安装
下载编译好的二进制文件,解压到本地和服务器的任意目录。
https://github.com/wangyu-/UDPspeeder/releases
### 运行
假设你有一个serverip为44.55.66.77有一个服务监听在udp 7777端口。 假设你需要加速本地到44.55.66.77:7777的流量。
```
在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流量会被加速。
###### 备注:
-f20:10 表示对每20个原始数据发送10个冗余包。
-k 指定一个字符串server/client间所有收发的包都会被异或改变协议特征防止UDPspeeder的协议被运营商针对。
###### 注意
要为UDPspeeder加速的应用设置好MTU(不是在UDPspeeder中是在被加速的应用中)建议设置为1200。
另外如果被加速的应用不能调整MTU也可以在UDPspeeder中通过`--mode 0 --mtu 1200`设置MTU。关于`--mode 0``--mtu`的具体含义请看下文。
# 进阶操作说明
### 命令选项
```
UDPspeeder V2
git version:8e7a8aed92 build date:Oct 25 2017 02:00:54
repository: https://github.com/wangyu-/UDPspeeder
usage:
run as client : ./this_program -c -l local_listen_ip:local_port -r server_ip:server_port [options]
run as server : ./this_program -s -l server_listen_ip:server_port -r remote_ip:remote_port [options]
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,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
-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.
default value:0.do not use if you dont know what it means.
--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
--decode-buf <number> size of buffer of fec decoder,unit:packet,default:2000
--fix-latency <number> try to stabilize latency,only for mode 0
--delay-capacity <number> max number of delayed packets
--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
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
```
### 包发送选项,两端设置可以不同。 只影响本地包发送。
##### `-f` 选项
设置fec参数影响数据的冗余度。
##### `--timeout` 选项
指定fec编码器在编码时候最多可以引入多大的延迟。越高fec越有效率加速游戏时调低可以降低延迟。
##### `--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选项决定切分的片段的长度mode 1模式中--mtu选项只起检查作用如果超过了--mtu指定的值数据包会被丢弃。
mode 0模式的流量消耗基本完全透明。mode 1因为涉及到数据按最大长度对齐所以流量消耗不是完全可预期。不过就实际使用来看mode 1消耗的额外流量不多。
mode 0模式数据包一般不会乱序除非网络本身有严重乱序mode 1模式被恢复的数据包可能会乱序不过UDP本来就允许乱序对绝大多数应用没有影响。mode 0模式反而可以纠正一些乱序情况。
##### `--report`  选项
数据发送和接受报告。开启后可以根据此数据推测出包速和丢包率等特征。
##### `-i` 选项
指定一个时间窗口长度为n毫秒。同一个fec分组的数据在发送时候会被均匀分散到这n毫秒中。可以对抗突发性的丢包。默认值是0因为这个功能需要用到时钟在某些虚拟机里时钟不稳定可能会导致个别包出现非常大的延迟所以默认关掉了。这个功能很有用默认参数效果不理想时可以尝试打开。
##### `-j` 选项
为原始数据的发送增加一个延迟抖动值。这样上层应用计算出来的RTT方差会更大以等待后续冗余包的到达不至于发生在冗余包到达之前就触发重传的尴尬。配合-t选项使用。正常情况下跨国网络本身的延迟抖动就很大可以不用设-j。这个功能也需要时钟默认关掉了不过一般情况应该不需要这个功能。
##### `--random-drop` 选项
随机丢包。模拟恶劣的网络环境时使用。如果你的网络现在没有多大丢包但是你想测试一下高丢包环境下各种FEC参数的表现可以开这个选项。
##### `-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还差。
对于游戏以外的应用推荐使用FEC。但是如果FEC版的默认参数在你那边效果很差而你又不会调可以先用多倍发包。
### V2版如何多倍发包
只要在设置-f参数时把x设置为1fec算法就退化为多倍发包了。例如-f1:1,表示2倍发包-f1:2表示3倍发包以此类推。另外可以加上`--mode 0 -q1`参数防止fec编码器试图积攒和合并数据获得最低的延迟。
2倍发包的完整参数
```
./speederv2 -s -l0.0.0.0:4096 -r127.0.0.1:7777 -f1:1 -k "passwd" --mode 0 -q1
./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参数
默认的FEC参数为-f20:10对每20个包额外发送10个冗余包也就是1.5倍发包。已经可以适应绝大多数的网络情况了对于10%的网络丢包可以降低到0.01%以下对于20%的网络丢包可以降低到2.5%。
如果你的网络丢包很低比如在3%以下,可以尝试调低参数。比如-f20:5也就是1.2倍发包这个参数已经足够把3%的丢包降低到0.01%以下了。
如果网络丢包超过20%,需要把-f20:10调大。
如果你实在不会配那么也可以用回V1版。
### 根据CPU处理能力来调整FEC参数
FEC算法很吃CPU,初次使用建议关注UDPspeeder的CPU占用。如果CPU被打满可以在冗余度不变的情况下把FEC分组大小调小否则的话效果可能很差。
比如-f20:10和-f10:5都是1.5倍的冗余度,而-f20:10的FEC分组大小是30个包-f10:5的FEC分组大小是15个包。-f20:10更费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加速任何流量
![image0](/images/Capture2.PNG)
具体配置见[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和$\*\*\*方案间快速切换
实际部署起来比图中看起来的还要简单不需要改路由表不需要写iptables规则和开启NAT需要做的只是用openvpn分配的ip访问$*** server
![image0](/images/cn/speeder_vpn_s.PNG)
(也可以把图中的$*** server换成其他的socks5 server这样就不需要$*** client了)
可以和BBR/锐速叠加BBR/锐速只要部署在VPS上就有效
# 编译教程
暂时先参考udp2raw的这篇教程几乎一样的过程
https://github.com/wangyu-/udp2raw-tunnel/blob/master/doc/build_guide.zh-cn.md

View File

@@ -1,12 +1,15 @@
# UDPspeeder
# UDPspeeder (v1)
![image0](/images/Capture7.PNG)
UDP双边加速工具降低丢包率配合vpn可以加速任何协议尤其适用于加速游戏和网页打开速度同时也是一个UDP连接的调试和统计工具。
这个是我自己稳定用了一个月的项目用来加速美服的Brawl Stars和亚服的Mobile Legend效果不错。加速前卡得几乎没法玩加速后就没怎么卡过了。
目前最新版是v2版这个是v1版的主页
#### 效果
![image0](/images/Capture8.PNG)
#### 原理简介
目前原理是多倍发包。以后会做各种优化比如对高频率的短包先合并再冗余FECForward Error Correction在包速低的时候多倍发包包速高时用FEC。
目前原理是多倍发包。以后会做各种优化比如对高频率的短包先合并再冗余FECForward Error Correction在包速低的时候多倍发包包速高时用FEC(这些功能在v2版里已经实现)
跟net-speeder比优势在于client和server会把收到的多余包自动去掉这个过程对上层透明没有兼容性问题。而且发出的冗余数据包会做长度和内容的随机化抓包是看不出发了冗余数据的所以不用担心vps被封的问题。
@@ -135,16 +138,16 @@ kcptun在udp层有RS code也是一种冗余传输通过openvpn把流量转
udp协议本身是ip协议加上了端口之后的直接封装udp继承了ip协议的实时/乱序到达特性更适合中转vpn。
#### UDPspeeder + kcptun/finalspeed + ss 同时加速tcp和udp流量
#### UDPspeeder + kcptun/finalspeed + $*** 同时加速tcp和udp流量
如果你需要用加速的tcp看视频和下载文件这样效果比vpn方案更好。不论是速度还是流量的耗费上。
![image0](/images/Capture3.PNG)
![image0](/images/cn/speeder_kcptun.PNG)
#### UDPspeeder + openvpn + ss 混合方案
也是我正在用的方案。优点是可以随时在vpn和ss方案间快速切换。
实际部署起来比图中看起来的还要简单。不需要改路由表需要做的只是用openvpn的ip访问ss server。
#### UDPspeeder + openvpn + $*** 混合方案
也是我正在用的方案。优点是可以随时在vpn和$*** 方案间快速切换。
实际部署起来比图中看起来的还要简单。不需要改路由表需要做的只是用openvpn的ip访问$*** server。
![image0](/images/Capture10.PNG)
(也可以把图中的ss server换成其他的socks5 server这样连ss client也不需要了)
![image0](/images/cn/speeder_vpn_s.PNG)
(也可以把图中的$*** server换成其他的socks5 server这样连$*** client也不需要了)
# 编译教程
暂时先参考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);

1
images/cn/11 Normal file
View File

@@ -0,0 +1 @@

BIN
images/cn/scp_compare.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

BIN
images/cn/scp_compare2.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

BIN
images/cn/speeder_vpn_s.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

BIN
images/cn/speedercn.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

BIN
images/en/fec.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

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 */

210
main.cpp
View File

@@ -8,7 +8,7 @@
#include "delay_manager.h"
#include "fec_manager.h"
using namespace std;
using namespace std;
typedef unsigned long long u64_t; //this works on most platform,avoid using the PRId64
typedef long long i64_t;
@@ -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;
@@ -1292,7 +1428,7 @@ void process_arg(int argc, char *argv[])
}
int no_l = 1, no_r = 1;
while ((opt = getopt_long(argc, argv, "l:r:hcsk:j:f:p:n:i:",long_options,&option_index)) != -1)
while ((opt = getopt_long(argc, argv, "l:r:hcsk:j:f:p:n:i:q:",long_options,&option_index)) != -1)
{
//string opt_key;
//opt_key+=opt;
@@ -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,13 +1511,14 @@ 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");
myexit(-1);
}
break;
case 'c':
is_client = 1;
break;
@@ -1514,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);
@@ -1523,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);
@@ -1532,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
{
@@ -1578,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_num=%d fec_data_num=%d fec_redundant_num=%d fec_mtu=%d fec_pending_time=%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[])