Skip to content

Commit

Permalink
Report connection errors when doing packet capture
Browse files Browse the repository at this point in the history
Properly free sockets if no connection can be established

Signed-off-by: Alexis Maiquez Murcia <[email protected]>
  • Loading branch information
Almamu committed Apr 7, 2022
1 parent 59cee68 commit 7408dbe
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions Editor/Capture/CaptureServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,17 @@ public void Listen()

private void ServerConnectionAccept(IAsyncResult ar)
{
Socket client = null;
Socket server = null;
try
{
Socket client = this.Socket.EndAccept(ar);
client = this.Socket.EndAccept(ar);
// begin accepting again as quickly as possible
this.Socket.BeginAccept(ServerConnectionAccept, null);
// do some logging to get a better overview of what's happening
Log.Information("Received new connection from " + client.GetRemoteAddress() + ". Connecting to the server");
// create a socket to communicate with the server
Socket server = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
server = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
// ensure we support both ipv4 and ipv6
server.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
// connect to the server
Expand All @@ -70,18 +72,55 @@ private void ServerConnectionAccept(IAsyncResult ar)
CaptureSocket socket = new CaptureSocket(++this.mClientCount, this.Processor, server, client);

this.OnStatusChange?.Invoke(this, "Accepted new connection on " + client.GetRemoteAddress());
return;
}
catch(ObjectDisposedException)
{
// ignored, object already disposed usually means the socket got disposed
}
catch(SocketException ex)
{
if (ex.SocketErrorCode == SocketError.ConnectionRefused)
{
Log.Warning("Cannot establish connection to the EVEmu server on {CaptureSettings.ServerAddress} {CaptureSettings.ServerPort}", CaptureSettings.ServerAddress, CaptureSettings.ServerPort);
this.OnStatusChange?.Invoke(this, $"Cannot establish connection to the EVEmu server on {CaptureSettings.ServerAddress} {CaptureSettings.ServerPort}");
}
else
{
this.ReportError(ex);
}
}
catch (Exception ex)
{
Log.Error($"Exception detected:{Environment.NewLine}{ex.Message}{Environment.NewLine}Stack trace: {Environment.NewLine}{ex.StackTrace}");
MessageBox.Show($"{ex.Message}{Environment.NewLine}Stack trace: {Environment.NewLine}{ex.StackTrace}", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.ReportError(ex);
}

// dispose of the sockets so they're closed
try
{
server?.Dispose();
}
catch(ObjectDisposedException)
{

}

try
{
client?.Dispose();
}
catch (ObjectDisposedException)
{

}
}

private void ReportError(Exception ex)
{
Log.Error($"Exception detected:{Environment.NewLine}{ex.Message}{Environment.NewLine}Stack trace: {Environment.NewLine}{ex.StackTrace}");
MessageBox.Show($"{ex.Message}{Environment.NewLine}Stack trace: {Environment.NewLine}{ex.StackTrace}", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

public void Dispose()
{
this.Socket.Dispose();
Expand Down

0 comments on commit 7408dbe

Please sign in to comment.