Skip to content

Commit

Permalink
Allow socket receive to time out, improve socket error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Extremelyd1 committed Jan 19, 2025
1 parent 2dda47c commit e79d861
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
16 changes: 10 additions & 6 deletions HKMP/Networking/Client/ClientDatagramTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,23 @@ public int GetSendLimit() {
/// <param name="waitMillis">The number of milliseconds to wait for data to receive.</param>
/// <returns>The number of bytes that were received, or -1 if no bytes were received in the given time.</returns>
public int Receive(byte[] buf, int off, int len, int waitMillis) {
// TODO: timeout after waitMillis
try {
_socket.ReceiveTimeout = waitMillis;
var numReceived = _socket.Receive(
buf,
off,
len,
SocketFlags.None
SocketFlags.None,
out var socketError
);
// Logger.Debug($"Client socket receive: {numReceived}");
return numReceived;

if (socketError == SocketError.Success) {
return numReceived;
}

Logger.Error($"UDP Socket Error on receive: {socketError}");
} catch (SocketException e) {
Logger.Error($"UDP Socket exception:\n{e}");
Logger.Error($"UDP Socket exception, ErrorCode: {e.ErrorCode}, Socket ErrorCode: {e.SocketErrorCode}, Exception:\n{e}");
}

return -1;
Expand All @@ -70,7 +75,6 @@ public int Receive(byte[] buf, int off, int len, int waitMillis) {
/// <param name="off">The offset in the buffer at which to start sending bytes.</param>
/// <param name="len">The number of bytes to send.</param>
public void Send(byte[] buf, int off, int len) {
// Logger.Debug($"Client sending {len} bytes of data");
_socket.Send(buf, off, len, SocketFlags.None);
}

Expand Down
8 changes: 2 additions & 6 deletions HKMP/Networking/Server/DtlsServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,8 @@ private void SocketReceiveLoop(CancellationToken cancellationToken) {
while (!cancellationToken.IsCancellationRequested) {
EndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);

// Logger.Debug("Blocking on server socket receive");
var numReceived = 0;
var buffer = new byte[1400];
var buffer = new byte[MaxPacketSize];

try {
numReceived = _socket.ReceiveFrom(
Expand All @@ -145,7 +144,7 @@ private void SocketReceiveLoop(CancellationToken cancellationToken) {
ref endPoint
);
} catch (SocketException e) {
Logger.Error($"UDP server socket exception:\n{e}");
Logger.Error($"UDP Socket exception, ErrorCode: {e.ErrorCode}, Socket ErrorCode: {e.SocketErrorCode}, Exception:\n{e}");
}

var ipEndPoint = (IPEndPoint) endPoint;
Expand All @@ -158,7 +157,6 @@ ref endPoint
// Set the IP endpoint of the datagram transport instance so it can send data to the correct IP
serverDatagramTransport.IPEndPoint = ipEndPoint;
} else {
// Logger.Debug($"Received data on server socket from existing client ({ipEndPoint}), length: {numReceived}");
serverDatagramTransport = dtlsServerClient.DatagramTransport;
}

Expand Down Expand Up @@ -249,8 +247,6 @@ private void ClientReceiveLoop(DtlsServerClient dtlsServerClient, CancellationTo
if (numReceived <= 0) {
continue;
}

// Logger.Debug($"DtlsServerClient ({dtlsServerClient.EndPoint}) received {numReceived} bytes of data, invoking event");

try {
DataReceivedEvent?.Invoke(dtlsServerClient, buffer, numReceived);
Expand Down
4 changes: 1 addition & 3 deletions HKMP/Networking/Server/ServerDatagramTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ public void Send(byte[] buf, int off, int len) {
Logger.Error("Cannot send because transport has no endpoint");
return;
}

// Logger.Debug($"Server sending {len} bytes of data to: {IPEndPoint}");


_socket.SendTo(buf, off, len, SocketFlags.None, IPEndPoint);
}

Expand Down

0 comments on commit e79d861

Please sign in to comment.