EService.cs 6.5 KB

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