Host.cs 4.8 KB

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