Replacing Networking (#271)

- [X] Removing Kestrel & Libuv
- [X] Cleaning up NetState
- [X] Removing System.IO.Pipelines
- [X] Cleaning up packet reading
- [X] Adds a maximum of 5000 sockets (configurable) to prevent OOM
- [X] Replaces the AsyncState with a thread-safe wrapped boolean called NetworkState
- [X] Removes Parallel.ForEach (no perf gain)
- [X] Removes custom houses compression on another thread
- [X] Test high load scenarios

Bumps release version
This commit is contained in:
Kamron Batman 2020-10-20 20:55:19 -07:00 committed by GitHub
parent 8603e31023
commit 369a27b800
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
47 changed files with 1780 additions and 1569 deletions

View file

@ -1,13 +1,28 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: NetState.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;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Connections;
using Server.Accounting;
using Server.Exceptions;
using Server.Gumps;
using Server.HuePickers;
using Server.Items;
@ -17,41 +32,66 @@ namespace Server.Network
{
public delegate void NetStateCreatedCallback(NetState ns);
public class AsyncState
{
public AsyncState(bool paused) => Paused = paused;
public bool Paused { get; set; }
}
public partial class NetState : IComparable<NetState>
{
private static readonly AsyncState m_PauseState = new AsyncState(true);
private static readonly AsyncState m_ResumeState = new AsyncState(false);
private static AsyncState m_AsyncState = m_ResumeState;
private static int IncomingPipeSize = 1024 * 64;
private static int OutgoingPipeSize = 1024 * 256;
private static int GumpCap = 512;
private static int HuePickerCap = 512;
private static int MenuCap = 512;
private static readonly ConcurrentQueue<NetState> m_Disposed = new ConcurrentQueue<NetState>();
private static NetworkState m_NetworkState = NetworkState.ResumeState;
public static NetStateCreatedCallback CreatedCallback { get; set; }
private readonly string m_ToString;
internal int m_AuthID;
private int m_Disposing;
internal int m_Seed;
private ClientVersion m_Version;
private byte[] m_IncomingBuffer;
private Pipe<byte> m_IncomingPipe;
private byte[] m_OutgoingBuffer;
private Pipe<byte> m_OutgoingPipe;
private long m_NextCheckActivity;
private volatile bool m_Running;
public NetState(ConnectionContext connection)
internal int m_AuthID;
internal int m_Seed;
public static void Configure()
{
IncomingPipeSize = ServerConfiguration.GetOrUpdateSetting("netstate.incomingPipeSize", IncomingPipeSize);
OutgoingPipeSize = ServerConfiguration.GetOrUpdateSetting("netstate.outgoingPipeSize", OutgoingPipeSize);
GumpCap = ServerConfiguration.GetOrUpdateSetting("netstate.gumpCap", GumpCap);
HuePickerCap = ServerConfiguration.GetOrUpdateSetting("netstate.huePickerCap", HuePickerCap);
MenuCap = ServerConfiguration.GetOrUpdateSetting("netstate.menuCap", MenuCap);
}
public static void Initialize()
{
var checkAliveDuration = TimeSpan.FromMinutes(1.5);
Timer.DelayCall(checkAliveDuration, checkAliveDuration, CheckAllAlive);
}
public NetState(Socket connection)
{
m_Running = false;
Connection = connection;
Seeded = false;
Gumps = new List<Gump>();
HuePickers = new List<HuePicker>();
Menus = new List<IMenu>();
Trades = new List<SecureTrade>();
m_IncomingBuffer = new byte[IncomingPipeSize];
m_IncomingPipe = new Pipe<byte>(m_IncomingBuffer);
m_OutgoingBuffer = new byte[OutgoingPipeSize];
m_OutgoingPipe = new Pipe<byte>(m_OutgoingBuffer);
m_NextCheckActivity = Core.TickCount + 30000;
try
{
Address = Utility.Intern(((IPEndPoint)Connection.RemoteEndPoint).Address);
m_ToString = Address.ToString();
Address = Utility.Intern((Connection?.RemoteEndPoint as IPEndPoint)?.Address);
m_ToString = Address?.ToString() ?? "(error)";
}
catch (Exception ex)
{
@ -62,14 +102,6 @@ namespace Server.Network
ConnectedOn = DateTime.UtcNow;
connection.ConnectionClosed.Register(
() =>
{
TcpServer.Instances.Remove(this);
Dispose();
}
);
CreatedCallback?.Invoke(this);
}
@ -80,21 +112,20 @@ namespace Server.Network
public DateTime ThrottledUntil { get; set; }
public IPAddress Address { get; }
public static AsyncState AsyncState => m_AsyncState;
public IPacketEncoder PacketEncoder { get; set; }
public static NetStateCreatedCallback CreatedCallback { get; set; }
public bool SentFirstPacket { get; set; }
public bool BlockAllPackets { get; set; }
public List<SecureTrade> Trades { get; }
public bool Running => m_Running;
public bool Seeded { get; set; }
public ConnectionContext Connection { get; private set; }
public Socket Connection { get; private set; }
public bool CompressionEnabled { get; set; }
@ -106,12 +137,6 @@ namespace Server.Network
public List<IMenu> Menus { get; private set; }
public static int GumpCap { get; set; } = 512;
public static int HuePickerCap { get; set; } = 512;
public static int MenuCap { get; set; } = 512;
public CityInfo[] CityInfo { get; set; }
public Mobile Mobile { get; set; }
@ -208,11 +233,13 @@ namespace Server.Network
return newTrade.From.Container;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteConsole(string text)
{
Console.WriteLine("Client: {0}: {1}", this, text);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteConsole(string format, params object[] args)
{
WriteConsole(string.Format(format, args));
@ -318,15 +345,15 @@ namespace Server.Network
public static void Pause()
{
m_AsyncState = Interlocked.Exchange(ref m_AsyncState, m_PauseState);
NetworkState.Pause(ref m_NetworkState);
}
public static void Resume()
{
m_AsyncState = Interlocked.Exchange(ref m_AsyncState, m_ResumeState);
NetworkState.Resume(ref m_NetworkState);
}
public virtual async void Send(Packet p)
public virtual void Send(Packet p)
{
if (Connection == null || BlockAllPackets)
{
@ -334,83 +361,101 @@ namespace Server.Network
return;
}
var outPipe = Connection.Transport.Output;
var currentThread = Thread.CurrentThread;
if (currentThread != Core.Thread)
{
Console.Error.WriteLine("Core: Attempted to send packet outside core thread! [{0}]", currentThread.ManagedThreadId);
#if DEBUG
throw new InvalidThreadException(nameof(Send));
#endif
}
var writer = m_OutgoingPipe.Writer;
try
{
// TODO: Rented memory
ReadOnlyMemory<byte> buffer = p.Compile(CompressionEnabled, out var length);
byte[] buffer = p.Compile(CompressionEnabled, out var length);
if (buffer.Length > 0 && length > 0)
{
var result = await outPipe.WriteAsync(buffer.Slice(0, length));
var result = writer.GetAvailable();
if (result.IsCanceled || result.IsCompleted)
if (result.Length >= length)
{
Dispose();
return;
result.CopyFrom(buffer.AsSpan(0, length));
writer.Advance((uint)length);
// Flush at the end of the game loop
}
else
{
WriteConsole("Too much data pending, disconnecting...");
Dispose();
}
}
else
{
WriteConsole("Didn't write anything!");
}
p.OnSend();
}
catch (SocketException ex)
{
Console.WriteLine(ex);
TraceException(ex);
Dispose();
}
catch (Exception ex)
{
#if DEBUG
Console.WriteLine(ex);
TraceException(ex);
#endif
Dispose();
}
}
public async Task ProcessIncoming(IMessagePumpService messagePumpService)
internal void Start()
{
var inPipe = Connection.Transport.Input;
if (Connection == null || m_Running)
{
return;
}
m_Running = true;
ThreadPool.UnsafeQueueUserWorkItem(RecvTask, null);
ThreadPool.UnsafeQueueUserWorkItem(SendTask, null);
}
private async void SendTask(object state)
{
var reader = m_OutgoingPipe.Reader;
try
{
while (true)
while (m_Running)
{
if (AsyncState.Paused)
var result = await reader.Read();
if (result.Length <= 0)
{
continue;
}
var result = await inPipe.ReadAsync();
if (result.IsCanceled || result.IsCompleted)
var buffer = result.Buffer;
var bytesWritten = await Connection.SendAsync(buffer, SocketFlags.None);
if (bytesWritten > 0)
{
return;
m_NextCheckActivity = Core.TickCount + 90000;
reader.Advance((uint)bytesWritten);
}
var seq = result.Buffer;
if (seq.IsEmpty)
{
break;
}
var pos = PacketHandlers.ProcessPacket(messagePumpService, this, seq);
if (pos <= 0)
{
break;
}
inPipe.AdvanceTo(seq.Slice(0, pos).End);
}
}
catch (SocketException ex)
catch (Exception ex)
{
#if DEBUG
Console.WriteLine(ex);
TraceException(ex);
}
catch
{
// Console.WriteLine(ex);
#endif
}
finally
{
@ -418,12 +463,165 @@ namespace Server.Network
}
}
private async void RecvTask(object state)
{
var socket = Connection;
var writer = m_IncomingPipe.Writer;
try
{
while (m_Running)
{
if (m_NetworkState == NetworkState.PauseState)
{
continue;
}
var result = writer.GetAvailable();
if (result.Length <= 0)
{
continue;
}
var buffer = result.Buffer;
var bytesWritten = await socket.ReceiveAsync(buffer, SocketFlags.None);
if (bytesWritten <= 0)
{
break;
}
writer.Advance((uint)bytesWritten);
m_NextCheckActivity = Core.TickCount + 90000;
// No need to flush
}
}
catch (Exception ex)
{
#if DEBUG
Console.WriteLine(ex);
TraceException(ex);
#endif
}
finally
{
Dispose();
}
}
public static void HandleAllReceives()
{
var clients = TcpServer.Instances;
for (int i = 0; i < clients.Count; ++i)
{
clients[i].HandleReceive();
}
}
public void HandleReceive()
{
if (Connection == null)
{
return;
}
try
{
var reader = m_IncomingPipe.Reader;
// Process as many packets as we can synchronously
while (true)
{
var result = reader.TryRead();
if (result.Length <= 0)
{
return;
}
var bytesProcessed = PacketHandlers.ProcessPacket(this, result.Buffer);
if (bytesProcessed <= 0)
{
// Error
// TODO: Throw exception instead?
if (bytesProcessed < 0)
{
Dispose();
}
return;
}
reader.Advance((uint)bytesProcessed);
}
}
catch (Exception ex)
{
#if DEBUG
Console.WriteLine(ex);
TraceException(ex);
#endif
Dispose();
}
}
public void Flush()
{
if (Connection != null)
{
m_OutgoingPipe.Writer.Flush();
}
}
public static void FlushAll()
{
var clients = TcpServer.Instances;
for (int i = 0; i < clients.Count; ++i)
{
clients[i].Flush();
}
}
public void CheckAlive(long curTicks)
{
if (Connection != null && m_NextCheckActivity - curTicks < 0)
{
WriteConsole("Disconnecting due to inactivity...");
Dispose();
}
}
public static void CheckAllAlive()
{
try
{
long curTicks = Core.TickCount;
var clients = TcpServer.Instances;
for (int i = 0; i < clients.Count; ++i)
{
clients[i].CheckAlive(curTicks);
}
}
catch (Exception ex)
{
TraceException(ex);
}
}
public bool CheckEncrypted(int packetID)
{
if (!SentFirstPacket && packetID != 0xF0 && packetID != 0xF1 && packetID != 0xCF && packetID != 0x80 &&
packetID != 0x91 && packetID != 0xA4 && packetID != 0xEF)
{
Console.WriteLine("Client: {0}: Encrypted client detected, disconnecting", this);
WriteConsole("Encrypted client detected, disconnecting");
Dispose();
return true;
}
@ -456,26 +654,25 @@ namespace Server.Network
public virtual void Dispose()
{
var disposing = Interlocked.Exchange(ref m_Disposing, 1);
if (disposing == 1)
if (Connection == null || Interlocked.Exchange(ref m_Disposing, 1) != 0)
{
return;
}
m_OutgoingPipe.Writer.Close();
try
{
Connection.Transport.Input.Complete();
Connection.Transport.Output.Complete();
Connection.Abort();
Task.Run(Connection.DisposeAsync).Wait();
Connection.Shutdown(SocketShutdown.Both);
}
catch (Exception ex)
{
TraceException(ex);
}
Connection = null;
m_Disposed.Enqueue(this);
finally
{
m_Disposed.Enqueue(this);
}
}
public static void ProcessDisposedQueue()
@ -498,6 +695,12 @@ namespace Server.Network
ns.Mobile = null;
}
ns.m_Running = false;
ns.Connection = null;
ns.m_IncomingBuffer = null;
ns.m_IncomingPipe = null;
ns.m_OutgoingBuffer = null;
ns.m_OutgoingPipe = null;
ns.Gumps.Clear();
ns.Menus.Clear();
ns.HuePickers.Clear();
@ -505,6 +708,8 @@ namespace Server.Network
ns.ServerInfo = null;
ns.CityInfo = null;
TcpServer.Instances.Remove(ns);
if (a != null)
{
ns.WriteConsole("Disconnected. [{0} Online] [{1}]", TcpServer.Instances.Count, a);