Mirror uses new Transport.Send version everywhere

This commit is contained in:
vis2k 2021-05-17 17:41:54 +08:00
parent 9a0cce64cd
commit e823de74bd
12 changed files with 41 additions and 45 deletions

View File

@ -91,7 +91,7 @@ internal void SendBatch(int channelId, Batch batch)
writer.Position + segment.Count >= max) writer.Position + segment.Count >= max)
{ {
// flush & reset writer // flush & reset writer
Transport.activeTransport.ServerSend(connectionId, channelId, writer.ToArraySegment()); Transport.activeTransport.ServerSend(connectionId, writer.ToArraySegment(), channelId);
writer.SetLength(0); writer.SetLength(0);
} }
@ -113,7 +113,7 @@ internal void SendBatch(int channelId, Batch batch)
// send it. // send it.
if (writer.Position > 0) if (writer.Position > 0)
{ {
Transport.activeTransport.ServerSend(connectionId, channelId, writer.ToArraySegment()); Transport.activeTransport.ServerSend(connectionId, writer.ToArraySegment(), channelId);
writer.SetLength(0); writer.SetLength(0);
} }
} }
@ -144,7 +144,7 @@ internal override void Send(ArraySegment<byte> segment, int channelId = Channels
batch.messages.Enqueue(writer); batch.messages.Enqueue(writer);
} }
// otherwise send directly to minimize latency // otherwise send directly to minimize latency
else Transport.activeTransport.ServerSend(connectionId, channelId, segment); else Transport.activeTransport.ServerSend(connectionId, segment, channelId);
} }
} }

View File

@ -13,7 +13,7 @@ internal override void Send(ArraySegment<byte> segment, int channelId = Channels
// validate packet size first. // validate packet size first.
if (ValidatePacketSize(segment, channelId)) if (ValidatePacketSize(segment, channelId))
{ {
Transport.activeTransport.ClientSend(channelId, segment); Transport.activeTransport.ClientSend(segment, channelId);
} }
} }

View File

@ -93,9 +93,9 @@ public override void ClientDisconnect()
available.ClientDisconnect(); available.ClientDisconnect();
} }
public override void ClientSend(int channelId, ArraySegment<byte> segment) public override void ClientSend(ArraySegment<byte> segment, int channelId)
{ {
available.ClientSend(channelId, segment); available.ClientSend(segment, channelId);
} }
// right now this just returns the first available uri, // right now this just returns the first available uri,
@ -117,9 +117,9 @@ public override void ServerDisconnect(int connectionId)
available.ServerDisconnect(connectionId); available.ServerDisconnect(connectionId);
} }
public override void ServerSend(int connectionId, int channelId, ArraySegment<byte> segment) public override void ServerSend(int connectionId, ArraySegment<byte> segment, int channelId)
{ {
available.ServerSend(connectionId, channelId, segment); available.ServerSend(connectionId, segment, channelId);
} }
public override void ServerStart() public override void ServerStart()

View File

@ -90,7 +90,7 @@ public override void ClientConnect(string address)
{ {
client.Connect(address, Port, NoDelay, Interval, FastResend, CongestionWindow, SendWindowSize, ReceiveWindowSize); client.Connect(address, Port, NoDelay, Interval, FastResend, CongestionWindow, SendWindowSize, ReceiveWindowSize);
} }
public override void ClientSend(int channelId, ArraySegment<byte> segment) public override void ClientSend(ArraySegment<byte> segment, int channelId)
{ {
// switch to kcp channel. // switch to kcp channel.
// unreliable or reliable. // unreliable or reliable.
@ -149,7 +149,7 @@ public override Uri ServerUri()
} }
public override bool ServerActive() => server.IsActive(); public override bool ServerActive() => server.IsActive();
public override void ServerStart() => server.Start(Port); public override void ServerStart() => server.Start(Port);
public override void ServerSend(int connectionId, int channelId, ArraySegment<byte> segment) public override void ServerSend(int connectionId, ArraySegment<byte> segment, int channelId)
{ {
// switch to kcp channel. // switch to kcp channel.
// unreliable or reliable. // unreliable or reliable.

View File

@ -91,7 +91,7 @@ float SimulateLatency(int channeldId)
} }
// helper function to simulate a send with latency/loss/scramble // helper function to simulate a send with latency/loss/scramble
void SimulateSend(int connectionId, int channelId, ArraySegment<byte> segment, float latency, List<QueuedMessage> reliableQueue, List<QueuedMessage> unreliableQueue) void SimulateSend(int connectionId, ArraySegment<byte> segment, int channelId, float latency, List<QueuedMessage> reliableQueue, List<QueuedMessage> unreliableQueue)
{ {
// segment is only valid after returning. copy it. // segment is only valid after returning. copy it.
// (allocates for now. it's only for testing anyway.) // (allocates for now. it's only for testing anyway.)
@ -161,10 +161,10 @@ public override void ClientDisconnect()
unreliableClientToServer.Clear(); unreliableClientToServer.Clear();
} }
public override void ClientSend(int channelId, ArraySegment<byte> segment) public override void ClientSend(ArraySegment<byte> segment, int channelId)
{ {
float latency = SimulateLatency(channelId); float latency = SimulateLatency(channelId);
SimulateSend(0, channelId, segment, latency, reliableClientToServer, unreliableClientToServer); SimulateSend(0, segment, channelId, latency, reliableClientToServer, unreliableClientToServer);
} }
public override Uri ServerUri() => wrap.ServerUri(); public override Uri ServerUri() => wrap.ServerUri();
@ -175,10 +175,10 @@ public override void ClientSend(int channelId, ArraySegment<byte> segment)
public override void ServerDisconnect(int connectionId) => wrap.ServerDisconnect(connectionId); public override void ServerDisconnect(int connectionId) => wrap.ServerDisconnect(connectionId);
public override void ServerSend(int connectionId, int channelId, ArraySegment<byte> segment) public override void ServerSend(int connectionId, ArraySegment<byte> segment, int channelId)
{ {
float latency = SimulateLatency(channelId); float latency = SimulateLatency(channelId);
SimulateSend(connectionId, channelId, segment, latency, reliableServerToClient, unreliableServerToClient); SimulateSend(connectionId, segment, channelId, latency, reliableServerToClient, unreliableServerToClient);
} }
public override void ServerStart() public override void ServerStart()
@ -209,7 +209,7 @@ public override void ClientLateUpdate()
if (message.time <= Time.time) if (message.time <= Time.time)
{ {
// send and eat // send and eat
wrap.ClientSend(Channels.Reliable, new ArraySegment<byte>(message.bytes)); wrap.ClientSend(new ArraySegment<byte>(message.bytes), Channels.Reliable);
reliableClientToServer.RemoveAt(0); reliableClientToServer.RemoveAt(0);
} }
// not enough time elapsed yet // not enough time elapsed yet
@ -224,7 +224,7 @@ public override void ClientLateUpdate()
if (message.time <= Time.time) if (message.time <= Time.time)
{ {
// send and eat // send and eat
wrap.ClientSend(Channels.Unreliable, new ArraySegment<byte>(message.bytes)); wrap.ClientSend(new ArraySegment<byte>(message.bytes), Channels.Unreliable);
unreliableClientToServer.RemoveAt(0); unreliableClientToServer.RemoveAt(0);
} }
// not enough time elapsed yet // not enough time elapsed yet
@ -244,7 +244,7 @@ public override void ServerLateUpdate()
if (message.time <= Time.time) if (message.time <= Time.time)
{ {
// send and eat // send and eat
wrap.ServerSend(message.connectionId, Channels.Reliable, new ArraySegment<byte>(message.bytes)); wrap.ServerSend(message.connectionId, new ArraySegment<byte>(message.bytes), Channels.Reliable);
reliableServerToClient.RemoveAt(0); reliableServerToClient.RemoveAt(0);
} }
// not enough time elapsed yet // not enough time elapsed yet
@ -259,7 +259,7 @@ public override void ServerLateUpdate()
if (message.time <= Time.time) if (message.time <= Time.time)
{ {
// send and eat // send and eat
wrap.ServerSend(message.connectionId, Channels.Unreliable, new ArraySegment<byte>(message.bytes)); wrap.ServerSend(message.connectionId, new ArraySegment<byte>(message.bytes), Channels.Unreliable);
unreliableServerToClient.RemoveAt(0); unreliableServerToClient.RemoveAt(0);
} }
// not enough time elapsed yet // not enough time elapsed yet

View File

@ -30,7 +30,7 @@ public override void ClientConnect(string address)
public override bool ClientConnected() => inner.ClientConnected(); public override bool ClientConnected() => inner.ClientConnected();
public override void ClientDisconnect() => inner.ClientDisconnect(); public override void ClientDisconnect() => inner.ClientDisconnect();
public override void ClientSend(int channelId, ArraySegment<byte> segment) => inner.ClientSend(channelId, segment); public override void ClientSend(ArraySegment<byte> segment, int channelId) => inner.ClientSend(segment, channelId);
#endregion #endregion
#region Server #region Server
@ -45,7 +45,7 @@ public override void ServerStart()
} }
public override void ServerStop() => inner.ServerStop(); public override void ServerStop() => inner.ServerStop();
public override void ServerSend(int connectionId, int channelId, ArraySegment<byte> segment) => inner.ServerSend(connectionId, channelId, segment); public override void ServerSend(int connectionId, ArraySegment<byte> segment, int channelId) => inner.ServerSend(connectionId, segment, channelId);
public override void ServerDisconnect(int connectionId) => inner.ServerDisconnect(connectionId); public override void ServerDisconnect(int connectionId) => inner.ServerDisconnect(connectionId);
public override string ServerGetClientAddress(int connectionId) => inner.ServerGetClientAddress(connectionId); public override string ServerGetClientAddress(int connectionId) => inner.ServerGetClientAddress(connectionId);
public override Uri ServerUri() => inner.ServerUri(); public override Uri ServerUri() => inner.ServerUri();

View File

@ -137,9 +137,9 @@ public override void ClientDisconnect()
available.ClientDisconnect(); available.ClientDisconnect();
} }
public override void ClientSend(int channelId, ArraySegment<byte> segment) public override void ClientSend(ArraySegment<byte> segment, int channelId)
{ {
available.ClientSend(channelId, segment); available.ClientSend(segment, channelId);
} }
#endregion #endregion
@ -231,7 +231,7 @@ public override void ServerDisconnect(int connectionId)
transports[transportId].ServerDisconnect(baseConnectionId); transports[transportId].ServerDisconnect(baseConnectionId);
} }
public override void ServerSend(int connectionId, int channelId, ArraySegment<byte> segment) public override void ServerSend(int connectionId, ArraySegment<byte> segment, int channelId)
{ {
int baseConnectionId = ToBaseId(connectionId); int baseConnectionId = ToBaseId(connectionId);
int transportId = ToTransportId(connectionId); int transportId = ToTransportId(connectionId);
@ -240,7 +240,7 @@ public override void ServerSend(int connectionId, int channelId, ArraySegment<by
{ {
if (i == transportId) if (i == transportId)
{ {
transports[i].ServerSend(baseConnectionId, channelId, segment); transports[i].ServerSend(baseConnectionId, segment, channelId);
} }
} }
} }

View File

@ -166,7 +166,7 @@ public override void ClientDisconnect()
client?.Disconnect(); client?.Disconnect();
} }
public override void ClientSend(int channelId, ArraySegment<byte> segment) public override void ClientSend(ArraySegment<byte> segment, int channelId)
{ {
if (!ClientConnected()) if (!ClientConnected())
{ {
@ -244,7 +244,7 @@ public override void ServerDisconnect(int connectionId)
server.KickClient(connectionId); server.KickClient(connectionId);
} }
public override void ServerSend(int connectionId, int channelId, ArraySegment<byte> segment) public override void ServerSend(int connectionId, ArraySegment<byte> segment, int channelId)
{ {
if (!ServerActive()) if (!ServerActive())
{ {
@ -265,7 +265,6 @@ public override void ServerSend(int connectionId, int channelId, ArraySegment<by
} }
server.SendOne(connectionId, segment); server.SendOne(connectionId, segment);
return;
} }
public override string ServerGetClientAddress(int connectionId) public override string ServerGetClientAddress(int connectionId)

View File

@ -131,7 +131,7 @@ public override void ClientConnect(Uri uri)
int serverPort = uri.IsDefaultPort ? port : uri.Port; int serverPort = uri.IsDefaultPort ? port : uri.Port;
client.Connect(uri.Host, serverPort); client.Connect(uri.Host, serverPort);
} }
public override void ClientSend(int channelId, ArraySegment<byte> segment) => client.Send(segment); public override void ClientSend(ArraySegment<byte> segment, int channelId) => client.Send(segment);
public override void ClientDisconnect() => client.Disconnect(); public override void ClientDisconnect() => client.Disconnect();
// messages should always be processed in early update // messages should always be processed in early update
public override void ClientEarlyUpdate() public override void ClientEarlyUpdate()
@ -158,7 +158,7 @@ public override Uri ServerUri()
} }
public override bool ServerActive() => server.Active; public override bool ServerActive() => server.Active;
public override void ServerStart() => server.Start(port); public override void ServerStart() => server.Start(port);
public override void ServerSend(int connectionId, int channelId, ArraySegment<byte> segment) => server.Send(connectionId, segment); public override void ServerSend(int connectionId, ArraySegment<byte> segment, int channelId) => server.Send(connectionId, segment);
public override void ServerDisconnect(int connectionId) => server.Disconnect(connectionId); public override void ServerDisconnect(int connectionId) => server.Disconnect(connectionId);
public override string ServerGetClientAddress(int connectionId) public override string ServerGetClientAddress(int connectionId)
{ {

View File

@ -52,7 +52,7 @@ public override void ClientConnect(string address)
clientConnected = true; clientConnected = true;
} }
} }
public override void ClientSend(int channelId, ArraySegment<byte> segment) public override void ClientSend(ArraySegment<byte> segment, int channelId)
{ {
// only if client connected // only if client connected
if (clientConnected) if (clientConnected)
@ -115,7 +115,7 @@ public override void ClientEarlyUpdate()
public override bool ServerActive() => serverActive; public override bool ServerActive() => serverActive;
public override Uri ServerUri() => throw new NotImplementedException(); public override Uri ServerUri() => throw new NotImplementedException();
public override void ServerStart() { serverActive = true; } public override void ServerStart() { serverActive = true; }
public override void ServerSend(int connectionId, int channelId, ArraySegment<byte> segment) public override void ServerSend(int connectionId, ArraySegment<byte> segment, int channelId)
{ {
// only if server is running and client is connected // only if server is running and client is connected
if (serverActive && clientConnected) if (serverActive && clientConnected)

View File

@ -106,14 +106,12 @@ public void TestClientSend(int channel)
const int count = 5; const int count = 5;
ArraySegment<byte> segment = new ArraySegment<byte>(array, offset, count); ArraySegment<byte> segment = new ArraySegment<byte>(array, offset, count);
middleware.ClientSend(channel, segment); middleware.ClientSend(segment, channel);
inner.Received(1).ClientSend(channel, Arg.Is<ArraySegment<byte>>(x => x.Array == array && x.Offset == offset && x.Count == count)); inner.Received(1).ClientSend(Arg.Is<ArraySegment<byte>>(x => x.Array == array && x.Offset == offset && x.Count == count), channel);
inner.Received(0).ClientSend(Arg.Is<int>(x => x != channel), Arg.Any<ArraySegment<byte>>()); inner.Received(0).ClientSend(Arg.Any<ArraySegment<byte>>(), Arg.Is<int>(x => x != channel));
} }
[Test] [Test]
[TestCase(true)] [TestCase(true)]
[TestCase(false)] [TestCase(false)]
@ -154,11 +152,11 @@ public void TestServerSend(int id, int channel)
const int count = 5; const int count = 5;
ArraySegment<byte> segment = new ArraySegment<byte>(array, offset, count); ArraySegment<byte> segment = new ArraySegment<byte>(array, offset, count);
middleware.ServerSend(id, channel, segment); middleware.ServerSend(id, segment, channel);
inner.Received(1).ServerSend(id, channel, Arg.Is<ArraySegment<byte>>(x => x.Array == array && x.Offset == offset && x.Count == count)); inner.Received(1).ServerSend(id, Arg.Is<ArraySegment<byte>>(x => x.Array == array && x.Offset == offset && x.Count == count), channel);
// only need to check first arg, // only need to check first arg,
inner.Received(0).ServerSend(Arg.Is<int>(x => x != id), Arg.Any<int>(), Arg.Any<ArraySegment<byte>>()); inner.Received(0).ServerSend(Arg.Is<int>(x => x != id), Arg.Any<ArraySegment<byte>>(), Arg.Any<int>());
} }
[Test] [Test]

View File

@ -128,9 +128,9 @@ public void TestClientSend()
byte[] data = { 1, 2, 3 }; byte[] data = { 1, 2, 3 };
ArraySegment<byte> segment = new ArraySegment<byte>(data); ArraySegment<byte> segment = new ArraySegment<byte>(data);
transport.ClientSend(3, segment); transport.ClientSend(segment, 3);
transport1.Received().ClientSend(3, segment); transport1.Received().ClientSend(segment, 3);
} }
[Test] [Test]
@ -175,11 +175,10 @@ public void TestServerConnected()
byte[] data = { 1, 2, 3 }; byte[] data = { 1, 2, 3 };
ArraySegment<byte> segment = new ArraySegment<byte>(data); ArraySegment<byte> segment = new ArraySegment<byte>(data);
// on connect, send a message back // on connect, send a message back
void SendMessage(int connectionId) void SendMessage(int connectionId)
{ {
transport.ServerSend(connectionId, 5, segment); transport.ServerSend(connectionId, segment, 5);
} }
// set event and Start to give event to inner // set event and Start to give event to inner
@ -188,7 +187,7 @@ void SendMessage(int connectionId)
transport1.OnServerConnected.Invoke(1); transport1.OnServerConnected.Invoke(1);
transport1.Received().ServerSend(1, 5, segment); transport1.Received().ServerSend(1, segment, 5);
} }