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

@ -119,7 +119,13 @@ 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 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();
@ -176,7 +182,13 @@ public override void ServerStart()
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)
{

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);
}
}