Host.cs 4.9 KB

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