fix(core): Core cleanup, adds event loop synchronization, and fixes gumps (#429)

- [X] Cleans up gump packets
- [X] Adds event loop task synchronization
- [X] Moves timer pause and cleans up the delay task timer class
- [X] Cleans up the conserve cpu
- [X] Renames variables to make them consistent with the new style
- [X] Removes process delta recursion checking.
- [X] Properly diposes net states. This fixes edge cases that may cause hanging connections to stay open longer than they should.
- [X] Fixes an issue with gump items not compiling properly

Users can now utilize `Timer.Pause()` since the code will be executed on the proper thread.

```cs
public void async void Talk()
{
    _canTalk = false;
    await Timer.Pause(Utility.RandomMinMax(5000, 8000)); // Talk after 5-8 seconds
    DoTalk();
    await Timer.Pause(Utility.RandomMinMax(12000, 25000)); // Reset ability to talk after 12-25 seconds
    _canTalk = true;
}
```
This commit is contained in:
Kamron Batman 2021-01-25 21:06:46 -08:00 committed by GitHub
parent 07751654b7
commit 6f0e1a22f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 763 additions and 449 deletions

View file

@ -32,5 +32,7 @@ namespace Server.Network
public Task<int> ReceiveAsync(IList<ArraySegment<byte>> buffer, SocketFlags flags);
public void Shutdown(SocketShutdown how);
public void Close();
}
}

View file

