Host.cs 4.6 KB

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