forked from RevenantX/LiteNetLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHolePunchServerTest.cs
More file actions
203 lines (173 loc) · 7.02 KB
/
HolePunchServerTest.cs
File metadata and controls
203 lines (173 loc) · 7.02 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using LiteNetLib;
namespace LibSample
{
class WaitPeer
{
public IPEndPoint InternalAddr { get; }
public IPEndPoint ExternalAddr { get; }
public DateTime RefreshTime { get; private set; }
public void Refresh()
{
RefreshTime = DateTime.UtcNow;
}
public WaitPeer(IPEndPoint internalAddr, IPEndPoint externalAddr)
{
Refresh();
InternalAddr = internalAddr;
ExternalAddr = externalAddr;
}
}
class HolePunchServerTest : INatPunchListener
{
private const int ServerPort = 50010;
private const string ConnectionKey = "test_key";
private static readonly TimeSpan KickTime = new TimeSpan(0, 0, 6);
private readonly Dictionary<string, WaitPeer> _waitingPeers = new Dictionary<string, WaitPeer>();
private readonly List<string> _peersToRemove = new List<string>();
private NetManager _puncher;
private NetManager _c1;
private NetManager _c2;
void INatPunchListener.OnNatIntroductionRequest(IPEndPoint localEndPoint, IPEndPoint remoteEndPoint, string token)
{
if (_waitingPeers.TryGetValue(token, out var wpeer))
{
if (wpeer.InternalAddr.Equals(localEndPoint) &&
wpeer.ExternalAddr.Equals(remoteEndPoint))
{
wpeer.Refresh();
return;
}
Console.WriteLine("Wait peer found, sending introduction...");
//found in list - introduce client and host to eachother
Console.WriteLine(
"host - i({0}) e({1})\nclient - i({2}) e({3})",
wpeer.InternalAddr,
wpeer.ExternalAddr,
localEndPoint,
remoteEndPoint);
_puncher.NatPunchModule.NatIntroduce(
wpeer.InternalAddr, // host internal
wpeer.ExternalAddr, // host external
localEndPoint, // client internal
remoteEndPoint, // client external
token // request token
);
//Clear dictionary
_waitingPeers.Remove(token);
}
else
{
Console.WriteLine("Wait peer created. i({0}) e({1})", localEndPoint, remoteEndPoint);
_waitingPeers[token] = new WaitPeer(localEndPoint, remoteEndPoint);
}
}
void INatPunchListener.OnNatIntroductionSuccess(IPEndPoint targetEndPoint, NatAddressType type, string token)
{
//Ignore we are server
}
public void Run()
{
Console.WriteLine("=== HolePunch Test ===");
EventBasedNetListener clientListener = new EventBasedNetListener();
EventBasedNatPunchListener natPunchListener1 = new EventBasedNatPunchListener();
EventBasedNatPunchListener natPunchListener2 = new EventBasedNatPunchListener();
clientListener.PeerConnectedEvent += peer =>
{
Console.WriteLine("PeerConnected: " + peer.EndPoint);
};
clientListener.ConnectionRequestEvent += request =>
{
request.AcceptIfKey(ConnectionKey);
};
clientListener.PeerDisconnectedEvent += (peer, disconnectInfo) =>
{
Console.WriteLine("PeerDisconnected: " + disconnectInfo.Reason);
if (disconnectInfo.AdditionalData.AvailableBytes > 0)
{
Console.WriteLine("Disconnect data: " + disconnectInfo.AdditionalData.GetInt());
}
};
natPunchListener1.NatIntroductionSuccess += (point, addrType, token) =>
{
var peer = _c1.Connect(point, ConnectionKey);
Console.WriteLine($"NatIntroductionSuccess C1. Connecting to C2: {point}, type: {addrType}, connection created: {peer != null}");
};
natPunchListener2.NatIntroductionSuccess += (point, addrType, token) =>
{
var peer = _c2.Connect(point, ConnectionKey);
Console.WriteLine($"NatIntroductionSuccess C2. Connecting to C1: {point}, type: {addrType}, connection created: {peer != null}");
};
_c1 = new NetManager(clientListener)
{
IPv6Enabled = IPv6Mode.DualMode,
NatPunchEnabled = true
};
_c1.NatPunchModule.Init(natPunchListener1);
_c1.Start();
_c2 = new NetManager(clientListener)
{
IPv6Enabled = IPv6Mode.DualMode,
NatPunchEnabled = true
};
_c2.NatPunchModule.Init(natPunchListener2);
_c2.Start();
_puncher = new NetManager(clientListener)
{
IPv6Enabled = IPv6Mode.DualMode,
NatPunchEnabled = true
};
_puncher.Start(ServerPort);
_puncher.NatPunchModule.Init(this);
_c1.NatPunchModule.SendNatIntroduceRequest("localhost", ServerPort, "token1");
_c2.NatPunchModule.SendNatIntroduceRequest("localhost", ServerPort, "token1");
// keep going until ESCAPE is pressed
Console.WriteLine("Press ESC to quit");
while (true)
{
if (Console.KeyAvailable)
{
var key = Console.ReadKey(true).Key;
if (key == ConsoleKey.Escape)
{
break;
}
if (key == ConsoleKey.A)
{
Console.WriteLine("C1 stopped");
_c1.DisconnectPeer(_c1.FirstPeer, new byte[] {1,2,3,4});
_c1.Stop();
}
}
DateTime nowTime = DateTime.UtcNow;
_c1.NatPunchModule.PollEvents();
_c2.NatPunchModule.PollEvents();
_puncher.NatPunchModule.PollEvents();
_c1.PollEvents();
_c2.PollEvents();
//check old peers
foreach (var waitPeer in _waitingPeers)
{
if (nowTime - waitPeer.Value.RefreshTime > KickTime)
{
_peersToRemove.Add(waitPeer.Key);
}
}
//remove
for (int i = 0; i < _peersToRemove.Count; i++)
{
Console.WriteLine("Kicking peer: " + _peersToRemove[i]);
_waitingPeers.Remove(_peersToRemove[i]);
}
_peersToRemove.Clear();
Thread.Sleep(10);
}
_c1.Stop();
_c2.Stop();
_puncher.Stop();
}
}
}