Skip to content

Commit

Permalink
Fix packets directly routed were not sent via ENet
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-r-elp committed Dec 22, 2023
1 parent 491bc96 commit 55576ec
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
27 changes: 26 additions & 1 deletion BeatTogether.DedicatedServer.Kernel/PacketDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
8 changes: 3 additions & 5 deletions BeatTogether.DedicatedServer.Kernel/PacketSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
{
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 55576ec

Please sign in to comment.