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 21, 2023
1 parent c6d8c41 commit 37c1cc6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
27 changes: 26 additions & 1 deletion BeatTogether.DedicatedServer.Kernel/PacketDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private void SendInternal(IPlayer player, ref SpanBuffer writer, DeliveryMethod
_logger.Verbose($"Sending packet (SenderId={ServerId}) to player {player.ConnectionId} with UserId {player.UserId} via LiteNet");
Send(player.Endpoint, writer.Data, deliveryMethod);
}

#region Sends
public void SendToNearbyPlayers(INetSerializable packet, DeliveryMethod deliveryMethod)
{
Expand Down Expand Up @@ -99,6 +99,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 @@ -122,6 +123,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 @@ -163,6 +177,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 @@ -176,6 +191,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
14 changes: 8 additions & 6 deletions BeatTogether.DedicatedServer.Kernel/PacketSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public override void OnReceive(EndPoint remoteEndPoint, ref SpanBuffer reader, D

if (packet == null)
{
_logger.Debug($"Failed to create packet.");
// skip any unprocessed bytes
var processedBytes = HandleRead.Offset - prevPosition;
try { HandleRead.SkipBytes((int)length - processedBytes); }
Expand All @@ -132,15 +133,17 @@ public override void OnReceive(EndPoint remoteEndPoint, ref SpanBuffer reader, D
{
if ((DateTime.UtcNow.Ticks - sender.TicksAtLastSyncState) / TimeSpan.TicksPerMillisecond < _playerRegistry.GetMillisBetweenSyncStatePackets())
{
_logger.Verbose($"Skipping sync state packet from {sender.ConnectionId} (Secret='{sender.Secret}').");
return;
}
method = DeliveryMethod.Unreliable;
sender.TicksAtLastSyncState = DateTime.UtcNow.Ticks;
}
if (packet is NodePoseSyncStateDeltaPacket)
{
if((DateTime.UtcNow.Ticks - sender.TicksAtLastSyncStateDelta) / TimeSpan.TicksPerMillisecond < _playerRegistry.GetMillisBetweenSyncStatePackets())
if ((DateTime.UtcNow.Ticks - sender.TicksAtLastSyncStateDelta) / TimeSpan.TicksPerMillisecond < _playerRegistry.GetMillisBetweenSyncStatePackets())
{
_logger.Verbose($"Skipping sync state packet from {sender.ConnectionId} (Secret='{sender.Secret}').");
return;
}
sender.TicksAtLastSyncStateDelta = DateTime.UtcNow.Ticks;
Expand All @@ -151,7 +154,7 @@ public override void OnReceive(EndPoint remoteEndPoint, ref SpanBuffer reader, D
var packetHandler = _serviceProvider.GetService(packetHandlerType);
if (packetHandler is null)
{
if (!packetType.Name.StartsWith("NodePoseSyncState"))
//if (!packetType.Name.StartsWith("NodePoseSyncState"))
_logger.Verbose($"No handler exists for packet of type '{packetType.Name}'.");

// skip any unprocessed bytes
Expand All @@ -168,6 +171,7 @@ public override void OnReceive(EndPoint remoteEndPoint, ref SpanBuffer reader, D
catch
{
// skip any unprocessed bytes
_logger.Debug($"Failed to read packet of type '{packetType.Name}'.");
var processedBytes = HandleRead.Offset - prevPosition;
try { HandleRead.SkipBytes((int)length - processedBytes); }
catch (EndOfBufferException) { _logger.Warning("Packet was an incorrect length"); goto RoutePacket; }
Expand Down Expand Up @@ -200,9 +204,7 @@ private void RoutePacket(IPlayer sender,
$"PacketOption='{routingHeader.PacketOption}' " +
$"(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 @@ -222,7 +224,7 @@ private void RoutePacket(IPlayer sender,
$"PacketOption='{routingHeader.PacketOption}' " +
$"(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 37c1cc6

Please sign in to comment.