@ -41,8 +41,8 @@ namespace Server.Network
public ClientVersion Version
{
get => m_Version;
set => ProtocolChanges = ProtocolChangesByVersion(m_Version = value);
get => _version;
set => ProtocolChanges = ProtocolChangesByVersion(_version = value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -87,9 +87,9 @@ namespace Server.Network
public bool NewSecureTrading => HasProtocolChanges(ProtocolChanges.NewSecureTrading);
public bool IsUOTDClient =>
(Flags & ClientFlags.UOTD) != 0 || m_Version?.Type == ClientType.UOTD;
(Flags & ClientFlags.UOTD) != 0 || _version?.Type == ClientType.UOTD;
public bool IsSAClient => m_Version?.Type == ClientType.SA;
public bool IsSAClient => _version?.Type == ClientType.SA;
private ExpansionInfo m_Expansion;

View file

@ -43,23 +43,25 @@ namespace Server.Network
private static int HuePickerCap = 512;
private static int MenuCap = 512;
private static readonly ConcurrentQueue<NetState> m_Disposed = new();
private static NetworkState m_NetworkState = NetworkState.ResumeState;
private static readonly Queue<NetState> FlushPending = new(2048);
private static readonly ConcurrentQueue<NetState> Disposed = new();
private static NetworkState NetworkState = NetworkState.ResumeState;
public static NetStateCreatedCallback CreatedCallback { get; set; }
private readonly string m_ToString;
private int m_Disposing;
private ClientVersion m_Version;
private readonly string _toString;
private int _disposing;
private ClientVersion _version;
private byte[] _recvBuffer;
private byte[] _sendBuffer;
private long m_NextCheckActivity;
private volatile bool m_Running;
private long _nextActivityCheck;
private volatile bool _running;
private volatile DecodePacket _packetDecoder;
private volatile EncodePacket _packetEncoder;
private bool _flushQueued = false;
internal int m_AuthID;
internal int m_Seed;
internal int _authId;
internal int _seed;
public static void Configure()
{
@ -78,7 +80,7 @@ namespace Server.Network
public NetState(ISocket connection)
{
m_Running = false;
_running = false;
Connection = connection;
Seeded = false;
Gumps = new List<Gump>();
@ -89,18 +91,18 @@ namespace Server.Network
RecvPipe = new Pipe<byte>(_recvBuffer);
_sendBuffer = GC.AllocateUninitializedArray<byte>(SendPipeSize);
SendPipe = new Pipe<byte>(_sendBuffer);
m_NextCheckActivity = Core.TickCount + 30000;
_nextActivityCheck = Core.TickCount + 30000;
try
{
Address = Utility.Intern((Connection?.RemoteEndPoint as IPEndPoint)?.Address);
m_ToString = Address?.ToString() ?? "(error)";
_toString = Address?.ToString() ?? "(error)";
}
catch (Exception ex)
{
TraceException(ex);
Address = IPAddress.None;
m_ToString = "(error)";
_toString = "(error)";
}
ConnectedOn = DateTime.UtcNow;
@ -134,7 +136,7 @@ namespace Server.Network
public List<SecureTrade> Trades { get; }
public bool Running => m_Running;
public bool Running => _running;
public bool Seeded { get; set; }
@ -162,9 +164,9 @@ namespace Server.Network
public IAccount Account { get; set; }
public bool IsDisposing => m_Disposing != 0;
public bool IsDisposing => _disposing != 0;
public int CompareTo(NetState other) => string.CompareOrdinal(m_ToString, other?.m_ToString);
public int CompareTo(NetState other) => string.CompareOrdinal(_toString, other?._toString);
public void ValidateAllTrades()
{
@ -358,16 +360,16 @@ namespace Server.Network
this.SendLaunchBrowser(url);
}
public override string ToString() => m_ToString;
public override string ToString() => _toString;
public static void Pause()
{
NetworkState.Pause(ref m_NetworkState);
NetworkState.Pause(ref NetworkState);
}
public static void Resume()
{
NetworkState.Resume(ref m_NetworkState);
NetworkState.Resume(ref NetworkState);
}
public bool GetSendBuffer(out CircularBuffer<byte> cBuffer)
@ -403,6 +405,12 @@ namespace Server.Network
}
SendPipe.Writer.Advance((uint)length);
if (!_flushQueued)
{
FlushPending.Enqueue(this);
_flushQueued = true;
}
}
catch (Exception ex)
{
@ -442,7 +450,11 @@ namespace Server.Network
result.CopyFrom(buffer.AsSpan(0, length));
writer.Advance((uint)length);
// Flush at the end of the game loop
if (!_flushQueued)
{
FlushPending.Enqueue(this);
_flushQueued = true;
}
}
else
{
@ -471,12 +483,12 @@ namespace Server.Network
internal void Start()
{
if (Connection == null || m_Running)
if (Connection == null || _running)
{
return;
}
m_Running = true;
_running = true;
ThreadPool.UnsafeQueueUserWorkItem(RecvTask, null);
ThreadPool.UnsafeQueueUserWorkItem(SendTask, null);
@ -488,7 +500,7 @@ namespace Server.Network
try
{
while (m_Running)
while (_running)
{
var result = await reader.Read();
if (result.IsClosed)
@ -505,7 +517,7 @@ namespace Server.Network
if (bytesWritten > 0)
{
m_NextCheckActivity = Core.TickCount + 90000;
_nextActivityCheck = Core.TickCount + 90000;
reader.Advance((uint)bytesWritten);
}
}
@ -536,9 +548,9 @@ namespace Server.Network
try
{
while (m_Running)
while (_running)
{
if (m_NetworkState == NetworkState.PauseState)
if (NetworkState == NetworkState.PauseState)
{
continue;
}
@ -564,7 +576,7 @@ namespace Server.Network
DecodePacket(result.Buffer, ref bytesWritten);
writer.Advance((uint)bytesWritten);
m_NextCheckActivity = Core.TickCount + 90000;
_nextActivityCheck = Core.TickCount + 90000;
// No need to flush
}
@ -582,19 +594,26 @@ namespace Server.Network
}
}
public static void HandleAllReceives()
public static int HandleAllReceives()
{
int count = 0;
foreach (var ns in TcpServer.Instances)
{
ns.HandleReceive();
if (ns.HandleReceive())
{
count++;
}
}
return count;
}
public void HandleReceive()
public bool HandleReceive()
{
if (Connection == null)
{
return;
return false;
}
try
@ -608,7 +627,7 @@ namespace Server.Network
if (result.IsClosed || result.Length <= 0)
{
return;
return false;
}
var bytesProcessed = this.ProcessPacket(result.Buffer);
@ -620,9 +639,10 @@ namespace Server.Network
if (bytesProcessed < 0)
{
Dispose();
return false;
}
return;
return true;
}
reader.Advance((uint)bytesProcessed);
@ -635,6 +655,7 @@ namespace Server.Network
TraceException(ex);
#endif
Dispose();
return false;
}
}
@ -644,19 +665,32 @@ namespace Server.Network
{
SendPipe.Writer.Flush();
}
_flushQueued = false;
}
public static void FlushAll()
public static int FlushAll()
{
foreach (var ns in TcpServer.Instances)
int count = 0;
while (FlushPending.TryDequeue(out var ns))
{
ns.Flush();
count++;
}
while (Disposed.TryDequeue(out var ns))
{
ns.Disconnect();
TcpServer.Instances.Remove(ns);
count++;
}
return count;
}
public void CheckAlive(long curTicks)
{
if (Connection != null && m_NextCheckActivity - curTicks < 0)
if (Connection != null && _nextActivityCheck - curTicks < 0)
{
WriteConsole("Disconnecting due to inactivity...");
Dispose();
@ -717,7 +751,7 @@ namespace Server.Network
public virtual void Dispose()
{
if (Connection == null || Interlocked.Exchange(ref m_Disposing, 1) != 0)
if (Connection == null || Interlocked.CompareExchange(ref _disposing, 1, 0) == 1)
{
return;
}
@ -728,59 +762,56 @@ namespace Server.Network
{
Connection.Shutdown(SocketShutdown.Both);
}
catch (Exception ex)
catch (SocketException ex)
{
TraceException(ex);
}
finally
try
{
m_Disposed.Enqueue(this);
Connection.Close();
}
catch (SocketException ex)
{
TraceException(ex);
}
Disposed.Enqueue(this);
}
public static void ProcessDisposedQueue()
private void Disconnect()
{
var breakout = 0;
_running = false;
Connection = null;
while (breakout++ < 200)
RecvPipe.Writer.Flush();
RecvPipe.Writer.Close();
var m = Mobile;
var a = Account;
if (m != null)
{
if (!m_Disposed.TryDequeue(out var ns))
{
break;
}
m.NetState = null;
Mobile = null;
}
var m = ns.Mobile;
var a = ns.Account;
Gumps.Clear();
Menus.Clear();
HuePickers.Clear();
Account = null;
ServerInfo = null;
CityInfo = null;
if (m != null)
{
m.NetState = null;
ns.Mobile = null;
}
var count = TcpServer.Instances.Count;
ns.m_Running = false;
ns.Connection = null;
ns._recvBuffer = null;
ns.RecvPipe = null;
ns._sendBuffer = null;
ns.SendPipe = null;
ns.Gumps.Clear();
ns.Menus.Clear();
ns.HuePickers.Clear();
ns.Account = null;
ns.ServerInfo = null;
ns.CityInfo = null;
TcpServer.Instances.Remove(ns);
if (a != null)
{
ns.WriteConsole("Disconnected. [{0} Online] [{1}]", TcpServer.Instances.Count, a);
}
else
{
ns.WriteConsole("Disconnected. [{0} Online]", TcpServer.Instances.Count);
}
if (a != null)
{
WriteConsole("Disconnected. [{0} Online] [{1}]", count, a);
}
else
{
WriteConsole("Disconnected. [{0} Online]", count);
}
}
}

View file

@ -17,24 +17,48 @@ using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace Server.Network
{
public class NetworkSocket : ISocket
{
public Socket Connection { get; }
public EndPoint LocalEndPoint => Connection.LocalEndPoint;
public EndPoint RemoteEndPoint => Connection.RemoteEndPoint;
private Socket _connection;
public NetworkSocket(Socket connection) => Connection = connection;
public Socket Connection
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _connection;
}
public EndPoint LocalEndPoint
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _connection.LocalEndPoint;
}
public EndPoint RemoteEndPoint
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _connection.RemoteEndPoint;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public NetworkSocket(Socket connection) => _connection = connection;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Task<int> SendAsync(IList<ArraySegment<byte>> buffers, SocketFlags flags) =>
Connection.SendAsync(buffers, flags);
_connection.SendAsync(buffers, flags);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Task<int> ReceiveAsync(IList<ArraySegment<byte>> buffers, SocketFlags flags) =>
Connection.ReceiveAsync(buffers, flags);
_connection.ReceiveAsync(buffers, flags);
public void Shutdown(SocketShutdown how) => Connection.Shutdown(how);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Shutdown(SocketShutdown how) => _connection.Shutdown(how);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Close() => _connection.Close();
}
}

View file

@ -29,10 +29,10 @@ namespace Server.Network
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Pause(ref NetworkState state) =>
Interlocked.Exchange(ref state, PauseState)?.Paused != true;
Interlocked.CompareExchange(ref state, PauseState, ResumeState) != PauseState;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Resume(ref NetworkState state) =>
Interlocked.Exchange(ref state, ResumeState)?.Paused == true;
Interlocked.CompareExchange(ref state, ResumeState, PauseState) != ResumeState;
}
}

View file

@ -283,9 +283,6 @@ namespace Server.Network
m.NetState?.Dispose();
// TODO: Make this wait one tick so we don't have to call it unnecessarily
NetState.ProcessDisposedQueue();
state.SendClientVersionRequest();
state.BlockAllPackets = true;
@ -395,8 +392,8 @@ namespace Server.Network
if (
!m_AuthIDWindow.TryGetValue(authID, out var ap) ||
state.m_AuthID != 0 && authID != state.m_AuthID ||
state.m_AuthID == 0 && authID != state.m_Seed
state._authId != 0 && authID != state._authId ||
state._authId == 0 && authID != state._seed
)
{
state.WriteConsole("Invalid client detected, disconnecting");
@ -445,19 +442,19 @@ namespace Server.Network
{
var si = info[index];
state.m_AuthID = GenerateAuthID(state);
state._authId = GenerateAuthID(state);
state.SentFirstPacket = false;
state.SendPlayServerAck(si, state.m_AuthID);
state.SendPlayServerAck(si, state._authId);
}
}
public static void LoginServerSeed(NetState state, CircularBufferReader reader, ref int packetLength)
{
state.m_Seed = reader.ReadInt32();
state._seed = reader.ReadInt32();
state.Seeded = true;
if (state.m_Seed == 0)
if (state._seed == 0)
{
state.WriteConsole("Invalid client detected, disconnecting");
state.Dispose();

View file

@ -106,7 +106,7 @@ namespace Server.Network
return -1;
}
ns.m_Seed = seed;
ns._seed = seed;
ns.Seeded = true;
return 4;

View file

@ -43,8 +43,12 @@ namespace Server.Network
ns.Send(writer.Span);
}
private static readonly byte[] _layoutBuffer = GC.AllocateUninitializedArray<byte>(0x20000);
private static readonly byte[] _stringsBuffer = GC.AllocateUninitializedArray<byte>(0x20000);
private static readonly byte[] _packBuffer = GC.AllocateUninitializedArray<byte>(0x20000);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void WritePacked(ref SpanWriter writer, ReadOnlySpan<byte> span)
private static void WritePacked(ReadOnlySpan<byte> span, ref SpanWriter writer)
{
var length = span.Length;
@ -54,21 +58,38 @@ namespace Server.Network
return;
}
var wantLength = 1 + span.Length * 1024 / 1000;
var wantLength = Zlib.MaxPackSize(length);
var packBuffer = _packBuffer;
byte[] rentedBuffer = null;
wantLength += 4095;
wantLength &= ~4095;
if (wantLength > packBuffer.Length)
{
packBuffer = rentedBuffer = ArrayPool<byte>.Shared.Rent(wantLength);
}
var packBuffer = ArrayPool<byte>.Shared.Rent(wantLength);
var packLength = wantLength;
Zlib.Pack(packBuffer, ref packLength, span, length, ZlibQuality.Default);
var error = Zlib.Pack(packBuffer, ref packLength, span, length, ZlibQuality.Default);
if (error != ZlibError.Okay)
{
Utility.PushColor(ConsoleColor.Red);
Console.WriteLine("Core: Gump compression failed {0}", error);
Utility.PopColor();
writer.Write(4);
writer.Write(0);
return;
}
writer.Write(4 + packLength);
writer.Write(length);
writer.Write(packBuffer.AsSpan(0, packLength));
ArrayPool<byte>.Shared.Return(packBuffer);
if (rentedBuffer != null)
{
ArrayPool<byte>.Shared.Return(rentedBuffer);
}
}
public static void SendDisplayGump(this NetState ns, Gump gump, out int switches, out int entries)
@ -83,7 +104,57 @@ namespace Server.Network
var packed = ns.Unpack;
var writer = new SpanWriter(512, true);
var layoutWriter = new SpanWriter(_layoutBuffer);
if (!gump.Draggable)
{
layoutWriter.Write(Gump.NoMove);
}
if (!gump.Closable)
{
layoutWriter.Write(Gump.NoClose);
}
if (!gump.Disposable)
{
layoutWriter.Write(Gump.NoDispose);
}
if (!gump.Resizable)
{
layoutWriter.Write(Gump.NoResize);
}
var stringsList = new OrderedHashSet<string>(11);
foreach (var entry in gump.Entries)
{
entry.AppendTo(ref layoutWriter, stringsList, ref entries, ref switches);
}
var stringsWriter = new SpanWriter(_stringsBuffer);
foreach (var str in stringsList)
{
var s = str ?? "";
stringsWriter.Write((ushort)s.Length);
stringsWriter.WriteBigUni(s);
}
int maxLength;
if (packed)
{
var worstLayoutLength = Zlib.MaxPackSize(layoutWriter.BytesWritten);
var worstStringsLength = Zlib.MaxPackSize(stringsWriter.BytesWritten);
maxLength = 40 + worstLayoutLength + worstStringsLength;
}
else
{
maxLength = 23 + layoutWriter.BytesWritten + stringsWriter.BytesWritten;
}
var writer = new SpanWriter(stackalloc byte[maxLength]);
writer.Write((byte)(packed ? 0xDD : 0xB0)); // Packet ID
writer.Seek(2, SeekOrigin.Current);
@ -92,77 +163,26 @@ namespace Server.Network
writer.Write(gump.X);
writer.Write(gump.Y);
var spanWriter = new SpanWriter(512, true);
if (!gump.Draggable)
{
spanWriter.Write(Gump.NoMove);
}
if (!gump.Closable)
{
spanWriter.Write(Gump.NoClose);
}
if (!gump.Disposable)
{
spanWriter.Write(Gump.NoDispose);
}
if (!gump.Resizable)
{
spanWriter.Write(Gump.NoResize);
}
var stringsList = new OrderedHashSet<string>(11);
foreach (var entry in gump.Entries)
{
entry.AppendTo(ref spanWriter, stringsList, ref entries, ref switches);
}
if (packed)
{
spanWriter.Write((byte)0); // Layout text terminator
WritePacked(ref writer, spanWriter.Span);
}
else
{
writer.Write((ushort)spanWriter.BytesWritten);
writer.Write(spanWriter.Span);
}
layoutWriter.Write((byte)0); // Layout text terminator
WritePacked(layoutWriter.Span, ref writer);
if (packed)
{
writer.Write(stringsList.Count);
WritePacked(stringsWriter.Span, ref writer);
}
else
{
writer.Write((ushort)layoutWriter.BytesWritten);
writer.Write(layoutWriter.Span);
writer.Write((ushort)stringsList.Count);
}
spanWriter.Seek(0, SeekOrigin.Begin);
foreach (var str in stringsList)
{
var s = str ?? "";
spanWriter.Write((ushort)s.Length);
spanWriter.WriteBigUni(s);
}
if (packed)
{
WritePacked(ref writer, spanWriter.Span);
}
else
{
writer.Write(spanWriter.Span);
writer.Write(stringsWriter.Span);
}
writer.WritePacketLength();
ns.Send(writer.Span);
spanWriter.Dispose(); // Can't use using and refs, so we dispose manually
}
public static void SendDisplaySignGump(this NetState ns, Serial serial, int gumpId, string unknown, string caption)

View file

@ -151,20 +151,24 @@ namespace Server.Network
}
}
public static void Slice()
public static int Slice()
{
int count = 0;
var limit = m_ConnectedQueue.Count;
while (count++ < 250)
while (m_ConnectedQueue.Count > 0 && --limit >= 0)
{
if (!m_ConnectedQueue.TryDequeue(out var ns))
{
break;
}
count++;
Instances.Add(ns);
ns.WriteConsole("Connected. [{0} Online]", Instances.Count);
}
return count;
}
private static async void BeginAcceptingSockets(this TcpListener listener)