Host.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #region License
  2. /*
  3. ENet for C#
  4. Copyright (c) 2011 James F. Bellinger <jfb@zer7.com>
  5. Permission to use, copy, modify, and/or distribute this software for any
  6. purpose with or without fee is hereby granted, provided that the above
  7. copyright notice and this permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #endregion
  17. using System;
  18. namespace ENet
  19. {
  20. public sealed unsafe class Host : IDisposable
  21. {
  22. private Native.ENetHost* host;
  23. ~Host()
  24. {
  25. this.Dispose(false);
  26. }
  27. private static void CheckChannelLimit(int channelLimit)
  28. {
  29. if (channelLimit < 0 || channelLimit > Native.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
  30. {
  31. throw new ArgumentOutOfRangeException("channelLimit");
  32. }
  33. }
  34. private void CheckCreated()
  35. {
  36. if (this.host == null)
  37. {
  38. throw new InvalidOperationException("Not created.");
  39. }
  40. }
  41. public void Create(ushort port, int peerLimit)
  42. {
  43. var address = new Address {Port = port};
  44. Create(address, peerLimit);
  45. }
  46. public void Create(Address? address, int peerLimit)
  47. {
  48. this.Create(address, peerLimit, 0);
  49. }
  50. public void Create(Address? address, int peerLimit, int channelLimit)
  51. {
  52. this.Create(address, peerLimit, channelLimit, 0, 0);
  53. }
  54. public void Create(Address? address, int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth)
  55. {
  56. if (this.host != null)
  57. {
  58. throw new InvalidOperationException("Already created.");
  59. }
  60. if (peerLimit < 0 || peerLimit > Native.ENET_PROTOCOL_MAXIMUM_PEER_ID)
  61. {
  62. throw new ArgumentOutOfRangeException("peerLimit");
  63. }
  64. CheckChannelLimit(channelLimit);
  65. if (address != null)
  66. {
  67. Native.ENetAddress nativeAddress = address.Value.NativeData;
  68. this.host = Native.enet_host_create(
  69. ref nativeAddress, (IntPtr) peerLimit, (IntPtr) channelLimit, incomingBandwidth,
  70. outgoingBandwidth);
  71. }
  72. else
  73. {
  74. this.host = Native.enet_host_create(
  75. null, (IntPtr) peerLimit, (IntPtr) channelLimit, incomingBandwidth,
  76. outgoingBandwidth);
  77. }
  78. if (this.host == null)
  79. {
  80. throw new ENetException(0, "Host creation call failed.");
  81. }
  82. }
  83. public void Dispose()
  84. {
  85. this.Dispose(true);
  86. }
  87. private void Dispose(bool disposing)
  88. {
  89. if (this.host == null)
  90. {
  91. return;
  92. }
  93. Native.enet_host_destroy(this.host);
  94. this.host = null;
  95. }
  96. public void Broadcast(byte channelID, ref Packet packet)
  97. {
  98. this.CheckCreated();
  99. packet.CheckCreated();
  100. Native.enet_host_broadcast(this.host, channelID, packet.NativeData);
  101. packet.NativeData = null; // Broadcast automatically clears this.
  102. }
  103. public void CompressWithRangeEncoder()
  104. {
  105. this.CheckCreated();
  106. Native.enet_host_compress_with_range_encoder(this.host);
  107. }
  108. public void DoNotCompress()
  109. {
  110. this.CheckCreated();
  111. Native.enet_host_compress(this.host, null);
  112. }
  113. public int CheckEvents(out Event e)
  114. {
  115. this.CheckCreated();
  116. Native.ENetEvent nativeEvent;
  117. int ret = Native.enet_host_check_events(this.host, out nativeEvent);
  118. if (ret <= 0)
  119. {
  120. e = new Event();
  121. return ret;
  122. }
  123. e = new Event(nativeEvent);
  124. return ret;
  125. }
  126. public Peer Connect(Address address, int channelLimit, uint data)
  127. {
  128. this.CheckCreated();
  129. CheckChannelLimit(channelLimit);
  130. Native.ENetAddress nativeAddress = address.NativeData;
  131. var peer = new Peer(Native.enet_host_connect(this.host, ref nativeAddress, (IntPtr) channelLimit, data));
  132. if (peer.NativeData == null)
  133. {
  134. throw new ENetException(0, "Host connect call failed.");
  135. }
  136. return peer;
  137. }
  138. public void Flush()
  139. {
  140. this.CheckCreated();
  141. Native.enet_host_flush(this.host);
  142. }
  143. public int Service(int timeout)
  144. {
  145. if (timeout < 0)
  146. {
  147. throw new ArgumentOutOfRangeException("timeout");
  148. }
  149. this.CheckCreated();
  150. return Native.enet_host_service(this.host, null, (uint) timeout);
  151. }
  152. public int Service(int timeout, out Event e)
  153. {
  154. if (timeout < 0)
  155. {
  156. throw new ArgumentOutOfRangeException("timeout");
  157. }
  158. this.CheckCreated();
  159. Native.ENetEvent nativeEvent;
  160. int ret = Native.enet_host_service(this.host, out nativeEvent, (uint) timeout);
  161. if (ret <= 0)
  162. {
  163. e = new Event();
  164. return ret;
  165. }
  166. e = new Event(nativeEvent);
  167. return ret;
  168. }
  169. public void SetBandwidthLimit(uint incomingBandwidth, uint outgoingBandwidth)
  170. {
  171. this.CheckCreated();
  172. Native.enet_host_bandwidth_limit(this.host, incomingBandwidth, outgoingBandwidth);
  173. }
  174. public void SetChannelLimit(int channelLimit)
  175. {
  176. CheckChannelLimit(channelLimit);
  177. this.CheckCreated();
  178. Native.enet_host_channel_limit(this.host, (IntPtr) channelLimit);
  179. }
  180. public bool IsSet
  181. {
  182. get
  183. {
  184. return this.host != null;
  185. }
  186. }
  187. public Native.ENetHost* NativeData
  188. {
  189. get
  190. {
  191. return this.host;
  192. }
  193. set
  194. {
  195. this.host = value;
  196. }
  197. }
  198. }
  199. }