IOService.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. using System;
  2. namespace ENet
  3. {
  4. public class IOService: IDisposable
  5. {
  6. static IOService()
  7. {
  8. Library.Initialize();
  9. }
  10. private readonly PeersManager peersManager = new PeersManager();
  11. public PeersManager PeersManager
  12. {
  13. get
  14. {
  15. return this.peersManager;
  16. }
  17. }
  18. protected IntPtr host;
  19. protected bool isRunning = true;
  20. private readonly object eventsLock = new object();
  21. private Action events;
  22. public IOService(string hostName, ushort port,
  23. uint peerLimit = NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID,
  24. uint channelLimit = 0,
  25. uint incomingBandwidth = 0,
  26. uint outgoingBandwidth = 0)
  27. {
  28. if (peerLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID)
  29. {
  30. throw new ArgumentOutOfRangeException("peerLimit");
  31. }
  32. if (channelLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
  33. {
  34. throw new ArgumentOutOfRangeException(string.Format("channelLimit: {0}", channelLimit));
  35. }
  36. var address = new Address { HostName = hostName, Port = port };
  37. ENetAddress nativeAddress = address.Struct;
  38. this.host = NativeMethods.enet_host_create(
  39. ref nativeAddress, peerLimit, channelLimit, incomingBandwidth,
  40. outgoingBandwidth);
  41. if (this.host == IntPtr.Zero)
  42. {
  43. throw new ENetException("Host creation call failed.");
  44. }
  45. }
  46. public IOService(
  47. uint peerLimit = NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID,
  48. uint channelLimit = 0,
  49. uint incomingBandwidth = 0,
  50. uint outgoingBandwidth = 0)
  51. {
  52. if (peerLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID)
  53. {
  54. throw new ArgumentOutOfRangeException("peerLimit");
  55. }
  56. if (channelLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
  57. {
  58. throw new ArgumentOutOfRangeException(string.Format("channelLimit: {0}", channelLimit));
  59. }
  60. this.host = NativeMethods.enet_host_create(
  61. IntPtr.Zero, peerLimit, channelLimit, incomingBandwidth, outgoingBandwidth);
  62. if (this.host == IntPtr.Zero)
  63. {
  64. throw new ENetException("Host creation call failed.");
  65. }
  66. }
  67. ~IOService()
  68. {
  69. this.Dispose(false);
  70. }
  71. public virtual void Dispose()
  72. {
  73. this.Dispose(true);
  74. GC.SuppressFinalize(this);
  75. }
  76. protected void Dispose(bool disposing)
  77. {
  78. if (this.host == IntPtr.Zero)
  79. {
  80. return;
  81. }
  82. NativeMethods.enet_host_destroy(this.host);
  83. this.host = IntPtr.Zero;
  84. }
  85. public IntPtr HostPtr
  86. {
  87. get
  88. {
  89. return this.host;
  90. }
  91. }
  92. public void EnableCrc()
  93. {
  94. NativeMethods.enet_enable_crc(this.host);
  95. }
  96. private Event GetEvent()
  97. {
  98. var enetEv = new ENetEvent();
  99. int ret = NativeMethods.enet_host_check_events(this.host, enetEv);
  100. if (ret <= 0)
  101. {
  102. return null;
  103. }
  104. var e = new Event(enetEv);
  105. return e;
  106. }
  107. public void CompressWithRangeCoder()
  108. {
  109. NativeMethods.enet_host_compress_with_range_coder(this.host);
  110. }
  111. public void DoNotCompress()
  112. {
  113. NativeMethods.enet_host_compress(this.host, IntPtr.Zero);
  114. }
  115. public void Flush()
  116. {
  117. NativeMethods.enet_host_flush(this.host);
  118. }
  119. public void SetBandwidthLimit(uint incomingBandwidth, uint outgoingBandwidth)
  120. {
  121. NativeMethods.enet_host_bandwidth_limit(this.host, incomingBandwidth, outgoingBandwidth);
  122. }
  123. public void SetChannelLimit(uint channelLimit)
  124. {
  125. if (channelLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
  126. {
  127. throw new ArgumentOutOfRangeException(string.Format("channelLimit: {0}", channelLimit));
  128. }
  129. NativeMethods.enet_host_channel_limit(this.host, channelLimit);
  130. }
  131. public event Action Events
  132. {
  133. add
  134. {
  135. lock (this.eventsLock)
  136. {
  137. this.events += value;
  138. }
  139. }
  140. remove
  141. {
  142. lock (this.eventsLock)
  143. {
  144. this.events -= value;
  145. }
  146. }
  147. }
  148. public void OnEvents()
  149. {
  150. Action local = null;
  151. lock (this.eventsLock)
  152. {
  153. if (this.events == null)
  154. {
  155. return;
  156. }
  157. local = this.events;
  158. this.events = null;
  159. }
  160. local();
  161. }
  162. public void Stop()
  163. {
  164. this.isRunning = false;
  165. }
  166. private int Service(int timeout)
  167. {
  168. if (timeout < 0)
  169. {
  170. throw new ArgumentOutOfRangeException(string.Format("timeout: {0}", timeout));
  171. }
  172. return NativeMethods.enet_host_service(this.host, null, (uint)timeout);
  173. }
  174. public void RunOnce(int timeout = 0)
  175. {
  176. if (timeout < 0)
  177. {
  178. throw new ArgumentOutOfRangeException(string.Format("timeout: {0}", timeout));
  179. }
  180. this.OnEvents();
  181. if (this.Service(timeout) < 0)
  182. {
  183. return;
  184. }
  185. while (true)
  186. {
  187. Event ev = this.GetEvent();
  188. if (ev == null)
  189. {
  190. return;
  191. }
  192. switch (ev.Type)
  193. {
  194. case EventType.Connect:
  195. {
  196. // 这是一个connect peer,否则是一个accept peer
  197. if (this.PeersManager.ContainsKey(ev.PeerPtr))
  198. {
  199. var peer = this.PeersManager[ev.PeerPtr];
  200. peer.ESocketEvent.OnConnected(ev);
  201. peer.ESocketEvent.Connected = null;
  202. }
  203. else
  204. {
  205. var peer = this.PeersManager[IntPtr.Zero];
  206. this.PeersManager.Remove(IntPtr.Zero);
  207. peer.PeerPtr = ev.PeerPtr;
  208. this.PeersManager.Add(peer.PeerPtr, peer);
  209. peer.ESocketEvent.OnConnected(ev);
  210. peer.ESocketEvent.Connected = null;
  211. }
  212. break;
  213. }
  214. case EventType.Receive:
  215. {
  216. var peer = this.PeersManager[ev.PeerPtr];
  217. peer.ESocketEvent.OnReceived(ev);
  218. peer.ESocketEvent.Received = null;
  219. break;
  220. }
  221. case EventType.Disconnect:
  222. {
  223. ev.EventState = EventState.DISCONNECTED;
  224. var peer = this.PeersManager[ev.PeerPtr];
  225. ESocketEvent peerEvent = peer.ESocketEvent;
  226. this.PeersManager.Remove(peer.PeerPtr);
  227. // enet_peer_disconnect会reset Peer,这里设置为0,防止再次Dispose
  228. peer.PeerPtr = IntPtr.Zero;
  229. if (peerEvent.Received != null)
  230. {
  231. peerEvent.OnReceived(ev);
  232. }
  233. else
  234. {
  235. peerEvent.OnDisconnect(ev);
  236. }
  237. break;
  238. }
  239. }
  240. }
  241. }
  242. public void Start(int timeout = 0)
  243. {
  244. while (this.isRunning)
  245. {
  246. this.RunOnce(timeout);
  247. }
  248. }
  249. }
  250. }