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:
parent
1fd60c6f9f
commit
c502af274e
3 changed files with 48 additions and 118 deletions
|
|
@ -185,11 +185,11 @@ namespace Server.Network
|
||||||
|
|
||||||
public bool Seeded { get; set; }
|
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; }
|
public bool CompressionEnabled { get; set; }
|
||||||
|
|
||||||
|
|
@ -999,9 +999,14 @@ namespace Server.Network
|
||||||
}
|
}
|
||||||
catch (SocketException ex)
|
catch (SocketException ex)
|
||||||
{
|
{
|
||||||
|
// Socket disconnected by client
|
||||||
|
if (ex.ErrorCode != 54)
|
||||||
|
{
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
Console.WriteLine(ex);
|
Console.WriteLine(ex);
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
Disconnect(string.Empty);
|
Disconnect(string.Empty);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|
@ -1062,8 +1067,8 @@ namespace Server.Network
|
||||||
|
|
||||||
while (Disposed.TryDequeue(out var ns))
|
while (Disposed.TryDequeue(out var ns))
|
||||||
{
|
{
|
||||||
ns.Dispose();
|
|
||||||
TcpServer.Instances.Remove(ns);
|
TcpServer.Instances.Remove(ns);
|
||||||
|
ns.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
|
|
@ -1139,17 +1144,12 @@ namespace Server.Network
|
||||||
|
|
||||||
_disconnectReason = reason;
|
_disconnectReason = reason;
|
||||||
|
|
||||||
if (Connection == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Disposed.Enqueue(this);
|
Disposed.Enqueue(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void TraceDisconnect()
|
public static void TraceDisconnect(string reason, string ip)
|
||||||
{
|
{
|
||||||
if (_disconnectReason == string.Empty)
|
if (reason == string.Empty)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -1159,8 +1159,8 @@ namespace Server.Network
|
||||||
using StreamWriter op = new StreamWriter("network-disconnects.log", true);
|
using StreamWriter op = new StreamWriter("network-disconnects.log", true);
|
||||||
op.WriteLine($"# {DateTime.UtcNow}");
|
op.WriteLine($"# {DateTime.UtcNow}");
|
||||||
|
|
||||||
op.WriteLine($"NetState: {this}");
|
op.WriteLine($"NetState: {ip}");
|
||||||
op.WriteLine(_disconnectReason);
|
op.WriteLine(reason);
|
||||||
|
|
||||||
op.WriteLine();
|
op.WriteLine();
|
||||||
op.WriteLine();
|
op.WriteLine();
|
||||||
|
|
@ -1173,7 +1173,7 @@ namespace Server.Network
|
||||||
|
|
||||||
private void Dispose()
|
private void Dispose()
|
||||||
{
|
{
|
||||||
TraceDisconnect();
|
TraceDisconnect(_disconnectReason, _toString);
|
||||||
|
|
||||||
RecvPipe.Writer.Close();
|
RecvPipe.Writer.Close();
|
||||||
SendPipe.Writer.Close();
|
SendPipe.Writer.Close();
|
||||||
|
|
|
||||||
|
|
@ -1,38 +0,0 @@
|
||||||
/*************************************************************************
|
|
||||||
* ModernUO *
|
|
||||||
* Copyright 2019-2020 - ModernUO Development Team *
|
|
||||||
* Email: hi@modernuo.com *
|
|
||||||
* File: NetworkState.cs *
|
|
||||||
* *
|
|
||||||
* This program is free software: you can redistribute it and/or modify *
|
|
||||||
* it under the terms of the GNU General Public License as published by *
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or *
|
|
||||||
* (at your option) any later version. *
|
|
||||||
* *
|
|
||||||
* You should have received a copy of the GNU General Public License *
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
||||||
*************************************************************************/
|
|
||||||
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using System.Threading;
|
|
||||||
|
|
||||||
namespace Server.Network
|
|
||||||
{
|
|
||||||
public class NetworkState
|
|
||||||
{
|
|
||||||
public static readonly NetworkState PauseState = new(true);
|
|
||||||
public static readonly NetworkState ResumeState = new(false);
|
|
||||||
|
|
||||||
public bool Paused { get; }
|
|
||||||
|
|
||||||
internal NetworkState(bool paused) => Paused = paused;
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static bool Pause(ref NetworkState state) =>
|
|
||||||
Interlocked.CompareExchange(ref state, PauseState, ResumeState) != PauseState;
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static bool Resume(ref NetworkState state) =>
|
|
||||||
Interlocked.CompareExchange(ref state, ResumeState, PauseState) != ResumeState;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -26,9 +26,9 @@ namespace Server.Network
|
||||||
{
|
{
|
||||||
public static class TcpServer
|
public static class TcpServer
|
||||||
{
|
{
|
||||||
private static NetworkState _networkState = NetworkState.ResumeState;
|
private const int MaxConnectionsPerLoop = 250;
|
||||||
|
|
||||||
// Sanity. 256 * 1024 * 5000 = ~1.3GB of ram
|
// Sanity. 256 * 1024 * 4096 = ~1.3GB of ram
|
||||||
public static int MaxConnections { get; set; } = 4096;
|
public static int MaxConnections { get; set; } = 4096;
|
||||||
|
|
||||||
private const long _listenerErrorMessageDelay = 10000; // 10 seconds
|
private const long _listenerErrorMessageDelay = 10000; // 10 seconds
|
||||||
|
|
@ -122,50 +122,15 @@ namespace Server.Network
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Pauses the TcpServer and stops accepting new sockets.
|
|
||||||
* This is thread-safe without using locks.
|
|
||||||
*/
|
|
||||||
public static void Pause()
|
|
||||||
{
|
|
||||||
NetworkState.Pause(ref _networkState);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Resumes accepting sockets on the TcpServer.
|
|
||||||
* This is thread-safe using a lock on the listeners
|
|
||||||
*/
|
|
||||||
public static void Resume()
|
|
||||||
{
|
|
||||||
if (!NetworkState.Resume(ref _networkState))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
lock (Listeners)
|
|
||||||
{
|
|
||||||
foreach (var listener in Listeners)
|
|
||||||
{
|
|
||||||
listener.BeginAcceptingSockets();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int Slice()
|
public static int Slice()
|
||||||
{
|
{
|
||||||
int count = 0;
|
int count = 0;
|
||||||
var limit = _connectedQueue.Count;
|
|
||||||
|
|
||||||
while (_connectedQueue.Count > 0 && --limit >= 0)
|
while (++count <= MaxConnectionsPerLoop && _connectedQueue.TryDequeue(out var ns))
|
||||||
{
|
{
|
||||||
if (!_connectedQueue.TryDequeue(out var ns))
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
count++;
|
|
||||||
Instances.Add(ns);
|
Instances.Add(ns);
|
||||||
ns.WriteConsole("Connected. [{0} Online]", Instances.Count);
|
ns.WriteConsole("Connected. [{0} Online]", Instances.Count);
|
||||||
|
ns.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
|
|
@ -175,55 +140,58 @@ namespace Server.Network
|
||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (_networkState.Paused)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Socket socket = null;
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
socket = await listener.AcceptSocketAsync().ConfigureAwait(false);
|
var socket = await listener.AcceptSocketAsync();
|
||||||
|
var rejected = false;
|
||||||
if (Instances.Count >= MaxConnections)
|
if (Instances.Count >= MaxConnections)
|
||||||
{
|
{
|
||||||
socket.Send(_socketRejected, SocketFlags.None);
|
rejected = true;
|
||||||
socket.Shutdown(SocketShutdown.Both);
|
|
||||||
socket.Close();
|
var ticks = Core.TickCount;
|
||||||
throw new MaxConnectionsException();
|
|
||||||
|
if (_nextMaximumSocketsReachedMessage <= ticks)
|
||||||
|
{
|
||||||
|
if (socket.RemoteEndPoint is IPEndPoint ipep)
|
||||||
|
{
|
||||||
|
var ip = ipep.Address.ToString();
|
||||||
|
Console.WriteLine("Listener {0}: Failed (Maximum connections reached)", ip);
|
||||||
|
NetState.TraceDisconnect("Maximum connections reached.", ip);
|
||||||
|
}
|
||||||
|
|
||||||
|
_nextMaximumSocketsReachedMessage = ticks + _listenerErrorMessageDelay;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var args = new SocketConnectEventArgs(socket);
|
var args = new SocketConnectEventArgs(socket);
|
||||||
EventSink.InvokeSocketConnect(args);
|
EventSink.InvokeSocketConnect(args);
|
||||||
|
|
||||||
if (!args.AllowConnection)
|
if (!args.AllowConnection)
|
||||||
|
{
|
||||||
|
rejected = true;
|
||||||
|
if (socket.RemoteEndPoint is IPEndPoint ipep)
|
||||||
|
{
|
||||||
|
var ip = ipep.Address.ToString();
|
||||||
|
NetState.TraceDisconnect("Rejected by socket event handler", ip);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rejected)
|
||||||
{
|
{
|
||||||
socket.Send(_socketRejected, SocketFlags.None);
|
socket.Send(_socketRejected, SocketFlags.None);
|
||||||
socket.Shutdown(SocketShutdown.Both);
|
socket.Shutdown(SocketShutdown.Both);
|
||||||
socket.Close();
|
socket.Close();
|
||||||
}
|
}
|
||||||
}
|
else
|
||||||
catch (MaxConnectionsException)
|
|
||||||
{
|
|
||||||
var ticks = Core.TickCount;
|
|
||||||
|
|
||||||
if (_nextMaximumSocketsReachedMessage <= ticks)
|
|
||||||
{
|
{
|
||||||
if (socket?.RemoteEndPoint is IPEndPoint ipep)
|
var ns = new NetState(new NetworkSocket(socket));
|
||||||
{
|
_connectedQueue.Enqueue(ns);
|
||||||
Console.WriteLine("Listener {0}:{1}: Failed (Maximum connections reached)", ipep.Address, ipep.Port);
|
|
||||||
}
|
|
||||||
_nextMaximumSocketsReachedMessage = ticks + _listenerErrorMessageDelay;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
// ignored
|
// ignored
|
||||||
}
|
}
|
||||||
|
|
||||||
var ns = new NetState(new NetworkSocket(socket));
|
|
||||||
_connectedQueue.Enqueue(ns);
|
|
||||||
ns.Start();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue