feature: Transport.OnClient/ServerDataSent events += (prepares for NetworkStatistics) (#3117)

* feature: Transport.OnClient/ServerDataSent events += (prepares for NetworkStatistics)

* comments
This commit is contained in:
vis2k 2022-03-11 13:14:06 +08:00
parent dc5162099e
commit b100307929
5 changed files with 53 additions and 9 deletions

View File

@ -42,6 +42,13 @@ public abstract class Transport : MonoBehaviour
/// <summary>Called by Transport when the client received a message from the server.</summary>
public Action<ArraySegment<byte>, int> OnClientDataReceived;
/// <summary>Called by Transport when the client sent a message to the server.</summary>
// Transports are responsible for calling it because:
// - groups it together with OnReceived responsibility
// - allows transports to decide if anything was sent or not
// - allows transports to decide the actual used channel (i.e. tcp always sending reliable)
public Action<ArraySegment<byte>, int> OnClientDataSent;
/// <summary>Called by Transport when the client encountered an error.</summary>
public Action<Exception> OnClientError;
@ -79,6 +86,13 @@ public virtual void ClientConnect(Uri uri)
/// <summary>Called by Transport when the server received a message from a client.</summary>
public Action<int, ArraySegment<byte>, int> OnServerDataReceived;
/// <summary>Called by Transport when the server sent a message to a client.</summary>
// Transports are responsible for calling it because:
// - groups it together with OnReceived responsibility
// - allows transports to decide if anything was sent or not
// - allows transports to decide the actual used channel (i.e. tcp always sending reliable)
public Action<int, ArraySegment<byte>, int> OnServerDataSent;
/// <summary>Called by Transport when a server's connection encountered a problem.</summary>
/// If a Disconnect will also be raised, raise the Error first.
public Action<int, Exception> OnServerError;

View File

@ -161,6 +161,9 @@ public override void ClientConnect(Uri uri)
public override void ClientSend(ArraySegment<byte> segment, int channelId)
{
client.Send(segment, ToKcpChannel(channelId));
// call event. might be null if no statistics are listening etc.
OnClientDataSent?.Invoke(segment, channelId);
}
public override void ClientDisconnect() => client.Disconnect();
// process incoming in early update
@ -188,6 +191,9 @@ public override Uri ServerUri()
public override void ServerSend(int connectionId, ArraySegment<byte> segment, int channelId)
{
server.Send(connectionId, segment, ToKcpChannel(channelId));
// call event. might be null if no statistics are listening etc.
OnServerDataSent?.Invoke(connectionId, segment, channelId);
}
public override void ServerDisconnect(int connectionId) => server.Disconnect(connectionId);
public override string ServerGetClientAddress(int connectionId) => server.GetClientAddress(connectionId);

View File

@ -181,6 +181,9 @@ public override void ClientSend(ArraySegment<byte> segment, int channelId)
}
client.Send(segment);
// call event. might be null if no statistics are listening etc.
OnClientDataSent?.Invoke(segment, Channels.Reliable);
}
// messages should always be processed in early update
@ -259,6 +262,9 @@ public override void ServerSend(int connectionId, ArraySegment<byte> segment, in
}
server.SendOne(connectionId, segment);
// call event. might be null if no statistics are listening etc.
OnServerDataSent?.Invoke(connectionId, segment, Channels.Reliable);
}
public override string ServerGetClientAddress(int connectionId)

View File

@ -81,7 +81,7 @@ public override bool Available()
}
// client
private void CreateClient()
private void CreateClient()
{
// create client
client = new Telepathy.Client(clientMaxMessageSize);
@ -104,7 +104,7 @@ private void CreateClient()
client.ReceiveQueueLimit = clientReceiveQueueLimit;
}
public override bool ClientConnected() => client != null && client.Connected;
public override void ClientConnect(string address)
public override void ClientConnect(string address)
{
CreateClient();
client.Connect(address, port);
@ -119,8 +119,14 @@ public override void ClientConnect(Uri uri)
int serverPort = uri.IsDefaultPort ? port : uri.Port;
client.Connect(uri.Host, serverPort);
}
public override void ClientSend(ArraySegment<byte> segment, int channelId) => client?.Send(segment);
public override void ClientDisconnect()
public override void ClientSend(ArraySegment<byte> segment, int channelId)
{
client?.Send(segment);
// call event. might be null if no statistics are listening etc.
OnClientDataSent?.Invoke(segment, Channels.Reliable);
}
public override void ClientDisconnect()
{
client?.Disconnect();
client = null;
@ -150,11 +156,11 @@ public override Uri ServerUri()
return builder.Uri;
}
public override bool ServerActive() => server != null && server.Active;
public override void ServerStart()
public override void ServerStart()
{
// create server
server = new Telepathy.Server(serverMaxMessageSize);
// server hooks
// other systems hook into transport events in OnCreate or
// OnStartRunning in no particular order. the only way to avoid
@ -172,11 +178,17 @@ public override void ServerStart()
server.ReceiveTimeout = ReceiveTimeout;
server.SendQueueLimit = serverSendQueueLimitPerConnection;
server.ReceiveQueueLimit = serverReceiveQueueLimitPerConnection;
server.Start(port);
}
public override void ServerSend(int connectionId, ArraySegment<byte> segment, int channelId) => server?.Send(connectionId, segment);
public override void ServerSend(int connectionId, ArraySegment<byte> segment, int channelId)
{
server?.Send(connectionId, segment);
// call event. might be null if no statistics are listening etc.
OnServerDataSent?.Invoke(connectionId, segment, Channels.Reliable);
}
public override void ServerDisconnect(int connectionId) => server?.Disconnect(connectionId);
public override string ServerGetClientAddress(int connectionId)
{
@ -197,7 +209,7 @@ public override string ServerGetClientAddress(int connectionId)
return "unknown";
}
}
public override void ServerStop()
public override void ServerStop()
{
server?.Stop();
server = null;

View File

@ -72,6 +72,9 @@ public override void ClientSend(ArraySegment<byte> segment, int channelId)
// add server data message with connId=1 because 0 is reserved
serverIncoming.Enqueue(new Message(1, EventType.Data, data));
// call event. might be null if no statistics are listening etc.
OnClientDataSent?.Invoke(segment, channelId);
}
}
public override void ClientDisconnect()
@ -147,6 +150,9 @@ public override void ServerSend(int connectionId, ArraySegment<byte> segment, in
// add client data message
clientIncoming.Enqueue(new Message(0, EventType.Data, data));
// call event. might be null if no statistics are listening etc.
OnServerDataSent?.Invoke(connectionId, segment, channelId);
}
}