-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathMultiplexTransportAdapter.cs
More file actions
333 lines (282 loc) · 13.2 KB
/
MultiplexTransportAdapter.cs
File metadata and controls
333 lines (282 loc) · 13.2 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using MLAPI.Transports.Tasks;
namespace MLAPI.Transports.Multiplex
{
/// <summary>
/// Multiplex transport adapter.
/// </summary>
public class MultiplexTransportAdapter : Transport
{
/// <summary>
/// The method to use to distribute the transport connectionIds in a fixed size 64 bit integer.
/// </summary>
public enum ConnectionIdSpreadMethod
{
/// <summary>
/// Drops the first few bits (left side) by shifting the transport clientId to the left and inserting the transportId in the first bits.
/// Ensure that ALL transports dont use the last bits in their produced clientId.
/// For incremental clientIds, this is the most space efficient assuming that every transport get used an equal amount.
/// </summary>
MakeRoomLastBits,
/// <summary>
/// Drops the first few bits (left side) and replaces them with the transport index.
/// Ensure that ALL transports dont use the first few bits in the produced clientId.
/// </summary>
ReplaceFirstBits,
/// <summary>
/// Drops the last few bits (right side) and replaces them with the transport index.
/// Ensure that ALL transports dont use the last bits in their produced clientId.
/// This option is for advanced users and will not work with the official MLAPI transports as they use the last bits.
/// </summary>
ReplaceLastBits,
/// <summary>
/// Drops the last few bits (right side) by shifting the transport clientId to the right and inserting the transportId in the first bits.
/// Ensure that ALL transports dont use the first bits in their produced clientId.
/// </summary>
MakeRoomFirstBits,
/// <summary>
/// Spreads the clientIds evenly among the transports.
/// </summary>
Spread
}
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
public ConnectionIdSpreadMethod SpreadMethod = ConnectionIdSpreadMethod.MakeRoomLastBits;
public Transport[] Transports = new Transport[0];
public override ulong ServerClientId => 0;
private byte _lastProcessedTransportIndex;
public override bool IsSupported => true;
public override void DisconnectLocalClient()
{
Transports[GetFirstSupportedTransportIndex()].DisconnectLocalClient();
}
public override void DisconnectRemoteClient(ulong clientId)
{
GetMultiplexTransportDetails(clientId, out byte transportId, out ulong connectionId);
Transports[transportId].DisconnectRemoteClient(connectionId);
}
public override ulong GetCurrentRtt(ulong clientId)
{
GetMultiplexTransportDetails(clientId, out byte transportId, out ulong connectionId);
return Transports[transportId].GetCurrentRtt(connectionId);
}
public override void Init()
{
for (int i = 0; i < Transports.Length; i++)
{
if (Transports[i].IsSupported)
{
Transports[i].Init();
}
}
}
public override NetEventType PollEvent(out ulong clientId, out byte channel, out ArraySegment<byte> payload, out float receiveTime)
{
if (_lastProcessedTransportIndex >= Transports.Length - 1)
_lastProcessedTransportIndex = 0;
for (byte i = _lastProcessedTransportIndex; i < Transports.Length; i++)
{
_lastProcessedTransportIndex = i;
if (Transports[i].IsSupported)
{
NetEventType @eventType = Transports[i].PollEvent(out ulong connectionId, out channel, out payload, out receiveTime);
if (@eventType != NetEventType.Nothing)
{
clientId = GetMLAPIClientId(i, connectionId, false);
return @eventType;
}
}
}
clientId = 0;
channel = 0;
payload = new ArraySegment<byte>();
receiveTime = 0;
return NetEventType.Nothing;
}
public override void Send(ulong clientId, ArraySegment<byte> data, byte channel)
{
GetMultiplexTransportDetails(clientId, out byte transportId, out ulong connectionId);
Transports[transportId].Send(connectionId, data, channel);
}
public override void Shutdown()
{
for (int i = 0; i < Transports.Length; i++)
{
if (Transports[i].IsSupported)
{
Transports[i].Shutdown();
}
}
}
public override SocketTasks StartClient()
{
List<SocketTask> socketTasks = new List<SocketTask>();
for (int i = 0; i < Transports.Length; i++)
{
if (Transports[i].IsSupported)
{
socketTasks.AddRange(Transports[i].StartClient().Tasks);
}
}
return new SocketTasks()
{
Tasks = socketTasks.ToArray()
};
}
public override SocketTasks StartServer()
{
List<SocketTask> socketTasks = new List<SocketTask>();
for (int i = 0; i < Transports.Length; i++)
{
if (Transports[i].IsSupported)
{
socketTasks.AddRange(Transports[i].StartServer().Tasks);
}
}
return new SocketTasks()
{
Tasks = socketTasks.ToArray()
};
}
public ulong GetMLAPIClientId(byte transportId, ulong connectionId, bool isServer)
{
if (isServer)
{
return ServerClientId;
}
else
{
switch (SpreadMethod)
{
case ConnectionIdSpreadMethod.ReplaceFirstBits:
{
// Calculate bits to store transportId
byte bits = (byte)UnityEngine.Mathf.CeilToInt(UnityEngine.Mathf.Log(Transports.Length, 2));
// Drop first bits of connectionId
ulong clientId = ((connectionId << bits) >> bits);
// Place transportId there
ulong shiftedTransportId = (ulong)transportId << ((sizeof(ulong) * 8) - bits);
return (clientId | shiftedTransportId) + 1;
}
case ConnectionIdSpreadMethod.MakeRoomFirstBits:
{
// Calculate bits to store transportId
byte bits = (byte)UnityEngine.Mathf.CeilToInt(UnityEngine.Mathf.Log(Transports.Length, 2));
// Drop first bits of connectionId
ulong clientId = (connectionId >> bits);
// Place transportId there
ulong shiftedTransportId = (ulong)transportId << ((sizeof(ulong) * 8) - bits);
return (clientId | shiftedTransportId) + 1;
}
case ConnectionIdSpreadMethod.ReplaceLastBits:
{
// Calculate bits to store transportId
byte bits = (byte)UnityEngine.Mathf.CeilToInt(UnityEngine.Mathf.Log(Transports.Length, 2));
// Drop the last bits of connectionId
ulong clientId = ((connectionId >> bits) << bits);
// Return the transport inserted at the end
return (clientId | transportId) + 1;
}
case ConnectionIdSpreadMethod.MakeRoomLastBits:
{
// Calculate bits to store transportId
byte bits = (byte)UnityEngine.Mathf.CeilToInt(UnityEngine.Mathf.Log(Transports.Length, 2));
// Drop the last bits of connectionId
ulong clientId = (connectionId << bits);
// Return the transport inserted at the end
return (clientId | transportId) + 1;
}
case ConnectionIdSpreadMethod.Spread:
{
return (connectionId * (ulong)Transports.Length + (ulong)transportId) + 1;
}
default:
{
return ServerClientId;
}
}
}
}
public void GetMultiplexTransportDetails(ulong clientId, out byte transportId, out ulong connectionId)
{
if (clientId == ServerClientId)
{
transportId = GetFirstSupportedTransportIndex();
connectionId = Transports[transportId].ServerClientId;
}
else
{
switch (SpreadMethod)
{
case ConnectionIdSpreadMethod.ReplaceFirstBits:
{
// The first clientId is reserved. Thus every clientId is always offset by 1
clientId--;
// Calculate bits to store transportId
byte bits = (byte)UnityEngine.Mathf.CeilToInt(UnityEngine.Mathf.Log(Transports.Length, 2));
transportId = (byte)(clientId >> ((sizeof(ulong) * 8) - bits));
connectionId = ((clientId << bits) >> bits);
break;
}
case ConnectionIdSpreadMethod.MakeRoomFirstBits:
{
// The first clientId is reserved. Thus every clientId is always offset by 1
clientId--;
// Calculate bits to store transportId
byte bits = (byte)UnityEngine.Mathf.CeilToInt(UnityEngine.Mathf.Log(Transports.Length, 2));
transportId = (byte)(clientId >> ((sizeof(ulong) * 8) - bits));
connectionId = (clientId << bits);
break;
}
case ConnectionIdSpreadMethod.ReplaceLastBits:
{
// The first clientId is reserved. Thus every clientId is always offset by 1
clientId--;
// Calculate bits to store transportId
byte bits = (byte)UnityEngine.Mathf.CeilToInt(UnityEngine.Mathf.Log(Transports.Length, 2));
transportId = (byte)((clientId << ((sizeof(ulong) * 8) - bits)) >> ((sizeof(ulong) * 8) - bits));
connectionId = ((clientId >> bits) << bits);
break;
}
case ConnectionIdSpreadMethod.MakeRoomLastBits:
{
// The first clientId is reserved. Thus every clientId is always offset by 1
clientId--;
// Calculate bits to store transportId
byte bits = (byte)UnityEngine.Mathf.CeilToInt(UnityEngine.Mathf.Log(Transports.Length, 2));
transportId = (byte)((clientId << ((sizeof(ulong) * 8) - bits)) >> ((sizeof(ulong) * 8) - bits));
connectionId = (clientId >> bits);
break;
}
case ConnectionIdSpreadMethod.Spread:
{
// The first clientId is reserved. Thus every clientId is always offset by 1
clientId--;
transportId = (byte)(clientId % (ulong)Transports.Length);
connectionId = (clientId / (ulong)Transports.Length);
break;
}
default:
{
transportId = GetFirstSupportedTransportIndex();
connectionId = Transports[transportId].ServerClientId;
break;
}
}
}
}
public byte GetFirstSupportedTransportIndex()
{
for (byte i = 0; i < Transports.Length; i++)
{
if (Transports[i].IsSupported)
{
return i;
}
}
return 0;
}
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
}
}