Skip to content

Commit

Permalink
Fix for packet routing, seems like that part works
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-r-elp committed Dec 27, 2023
1 parent 936b5e5 commit b2b61f6
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions BeatTogether.DedicatedServer.Kernel/PacketSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,16 @@ public override void OnReceive(EndPoint remoteEndPoint, ref SpanBuffer reader, D
while (HandleRead.RemainingSize > 0)
{
uint length;
try { length = HandleRead.ReadVarUInt(); }
try { length = HandleRead.ReadVarUInt(); _logger.Verbose($"HandleRead read length {length} cast {(int)length} available bytes {HandleRead.RemainingSize}"); }
catch (EndOfBufferException) { _logger.Warning("Packet was an incorrect length"); goto RoutePacket; }

_logger.Verbose($"BytesRead");

if (length < HandleRead.RemainingSize)
{
_logger.Information($"Packet might be a MultiPacket length {length} remainingbytes {HandleRead.RemainingSize}");
}

if (HandleRead.RemainingSize < length)
{
_logger.Warning($"Packet fragmented (RemainingSize={HandleRead.RemainingSize}, Expected={length}).");
Expand Down Expand Up @@ -161,10 +169,9 @@ public override void OnReceive(EndPoint remoteEndPoint, ref SpanBuffer reader, D
//reader.SkipBytes(bytesToRead);
//RoutePacket(sender, routingHeader, ref readerSlice, method);

// TODO: Check packet registry
var processedBytes = Math.Min(HandleRead.Offset - prevPosition, HandleRead.RemainingSize);
int lengthToRead = (int)length + 2;
var bytesToRead = Math.Min(lengthToRead, HandleRead.RemainingSize);
int lengthToRead = (int)length;
var bytesToRead = Math.Min(lengthToRead - processedBytes, HandleRead.RemainingSize);
var bytes = HandleRead.ReadBytes(bytesToRead);
QueueRoutePacket(sender, routingHeader, ref writer, ref legacyWriter, bytes, lengthToRead, packetId);
_logger.Verbose(
Expand Down Expand Up @@ -224,16 +231,17 @@ public override void OnReceive(EndPoint remoteEndPoint, ref SpanBuffer reader, D
// Is packet meant to be routed?
if (routingHeader.ReceiverId != 0)
{
var processedBytes = Math.Min(HandleRead.Offset - prevPosition, HandleRead.RemainingSize);
int lengthToRead = (int)length + 2;
var bytesToRead = Math.Min(lengthToRead, HandleRead.RemainingSize);
var processedBytes = HandleRead.Offset - prevPosition /*Math.Min(HandleRead.Offset - prevPosition, HandleRead.RemainingSize)*/;
int lengthToRead = (int)length;
var bytesToRead = lengthToRead - processedBytes /*Math.Min(lengthToRead - processedBytes, HandleRead.RemainingSize)*/;
var bytes = HandleRead.ReadBytes(bytesToRead);
QueueRoutePacket(sender, routingHeader, ref writer, ref legacyWriter, bytes, lengthToRead, packetId);
_logger.Verbose(
$"Attempting to Route unhandled packet from {sender.ConnectionId} -> {(routingHeader.ReceiverId == AllConnectionIds ? "all players" : routingHeader.ReceiverId)} " +
$"PacketOption='{routingHeader.PacketOption}' " +
$"ProcessedBytes='{processedBytes}' BytesToRead='{bytesToRead}' " +
$"Length='{length}' ProcessedBytes='{processedBytes}' BytesToRead='{bytesToRead}' " +
$"BytesRemainingSize='{HandleRead.RemainingSize}' " +
$"lengthToRead - processedBytes = '{lengthToRead - processedBytes}' Offset - prevPosition = '{HandleRead.Offset - prevPosition}' " +
$"BytesRemainingData='{BitConverter.ToString(HandleRead.RemainingData.ToArray())}' " +
$"BytesRead='{BitConverter.ToString(bytes.ToArray())}' " +
$"(Secret='{sender.Secret}', DeliveryMethod={method})."
Expand Down Expand Up @@ -334,8 +342,6 @@ private void QueueRoutePacket(
IPlayer sender, (byte SenderId, byte ReceiverId, PacketOption PacketOption) routingHeader,
ref SpanBuffer writer, ref SpanBuffer legacyWriter, Span<byte> data, int length, Queue<(byte? basePacketId, string? mpCorePacketId)> packetIds)
{
if (writer.Offset == 0 && legacyWriter.Offset == 0)
return;
routingHeader.SenderId = sender.ConnectionId;
if (routingHeader.ReceiverId == AllConnectionIds)
{
Expand Down Expand Up @@ -405,6 +411,11 @@ private void QueueRoutePacket(

private void SendQueue(IPlayer sender, (byte SenderId, byte ReceiverId, PacketOption PacketOption) routingHeader, ref SpanBuffer writer, ref SpanBuffer legacyWriter, DeliveryMethod deliveryMethod)
{
if (writer.Offset == 0 && legacyWriter.Offset == 0)
{
_logger.Verbose($"No packets in SendQueue to send");
return;
}
_logger.Verbose($"Sending Queue for RoutedPacket");
if (routingHeader.ReceiverId == AllConnectionIds)
{
Expand Down

0 comments on commit b2b61f6

Please sign in to comment.