-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathKcpRttExampleClient.cs
179 lines (143 loc) · 4.8 KB
/
KcpRttExampleClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Linq;
using System.Net;
using System.Timers;
using base_kcp;
using DotNetty.Buffers;
using dotNetty_kcp;
using fec;
namespace example_Kcp
{
public class KcpRttExampleClient : KcpListener
{
public static int fastResend = 0;
private static Ukcp _ukcp;
public static void start()
{
ChannelConfig channelConfig = new ChannelConfig();
channelConfig.initNodelay(true,40,2,true);
channelConfig.Sndwnd=512;
channelConfig.Rcvwnd=512;
channelConfig.Mtu=512;
channelConfig.FecDataShardCount=3;
channelConfig.FecParityShardCount=1;
channelConfig.AckNoDelay=true;
channelConfig.Crc32Check = true;
channelConfig.Conv = 55;
//channelConfig.setTimeoutMillis(10000);
KcpClient kcpClient = new KcpClient();
kcpClient.init(channelConfig);
KcpRttExampleClient kcpClientRttExample = new KcpRttExampleClient();
//kcpClient.connect(new InetSocketAddress("127.0.0.1",20003),channelConfig,kcpClientRttExample);
EndPoint remoteAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"),20003);
_ukcp = kcpClient.connect(remoteAddress,channelConfig,kcpClientRttExample);
try
{
kcpClientRttExample.init();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
private IByteBuffer data;
private int[] rtts;
private volatile int count;
private long startTime;
Timer timer20 = new Timer();
ElapsedEventHandler sendhandler;
private ElapsedEventHandler overHandler;
public void init()
{
sendhandler = new ElapsedEventHandler(sendFunc);
overHandler = new ElapsedEventHandler(overPrint);
timer20.Elapsed += sendhandler;
}
public KcpRttExampleClient()
{
data = Unpooled.DirectBuffer(200);
for (int i = 0; i < data.Capacity; i++)
{
data.WriteByte((byte) i);
}
rtts = new int[300];
for (int i = 0; i < rtts.Length; i++)
{
rtts[i] = -1;
}
timer20.Enabled = true;
timer20.Interval = 20;
timer20.Start();
startTime = KcpUntils.currentMs();
}
private void overPrint(object source, ElapsedEventArgs e)
{
var sum = rtts.Sum();
var max = rtts.Max();
Console.WriteLine("average: " + (sum / rtts.Length)+" max:"+max);
Console.WriteLine(Snmp.snmp.ToString());
timer20.Elapsed -= overHandler;
}
private void sendFunc(object source, ElapsedEventArgs e)
{
var byteBuf = rttMsg(++count);
_ukcp.write(byteBuf);
byteBuf.Release();
if (count >= rtts.Length) {
// finish
timer20.Elapsed -= sendhandler;
byteBuf = rttMsg(-1);
_ukcp.write(byteBuf);
byteBuf.Release();
}
}
/**
* count+timestamp+dataLen+data
*
* @param count
* @return
*/
public IByteBuffer rttMsg(int count) {
IByteBuffer buf = Unpooled.DirectBuffer(10);
buf.WriteShort(count);
buf.WriteInt((int) (KcpUntils.currentMs()- startTime));
//int dataLen = new Random().nextInt(200);
//buf.writeBytes(new byte[dataLen]);
int dataLen = data.ReadableBytes;
buf.WriteShort(dataLen);
buf.WriteBytes(data, data.ReaderIndex, dataLen);
return buf;
}
public void onConnected(Ukcp ukcp)
{
}
public void handleReceive(IByteBuffer byteBuf, Ukcp ukcp)
{
int curCount = byteBuf.ReadShort();
if (curCount == -1)
{
timer20.Elapsed += overHandler;
}
else
{
int idx = curCount - 1;
long time = byteBuf.ReadInt();
if (rtts[idx] != -1)
{
Console.WriteLine("end");
}
//log.info("rcv count {} {}", curCount, System.currentTimeMillis());
rtts[idx] = (int) (KcpUntils.currentMs() - startTime - time);
Console.WriteLine("rtt : " + curCount + " " + rtts[idx]);
}
}
public void handleException(Exception ex, Ukcp ukcp)
{
Console.WriteLine(ex);
}
public void handleClose(Ukcp ukcp)
{
}
}
}