| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- using System;
- using System.Collections.Generic;
- namespace ENet
- {
- public sealed class EService: IDisposable
- {
- static EService()
- {
- Library.Initialize();
- }
- private readonly PeersManager peersManager = new PeersManager();
- private readonly LinkedList<EEvent> connEEvents = new LinkedList<EEvent>();
- internal PeersManager PeersManager
- {
- get
- {
- return this.peersManager;
- }
- }
- internal LinkedList<EEvent> ConnEEvents
- {
- get
- {
- return this.connEEvents;
- }
- }
- private IntPtr host;
- private bool isRunning = true;
- private readonly object eventsLock = new object();
- private Action events;
- public EService(
- string hostName, ushort port,
- uint peerLimit = NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID, uint channelLimit = 0,
- uint incomingBandwidth = 0, uint outgoingBandwidth = 0)
- {
- if (peerLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID)
- {
- throw new ArgumentOutOfRangeException(string.Format("peerLimit: {0}", peerLimit));
- }
- if (channelLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
- {
- throw new ArgumentOutOfRangeException(string.Format("channelLimit: {0}",
- channelLimit));
- }
- var address = new Address { HostName = hostName, Port = port };
- ENetAddress nativeAddress = address.Struct;
- this.host = NativeMethods.EnetHostCreate(ref nativeAddress, peerLimit, channelLimit,
- incomingBandwidth, outgoingBandwidth);
- if (this.host == IntPtr.Zero)
- {
- throw new EException("Host creation call failed.");
- }
- }
- public EService(
- uint peerLimit = NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID, uint channelLimit = 0,
- uint incomingBandwidth = 0, uint outgoingBandwidth = 0)
- {
- if (peerLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID)
- {
- throw new ArgumentOutOfRangeException(string.Format("peerLimit: {0}", peerLimit));
- }
- if (channelLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
- {
- throw new ArgumentOutOfRangeException(string.Format("channelLimit: {0}",
- channelLimit));
- }
- this.host = NativeMethods.EnetHostCreate(IntPtr.Zero, peerLimit, channelLimit,
- incomingBandwidth, outgoingBandwidth);
- if (this.host == IntPtr.Zero)
- {
- throw new EException("Host creation call failed.");
- }
- }
- ~EService()
- {
- this.Dispose(false);
- }
- public void Dispose()
- {
- this.Dispose(true);
- GC.SuppressFinalize(this);
- }
- private void Dispose(bool disposing)
- {
- if (this.host == IntPtr.Zero)
- {
- return;
- }
- NativeMethods.EnetHostDestroy(this.host);
- this.host = IntPtr.Zero;
- }
- public IntPtr HostPtr
- {
- get
- {
- return this.host;
- }
- }
- public void EnableCrc()
- {
- NativeMethods.EnetEnableCrc(this.host);
- }
- private EEvent GetEvent()
- {
- var enetEv = new ENetEvent();
- int ret = NativeMethods.EnetHostCheckEvents(this.host, enetEv);
- if (ret <= 0)
- {
- return null;
- }
- var e = new EEvent(enetEv);
- return e;
- }
- public void CompressWithRangeCoder()
- {
- NativeMethods.EnetHostCompressWithRangeCoder(this.host);
- }
- public void DoNotCompress()
- {
- NativeMethods.EnetHostCompress(this.host, IntPtr.Zero);
- }
- public void Flush()
- {
- NativeMethods.EnetHostFlush(this.host);
- }
- public void SetBandwidthLimit(uint incomingBandwidth, uint outgoingBandwidth)
- {
- NativeMethods.EnetHostBandwidthLimit(this.host, incomingBandwidth, outgoingBandwidth);
- }
- public void SetChannelLimit(uint channelLimit)
- {
- if (channelLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
- {
- throw new ArgumentOutOfRangeException(string.Format("channelLimit: {0}",
- channelLimit));
- }
- NativeMethods.EnetHostChannelLimit(this.host, channelLimit);
- }
- public event Action Events
- {
- add
- {
- lock (this.eventsLock)
- {
- this.events += value;
- }
- }
- remove
- {
- lock (this.eventsLock)
- {
- this.events -= value;
- }
- }
- }
- public void OnEvents()
- {
- Action local = null;
- lock (this.eventsLock)
- {
- if (this.events == null)
- {
- return;
- }
- local = this.events;
- this.events = null;
- }
- local();
- }
- public void Stop()
- {
- this.isRunning = false;
- }
- private int Service(int timeout)
- {
- if (timeout < 0)
- {
- throw new ArgumentOutOfRangeException(string.Format("timeout: {0}", timeout));
- }
- return NativeMethods.EnetHostService(this.host, null, (uint) timeout);
- }
- public void RunOnce(int timeout = 0)
- {
- if (timeout < 0)
- {
- throw new ArgumentOutOfRangeException(string.Format("timeout: {0}", timeout));
- }
- this.OnEvents();
- if (this.Service(timeout) < 0)
- {
- return;
- }
- while (true)
- {
- EEvent eEvent = this.GetEvent();
- if (eEvent == null)
- {
- return;
- }
- switch (eEvent.Type)
- {
- case EventType.Connect:
- {
- // 这是一个connect peer
- if (this.PeersManager.ContainsKey(eEvent.PeerPtr))
- {
- ESocket eSocket = this.PeersManager[eEvent.PeerPtr];
- eSocket.OnConnected(eEvent);
- }
- // accept peer
- else
- {
- // 如果server端没有acceptasync,则请求放入队列
- if (!this.PeersManager.ContainsKey(IntPtr.Zero))
- {
- this.connEEvents.AddLast(eEvent);
- }
- else
- {
- ESocket eSocket = this.PeersManager[IntPtr.Zero];
- eSocket.OnConnected(eEvent);
- }
- }
- break;
- }
- case EventType.Receive:
- {
- ESocket eSocket = this.PeersManager[eEvent.PeerPtr];
- eSocket.OnReceived(eEvent);
- break;
- }
- case EventType.Disconnect:
- {
- // 如果链接还在缓存中,则删除
- foreach (EEvent connEEvent in this.connEEvents)
- {
- if (connEEvent.PeerPtr != eEvent.PeerPtr)
- {
- continue;
- }
- this.connEEvents.Remove(connEEvent);
- return;
- }
- // 链接已经被应用层接收
- eEvent.EventState = EventState.DISCONNECTED;
- ESocket eSocket = this.PeersManager[eEvent.PeerPtr];
- this.PeersManager.Remove(eEvent.PeerPtr);
- // 等待的task将抛出异常
- if (eSocket.Connected != null)
- {
- eSocket.OnConnected(eEvent);
- }
- else if (eSocket.Received != null)
- {
- eSocket.OnReceived(eEvent);
- }
- else if (eSocket.Disconnect != null)
- {
- eSocket.OnDisconnect(eEvent);
- }
- eSocket.OnError(ErrorCode.ClientDisconnect);
- break;
- }
- }
- }
- }
- public void Start(int timeout = 0)
- {
- while (this.isRunning)
- {
- this.RunOnce(timeout);
- }
- }
- }
- }
|