EService.cs 6.5 KB

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