Skip to content

Commit

Permalink
Attempt properly route packets between game versions
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-r-elp committed Dec 24, 2023
1 parent 90e2eaf commit e3328ce
Showing 1 changed file with 73 additions and 16 deletions.
89 changes: 73 additions & 16 deletions BeatTogether.DedicatedServer.Kernel/PacketSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ public override void OnReceive(EndPoint remoteEndPoint, ref SpanBuffer reader, D
}
SpanBuffer HandleRead = new(reader.RemainingData.ToArray());

_logger.Verbose(
$"Received packet from {routingHeader.SenderId} ({sender.ClientVersionString}) " +
$"-> {routingHeader.ReceiverId} " +
$"PacketOption='{routingHeader.PacketOption}' " +
$"(Secret='{sender.Secret}', DeliveryMethod={method})."
);

while (HandleRead.RemainingSize > 0)
{
Expand Down Expand Up @@ -120,10 +126,35 @@ 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); }
catch (EndOfBufferException) { _logger.Warning("Packet was an incorrect length"); goto RoutePacket; }

//Is packet meant to be routed?
if (routingHeader.ReceiverId != 0)
{
// Route packet to other players and skip read bytes
//var processedBytes = HandleRead.Offset - prevPosition;
//var bytesToRead = Math.Min((int)length - processedBytes, HandleRead.RemainingSize);
//var readerSlice = new SpanBuffer(HandleRead.ReadBytes(bytesToRead));
//reader.SkipBytes(bytesToRead);
//RoutePacket(sender, routingHeader, ref readerSlice, method);

var processedBytes = Math.Min(HandleRead.Offset - prevPosition, HandleRead.RemainingSize);
var bytesToRead = Math.Min((int)length - 1, HandleRead.RemainingSize);
_logger.Verbose(
$"Attempting to Route unhandled packet from {routingHeader.SenderId} -> {routingHeader.ReceiverId} " +
$"PacketOption='{routingHeader.PacketOption}' " +
$"ProcessedBytes='{processedBytes}' BytesToRead='{bytesToRead}' " +
$"(Secret='{sender.Secret}', DeliveryMethod={method})."
);
var readerSlice = new SpanBuffer(HandleRead.ReadBytes(bytesToRead));
RoutePacket(sender, routingHeader, ref readerSlice, method);
}
else
{
//skip any unprocessed bytes
var processedBytes = HandleRead.Offset - prevPosition;
try { HandleRead.SkipBytes((int)length - processedBytes); }
catch (EndOfBufferException) { _logger.Warning("Packet was an incorrect length"); goto RoutePacket; }
}
continue;
}
if(packet is NoteSpawnPacket || packet is ObstacleSpawnPacket || packet is SliderSpawnPacket) //Note packet logic
Expand Down Expand Up @@ -162,10 +193,32 @@ public override void OnReceive(EndPoint remoteEndPoint, ref SpanBuffer reader, D
//if (!packetType.Name.StartsWith("NodePoseSyncState"))
_logger.Verbose($"No handler exists for packet of type '{packetType.Name}'.");

// skip any unprocessed bytes
var processedBytes = HandleRead.Offset - prevPosition;
try { HandleRead.SkipBytes((int)length - processedBytes); }
catch (EndOfBufferException) { _logger.Warning("Packet was an incorrect length"); goto RoutePacket; }
// Is packet meant to be routed?
if (routingHeader.ReceiverId != 0)
{
// Route packet to other players and skip read bytes
//var bytesToRead = Math.Min((int)length, HandleRead.RemainingSize);
//var remainingData = reader.RemainingData;
//var readerSlice = new SpanBuffer(HandleRead.Data.Slice(HandleRead.Offset, bytesToRead));
var processedBytes = Math.Min(HandleRead.Offset - prevPosition, HandleRead.RemainingSize);
var bytesToRead = Math.Min((int)length - 1, HandleRead.RemainingSize);
_logger.Verbose(
$"Attempting to Route unhandled packet from {routingHeader.SenderId} -> {routingHeader.ReceiverId} " +
$"PacketOption='{routingHeader.PacketOption}' " +
$"ProcessedBytes='{processedBytes}' BytesToRead='{bytesToRead}' " +
$"(Secret='{sender.Secret}', DeliveryMethod={method})."
);
var readerSlice = new SpanBuffer(HandleRead.ReadBytes(bytesToRead));
//reader.SkipBytes(processedBytes);
RoutePacket(sender, routingHeader, ref readerSlice, method);
}
else
{
//skip any unprocessed bytes
var processedBytes = HandleRead.Offset - prevPosition;
try { HandleRead.SkipBytes((int)length - processedBytes); }
catch (EndOfBufferException) { _logger.Warning("Packet was an incorrect length"); goto RoutePacket; }
}
continue;
}
else if (packetHandler is null && packet is IVersionedNetSerializable versionedPacket)
Expand All @@ -175,7 +228,7 @@ public override void OnReceive(EndPoint remoteEndPoint, ref SpanBuffer reader, D
_logger.Debug($"Reading versioned packet of type '{packetType.Name}' with version '{clientVersion}'.");
versionedPacket.ReadFrom(ref HandleRead, clientVersion);
}
catch(Exception e)
catch (Exception e)
{
_logger.Error($"Failed to read packet of type '{packetType.Name}' with version '{clientVersion}'.");
_logger.Error(e.Message);
Expand All @@ -188,7 +241,7 @@ public override void OnReceive(EndPoint remoteEndPoint, ref SpanBuffer reader, D
}
if (routingHeader.ReceiverId == AllConnectionIds)
_packetDispatcher.SendExcludingPlayer(sender, versionedPacket, method);
else
else if (routingHeader.ReceiverId != 0)
{
if (!_playerRegistry.TryGetPlayer(routingHeader.ReceiverId, out var receiver))
{
Expand All @@ -203,7 +256,6 @@ public override void OnReceive(EndPoint remoteEndPoint, ref SpanBuffer reader, D
continue;
}


try
{
if (packet is IVersionedNetSerializable versionedPacket)
Expand All @@ -226,10 +278,18 @@ public override void OnReceive(EndPoint remoteEndPoint, ref SpanBuffer reader, D

((Abstractions.IPacketHandler)packetHandler).Handle(sender, packet);
}
return;
RoutePacket:
//Is this packet meant to be routed?
//Is this packet meant to be routed ?
if (routingHeader.ReceiverId != 0)
{
_logger.Verbose(
$"Reached route packet function {routingHeader.SenderId} -> {routingHeader.ReceiverId} " +
$"PacketOption='{routingHeader.PacketOption}' " +
$"(Secret='{sender.Secret}', DeliveryMethod={method}, RemainingSize={reader.RemainingSize})."
);
RoutePacket(sender, routingHeader, ref reader, method);
}
}

#region Private Methods
Expand All @@ -242,9 +302,6 @@ private void RoutePacket(IPlayer sender,
var writer = new SpanBuffer(stackalloc byte[412]);
if (routingHeader.ReceiverId == AllConnectionIds)
{
//if (isLegacyPlayer)
// writer.WriteLegacyRoutingHeader(routingHeader.SenderId, routingHeader.ReceiverId, routingHeader.PacketOption);
//else
var legacyWriter = new SpanBuffer(stackalloc byte[412]);
legacyWriter.WriteLegacyRoutingHeader(routingHeader.SenderId, routingHeader.ReceiverId, routingHeader.PacketOption);
writer.WriteRoutingHeader(routingHeader.SenderId, routingHeader.ReceiverId, routingHeader.PacketOption);
Expand Down Expand Up @@ -272,7 +329,7 @@ private void RoutePacket(IPlayer sender,
}

if (receiver.ClientVersion < ClientVersions.NewPacketVersion)
writer.WriteLegacyRoutingHeader(routingHeader.SenderId, LocalConnectionId);
writer.WriteLegacyRoutingHeader(routingHeader.SenderId, LocalConnectionId, routingHeader.PacketOption);
else
writer.WriteRoutingHeader(routingHeader.SenderId, LocalConnectionId, routingHeader.PacketOption);
writer.WriteBytes(reader.RemainingData);
Expand Down

0 comments on commit e3328ce

Please sign in to comment.