MultiplexTransport: Id calculations made static for easier testing

This commit is contained in:
vis2k 2022-12-03 18:31:06 -05:00
parent f4d4563a43
commit 576bcd80d7

View File

@ -135,14 +135,14 @@ public override void ClientSend(ArraySegment<byte> segment, int channelId)
// transport 0 will produce connection ids [0, 3, 6, 9, ...] // transport 0 will produce connection ids [0, 3, 6, 9, ...]
// transport 1 will produce connection ids [1, 4, 7, 10, ...] // transport 1 will produce connection ids [1, 4, 7, 10, ...]
// transport 2 will produce connection ids [2, 5, 8, 11, ...] // transport 2 will produce connection ids [2, 5, 8, 11, ...]
int FromBaseId(int transportId, int connectionId) => public static int FromBaseId(int transportId, int connectionId, int transportAmount) =>
connectionId * transports.Length + transportId; connectionId * transportAmount + transportId;
int ToBaseId(int connectionId) => public static int ToBaseId(int connectionId, int transportAmount) =>
connectionId / transports.Length; connectionId / transportAmount;
int ToTransportId(int connectionId) => public static int ToTransportId(int connectionId, int transportAmount) =>
connectionId % transports.Length; connectionId % transportAmount;
void AddServerCallbacks() void AddServerCallbacks()
{ {
@ -156,21 +156,21 @@ void AddServerCallbacks()
transport.OnServerConnected = (baseConnectionId => transport.OnServerConnected = (baseConnectionId =>
{ {
OnServerConnected.Invoke(FromBaseId(locali, baseConnectionId)); OnServerConnected.Invoke(FromBaseId(locali, baseConnectionId, transports.Length));
}); });
transport.OnServerDataReceived = (baseConnectionId, data, channel) => transport.OnServerDataReceived = (baseConnectionId, data, channel) =>
{ {
OnServerDataReceived.Invoke(FromBaseId(locali, baseConnectionId), data, channel); OnServerDataReceived.Invoke(FromBaseId(locali, baseConnectionId, transports.Length), data, channel);
}; };
transport.OnServerError = (baseConnectionId, error, reason) => transport.OnServerError = (baseConnectionId, error, reason) =>
{ {
OnServerError.Invoke(FromBaseId(locali, baseConnectionId), error, reason); OnServerError.Invoke(FromBaseId(locali, baseConnectionId, transports.Length), error, reason);
}; };
transport.OnServerDisconnected = baseConnectionId => transport.OnServerDisconnected = baseConnectionId =>
{ {
OnServerDisconnected.Invoke(FromBaseId(locali, baseConnectionId)); OnServerDisconnected.Invoke(FromBaseId(locali, baseConnectionId, transports.Length));
}; };
} }
} }
@ -198,22 +198,22 @@ public override bool ServerActive()
public override string ServerGetClientAddress(int connectionId) public override string ServerGetClientAddress(int connectionId)
{ {
int baseConnectionId = ToBaseId(connectionId); int baseConnectionId = ToBaseId(connectionId, transports.Length);
int transportId = ToTransportId(connectionId); int transportId = ToTransportId(connectionId, transports.Length);
return transports[transportId].ServerGetClientAddress(baseConnectionId); return transports[transportId].ServerGetClientAddress(baseConnectionId);
} }
public override void ServerDisconnect(int connectionId) public override void ServerDisconnect(int connectionId)
{ {
int baseConnectionId = ToBaseId(connectionId); int baseConnectionId = ToBaseId(connectionId, transports.Length);
int transportId = ToTransportId(connectionId); int transportId = ToTransportId(connectionId, transports.Length);
transports[transportId].ServerDisconnect(baseConnectionId); transports[transportId].ServerDisconnect(baseConnectionId);
} }
public override void ServerSend(int connectionId, ArraySegment<byte> segment, int channelId) public override void ServerSend(int connectionId, ArraySegment<byte> segment, int channelId)
{ {
int baseConnectionId = ToBaseId(connectionId); int baseConnectionId = ToBaseId(connectionId, transports.Length);
int transportId = ToTransportId(connectionId); int transportId = ToTransportId(connectionId, transports.Length);
for (int i = 0; i < transports.Length; ++i) for (int i = 0; i < transports.Length; ++i)
{ {