Skip to content

Commit

Permalink
refactor: moving static send to NetworkServer (MirageNet#692)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: moving NetworkPlayer.Send to NetworkServer.SendToMany
  • Loading branch information
James-Frowen authored Mar 13, 2021
1 parent bfffa35 commit 5b19dc3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Assets/Mirage/Components/LobbyReady.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void SendToReady<T>(NetworkIdentity identity, T msg, bool includeOwner =
}
}

NetworkPlayer.Send(connectionsCache, msg, channelId);
NetworkServer.SendToMany(connectionsCache, msg, channelId);
}
}
}
4 changes: 2 additions & 2 deletions Assets/Mirage/Runtime/NetworkIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,7 @@ internal void SendToObservers<T>(T msg, bool includeOwner = true, int channelId

if (includeOwner)
{
NetworkPlayer.Send(observers, msg, channelId);
NetworkServer.SendToMany(observers, msg, channelId);
}
else
{
Expand All @@ -1291,7 +1291,7 @@ internal void SendToObservers<T>(T msg, bool includeOwner = true, int channelId
connectionsExcludeSelf.Add(conn);
}
}
NetworkPlayer.Send(connectionsExcludeSelf, msg, channelId);
NetworkServer.SendToMany(connectionsExcludeSelf, msg, channelId);
}
}

Expand Down
20 changes: 0 additions & 20 deletions Assets/Mirage/Runtime/NetworkPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,26 +174,6 @@ public void ClearHandlers()
messageHandlers.Clear();
}

public static void Send<T>(IEnumerable<INetworkPlayer> connections, T msg, int channelId = Channel.Reliable)
{
using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter())
{
// pack message into byte[] once
MessagePacker.Pack(msg, writer);
var segment = writer.ToArraySegment();
int count = 0;

foreach (INetworkPlayer conn in connections)
{
// send to all connections, but don't wait for them
conn.Send(segment, channelId);
count++;
}

NetworkDiagnostics.OnSend(msg, channelId, segment.Count, count);
}
}

/// <summary>
/// This sends a network message to the connection.
/// </summary>
Expand Down
22 changes: 21 additions & 1 deletion Assets/Mirage/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,27 @@ internal void SetLocalConnection(NetworkClient client, IConnection tconn)
public void SendToAll<T>(T msg, int channelId = Channel.Reliable)
{
if (logger.LogEnabled()) logger.Log("Server.SendToAll id:" + typeof(T));
NetworkPlayer.Send(connections, msg, channelId);
SendToMany(connections, msg, channelId);
}

public static void SendToMany<T>(IEnumerable<INetworkPlayer> connections, T msg, int channelId = Channel.Reliable)
{
using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter())
{
// pack message into byte[] once
MessagePacker.Pack(msg, writer);
var segment = writer.ToArraySegment();
int count = 0;

foreach (INetworkPlayer conn in connections)
{
// send to all connections, but don't wait for them
conn.Send(segment, channelId);
count++;
}

NetworkDiagnostics.OnSend(msg, channelId, segment.Count, count);
}
}

async UniTaskVoid ConnectionAcceptedAsync(INetworkPlayer conn)
Expand Down
1 change: 1 addition & 0 deletions Assets/Mirage/Weaver/Processors/ReaderWriterProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ private static bool IsMessageMethod(MethodReference method)
method.Is<NetworkPlayer>(nameof(NetworkPlayer.UnregisterHandler)) ||
method.Is<NetworkClient>(nameof(NetworkClient.Send)) ||
method.Is<NetworkServer>(nameof(NetworkServer.SendToAll)) ||
method.Is<NetworkServer>(nameof(NetworkServer.SendToMany)) ||
method.Is<INetworkServer>(nameof(INetworkServer.SendToAll));
}

Expand Down

0 comments on commit 5b19dc3

Please sign in to comment.