This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDarkRiftTransport.cs
340 lines (316 loc) · 10.9 KB
/
DarkRiftTransport.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using LiteNetLib;
using LiteNetLib.Utils;
using LiteNetLibManager;
using DarkRift;
using DarkRift.Server;
using DarkRift.Client;
public class DarkRiftTransport : ITransport
{
private readonly Dictionary<long, IClient> _serverPeers;
private readonly Queue<TransportEventData> _clientEventQueue;
private readonly Queue<TransportEventData> _serverEventQueue;
public DarkRiftClient Client { get; private set; }
public DarkRiftServer Server { get; private set; }
public int ServerPeersCount
{
get
{
if (Server != null)
return Server.ClientManager.Count;
return 0;
}
}
public int ServerMaxConnections { get; private set; }
public bool IsClientStarted
{
get { return Client != null && Client.ConnectionState == DarkRift.ConnectionState.Connected; }
}
public bool IsServerStarted
{
get { return Server != null; }
}
public bool HasImplementedPing
{
get { return false; }
}
public DarkRiftTransport()
{
_serverPeers = new Dictionary<long, IClient>();
_clientEventQueue = new Queue<TransportEventData>();
_serverEventQueue = new Queue<TransportEventData>();
}
public bool ClientReceive(out TransportEventData eventData)
{
eventData = default(TransportEventData);
if (Client == null)
return false;
if (_clientEventQueue.Count == 0)
return false;
eventData = _clientEventQueue.Dequeue();
return true;
}
public bool ClientSend(byte dataChannel, DeliveryMethod deliveryMethod, NetDataWriter writer)
{
using (DarkRiftWriter drWriter = DarkRiftWriter.Create(writer.Length))
{
drWriter.WriteRaw(writer.Data, 0, writer.Length);
using (Message message = Message.Create(0, drWriter))
{
return Client.SendMessage(message, GetSendMode(deliveryMethod));
}
}
}
public void Destroy()
{
StopClient();
StopServer();
}
public bool ServerDisconnect(long connectionId)
{
if (IsServerStarted && _serverPeers.ContainsKey(connectionId))
{
if (_serverPeers[connectionId].Disconnect())
{
_serverPeers.Remove(connectionId);
return true;
}
}
return false;
}
public bool ServerReceive(out TransportEventData eventData)
{
eventData = default(TransportEventData);
if (Server == null)
return false;
if (_serverEventQueue.Count == 0)
return false;
eventData = _serverEventQueue.Dequeue();
if (eventData.type == ENetworkEvent.DataEvent && eventData.reader == null)
return false;
return true;
}
public bool ServerSend(long connectionId, byte dataChannel, DeliveryMethod deliveryMethod, NetDataWriter writer)
{
if (IsServerStarted && _serverPeers.ContainsKey(connectionId) && _serverPeers[connectionId].ConnectionState == DarkRift.ConnectionState.Connected)
{
using (DarkRiftWriter drWriter = DarkRiftWriter.Create(writer.Length))
{
drWriter.WriteRaw(writer.Data, 0, writer.Length);
using (Message message = Message.Create(0, drWriter))
{
return _serverPeers[connectionId].SendMessage(message, GetSendMode(deliveryMethod));
}
}
}
return false;
}
public bool StartClient(string address, int port)
{
if (IsClientStarted)
return false;
_clientEventQueue.Clear();
Client = new DarkRiftClient();
Client.Disconnected += Client_Disconnected;
Client.MessageReceived += Client_MessageReceived;
if (address.Equals("localhost"))
address = "127.0.0.1";
Client.ConnectInBackground(IPAddress.Parse(address), port, IPVersion.IPv4, (exception) =>
{
if (exception != null)
{
UnityEngine.Debug.LogException(exception);
_clientEventQueue.Enqueue(new TransportEventData()
{
type = ENetworkEvent.ErrorEvent,
socketError = SocketError.ConnectionRefused,
});
}
else
{
_clientEventQueue.Enqueue(new TransportEventData()
{
type = ENetworkEvent.ConnectEvent,
});
}
});
return true;
}
private void Client_Disconnected(object sender, DisconnectedEventArgs e)
{
_clientEventQueue.Enqueue(GetDisconnectEvent(0, e.LocalDisconnect, e.Error));
}
private void Client_MessageReceived(object sender, DarkRift.Client.MessageReceivedEventArgs e)
{
// Add receive message to list, will read it later by `ClientReceive` function
using (Message message = e.GetMessage())
{
using (DarkRiftReader reader = message.GetReader())
{
_clientEventQueue.Enqueue(new TransportEventData()
{
type = ENetworkEvent.DataEvent,
reader = new NetDataReader(reader.ReadRaw(reader.Length)),
});
}
}
}
public bool StartServer(int port, int maxConnections)
{
if (IsServerStarted)
return false;
ServerMaxConnections = maxConnections;
_serverPeers.Clear();
_serverEventQueue.Clear();
Server = new DarkRiftServer(new ServerSpawnData(IPAddress.Any, (ushort)port, IPVersion.IPv4));
Server.ClientManager.ClientConnected += Server_ClientManager_ClientConnected;
Server.ClientManager.ClientDisconnected += Server_ClientManager_ClientDisconnected;
Server.Start();
return true;
}
private void Server_ClientManager_ClientConnected(object sender, ClientConnectedEventArgs e)
{
if (ServerPeersCount >= ServerMaxConnections)
{
e.Client.Disconnect();
return;
}
e.Client.MessageReceived += Server_ClientManager_Client_MessageReceived;
_serverPeers[e.Client.ID] = e.Client;
_serverEventQueue.Enqueue(new TransportEventData()
{
type = ENetworkEvent.ConnectEvent,
connectionId = e.Client.ID,
});
}
private void Server_ClientManager_Client_MessageReceived(object sender, DarkRift.Server.MessageReceivedEventArgs e)
{
// Add receive message to list, will read it later by `ServerReceive` function
using (Message message = e.GetMessage())
{
using (DarkRiftReader reader = message.GetReader())
{
_serverEventQueue.Enqueue(new TransportEventData()
{
type = ENetworkEvent.DataEvent,
connectionId = e.Client.ID,
reader = new NetDataReader(reader.ReadRaw(reader.Length)),
});
}
}
}
private void Server_ClientManager_ClientDisconnected(object sender, ClientDisconnectedEventArgs e)
{
_serverPeers.Remove(e.Client.ID);
_serverEventQueue.Enqueue(GetDisconnectEvent(e.Client.ID, e.LocalDisconnect, e.Error));
}
public void StopClient()
{
if (Client != null)
{
Client.Disconnect();
Client.Dispose();
}
Client = null;
}
public void StopServer()
{
if (Server != null)
Server.Dispose();
Server = null;
}
public TransportEventData GetDisconnectEvent(ushort connectionId, bool localDisconnect, SocketError error)
{
TransportEventData result = new TransportEventData()
{
type = ENetworkEvent.DisconnectEvent,
connectionId = connectionId,
};
if (localDisconnect)
{
result.disconnectInfo = new DisconnectInfo()
{
Reason = DisconnectReason.DisconnectPeerCalled,
SocketErrorCode = error,
};
}
else
{
switch (error)
{
case SocketError.ConnectionReset:
result.disconnectInfo = new DisconnectInfo()
{
Reason = DisconnectReason.ConnectionFailed,
SocketErrorCode = error,
};
break;
case SocketError.TimedOut:
result.disconnectInfo = new DisconnectInfo()
{
Reason = DisconnectReason.Timeout,
SocketErrorCode = error,
};
break;
case SocketError.HostUnreachable:
result.disconnectInfo = new DisconnectInfo()
{
Reason = DisconnectReason.HostUnreachable,
SocketErrorCode = error,
};
break;
case SocketError.NetworkUnreachable:
result.disconnectInfo = new DisconnectInfo()
{
Reason = DisconnectReason.NetworkUnreachable,
SocketErrorCode = error,
};
break;
case SocketError.ConnectionAborted:
result.disconnectInfo = new DisconnectInfo()
{
Reason = DisconnectReason.RemoteConnectionClose,
SocketErrorCode = error,
};
break;
case SocketError.ConnectionRefused:
result.disconnectInfo = new DisconnectInfo()
{
Reason = DisconnectReason.ConnectionRejected,
SocketErrorCode = error,
};
break;
default:
result.disconnectInfo = new DisconnectInfo()
{
SocketErrorCode = error,
};
break;
}
}
return result;
}
public SendMode GetSendMode(DeliveryMethod deliveryMethod)
{
switch (deliveryMethod)
{
case DeliveryMethod.ReliableOrdered:
case DeliveryMethod.ReliableUnordered:
case DeliveryMethod.ReliableSequenced:
return SendMode.Reliable;
case DeliveryMethod.Sequenced:
default:
return SendMode.Unreliable;
}
}
public long GetClientRtt()
{
throw new System.NotImplementedException();
}
public long GetServerRtt(long connectionId)
{
throw new System.NotImplementedException();
}
}