Host.cs 2.7 KB

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