Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(transports): removing sendAsync from transports #673

Merged
merged 14 commits into from
Mar 8, 2021
Merged
7 changes: 1 addition & 6 deletions Assets/Mirage/Runtime/INetworkClient.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using Cysharp.Threading.Tasks;
using UnityEngine.Events;

namespace Mirage
{
public interface INetworkClient
public interface INetworkClient : IMessageSender
{

/// <summary>
Expand Down Expand Up @@ -38,9 +37,5 @@ public interface INetworkClient
bool IsLocalClient { get; }

void Disconnect();

void Send<T>(T message, int channelId = Channel.Reliable);

UniTask SendAsync<T>(T message, int channelId = Channel.Reliable);
}
}
44 changes: 36 additions & 8 deletions Assets/Mirage/Runtime/INetworkConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@
namespace Mirage
{
/// <summary>
/// An object that can send and receive messages
/// An object that can send messages
/// </summary>
public interface IMessageHandler
public interface IMessageSender
{
void Send<T>(T msg, int channelId = Channel.Reliable);

void Send(ArraySegment<byte> segment, int channelId = Channel.Reliable);
}

/// <summary>
/// An object that can receive messages
/// </summary>
public interface IMessageReceiver
{
void RegisterHandler<T>(Action<INetworkConnection, T> handler);

Expand All @@ -17,22 +27,32 @@ public interface IMessageHandler

void ClearHandlers();

void Send<T>(T msg, int channelId = Channel.Reliable);

UniTask SendAsync<T>(T msg, int channelId = Channel.Reliable);

UniTask SendAsync(ArraySegment<byte> segment, int channelId = Channel.Reliable);

/// <summary>
/// ProcessMessages loop, should loop unitil object is closed
/// </summary>
/// <returns></returns>
UniTask ProcessMessagesAsync();
}

/// <summary>
/// An object that can send notify messages
/// </summary>
public interface INotifySender
{
/// <summary>
/// Sends a message, but notify when it is delivered or lost
/// </summary>
/// <typeparam name="T">type of message to send</typeparam>
/// <param name="msg">message to send</param>
/// <param name="token">a arbitrary object that the sender will receive with their notification</param>
void SendNotify<T>(T msg, object token, int channelId = Channel.Unreliable);
}

/// <summary>
/// An object that can receive notify messages
/// </summary>
public interface INotifyReceiver
{
/// <summary>
/// Raised when a message is delivered
/// </summary>
Expand All @@ -44,6 +64,14 @@ public interface IMessageHandler
event Action<INetworkConnection, object> NotifyLost;
}

/// <summary>
/// An object that can send and receive messages and notify messages
/// </summary>
public interface IMessageHandler : IMessageSender, IMessageReceiver, INotifySender, INotifyReceiver
{

}

/// <summary>
/// An object that can observe NetworkIdentities.
/// this is useful for interest management
Expand Down
4 changes: 2 additions & 2 deletions Assets/Mirage/Runtime/NetworkBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ protected internal void SendServerRpcInternal(Type invokeClass, string cmdName,
payload = writer.ToArraySegment()
};

Client.SendAsync(message, channelId).Forget();
Client.Send(message, channelId);
}

private void ValidateServerRpc(Type invokeClass, string cmdName, bool requireAuthority)
Expand Down Expand Up @@ -284,7 +284,7 @@ protected internal UniTask<T> SendServerRpcWithReturn<T>(Type invokeClass, strin
payload = writer.ToArraySegment()
};

Client.SendAsync(message, channelId).Forget();
Client.Send(message, channelId);

return task;
}
Expand Down
8 changes: 4 additions & 4 deletions Assets/Mirage/Runtime/NetworkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,14 @@ public void Disconnect()
/// <param name="message"></param>
/// <param name="channelId"></param>
/// <returns>True if message was sent.</returns>
public UniTask SendAsync<T>(T message, int channelId = Channel.Reliable)
public void Send<T>(T message, int channelId = Channel.Reliable)
{
return Connection.SendAsync(message, channelId);
Connection.Send(message, channelId);
}

public void Send<T>(T message, int channelId = Channel.Reliable)
public void Send(ArraySegment<byte> segment, int channelId = Channel.Reliable)
{
Connection.SendAsync(message, channelId).Forget();
Connection.Send(segment, channelId);
}

internal void Update()
Expand Down
26 changes: 7 additions & 19 deletions Assets/Mirage/Runtime/NetworkConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,6 @@ public void ClearHandlers()
messageHandlers.Clear();
}

