From 55576ec734336db996941970cb4f7368858a27f6 Mon Sep 17 00:00:00 2001 From: EnderdracheLP Date: Thu, 21 Dec 2023 22:40:15 +0100 Subject: [PATCH] Fix packets directly routed were not sent via ENet --- .../PacketDispatcher.cs | 27 ++++++++++++++++++- .../PacketSource.cs | 8 +++--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/BeatTogether.DedicatedServer.Kernel/PacketDispatcher.cs b/BeatTogether.DedicatedServer.Kernel/PacketDispatcher.cs index 3c5d7795..0a7f307a 100644 --- a/BeatTogether.DedicatedServer.Kernel/PacketDispatcher.cs +++ b/BeatTogether.DedicatedServer.Kernel/PacketDispatcher.cs @@ -50,7 +50,7 @@ private void SendInternal(IPlayer player, ref SpanBuffer writer, DeliveryMethod // LiteNet send Send(player.Endpoint, writer.Data, deliveryMethod); } - + #region Sends public void SendToNearbyPlayers(INetSerializable packet, DeliveryMethod deliveryMethod) { @@ -97,6 +97,7 @@ public void SendExcludingPlayer(IPlayer excludedPlayer, INetSerializable packet, if (player.ConnectionId != excludedPlayer.ConnectionId) SendInternal(player, ref writer, deliveryMethod); } + public void SendExcludingPlayer(IPlayer excludedPlayer, INetSerializable[] packets, DeliveryMethod deliveryMethod) { _logger.Debug( @@ -113,6 +114,19 @@ public void SendExcludingPlayer(IPlayer excludedPlayer, INetSerializable[] packe SendInternal(player, ref writer, deliveryMethod); } + public void RouteExcludingPlayer(IPlayer excludedPlayer, ref SpanBuffer writer, DeliveryMethod deliveryMethod) + { + _logger.Debug( + $"Sending routed packet " + + $"(ExcludedId={excludedPlayer.ConnectionId})" + ); + + foreach (IPlayer player in _playerRegistry.Players) + if (player.ConnectionId != excludedPlayer.ConnectionId) + SendInternal(player, ref writer, deliveryMethod); + } + + public void SendFromPlayer(IPlayer fromPlayer, INetSerializable packet, DeliveryMethod deliveryMethod) { _logger.Debug( @@ -154,6 +168,7 @@ public void SendFromPlayerToPlayer(IPlayer fromPlayer, IPlayer toPlayer, INetSer WriteOne(ref writer, packet); SendInternal(toPlayer, ref writer, deliveryMethod); } + public void SendFromPlayerToPlayer(IPlayer fromPlayer, IPlayer toPlayer, INetSerializable[] packets, DeliveryMethod deliveryMethod) { _logger.Debug( @@ -167,6 +182,16 @@ public void SendFromPlayerToPlayer(IPlayer fromPlayer, IPlayer toPlayer, INetSer SendInternal(toPlayer, ref writer, deliveryMethod); } + public void RouteFromPlayerToPlayer(IPlayer fromPlayer, IPlayer toPlayer, ref SpanBuffer writer, DeliveryMethod deliveryMethod) + { + _logger.Debug( + $"Sending routed packet " + + $"(SenderId={fromPlayer.ConnectionId}, ReceiverId={LocalConnectionId})." + ); + + SendInternal(toPlayer, ref writer, deliveryMethod); + } + public void SendToPlayer(IPlayer player, INetSerializable packet, DeliveryMethod deliveryMethod) { _logger.Debug( diff --git a/BeatTogether.DedicatedServer.Kernel/PacketSource.cs b/BeatTogether.DedicatedServer.Kernel/PacketSource.cs index 8154f71a..f1b020ab 100644 --- a/BeatTogether.DedicatedServer.Kernel/PacketSource.cs +++ b/BeatTogether.DedicatedServer.Kernel/PacketSource.cs @@ -137,7 +137,7 @@ public override void OnReceive(EndPoint remoteEndPoint, ref SpanBuffer reader, D } if (packet is NodePoseSyncStateDeltaPacket) { - if((DateTime.UtcNow.Ticks - sender.TicksAtLastSyncStateDelta) / TimeSpan.TicksPerMillisecond < _playerRegistry.GetMillisBetweenSyncStatePackets()) + if ((DateTime.UtcNow.Ticks - sender.TicksAtLastSyncStateDelta) / TimeSpan.TicksPerMillisecond < _playerRegistry.GetMillisBetweenSyncStatePackets()) { return; } @@ -196,9 +196,7 @@ private void RoutePacket(IPlayer sender, $"Routing packet from {routingHeader.SenderId} -> all players " + $"(Secret='{sender.Secret}', DeliveryMethod={deliveryMethod})." ); - foreach (var player in _playerRegistry.Players) - if (player != sender) - _packetDispatcher.Send(player.Endpoint, writer.Data, deliveryMethod); + _packetDispatcher.RouteExcludingPlayer(sender, ref writer, deliveryMethod); } else { @@ -217,7 +215,7 @@ private void RoutePacket(IPlayer sender, $"Routing packet from {routingHeader.SenderId} -> {routingHeader.ReceiverId} " + $"(Secret='{sender.Secret}', DeliveryMethod={deliveryMethod})." ); - _packetDispatcher.Send(receiver.Endpoint, writer.Data, deliveryMethod); + _packetDispatcher.RouteFromPlayerToPlayer(sender, receiver, ref writer, deliveryMethod); } }