Host.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. namespace ENet
  3. {
  4. public abstract class Host: IDisposable
  5. {
  6. static Host()
  7. {
  8. Library.Initialize();
  9. }
  10. private readonly PeersManager peersManager = new PeersManager();
  11. protected PeersManager PeersManager
  12. {
  13. get
  14. {
  15. return this.peersManager;
  16. }
  17. }
  18. protected IntPtr host;
  19. protected bool isRunning = true;
  20. private readonly object eventsLock = new object();
  21. private Action events;
  22. ~Host()
  23. {
  24. this.Dispose(false);
  25. }
  26. public virtual void Dispose()
  27. {
  28. this.Dispose(true);
  29. GC.SuppressFinalize(this);
  30. }
  31. protected void Dispose(bool disposing)
  32. {
  33. if (this.host == IntPtr.Zero)
  34. {
  35. return;
  36. }
  37. NativeMethods.enet_host_destroy(this.host);
  38. this.host = IntPtr.Zero;
  39. }
  40. protected void EnableCrc()
  41. {
  42. NativeMethods.enet_enable_crc(this.host);
  43. }
  44. protected static void CheckChannelLimit(uint channelLimit)
  45. {
  46. if (channelLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
  47. {
  48. throw new ArgumentOutOfRangeException("channelLimit");
  49. }
  50. }
  51. protected int CheckEvents(out Event e)
  52. {
  53. var enetEv = new ENetEvent();
  54. int ret = NativeMethods.enet_host_check_events(this.host, enetEv);
  55. e = new Event(enetEv);
  56. return ret;
  57. }
  58. protected int Service(int timeout)
  59. {
  60. if (timeout < 0)
  61. {
  62. throw new ArgumentOutOfRangeException("timeout");
  63. }
  64. return NativeMethods.enet_host_service(this.host, null, (uint) timeout);
  65. }
  66. public void Broadcast(byte channelID, ref Packet packet)
  67. {
  68. NativeMethods.enet_host_broadcast(this.host, channelID, packet.PacketPtr);
  69. }
  70. public void CompressWithRangeEncoder()
  71. {
  72. NativeMethods.enet_host_compress_with_range_encoder(this.host);
  73. }
  74. public void DoNotCompress()
  75. {
  76. NativeMethods.enet_host_compress(this.host, IntPtr.Zero);
  77. }
  78. public void Flush()
  79. {
  80. NativeMethods.enet_host_flush(this.host);
  81. }
  82. public void SetBandwidthLimit(uint incomingBandwidth, uint outgoingBandwidth)
  83. {
  84. NativeMethods.enet_host_bandwidth_limit(this.host, incomingBandwidth, outgoingBandwidth);
  85. }
  86. public void SetChannelLimit(uint channelLimit)
  87. {
  88. CheckChannelLimit(channelLimit);
  89. NativeMethods.enet_host_channel_limit(this.host, channelLimit);
  90. }
  91. public event Action Events
  92. {
  93. add
  94. {
  95. lock (this.eventsLock)
  96. {
  97. this.events += value;
  98. }
  99. }
  100. remove
  101. {
  102. lock (this.eventsLock)
  103. {
  104. this.events -= value;
  105. }
  106. }
  107. }
  108. protected void OnEvents()
  109. {
  110. Action local = null;
  111. lock (this.eventsLock)
  112. {
  113. if (this.events == null)
  114. {
  115. return;
  116. }
  117. local = this.events;
  118. this.events = null;
  119. }
  120. local();
  121. }
  122. public void Stop()
  123. {
  124. this.isRunning = false;
  125. }
  126. }
  127. }