-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandler.cs
More file actions
184 lines (166 loc) · 5.72 KB
/
Copy pathHandler.cs
File metadata and controls
184 lines (166 loc) · 5.72 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
using System;
using RakNet.Messages;
namespace RakNet;
/// <summary>
/// Connection handler for the client (dialer) side. Handles RakNet-internal
/// packets received from the server, including the connection request accepted
/// handshake, connected pings/pongs, and disconnect notifications.
/// </summary>
public class DialerConnectionHandler : IConnectionHandler
{
private readonly Action<string>? _errorLog;
public DialerConnectionHandler(Action<string>? errorLog = null)
{
_errorLog = errorLog;
}
public bool LimitsEnabled() => false;
public void Close(Conn conn)
{
try { conn.RemoteEndPoint.ToString(); } catch { }
}
public (bool handled, Exception? error) Handle(Conn conn, byte[] b)
{
if (b.Length == 0) return (false, null);
switch (b[0])
{
case Id.ConnectionRequest:
return (true, new Exception("unexpected CONNECTION_REQUEST packet"));
case Id.ConnectionRequestAccepted:
return (true, HandleConnectionRequestAccepted(conn, b.AsSpan(1)));
case Id.NewIncomingConnection:
return (true, new Exception("unexpected NEW_INCOMING_CONNECTION packet"));
case Id.ConnectedPing:
return (true, HandleConnectedPing(conn, b.AsSpan(1)));
case Id.ConnectedPong:
return (true, HandleConnectedPong(b.AsSpan(1)));
case Id.DisconnectNotification:
conn.CloseImmediately();
return (true, null);
case Id.DetectLostConnections:
return (true, conn.SendInternal(new ConnectedPing { PingTime = Conn.Timestamp() }));
default:
return (false, null);
}
}
private Exception? HandleConnectionRequestAccepted(Conn conn, ReadOnlySpan<byte> b)
{
var pk = new ConnectionRequestAccepted();
try
{
pk.Unmarshal(b);
}
catch (Exception ex)
{
return new Exception("read CONNECTION_REQUEST_ACCEPTED", ex);
}
if (conn.IsConnected)
return new Exception("unexpected additional CONNECTION_REQUEST_ACCEPTED packet");
var err = conn.SendInternal(new NewIncomingConnection
{
ServerAddress = conn.RemoteEndPoint,
PingTime = pk.PongTime,
PongTime = Conn.Timestamp()
});
conn.SignalConnected();
return err;
}
internal static Exception? HandleConnectedPing(Conn conn, ReadOnlySpan<byte> b)
{
var pk = new ConnectedPing();
try
{
pk.Unmarshal(b);
}
catch (Exception ex)
{
return new Exception("read CONNECTED_PING", ex);
}
return conn.SendUnreliable(new ConnectedPong
{
PingTime = pk.PingTime,
PongTime = Conn.Timestamp()
});
}
internal static Exception? HandleConnectedPong(ReadOnlySpan<byte> b)
{
var pk = new ConnectedPong();
try
{
pk.Unmarshal(b);
}
catch (Exception ex)
{
return new Exception("read CONNECTED_PONG", ex);
}
if (pk.PingTime > Conn.Timestamp())
return new Exception("handle CONNECTED_PONG: timestamp is in the future");
return null;
}
}
/// <summary>
/// Connection handler for the server (listener) side. Handles RakNet-internal
/// packets from clients, including connection requests and the new incoming
/// connection handshake.
/// </summary>
public class ListenerConnectionHandler : IConnectionHandler
{
private readonly Listener _listener;
public ListenerConnectionHandler(Listener listener)
{
_listener = listener;
}
public bool LimitsEnabled() => true;
public void Close(Conn conn)
{
_listener.RemoveConnection(conn.RemoteEndPoint);
}
public (bool handled, Exception? error) Handle(Conn conn, byte[] b)
{
if (b.Length == 0) return (false, null);
switch (b[0])
{
case Id.ConnectionRequest:
return (true, HandleConnectionRequest(conn, b.AsSpan(1)));
case Id.ConnectionRequestAccepted:
return (true, new Exception("unexpected CONNECTION_REQUEST_ACCEPTED packet"));
case Id.NewIncomingConnection:
return (true, HandleNewIncomingConnection(conn));
case Id.ConnectedPing:
return (true, DialerConnectionHandler.HandleConnectedPing(conn, b.AsSpan(1)));
case Id.ConnectedPong:
return (true, DialerConnectionHandler.HandleConnectedPong(b.AsSpan(1)));
case Id.DisconnectNotification:
conn.CloseImmediately();
return (true, null);
case Id.DetectLostConnections:
return (true, conn.SendInternal(new ConnectedPing { PingTime = Conn.Timestamp() }));
default:
return (false, null);
}
}
private Exception? HandleConnectionRequest(Conn conn, ReadOnlySpan<byte> b)
{
var pk = new ConnectionRequest();
try
{
pk.Unmarshal(b);
}
catch (Exception ex)
{
return new Exception("read CONNECTION_REQUEST", ex);
}
return conn.SendInternal(new ConnectionRequestAccepted
{
ClientAddress = conn.RemoteEndPoint,
PingTime = pk.RequestTime,
PongTime = Conn.Timestamp()
});
}
private Exception? HandleNewIncomingConnection(Conn conn)
{
if (conn.IsConnected)
return new Exception("unexpected additional NEW_INCOMING_CONNECTION packet");
conn.SignalConnected();
return null;
}
}