Reorganizes Project (#41)
This commit is contained in:
parent
08bf44af9a
commit
3614a66aee
3499 changed files with 79 additions and 55 deletions
77
Projects/Server/Network/MessagePump.cs
Normal file
77
Projects/Server/Network/MessagePump.cs
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
|
||||
/***************************************************************************
|
||||
* MessagePump.cs
|
||||
* -------------------
|
||||
* begin : May 1, 2002
|
||||
* copyright : (C) The RunUO Software Team
|
||||
* email : info@runuo.com
|
||||
*
|
||||
* $Id$
|
||||
*
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
*
|
||||
* 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 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
|
||||
namespace Server.Network
|
||||
{
|
||||
public class MessagePump
|
||||
{
|
||||
private ConcurrentQueue<Work> m_WorkQueue = new ConcurrentQueue<Work>();
|
||||
public Listener[] Listeners => new Listener[0];
|
||||
|
||||
public void AddListener(IPEndPoint ipep)
|
||||
{
|
||||
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 void QueueWork(NetState ns, in ReadOnlySequence<byte> seq, OnPacketReceive onReceive)
|
||||
{
|
||||
m_WorkQueue.Enqueue(new Work(ns, seq, onReceive));
|
||||
Core.Set();
|
||||
}
|
||||
|
||||
public void DoWork()
|
||||
{
|
||||
int count = m_WorkQueue.Count;
|
||||
while (count-- > 0)
|
||||
{
|
||||
if (!m_WorkQueue.TryDequeue(out Work work))
|
||||
break;
|
||||
|
||||
work.OnReceive(work.State, new PacketReader(work.Sequence));
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Optimize this with a pool
|
||||
private class Work
|
||||
{
|
||||
public NetState State;
|
||||
public ReadOnlySequence<byte> Sequence;
|
||||
public OnPacketReceive OnReceive;
|
||||
|
||||
public Work(NetState ns, in ReadOnlySequence<byte> seq, OnPacketReceive onReceive)
|
||||
{
|
||||
State = ns;
|
||||
Sequence = seq;
|
||||
OnReceive = onReceive;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue