fix: Fixes networking issues (#1958)
### Summary - Puts back `EventSink.SocketConnect`. - Reverts networking change to push the networking to a separate thread. - Reverts changes to the firewall by removing the firewall queue. - Fixes listeners not shutting down with the server. - Fixes race condition causing connections to get stuck even after they are disposed. > [!NOTE] > **Developer Note** > Networking has been reverted back to using the main thread instead of a background thread. This alleviated complexity and the requirement for concurrent queues all over the place.
This commit is contained in:
parent
97c53e656e
commit
e0fcde885c
10 changed files with 211 additions and 340 deletions
|
|
@ -32,8 +32,6 @@ using System.Runtime.InteropServices;
|
|||
|
||||
namespace Server.Network;
|
||||
|
||||
public delegate void NetStateCreatedCallback(NetState ns);
|
||||
|
||||
public delegate void DecodePacket(Span<byte> buffer, ref int length);
|
||||
public delegate int EncodePacket(ReadOnlySpan<byte> inputBuffer, Span<byte> outputBuffer);
|
||||
|
||||
|
|
@ -56,8 +54,6 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
|
|||
private static readonly Queue<NetState> _throttled = new(256);
|
||||
private static readonly Queue<NetState> _throttledPending = new(256);
|
||||
|
||||
public static NetStateCreatedCallback CreatedCallback { get; set; }
|
||||
|
||||
private static readonly SortedSet<NetState> _connecting = new(NetStateConnectingComparer.Instance);
|
||||
private static readonly HashSet<NetState> _instances = new(2048);
|
||||
public static IReadOnlySet<NetState> Instances => _instances;
|
||||
|
|
@ -68,8 +64,8 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
|
|||
private volatile DecodePacket _packetDecoder;
|
||||
private volatile EncodePacket _packetEncoder;
|
||||
private bool _flushQueued;
|
||||
private readonly long[] _packetThrottles = new long[0x100];
|
||||
private readonly long[] _packetCounts = new long[0x100];
|
||||
private long[] _packetThrottles;
|
||||
private long[] _packetCounts;
|
||||
private string _disconnectReason = string.Empty;
|
||||
|
||||
internal ParserState _parserState = ParserState.AwaitingNextPacket;
|
||||
|
|
@ -143,8 +139,12 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
|
|||
_toString = "(error)";
|
||||
}
|
||||
|
||||
_instances.Add(this);
|
||||
_connecting.Add(this);
|
||||
_handle = GCHandle.Alloc(this);
|
||||
|
||||
LogInfo($"Connected. [{_instances.Count} Online]");
|
||||
|
||||
try
|
||||
{
|
||||
_pollGroup.Add(connection, _handle);
|
||||
|
|
@ -254,22 +254,30 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
|
|||
{
|
||||
if (packetID is >= 0 and < 0x100)
|
||||
{
|
||||
_packetThrottles ??= new long[0x100];
|
||||
_packetThrottles[packetID] = Core.TickCount;
|
||||
}
|
||||
}
|
||||
|
||||
public long GetPacketTime(int packetID) => packetID is >= 0 and < 0x100 ? _packetThrottles[packetID] : 0;
|
||||
public long GetPacketTime(int packetID) =>
|
||||
packetID is >= 0 and < 0x100 && _packetThrottles != null ? _packetThrottles[packetID] : 0;
|
||||
|
||||
private void UpdatePacketCount(int packetID)
|
||||
{
|
||||
if (packetID is >= 0 and < 0x100)
|
||||
{
|
||||
_packetCounts ??= new long[0x100];
|
||||
_packetCounts[packetID]++;
|
||||
}
|
||||
}
|
||||
|
||||
public int CheckPacketCounts()
|
||||
{
|
||||
if (_packetCounts == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (int i = 0; i < _packetCounts.Length; i++)
|
||||
{
|
||||
long count = _packetCounts[i];
|
||||
|
|
@ -917,7 +925,7 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
|
|||
var ns = _connecting.Min;
|
||||
var socketTime = ns.ConnectedOn;
|
||||
|
||||
// If the socket has been connected for less than 2 seconds, we can stop checking
|
||||
// If the socket has been connected for less than the limit, we can stop checking
|
||||
if (now - socketTime < ConnectingSocketIdleLimit)
|
||||
{
|
||||
break;
|
||||
|
|
@ -946,17 +954,6 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
|
|||
{
|
||||
DisconnectUnattachedSockets();
|
||||
|
||||
const int maxEntriesPerLoop = 32;
|
||||
var count = 0;
|
||||
while (++count <= maxEntriesPerLoop && TcpServer.ConnectedQueue.TryDequeue(out var ns))
|
||||
{
|
||||
CreatedCallback?.Invoke(ns);
|
||||
|
||||
_instances.Add(ns);
|
||||
_connecting.Add(ns); // Add to the connecting set, and remove them when they authenticated.
|
||||
ns.LogInfo($"Connected. [{Instances.Count} Online]");
|
||||
}
|
||||
|
||||
while (_throttled.Count > 0)
|
||||
{
|
||||
var ns = _throttled.Dequeue();
|
||||
|
|
@ -972,7 +969,7 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
|
|||
_throttled.Enqueue(_throttledPending.Dequeue());
|
||||
}
|
||||
|
||||
count = _pollGroup.Poll(_polledStates);
|
||||
var count = _pollGroup.Poll(_polledStates);
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue