MultiplexTransport: ID functions renamed to be more obvious

This commit is contained in:
vis2k 2022-12-03 18:56:54 -05:00
parent 0dfd4e15cc
commit 2b1ebea7b7
2 changed files with 53 additions and 53 deletions

View File

@ -28,68 +28,68 @@ public void Setup()
public override void TearDown() => base.TearDown();
[Test]
public void FromBaseId()
public void MultiplexConnectionId()
{
// if we have 3 transports, then
// transport 0 will produce connection ids [0, 3, 6, 9, ...]
const int transportAmount = 3;
Assert.That(MultiplexTransport.FromBaseId(0, 0, transportAmount), Is.EqualTo(0));
Assert.That(MultiplexTransport.FromBaseId(0, 1, transportAmount), Is.EqualTo(3));
Assert.That(MultiplexTransport.FromBaseId(0, 2, transportAmount), Is.EqualTo(6));
Assert.That(MultiplexTransport.FromBaseId(0, 3, transportAmount), Is.EqualTo(9));
Assert.That(MultiplexTransport.MultiplexConnectionId(0, 0, transportAmount), Is.EqualTo(0));
Assert.That(MultiplexTransport.MultiplexConnectionId(0, 1, transportAmount), Is.EqualTo(3));
Assert.That(MultiplexTransport.MultiplexConnectionId(0, 2, transportAmount), Is.EqualTo(6));
Assert.That(MultiplexTransport.MultiplexConnectionId(0, 3, transportAmount), Is.EqualTo(9));
// transport 1 will produce connection ids [1, 4, 7, 10, ...]
Assert.That(MultiplexTransport.FromBaseId(1, 0, transportAmount), Is.EqualTo(1));
Assert.That(MultiplexTransport.FromBaseId(1, 1, transportAmount), Is.EqualTo(4));
Assert.That(MultiplexTransport.FromBaseId(1, 2, transportAmount), Is.EqualTo(7));
Assert.That(MultiplexTransport.FromBaseId(1, 3, transportAmount), Is.EqualTo(10));
Assert.That(MultiplexTransport.MultiplexConnectionId(1, 0, transportAmount), Is.EqualTo(1));
Assert.That(MultiplexTransport.MultiplexConnectionId(1, 1, transportAmount), Is.EqualTo(4));
Assert.That(MultiplexTransport.MultiplexConnectionId(1, 2, transportAmount), Is.EqualTo(7));
Assert.That(MultiplexTransport.MultiplexConnectionId(1, 3, transportAmount), Is.EqualTo(10));
// transport 2 will produce connection ids [2, 5, 8, 11, ...]
Assert.That(MultiplexTransport.FromBaseId(2, 0, transportAmount), Is.EqualTo(2));
Assert.That(MultiplexTransport.FromBaseId(2, 1, transportAmount), Is.EqualTo(5));
Assert.That(MultiplexTransport.FromBaseId(2, 2, transportAmount), Is.EqualTo(8));
Assert.That(MultiplexTransport.FromBaseId(2, 3, transportAmount), Is.EqualTo(11));
Assert.That(MultiplexTransport.MultiplexConnectionId(2, 0, transportAmount), Is.EqualTo(2));
Assert.That(MultiplexTransport.MultiplexConnectionId(2, 1, transportAmount), Is.EqualTo(5));
Assert.That(MultiplexTransport.MultiplexConnectionId(2, 2, transportAmount), Is.EqualTo(8));
Assert.That(MultiplexTransport.MultiplexConnectionId(2, 3, transportAmount), Is.EqualTo(11));
}
[Test]
public void ToBaseId()
public void OriginalConnectionId()
{
const int transportAmount = 3;
Assert.That(MultiplexTransport.ToBaseId(0, transportAmount), Is.EqualTo(0));
Assert.That(MultiplexTransport.ToBaseId(1, transportAmount), Is.EqualTo(0));
Assert.That(MultiplexTransport.ToBaseId(2, transportAmount), Is.EqualTo(0));
Assert.That(MultiplexTransport.OriginalConnectionId(0, transportAmount), Is.EqualTo(0));
Assert.That(MultiplexTransport.OriginalConnectionId(1, transportAmount), Is.EqualTo(0));
Assert.That(MultiplexTransport.OriginalConnectionId(2, transportAmount), Is.EqualTo(0));
Assert.That(MultiplexTransport.ToBaseId(3, transportAmount), Is.EqualTo(1));
Assert.That(MultiplexTransport.ToBaseId(4, transportAmount), Is.EqualTo(1));
Assert.That(MultiplexTransport.ToBaseId(5, transportAmount), Is.EqualTo(1));
Assert.That(MultiplexTransport.OriginalConnectionId(3, transportAmount), Is.EqualTo(1));
Assert.That(MultiplexTransport.OriginalConnectionId(4, transportAmount), Is.EqualTo(1));
Assert.That(MultiplexTransport.OriginalConnectionId(5, transportAmount), Is.EqualTo(1));
Assert.That(MultiplexTransport.ToBaseId(6, transportAmount), Is.EqualTo(2));
Assert.That(MultiplexTransport.ToBaseId(7, transportAmount), Is.EqualTo(2));
Assert.That(MultiplexTransport.ToBaseId(8, transportAmount), Is.EqualTo(2));
Assert.That(MultiplexTransport.OriginalConnectionId(6, transportAmount), Is.EqualTo(2));
Assert.That(MultiplexTransport.OriginalConnectionId(7, transportAmount), Is.EqualTo(2));
Assert.That(MultiplexTransport.OriginalConnectionId(8, transportAmount), Is.EqualTo(2));
Assert.That(MultiplexTransport.ToBaseId(9, transportAmount), Is.EqualTo(3));
Assert.That(MultiplexTransport.OriginalConnectionId(9, transportAmount), Is.EqualTo(3));
}
[Test]
public void ToTransportId()
public void OriginalTransportId()
{
const int transportAmount = 3;
Assert.That(MultiplexTransport.ToTransportId(0, transportAmount), Is.EqualTo(0));
Assert.That(MultiplexTransport.ToTransportId(1, transportAmount), Is.EqualTo(1));
Assert.That(MultiplexTransport.ToTransportId(2, transportAmount), Is.EqualTo(2));
Assert.That(MultiplexTransport.OriginalTransportId(0, transportAmount), Is.EqualTo(0));
Assert.That(MultiplexTransport.OriginalTransportId(1, transportAmount), Is.EqualTo(1));
Assert.That(MultiplexTransport.OriginalTransportId(2, transportAmount), Is.EqualTo(2));
Assert.That(MultiplexTransport.ToTransportId(3, transportAmount), Is.EqualTo(0));
Assert.That(MultiplexTransport.ToTransportId(4, transportAmount), Is.EqualTo(1));
Assert.That(MultiplexTransport.ToTransportId(5, transportAmount), Is.EqualTo(2));
Assert.That(MultiplexTransport.OriginalTransportId(3, transportAmount), Is.EqualTo(0));
Assert.That(MultiplexTransport.OriginalTransportId(4, transportAmount), Is.EqualTo(1));
Assert.That(MultiplexTransport.OriginalTransportId(5, transportAmount), Is.EqualTo(2));
Assert.That(MultiplexTransport.ToTransportId(6, transportAmount), Is.EqualTo(0));
Assert.That(MultiplexTransport.ToTransportId(7, transportAmount), Is.EqualTo(1));
Assert.That(MultiplexTransport.ToTransportId(8, transportAmount), Is.EqualTo(2));
Assert.That(MultiplexTransport.OriginalTransportId(6, transportAmount), Is.EqualTo(0));
Assert.That(MultiplexTransport.OriginalTransportId(7, transportAmount), Is.EqualTo(1));
Assert.That(MultiplexTransport.OriginalTransportId(8, transportAmount), Is.EqualTo(2));
Assert.That(MultiplexTransport.ToTransportId(9, transportAmount), Is.EqualTo(0));
Assert.That(MultiplexTransport.OriginalTransportId(9, transportAmount), Is.EqualTo(0));
}
// A Test behaves as an ordinary method

View File

@ -136,17 +136,17 @@ public override void ClientSend(ArraySegment<byte> segment, int channelId)
// transport 1 will produce connection ids [1, 4, 7, 10, ...]
// transport 2 will produce connection ids [2, 5, 8, 11, ...]
// convert transport relative connId to multiplexed connId
public static int FromBaseId(int transportId, int connectionId, int transportAmount) =>
// convert original transport connId to multiplexed connId
public static int MultiplexConnectionId(int transportId, int connectionId, int transportAmount) =>
connectionId * transportAmount + transportId;
// convert multiplexed connectionId back to transport relative connId
public static int ToBaseId(int connectionId, int transportAmount) =>
connectionId / transportAmount;
// convert multiplexed connectionId back to original transport connId
public static int OriginalConnectionId(int multiplexConnectionId, int transportAmount) =>
multiplexConnectionId / transportAmount;
// convert multiplexed connectionid back to transport id
public static int ToTransportId(int connectionId, int transportAmount) =>
connectionId % transportAmount;
// convert multiplexed connectionId back to original transportId
public static int OriginalTransportId(int multiplexConnectionId, int transportAmount) =>
multiplexConnectionId % transportAmount;
void AddServerCallbacks()
{
@ -160,21 +160,21 @@ void AddServerCallbacks()
transport.OnServerConnected = (baseConnectionId =>
{
OnServerConnected.Invoke(FromBaseId(locali, baseConnectionId, transports.Length));
OnServerConnected.Invoke(MultiplexConnectionId(locali, baseConnectionId, transports.Length));
});
transport.OnServerDataReceived = (baseConnectionId, data, channel) =>
{
OnServerDataReceived.Invoke(FromBaseId(locali, baseConnectionId, transports.Length), data, channel);
OnServerDataReceived.Invoke(MultiplexConnectionId(locali, baseConnectionId, transports.Length), data, channel);
};
transport.OnServerError = (baseConnectionId, error, reason) =>
{
OnServerError.Invoke(FromBaseId(locali, baseConnectionId, transports.Length), error, reason);
OnServerError.Invoke(MultiplexConnectionId(locali, baseConnectionId, transports.Length), error, reason);
};
transport.OnServerDisconnected = baseConnectionId =>
{
OnServerDisconnected.Invoke(FromBaseId(locali, baseConnectionId, transports.Length));
OnServerDisconnected.Invoke(MultiplexConnectionId(locali, baseConnectionId, transports.Length));
};
}
}
@ -196,22 +196,22 @@ public override bool ServerActive()
public override string ServerGetClientAddress(int connectionId)
{
int baseConnectionId = ToBaseId(connectionId, transports.Length);
int transportId = ToTransportId(connectionId, transports.Length);
int baseConnectionId = OriginalConnectionId(connectionId, transports.Length);
int transportId = OriginalTransportId(connectionId, transports.Length);
return transports[transportId].ServerGetClientAddress(baseConnectionId);
}
public override void ServerDisconnect(int connectionId)
{
int baseConnectionId = ToBaseId(connectionId, transports.Length);
int transportId = ToTransportId(connectionId, transports.Length);
int baseConnectionId = OriginalConnectionId(connectionId, transports.Length);
int transportId = OriginalTransportId(connectionId, transports.Length);
transports[transportId].ServerDisconnect(baseConnectionId);
}
public override void ServerSend(int connectionId, ArraySegment<byte> segment, int channelId)
{
int baseConnectionId = ToBaseId(connectionId, transports.Length);
int transportId = ToTransportId(connectionId, transports.Length);
int baseConnectionId = OriginalConnectionId(connectionId, transports.Length);
int transportId = OriginalTransportId(connectionId, transports.Length);
for (int i = 0; i < transports.Length; ++i)
if (i == transportId)