Skip to content

Commit

Permalink
fix(SocketLayer): fixing socket receive trying to handle message when…
Browse files Browse the repository at this point in the history
… length is negative

UDP socket returns -1 when there is socket error. mirage would pass this -1 into metrics and try to handle the message. Instead Mirage should just ignore these messages
  • Loading branch information
James-Frowen committed May 3, 2024
1 parent 3be18d2 commit b078387
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions Assets/Mirage/Runtime/SocketLayer/Peer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,15 @@ private void ReceiveLoop()
{
using (var buffer = _bufferPool.Take())
{
while (_socket.Poll())
// check active, because socket might have been closed by message handler
while (_active && _socket.Poll())
{
var length = _socket.Receive(buffer.array, out var receiveEndPoint);
if (length < 0)
{
_logger.Log(LogType.Warning, $"Receive returned less than 0 bytes, length={length}");
continue;
}

// this should never happen. buffer size is only MTU, if socket returns higher length then it has a bug.
if (length > _maxPacketSize)
Expand All @@ -262,9 +268,6 @@ private void ReceiveLoop()
_metrics?.OnReceiveUnconnected(length);
HandleNewConnection(receiveEndPoint, packet);
}

// socket might have been closed by message handler
if (!_active) { break; }
}
}
}
Expand Down

0 comments on commit b078387

Please sign in to comment.