fix(core): Fixes a few netstate issues (#436)

- [X] Fixes the order of disconnect/dispose
- [X] Removes IsDisposing
This commit is contained in:
Kamron Batman 2021-02-01 20:37:51 -08:00 committed by GitHub
parent f147cb119f
commit c6e93a6814
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 29 deletions

View file

@ -50,15 +50,12 @@ namespace Server.Network
public static NetStateCreatedCallback CreatedCallback { get; set; }
private readonly string _toString;
private int _disposing;
private ClientVersion _version;
private byte[] _recvBuffer;
private byte[] _sendBuffer;
private long _nextActivityCheck;
private volatile bool _running;
private volatile DecodePacket _packetDecoder;
private volatile EncodePacket _packetEncoder;
private bool _flushQueued = false;
private bool _flushQueued;
internal int _authId;
internal int _seed;
@ -87,10 +84,10 @@ namespace Server.Network
HuePickers = new List<HuePicker>();
Menus = new List<IMenu>();
Trades = new List<SecureTrade>();
_recvBuffer = GC.AllocateUninitializedArray<byte>(RecvPipeSize);
RecvPipe = new Pipe<byte>(_recvBuffer);
_sendBuffer = GC.AllocateUninitializedArray<byte>(SendPipeSize);
SendPipe = new Pipe<byte>(_sendBuffer);
var recvBuffer = GC.AllocateUninitializedArray<byte>(RecvPipeSize);
RecvPipe = new Pipe<byte>(recvBuffer);
var sendBuffer = GC.AllocateUninitializedArray<byte>(SendPipeSize);
SendPipe = new Pipe<byte>(sendBuffer);
_nextActivityCheck = Core.TickCount + 30000;
try
@ -164,8 +161,6 @@ namespace Server.Network
public IAccount Account { get; set; }
public bool IsDisposing => _disposing != 0;
public int CompareTo(NetState other) => string.CompareOrdinal(_toString, other?._toString);
public void ValidateAllTrades()
@ -751,12 +746,12 @@ namespace Server.Network
public virtual void Dispose()
{
if (Connection == null || Interlocked.CompareExchange(ref _disposing, 1, 0) == 1)
if (Connection == null || !_running)
{
return;
}
SendPipe.Writer.Close();
_running = false;
try
{
@ -781,11 +776,10 @@ namespace Server.Network
private void Disconnect()
{
_running = false;
Connection = null;
RecvPipe.Writer.Flush();
RecvPipe.Writer.Close();
SendPipe.Writer.Close();
var m = Mobile;
var a = Account;

View file

@ -26,22 +26,22 @@ namespace Server.Network
{
public static class TcpServer
{
private static NetworkState m_NetworkState = NetworkState.ResumeState;
private static NetworkState _networkState = NetworkState.ResumeState;
// Sanity. 256 * 1024 * 5000 = ~1.3GB of ram
public static int MaxConnections { get; set; } = 5000;
public static int MaxConnections { get; set; } = 4096;
private const long _listenerErrorMessageDelay = 10000; // 10 seconds
private static long _nextMaximumSocketsReachedMessage;
// AccountLoginReject BadComm
private static readonly byte[] socketRejected = { 0x82, 0xFF };
private static readonly byte[] _socketRejected = { 0x82, 0xFF };
public static IPEndPoint[] ListeningAddresses { get; private set; }
public static TcpListener[] Listeners { get; private set; }
public static HashSet<NetState> Instances { get; } = new(128);
public static HashSet<NetState> Instances { get; } = new(2048);
public static ConcurrentQueue<NetState> m_ConnectedQueue = new();
private static readonly ConcurrentQueue<NetState> _connectedQueue = new();
public static void Configure()
{
@ -128,7 +128,7 @@ namespace Server.Network
*/
public static void Pause()
{
NetworkState.Pause(ref m_NetworkState);
NetworkState.Pause(ref _networkState);
}
/**
@ -137,7 +137,7 @@ namespace Server.Network
*/
public static void Resume()
{
if (!NetworkState.Resume(ref m_NetworkState))
if (!NetworkState.Resume(ref _networkState))
{
return;
}
@ -154,11 +154,11 @@ namespace Server.Network
public static int Slice()
{
int count = 0;
var limit = m_ConnectedQueue.Count;
var limit = _connectedQueue.Count;
while (m_ConnectedQueue.Count > 0 && --limit >= 0)
while (_connectedQueue.Count > 0 && --limit >= 0)
{
if (!m_ConnectedQueue.TryDequeue(out var ns))
if (!_connectedQueue.TryDequeue(out var ns))
{
break;
}
@ -175,7 +175,7 @@ namespace Server.Network
{
while (true)
{
if (m_NetworkState.Paused)
if (_networkState.Paused)
{
return;
}
@ -187,7 +187,7 @@ namespace Server.Network
socket = await listener.AcceptSocketAsync().ConfigureAwait(false);
if (Instances.Count >= MaxConnections)
{
socket.Send(socketRejected, SocketFlags.None);
socket.Send(_socketRejected, SocketFlags.None);
socket.Shutdown(SocketShutdown.Both);
socket.Close();
throw new MaxConnectionsException();
@ -198,7 +198,7 @@ namespace Server.Network
if (!args.AllowConnection)
{
socket.Send(socketRejected, SocketFlags.None);
socket.Send(_socketRejected, SocketFlags.None);
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
@ -222,7 +222,7 @@ namespace Server.Network
}
var ns = new NetState(new NetworkSocket(socket));
m_ConnectedQueue.Enqueue(ns);
_connectedQueue.Enqueue(ns);
ns.Start();
}
}