/// <summary>
/// This sends a network message to the connection.
/// </summary>
/// <typeparam name="T">The message type</typeparam>
/// <param name="msg">The message to send</param>
/// <param name="channelId">The transport layer channel to send on.</param>
/// <returns></returns>
public virtual void Send<T>(T msg, int channelId = Channel.Reliable)
{
SendAsync(msg, channelId).Forget();
}

public static void Send<T>(IEnumerable<INetworkConnection> connections, T msg, int channelId = Channel.Reliable)
{
using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter())
Expand All @@ -203,7 +191,7 @@ public static void Send<T>(IEnumerable<INetworkConnection> connections, T msg, i
foreach (INetworkConnection conn in connections)
{
// send to all connections, but don't wait for them
conn.SendAsync(segment, channelId).Forget();
conn.Send(segment, channelId);
count++;
}

Expand All @@ -212,28 +200,28 @@ public static void Send<T>(IEnumerable<INetworkConnection> connections, T msg, i
}

/// <summary>
/// This sends a network message to the connection. You can await it to check for errors
/// This sends a network message to the connection.
/// </summary>
/// <typeparam name="T">The message type</typeparam>
/// <param name="msg">The message to send.</param>
/// <param name="channelId">The transport layer channel to send on.</param>
/// <returns></returns>
public virtual UniTask SendAsync<T>(T msg, int channelId = Channel.Reliable)
public virtual void Send<T>(T msg, int channelId = Channel.Reliable)
{
using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter())
{
// pack message and send allocation free
MessagePacker.Pack(msg, writer);
NetworkDiagnostics.OnSend(msg, channelId, writer.Length, 1);
return SendAsync(writer.ToArraySegment(), channelId);
Send(writer.ToArraySegment(), channelId);
}
}

// internal because no one except Mirage should send bytes directly to
// the client. they would be detected as a message. send messages instead.
public UniTask SendAsync(ArraySegment<byte> segment, int channelId = Channel.Reliable)
public void Send(ArraySegment<byte> segment, int channelId = Channel.Reliable)
{
return connection.SendAsync(segment, channelId);
connection.Send(segment, channelId);
}


Expand Down Expand Up @@ -429,7 +417,7 @@ public void SendNotify<T>(T msg, object token, int channelId = Channel.Unreliabl
MessagePacker.Pack(notifyPacket, writer);
MessagePacker.Pack(msg, writer);
NetworkDiagnostics.OnSend(msg, channelId, writer.Length, 1);
SendAsync(writer.ToArraySegment(), channelId).Forget();
Send(writer.ToArraySegment(), channelId);
lastNotifySentTime = Time.unscaledTime;
}

Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirage/Runtime/Transport/IConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class Channel

public interface IConnection
{
UniTask SendAsync(ArraySegment<byte> data, int channel = Channel.Reliable);
void Send(ArraySegment<byte> data, int channel = Channel.Reliable);

/// <summary>
/// reads a message from connection
Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirage/Runtime/Transport/Kcp/KcpClientConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected async UniTask HandshakeAsync(int bits)

var data = new ArraySegment<byte>(hello, 0, length);
// send a greeting and see if the server replies
await SendAsync(data);
Send(data);

var stream = new MemoryStream();
try
Expand Down
6 changes: 2 additions & 4 deletions Assets/Mirage/Runtime/Transport/Kcp/KcpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,12 @@ private void SendWithChecksum(byte[] data, int length)
}
}

public UniTask SendAsync(ArraySegment<byte> data, int channel = Channel.Reliable)
public void Send(ArraySegment<byte> data, int channel = Channel.Reliable)
{
if (channel == Channel.Reliable)
kcp.Send(data.Array, data.Offset, data.Count);
else if (channel == Channel.Unreliable)
unreliable.Send(data.Array, data.Offset, data.Count);

return UniTask.CompletedTask;
}

/// <summary>
Expand Down Expand Up @@ -283,7 +281,7 @@ public virtual void Disconnect()
{
try
{
SendAsync(Goodby).Forget();
Send(Goodby);
kcp.Flush();
}
catch (SocketException)
Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirage/Runtime/Transport/Kcp/KcpServerConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public KcpServerConnection(Socket socket, EndPoint remoteEndpoint, KcpDelayMode
internal async UniTask HandshakeAsync()
{
// send a greeting and see if the server replies
await SendAsync(Hello);
Send(Hello);
var stream = new MemoryStream();

try
Expand Down
3 changes: 1 addition & 2 deletions Assets/Mirage/Runtime/Transport/PipeConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,12 @@ public async UniTask<int> ReceiveAsync(MemoryStream buffer)
return 0;
}

public UniTask SendAsync(ArraySegment<byte> data, int channel = Channel.Reliable)
public void Send(ArraySegment<byte> data, int channel = Channel.Reliable)
{
// add some data to the writer in the connected connection
// and increase the message count
connected.writer.WriteBytesAndSizeSegment(data);
connected.MessageCount.Release();
return UniTask.CompletedTask;
}
}
}
12 changes: 3 additions & 9 deletions Assets/Mirage/Weaver/Processors/ReaderWriterProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,13 @@ private static bool IsMessageMethod(MethodReference method)
method.Is(typeof(MessagePacker), nameof(MessagePacker.Pack)) ||
method.Is(typeof(MessagePacker), nameof(MessagePacker.GetId)) ||
method.Is(typeof(MessagePacker), nameof(MessagePacker.Unpack)) ||
method.Is<IMessageHandler>(nameof(IMessageHandler.Send)) ||
method.Is<IMessageHandler>(nameof(IMessageHandler.SendAsync)) ||
method.Is<IMessageHandler>(nameof(IMessageHandler.RegisterHandler)) ||
method.Is<IMessageHandler>(nameof(IMessageHandler.UnregisterHandler)) ||
method.Is<IMessageHandler>(nameof(IMessageHandler.Send)) ||
method.Is<IMessageSender>(nameof(IMessageSender.Send)) ||
method.Is<IMessageSender>(nameof(IMessageReceiver.RegisterHandler)) ||
method.Is<IMessageSender>(nameof(IMessageReceiver.UnregisterHandler)) ||
method.Is<NetworkConnection>(nameof(NetworkConnection.Send)) ||
method.Is<NetworkConnection>(nameof(NetworkConnection.SendAsync)) ||
method.Is<NetworkConnection>(nameof(NetworkConnection.RegisterHandler)) ||
method.Is<NetworkConnection>(nameof(NetworkConnection.UnregisterHandler)) ||
method.Is<INetworkClient>(nameof(INetworkClient.Send)) ||
method.Is<INetworkClient>(nameof(INetworkClient.SendAsync)) ||
method.Is<NetworkClient>(nameof(NetworkClient.Send)) ||
method.Is<NetworkClient>(nameof(NetworkClient.SendAsync)) ||
method.Is<NetworkServer>(nameof(NetworkServer.SendToAll)) ||
method.Is<INetworkServer>(nameof(INetworkServer.SendToAll));
}
Expand Down
12 changes: 6 additions & 6 deletions Assets/Tests/Editor/PipeConnectionTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections;
using System.IO;
using System.Net;
Expand All @@ -23,9 +23,9 @@ public void Setup()
(c1, c2) = PipeConnection.CreatePipe();
}

private static UniTask SendData(IConnection c, byte[] data)
private static void SendData(IConnection c, byte[] data)
{
return c.SendAsync(new ArraySegment<byte>(data));
c.Send(new ArraySegment<byte>(data));
}


Expand All @@ -41,16 +41,16 @@ private static async Task ExpectData(IConnection c, byte[] expected)
[UnityTest]
public IEnumerator TestSendAndReceive() => RunAsync(async () =>
{
await SendData(c1, new byte[] { 1, 2, 3, 4 });
SendData(c1, new byte[] { 1, 2, 3, 4 });

await ExpectData(c2, new byte[] { 1, 2, 3, 4 });
});

[UnityTest]
public IEnumerator TestSendAndReceiveMultiple() => RunAsync(async () =>
{
await SendData(c1, new byte[] { 1, 2, 3, 4 });
await SendData(c1, new byte[] { 5, 6, 7, 8 });
SendData(c1, new byte[] { 1, 2, 3, 4 });
SendData(c1, new byte[] { 5, 6, 7, 8 });

await ExpectData(c2, new byte[] { 1, 2, 3, 4 });
await ExpectData(c2, new byte[] { 5, 6, 7, 8 });
Expand Down
6 changes: 3 additions & 3 deletions Assets/Tests/Runtime/Serializers/NetworkConnectionTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using NSubstitute;
using NUnit.Framework;
Expand Down Expand Up @@ -38,7 +38,7 @@ void ParsePacket(ArraySegment<byte> data)
Array.Copy(data.Array, data.Offset, lastSerializedPacket, 0, data.Count);
}

mockTransportConnection.SendAsync(
mockTransportConnection.Send(
Arg.Do<ArraySegment<byte>>(ParsePacket), Channel.Unreliable);

connection = new NetworkConnection(mockTransportConnection);
Expand Down Expand Up @@ -271,4 +271,4 @@ public void NotAcknowledgedYet()

#endregion
}
}
}
Loading