EService.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ENet
  4. {
  5. public sealed class EService: IDisposable
  6. {
  7. static EService()
  8. {
  9. Library.Initialize();
  10. }
  11. private readonly PeersManager peersManager = new PeersManager();
  12. private readonly LinkedList<EEvent> connEEvents = new LinkedList<EEvent>();
  13. internal PeersManager PeersManager
  14. {
  15. get
  16. {
  17. return this.peersManager;
  18. }
  19. }
  20. internal LinkedList<EEvent> ConnEEvents
  21. {
  22. get
  23. {
  24. return this.connEEvents;
  25. }
  26. }
  27. private IntPtr host;
  28. private bool isRunning = true;
  29. private readonly object eventsLock = new object();
  30. private Action events;
  31. public EService(
  32. string hostName, ushort port, uint peerLimit = NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID,
  33. uint channelLimit = 0, uint incomingBandwidth = 0, uint outgoingBandwidth = 0)
  34. {
  35. if (peerLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID)
  36. {
  37. throw new ArgumentOutOfRangeException(string.Format("peerLimit: {0}", peerLimit));
  38. }
  39. if (channelLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
  40. {
  41. throw new ArgumentOutOfRangeException(string.Format("channelLimit: {0}", channelLimit));
  42. }
  43. var address = new Address { HostName = hostName, Port = port };
  44. ENetAddress nativeAddress = address.Struct;
  45. this.host = NativeMethods.EnetHostCreate(ref nativeAddress, peerLimit, channelLimit,
  46. incomingBandwidth, outgoingBandwidth);
  47. if (this.host == IntPtr.Zero)
  48. {
  49. throw new EException("Host creation call failed.");
  50. }
  51. }
  52. public EService(
  53. uint peerLimit = NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID, uint channelLimit = 0,
  54. uint incomingBandwidth = 0, uint outgoingBandwidth = 0)
  55. {
  56. if (peerLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID)
  57. {
  58. throw new ArgumentOutOfRangeException(string.Format("peerLimit: {0}", peerLimit));
  59. }
  60. if (channelLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
  61. {
  62. throw new ArgumentOutOfRangeException(string.Format("channelLimit: {0}", channelLimit));
  63. }
  64. this.host = NativeMethods.EnetHostCreate(IntPtr.Zero, peerLimit, channelLimit, incomingBandwidth,
  65. outgoingBandwidth);
  66. if (this.host == IntPtr.Zero)
  67. {
  68. throw new EException("Host creation call failed.");
  69. }
  70. }
  71. ~EService()
  72. {
  73. this.Dispose(false);
  74. }
  75. public void Dispose()
  76. {
  77. this.Dispose(true);
  78. GC.SuppressFinalize(this);
  79. }
  80. private void Dispose(bool disposing)
  81. {
  82. if (this.host == IntPtr.Zero)
  83. {
  84. return;
  85. }
  86. NativeMethods.EnetHostDestroy(this.host);
  87. this.host = IntPtr.Zero;
  88. }
  89. public IntPtr HostPtr
  90. {
  91. get
  92. {
  93. return this.host;
  94. }
  95. }
  96. public void EnableCrc()
  97. {
  98. NativeMethods.EnetEnableCrc(this.host);
  99. }
  100. private EEvent GetEvent()
  101. {
  102. var enetEv = new ENetEvent();
  103. int ret = NativeMethods.EnetHostCheckEvents(this.host, enetEv);
  104. if (ret <= 0)
  105. {
  106. return null;
  107. }
  108. var e = new EEvent(enetEv);
  109. return e;
  110. }
  111. public void CompressWithRangeCoder()
  112. {
  113. NativeMethods.EnetHostCompressWithRangeCoder(this.host);
  114. }
  115. public void DoNotCompress()
  116. {
  117. NativeMethods.EnetHostCompress(this.host, IntPtr.Zero);
  118. }
  119. public void Flush()
  120. {
  121. NativeMethods.EnetHostFlush(this.host);
  122. }
  123. public void SetBandwidthLimit(uint incomingBandwidth, uint outgoingBandwidth)
  124. {
  125. NativeMethods.EnetHostBandwidthLimit(this.host, incomingBandwidth, outgoingBandwidth);
  126. }
  127. public void SetChannelLimit(uint channelLimit)
  128. {
  129. if (channelLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
  130. {
  131. throw new ArgumentOutOfRangeException(string.Format("channelLimit: {0}", channelLimit));
  132. }
  133. NativeMethods.EnetHostChannelLimit(this.host, channelLimit);
  134. }
  135. public event Action Events
  136. {
  137. add
  138. {
  139. lock (this.eventsLock)
  140. {
  141. this.events += value;
  142. }
  143. }
  144. remove
  145. {
  146. lock (this.eventsLock)
  147. {
  148. this.events -= value;
  149. }
  150. }
  151. }
  152. public void OnEvents()
  153. {
  154. Action local = null;
  155. lock (this.eventsLock)
  156. {
  157. if (this.events == null)
  158. {
  159. return;
  160. }
  161. local = this.events;
  162. this.events = null;
  163. }
  164. local();
  165. }
  166. public void Stop()
  167. {
  168. this.isRunning = false;
  169. }
  170. private int Service(int timeout)
  171. {
  172. if (timeout < 0)
  173. {
  174. throw new ArgumentOutOfRangeException(string.Format("timeout: {0}", timeout));
  175. }
  176. return NativeMethods.EnetHostService(this.host, null, (uint) timeout);
  177. }
  178. public void RunOnce(int timeout = 0)
  179. {
  180. if (timeout < 0)
  181. {
  182. throw new ArgumentOutOfRangeException(string.Format("timeout: {0}", timeout));
  183. }
  184. this.OnEvents();
  185. if (this.Service(timeout) < 0)
  186. {
  187. return;
  188. }
  189. while (true)
  190. {
  191. EEvent eEvent = this.GetEvent();
  192. if (eEvent == null)
  193. {
  194. return;
  195. }
  196. switch (eEvent.Type)
  197. {
  198. case EventType.Connect:
  199. {
  200. // 这是一个connect peer
  201. if (this.PeersManager.ContainsKey(eEvent.PeerPtr))
  202. {
  203. ESocket eSocket = this.PeersManager[eEvent.PeerPtr];
  204. eSocket.OnConnected(eEvent);
  205. }
  206. // accept peer
  207. else
  208. {
  209. // 如果server端没有acceptasync,则请求放入队列
  210. if (!this.PeersManager.ContainsKey(IntPtr.Zero))
  211. {
  212. this.connEEvents.AddLast(eEvent);
  213. }
  214. else
  215. {
  216. ESocket eSocket = this.PeersManager[IntPtr.Zero];
  217. eSocket.OnConnected(eEvent);
  218. }
  219. }
  220. break;
  221. }
  222. case EventType.Receive:
  223. {
  224. ESocket eSocket = this.PeersManager[eEvent.PeerPtr];
  225. eSocket.OnReceived(eEvent);
  226. break;
  227. }
  228. case EventType.Disconnect:
  229. {
  230. // 如果链接还在缓存中,则删除
  231. foreach (EEvent connEEvent in this.connEEvents)
  232. {
  233. if (connEEvent.PeerPtr != eEvent.PeerPtr)
  234. {
  235. continue;
  236. }
  237. this.connEEvents.Remove(connEEvent);
  238. return;
  239. }
  240. // 链接已经被应用层接收
  241. eEvent.EventState = EventState.DISCONNECTED;
  242. ESocket eSocket = this.PeersManager[eEvent.PeerPtr];
  243. this.PeersManager.Remove(eEvent.PeerPtr);
  244. // 等待的task将抛出异常
  245. if (eSocket.Connected != null)
  246. {
  247. eSocket.OnConnected(eEvent);
  248. }
  249. else if (eSocket.Received != null)
  250. {
  251. eSocket.OnReceived(eEvent);
  252. }
  253. else if (eSocket.Disconnect != null)
  254. {
  255. eSocket.OnDisconnect(eEvent);
  256. }
  257. eSocket.OnError(ErrorCode.ClientDisconnect);
  258. break;
  259. }
  260. }
  261. }
  262. }
  263. public void Start(int timeout = 0)
  264. {
  265. while (this.isRunning)
  266. {
  267. this.RunOnce(timeout);
  268. }
  269. }
  270. }
  271. }