fix(core): Tightens the netstate connection (#490)

- [X] Tightens TCP Server rejection/limiting
- [X] Relaxes receive task erroring so it doesn't spam on debug
- [X] Fixes off-by-one with number of netstates connected
- [X] Moves NetState start() to after it is attached to the Instances list.

Notes:
- In debug mode you may get socket exceptions related to the RecvTask or SendTask attempting to send/receive from a dead/broken socket. This can happen naturally in situations where the client is having trouble maintaining the connection, or abruptly disconnects.
This commit is contained in:
Kamron Batman 2021-02-08 13:33:36 -08:00 committed by GitHub
parent 1fd60c6f9f
commit c502af274e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 118 deletions

View file

@ -185,11 +185,11 @@ namespace Server.Network
public bool Seeded { get; set; }
public Pipe<byte> RecvPipe { get; private set; }
public Pipe<byte> RecvPipe { get; }
public Pipe<byte> SendPipe { get; private set; }
public Pipe<byte> SendPipe { get; }
public ISocket Connection { get; private set; }
public ISocket Connection { get; }
public bool CompressionEnabled { get; set; }
@ -999,9 +999,14 @@ namespace Server.Network
}
catch (SocketException ex)
{
// Socket disconnected by client
if (ex.ErrorCode != 54)
{
#if DEBUG
Console.WriteLine(ex);
#endif
}
Disconnect(string.Empty);
}
catch (Exception ex)
@ -1062,8 +1067,8 @@ namespace Server.Network
while (Disposed.TryDequeue(out var ns))
{
ns.Dispose();
TcpServer.Instances.Remove(ns);
ns.Dispose();
}
return count;
@ -1139,17 +1144,12 @@ namespace Server.Network
_disconnectReason = reason;
if (Connection == null)
{
return;
}
Disposed.Enqueue(this);
}
public void TraceDisconnect()
public static void TraceDisconnect(string reason, string ip)
{
if (_disconnectReason == string.Empty)
if (reason == string.Empty)
{
return;
}
@ -1159,8 +1159,8 @@ namespace Server.Network
using StreamWriter op = new StreamWriter("network-disconnects.log", true);
op.WriteLine($"# {DateTime.UtcNow}");
op.WriteLine($"NetState: {this}");
op.WriteLine(_disconnectReason);
op.WriteLine($"NetState: {ip}");
op.WriteLine(reason);
op.WriteLine();
op.WriteLine();
@ -1173,7 +1173,7 @@ namespace Server.Network
private void Dispose()
{
TraceDisconnect();
TraceDisconnect(_disconnectReason, _toString);
RecvPipe.Writer.Close();
SendPipe.Writer.Close();