EService.cs 9.7 KB

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