Host.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using System.Threading.Tasks;
  3. namespace ENet
  4. {
  5. public sealed class Host: IDisposable
  6. {
  7. private readonly PeersManager peersManager = new PeersManager();
  8. public PeersManager PeersManager
  9. {
  10. get
  11. {
  12. return this.peersManager;
  13. }
  14. }
  15. private IntPtr host;
  16. private Action<Event> acceptHandler;
  17. private readonly object eventsLock = new object();
  18. private Action events;
  19. public Host(Address address, uint peerLimit = NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID,
  20. uint channelLimit = 0, uint incomingBandwidth = 0,
  21. uint outgoingBandwidth = 0, bool enableCrc = true)
  22. {
  23. if (peerLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID)
  24. {
  25. throw new ArgumentOutOfRangeException("peerLimit");
  26. }
  27. CheckChannelLimit(channelLimit);
  28. ENetAddress nativeAddress = address.Struct;
  29. this.host = NativeMethods.enet_host_create(ref nativeAddress, peerLimit, channelLimit, incomingBandwidth, outgoingBandwidth);
  30. if (this.host == IntPtr.Zero)
  31. {
  32. throw new ENetException(0, "Host creation call failed.");
  33. }
  34. if (enableCrc)
  35. {
  36. NativeMethods.enet_enable_crc(this.host);
  37. }
  38. }
  39. public Host(uint peerLimit = NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID,
  40. uint channelLimit = 0, uint incomingBandwidth = 0,
  41. uint outgoingBandwidth = 0, bool enableCrc = true)
  42. {
  43. if (peerLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID)
  44. {
  45. throw new ArgumentOutOfRangeException("peerLimit");
  46. }
  47. CheckChannelLimit(channelLimit);
  48. this.host = NativeMethods.enet_host_create(IntPtr.Zero, peerLimit, channelLimit, incomingBandwidth, outgoingBandwidth);
  49. if (this.host == IntPtr.Zero)
  50. {
  51. throw new ENetException(0, "Host creation call failed.");
  52. }
  53. if (enableCrc)
  54. {
  55. NativeMethods.enet_enable_crc(this.host);
  56. }
  57. }
  58. ~Host()
  59. {
  60. this.Dispose(false);
  61. }
  62. public void Dispose()
  63. {
  64. this.Dispose(true);
  65. GC.SuppressFinalize(this);
  66. }
  67. private void Dispose(bool disposing)
  68. {
  69. if (this.host == IntPtr.Zero)
  70. {
  71. return;
  72. }
  73. NativeMethods.enet_host_destroy(this.host);
  74. this.host = IntPtr.Zero;
  75. }
  76. private static void CheckChannelLimit(uint channelLimit)
  77. {
  78. if (channelLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
  79. {
  80. throw new ArgumentOutOfRangeException("channelLimit");
  81. }
  82. }
  83. private int CheckEvents(out Event e)
  84. {
  85. var enetEv = new ENetEvent();
  86. int ret = NativeMethods.enet_host_check_events(this.host, enetEv);
  87. e = new Event(this, enetEv);
  88. return ret;
  89. }
  90. private int Service(int timeout)
  91. {
  92. if (timeout < 0)
  93. {
  94. throw new ArgumentOutOfRangeException("timeout");
  95. }
  96. return NativeMethods.enet_host_service(this.host, null, (uint) timeout);
  97. }
  98. public void Broadcast(byte channelID, ref Packet packet)
  99. {
  100. NativeMethods.enet_host_broadcast(this.host, channelID, packet.NativePtr);
  101. }
  102. public void CompressWithRangeEncoder()
  103. {
  104. NativeMethods.enet_host_compress_with_range_encoder(this.host);
  105. }
  106. public void DoNotCompress()
  107. {
  108. NativeMethods.enet_host_compress(this.host, IntPtr.Zero);
  109. }
  110. public Task<Peer> ConnectAsync(
  111. Address address, uint channelLimit = NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT, uint data = 0)
  112. {
  113. CheckChannelLimit(channelLimit);
  114. var tcs = new TaskCompletionSource<Peer>();
  115. ENetAddress nativeAddress = address.Struct;
  116. IntPtr p = NativeMethods.enet_host_connect(this.host, ref nativeAddress, channelLimit, data);
  117. if (p == IntPtr.Zero)
  118. {
  119. throw new ENetException(0, "Host connect call failed.");
  120. }
  121. var peer = new Peer(this, p);
  122. this.PeersManager[p].PeerEvent.Connected += e => tcs.TrySetResult(peer);
  123. return tcs.Task;
  124. }
  125. public Task<Peer> AcceptAsync()
  126. {
  127. if (acceptHandler != null)
  128. {
  129. throw new ENetException(0, "don't accept twice, when last accept not return!");
  130. }
  131. var tcs = new TaskCompletionSource<Peer>();
  132. acceptHandler += e => tcs.TrySetResult(e.Peer);
  133. return tcs.Task;
  134. }
  135. public void Flush()
  136. {
  137. NativeMethods.enet_host_flush(this.host);
  138. }
  139. public void SetBandwidthLimit(uint incomingBandwidth, uint outgoingBandwidth)
  140. {
  141. NativeMethods.enet_host_bandwidth_limit(this.host, incomingBandwidth, outgoingBandwidth);
  142. }
  143. public void SetChannelLimit(uint channelLimit)
  144. {
  145. CheckChannelLimit(channelLimit);
  146. NativeMethods.enet_host_channel_limit(this.host, channelLimit);
  147. }
  148. public event Action Events
  149. {
  150. add
  151. {
  152. lock (this.eventsLock)
  153. {
  154. this.events += value;
  155. }
  156. }
  157. remove
  158. {
  159. lock (this.eventsLock)
  160. {
  161. this.events -= value;
  162. }
  163. }
  164. }
  165. private void OnExecuteEvents()
  166. {
  167. Action local = null;
  168. lock (this.eventsLock)
  169. {
  170. if (this.events == null)
  171. {
  172. return;
  173. }
  174. local = this.events;
  175. this.events = null;
  176. }
  177. local();
  178. }
  179. public void Run()
  180. {
  181. this.OnExecuteEvents();
  182. if (this.Service(0) < 0)
  183. {
  184. return;
  185. }
  186. Event ev;
  187. while (this.CheckEvents(out ev) > 0)
  188. {
  189. switch (ev.Type)
  190. {
  191. case EventType.Connect:
  192. {
  193. // 如果PeersManager包含了peer,则这次是connect事件
  194. // 反之是accept事件
  195. if (this.PeersManager.ContainsKey(ev.Ev.peer))
  196. {
  197. ev.Peer.PeerEvent.OnConnected(ev);
  198. }
  199. else
  200. {
  201. if (acceptHandler != null)
  202. {
  203. acceptHandler(ev);
  204. acceptHandler = null;
  205. }
  206. }
  207. break;
  208. }
  209. case EventType.Receive:
  210. {
  211. ev.Peer.PeerEvent.OnReceived(ev);
  212. break;
  213. }
  214. case EventType.Disconnect:
  215. {
  216. ev.Peer.PeerEvent.OnDisconnect(ev);
  217. break;
  218. }
  219. }
  220. }
  221. }
  222. }
  223. }