Host.cs 4.8 KB

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