Targets .NET Core 3.0 (#39)
This commit is contained in:
parent
0993c03b70
commit
08bf44af9a
125 changed files with 1473 additions and 14244 deletions
|
|
@ -1,3 +1,4 @@
|
|||
|
||||
/***************************************************************************
|
||||
* MessagePump.cs
|
||||
* -------------------
|
||||
|
|
@ -19,271 +20,57 @@
|
|||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using Server.Diagnostics;
|
||||
|
||||
namespace Server.Network
|
||||
{
|
||||
public class MessagePump
|
||||
{
|
||||
private const int BufferSize = 4096;
|
||||
private BufferPool m_Buffers = new BufferPool("Processor", 4, BufferSize);
|
||||
private Queue<NetState> m_Queue;
|
||||
private Queue<NetState> m_Throttled;
|
||||
private Queue<NetState> m_WorkingQueue;
|
||||
private ConcurrentQueue<Work> m_WorkQueue = new ConcurrentQueue<Work>();
|
||||
public Listener[] Listeners => new Listener[0];
|
||||
|
||||
public MessagePump()
|
||||
public void AddListener(IPEndPoint ipep)
|
||||
{
|
||||
IPEndPoint[] ipep = Listener.EndPoints;
|
||||
|
||||
Listeners = new Listener[ipep.Length];
|
||||
|
||||
bool success = false;
|
||||
|
||||
do
|
||||
{
|
||||
for (int i = 0; i < ipep.Length; i++)
|
||||
{
|
||||
Listener l = new Listener(ipep[i]);
|
||||
if (!success)
|
||||
success = true;
|
||||
Listeners[i] = l;
|
||||
}
|
||||
|
||||
if (!success)
|
||||
{
|
||||
Console.WriteLine("Retrying...");
|
||||
Thread.Sleep(10000);
|
||||
}
|
||||
} while (!success);
|
||||
|
||||
m_Queue = new Queue<NetState>();
|
||||
m_WorkingQueue = new Queue<NetState>();
|
||||
m_Throttled = new Queue<NetState>();
|
||||
Listener[] listeners = new Listener[Listeners.Length + 1];
|
||||
Array.Copy(Listeners, listeners, Listeners.Length);
|
||||
Listener listener = new Listener(ipep);
|
||||
_ = listener.Start(this);
|
||||
listeners[Listeners.Length] = listener;
|
||||
}
|
||||
|
||||
public Listener[] Listeners{ get; set; }
|
||||
|
||||
public void AddListener(Listener l)
|
||||
public void QueueWork(NetState ns, in ReadOnlySequence<byte> seq, OnPacketReceive onReceive)
|
||||
{
|
||||
Listener[] old = Listeners;
|
||||
|
||||
Listeners = new Listener[old.Length + 1];
|
||||
|
||||
for (int i = 0; i < old.Length; ++i)
|
||||
Listeners[i] = old[i];
|
||||
|
||||
Listeners[old.Length] = l;
|
||||
}
|
||||
|
||||
private void CheckListener()
|
||||
{
|
||||
for (int j = 0; j < Listeners.Length; ++j)
|
||||
{
|
||||
Socket[] accepted = Listeners[j].Slice();
|
||||
|
||||
for (int i = 0; i < accepted.Length; ++i)
|
||||
{
|
||||
NetState ns = new NetState(accepted[i], this);
|
||||
ns.Start();
|
||||
|
||||
if (ns.Running)
|
||||
Console.WriteLine("Client: {0}: Connected. [{1} Online]", ns, NetState.Instances.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnReceive(NetState ns)
|
||||
{
|
||||
lock (this)
|
||||
{
|
||||
m_Queue.Enqueue(ns);
|
||||
}
|
||||
|
||||
m_WorkQueue.Enqueue(new Work(ns, seq, onReceive));
|
||||
Core.Set();
|
||||
}
|
||||
|
||||
public void Slice()
|
||||
public void DoWork()
|
||||
{
|
||||
CheckListener();
|
||||
|
||||
lock (this)
|
||||
int count = m_WorkQueue.Count;
|
||||
while (count-- > 0)
|
||||
{
|
||||
Queue<NetState> temp = m_WorkingQueue;
|
||||
m_WorkingQueue = m_Queue;
|
||||
m_Queue = temp;
|
||||
}
|
||||
if (!m_WorkQueue.TryDequeue(out Work work))
|
||||
break;
|
||||
|
||||
while (m_WorkingQueue.Count > 0)
|
||||
{
|
||||
NetState ns = m_WorkingQueue.Dequeue();
|
||||
|
||||
if (ns.Running)
|
||||
HandleReceive(ns);
|
||||
}
|
||||
|
||||
lock (this)
|
||||
{
|
||||
while (m_Throttled.Count > 0)
|
||||
m_Queue.Enqueue(m_Throttled.Dequeue());
|
||||
work.OnReceive(work.State, new PacketReader(work.Sequence));
|
||||
}
|
||||
}
|
||||
|
||||
private bool HandleSeed(NetState ns, ByteQueue buffer)
|
||||
// TODO: Optimize this with a pool
|
||||
private class Work
|
||||
{
|
||||
if (buffer.GetPacketID() == 0xEF)
|
||||
public NetState State;
|
||||
public ReadOnlySequence<byte> Sequence;
|
||||
public OnPacketReceive OnReceive;
|
||||
|
||||
public Work(NetState ns, in ReadOnlySequence<byte> seq, OnPacketReceive onReceive)
|
||||
{
|
||||
// new packet in client 6.0.5.0 replaces the traditional seed method with a seed packet
|
||||
// 0xEF = 239 = multicast IP, so this should never appear in a normal seed. So this is backwards compatible with older clients.
|
||||
ns.Seeded = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (buffer.Length >= 4)
|
||||
{
|
||||
byte[] m_Peek = new byte[4];
|
||||
|
||||
buffer.Dequeue(m_Peek, 0, 4);
|
||||
|
||||
int seed = (m_Peek[0] << 24) | (m_Peek[1] << 16) | (m_Peek[2] << 8) | m_Peek[3];
|
||||
|
||||
if (seed == 0)
|
||||
{
|
||||
Console.WriteLine("Login: {0}: Invalid client detected, disconnecting", ns);
|
||||
ns.Dispose();
|
||||
return false;
|
||||
}
|
||||
|
||||
ns.m_Seed = seed;
|
||||
ns.Seeded = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool CheckEncrypted(NetState ns, int packetID)
|
||||
{
|
||||
if (!ns.SentFirstPacket && packetID != 0xF0 && packetID != 0xF1 && packetID != 0xCF && packetID != 0x80 &&
|
||||
packetID != 0x91 && packetID != 0xA4 && packetID != 0xEF)
|
||||
{
|
||||
Console.WriteLine("Client: {0}: Encrypted client detected, disconnecting", ns);
|
||||
ns.Dispose();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void HandleReceive(NetState ns)
|
||||
{
|
||||
ByteQueue buffer = ns.Buffer;
|
||||
|
||||
if (buffer == null || buffer.Length <= 0)
|
||||
return;
|
||||
|
||||
lock (buffer)
|
||||
{
|
||||
if (!ns.Seeded)
|
||||
if (!HandleSeed(ns, buffer))
|
||||
return;
|
||||
|
||||
int length = buffer.Length;
|
||||
|
||||
while (length > 0 && ns.Running)
|
||||
{
|
||||
int packetID = buffer.GetPacketID();
|
||||
|
||||
if (CheckEncrypted(ns, packetID))
|
||||
break;
|
||||
|
||||
PacketHandler handler = ns.GetHandler(packetID);
|
||||
|
||||
if (handler == null)
|
||||
{
|
||||
byte[] data = new byte[length];
|
||||
length = buffer.Dequeue(data, 0, length);
|
||||
new PacketReader(data, length, false).Trace(ns);
|
||||
break;
|
||||
}
|
||||
|
||||
int packetLength = handler.Length;
|
||||
|
||||
if (packetLength <= 0)
|
||||
{
|
||||
if (length >= 3)
|
||||
{
|
||||
packetLength = buffer.GetPacketLength();
|
||||
|
||||
if (packetLength < 3)
|
||||
{
|
||||
ns.Dispose();
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (length >= packetLength)
|
||||
{
|
||||
if (handler.Ingame)
|
||||
{
|
||||
if (ns.Mobile == null)
|
||||
{
|
||||
Console.WriteLine(
|
||||
"Client: {0}: Sent ingame packet (0x{1:X2}) before having been attached to a mobile", ns,
|
||||
packetID);
|
||||
ns.Dispose();
|
||||
break;
|
||||
}
|
||||
|
||||
if (ns.Mobile.Deleted)
|
||||
{
|
||||
ns.Dispose();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ThrottlePacketCallback throttler = handler.ThrottleCallback;
|
||||
|
||||
if (throttler?.Invoke(ns) == false)
|
||||
{
|
||||
m_Throttled.Enqueue(ns);
|
||||
return;
|
||||
}
|
||||
|
||||
PacketReceiveProfile prof = null;
|
||||
|
||||
if (Core.Profiling)
|
||||
prof = PacketReceiveProfile.Acquire(packetID);
|
||||
|
||||
prof?.Start();
|
||||
|
||||
byte[] packetBuffer = BufferSize >= packetLength ? m_Buffers.AcquireBuffer() : new byte[packetLength];
|
||||
|
||||
packetLength = buffer.Dequeue(packetBuffer, 0, packetLength);
|
||||
|
||||
PacketReader r = new PacketReader(packetBuffer, packetLength, handler.Length != 0);
|
||||
|
||||
handler.OnReceive(ns, r);
|
||||
length = buffer.Length;
|
||||
|
||||
if (BufferSize >= packetLength)
|
||||
m_Buffers.ReleaseBuffer(packetBuffer);
|
||||
|
||||
prof?.Finish(packetLength);
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
State = ns;
|
||||
Sequence = seq;
|
||||
OnReceive = onReceive;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue