|
|
@@ -3,11 +3,11 @@ using System.Threading.Tasks;
|
|
|
|
|
|
namespace ENet
|
|
|
{
|
|
|
- public sealed class Host: IDisposable
|
|
|
+ public abstract class Host: IDisposable
|
|
|
{
|
|
|
private readonly PeersManager peersManager = new PeersManager();
|
|
|
|
|
|
- public PeersManager PeersManager
|
|
|
+ protected PeersManager PeersManager
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
@@ -15,73 +15,23 @@ namespace ENet
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private IntPtr host;
|
|
|
- private Action<Event> acceptHandler;
|
|
|
+ protected IntPtr host;
|
|
|
+ protected bool isRunning = true;
|
|
|
private readonly object eventsLock = new object();
|
|
|
private Action events;
|
|
|
- private bool isRunning = true;
|
|
|
-
|
|
|
- public Host(Address address, uint peerLimit = NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID,
|
|
|
- uint channelLimit = 0, uint incomingBandwidth = 0,
|
|
|
- uint outgoingBandwidth = 0, bool enableCrc = true)
|
|
|
- {
|
|
|
- if (peerLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID)
|
|
|
- {
|
|
|
- throw new ArgumentOutOfRangeException("peerLimit");
|
|
|
- }
|
|
|
- CheckChannelLimit(channelLimit);
|
|
|
-
|
|
|
- ENetAddress nativeAddress = address.Struct;
|
|
|
- this.host = NativeMethods.enet_host_create(
|
|
|
- ref nativeAddress, peerLimit,
|
|
|
- channelLimit, incomingBandwidth, outgoingBandwidth);
|
|
|
-
|
|
|
- if (this.host == IntPtr.Zero)
|
|
|
- {
|
|
|
- throw new ENetException(0, "Host creation call failed.");
|
|
|
- }
|
|
|
-
|
|
|
- if (enableCrc)
|
|
|
- {
|
|
|
- NativeMethods.enet_enable_crc(this.host);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public Host(uint peerLimit = NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID,
|
|
|
- uint channelLimit = 0, uint incomingBandwidth = 0,
|
|
|
- uint outgoingBandwidth = 0, bool enableCrc = true)
|
|
|
- {
|
|
|
- if (peerLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID)
|
|
|
- {
|
|
|
- throw new ArgumentOutOfRangeException("peerLimit");
|
|
|
- }
|
|
|
- CheckChannelLimit(channelLimit);
|
|
|
-
|
|
|
- this.host = NativeMethods.enet_host_create(IntPtr.Zero, peerLimit, channelLimit, incomingBandwidth, outgoingBandwidth);
|
|
|
-
|
|
|
- if (this.host == IntPtr.Zero)
|
|
|
- {
|
|
|
- throw new ENetException(0, "Host creation call failed.");
|
|
|
- }
|
|
|
-
|
|
|
- if (enableCrc)
|
|
|
- {
|
|
|
- NativeMethods.enet_enable_crc(this.host);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
~Host()
|
|
|
{
|
|
|
this.Dispose(false);
|
|
|
}
|
|
|
|
|
|
- public void Dispose()
|
|
|
+ public virtual void Dispose()
|
|
|
{
|
|
|
this.Dispose(true);
|
|
|
GC.SuppressFinalize(this);
|
|
|
}
|
|
|
|
|
|
- private void Dispose(bool disposing)
|
|
|
+ protected void Dispose(bool disposing)
|
|
|
{
|
|
|
if (this.host == IntPtr.Zero)
|
|
|
{
|
|
|
@@ -93,7 +43,12 @@ namespace ENet
|
|
|
this.host = IntPtr.Zero;
|
|
|
}
|
|
|
|
|
|
- private static void CheckChannelLimit(uint channelLimit)
|
|
|
+ protected void EnableCrc()
|
|
|
+ {
|
|
|
+ NativeMethods.enet_enable_crc(this.host);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected static void CheckChannelLimit(uint channelLimit)
|
|
|
{
|
|
|
if (channelLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
|
|
|
{
|
|
|
@@ -101,15 +56,15 @@ namespace ENet
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private int CheckEvents(out Event e)
|
|
|
+ protected int CheckEvents(out Event e)
|
|
|
{
|
|
|
var enetEv = new ENetEvent();
|
|
|
int ret = NativeMethods.enet_host_check_events(this.host, enetEv);
|
|
|
- e = new Event(this, enetEv);
|
|
|
+ e = new Event(enetEv);
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
- private int Service(int timeout)
|
|
|
+ protected int Service(int timeout)
|
|
|
{
|
|
|
if (timeout < 0)
|
|
|
{
|
|
|
@@ -133,34 +88,6 @@ namespace ENet
|
|
|
NativeMethods.enet_host_compress(this.host, IntPtr.Zero);
|
|
|
}
|
|
|
|
|
|
- public Task<Peer> ConnectAsync(
|
|
|
- Address address, uint channelLimit = NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT, uint data = 0)
|
|
|
- {
|
|
|
- CheckChannelLimit(channelLimit);
|
|
|
-
|
|
|
- var tcs = new TaskCompletionSource<Peer>();
|
|
|
- ENetAddress nativeAddress = address.Struct;
|
|
|
- IntPtr p = NativeMethods.enet_host_connect(this.host, ref nativeAddress, channelLimit, data);
|
|
|
- if (p == IntPtr.Zero)
|
|
|
- {
|
|
|
- throw new ENetException(0, "Host connect call failed.");
|
|
|
- }
|
|
|
- var peer = new Peer(this, p);
|
|
|
- this.PeersManager[p].PeerEvent.Connected += e => tcs.TrySetResult(peer);
|
|
|
- return tcs.Task;
|
|
|
- }
|
|
|
-
|
|
|
- public Task<Peer> AcceptAsync()
|
|
|
- {
|
|
|
- if (acceptHandler != null)
|
|
|
- {
|
|
|
- throw new ENetException(0, "don't accept twice, when last accept not return!");
|
|
|
- }
|
|
|
- var tcs = new TaskCompletionSource<Peer>();
|
|
|
- acceptHandler += e => tcs.TrySetResult(e.Peer);
|
|
|
- return tcs.Task;
|
|
|
- }
|
|
|
-
|
|
|
public void Flush()
|
|
|
{
|
|
|
NativeMethods.enet_host_flush(this.host);
|
|
|
@@ -195,7 +122,7 @@ namespace ENet
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void OnExecuteEvents()
|
|
|
+ protected void OnExecuteEvents()
|
|
|
{
|
|
|
Action local = null;
|
|
|
lock (this.eventsLock)
|
|
|
@@ -214,54 +141,5 @@ namespace ENet
|
|
|
{
|
|
|
isRunning = false;
|
|
|
}
|
|
|
-
|
|
|
- public void Run(int timeout = 0)
|
|
|
- {
|
|
|
- while (isRunning)
|
|
|
- {
|
|
|
- this.OnExecuteEvents();
|
|
|
-
|
|
|
- if (this.Service(timeout) < 0)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- Event ev;
|
|
|
- while (this.CheckEvents(out ev) > 0)
|
|
|
- {
|
|
|
- switch (ev.Type)
|
|
|
- {
|
|
|
- case EventType.Connect:
|
|
|
- {
|
|
|
- // 如果PeersManager包含了peer,则这次是connect事件
|
|
|
- // 反之是accept事件
|
|
|
- if (this.PeersManager.ContainsKey(ev.Ev.peer))
|
|
|
- {
|
|
|
- ev.Peer.PeerEvent.OnConnected(ev);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- if (acceptHandler != null)
|
|
|
- {
|
|
|
- acceptHandler(ev);
|
|
|
- acceptHandler = null;
|
|
|
- }
|
|
|
- }
|
|
|
- break;
|
|
|
- }
|
|
|
- case EventType.Receive:
|
|
|
- {
|
|
|
- ev.Peer.PeerEvent.OnReceived(ev);
|
|
|
- break;
|
|
|
- }
|
|
|
- case EventType.Disconnect:
|
|
|
- {
|
|
|
- ev.Peer.PeerEvent.OnDisconnect(ev);
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
}
|
|
|
}
|