Host.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 Event GetEvent()
  52. {
  53. var enetEv = new ENetEvent();
  54. int ret = NativeMethods.enet_host_check_events(this.host, enetEv);
  55. if (ret <= 0)
  56. {
  57. return null;
  58. }
  59. var e = new Event(enetEv);
  60. return e;
  61. }
  62. protected int Service(int timeout)
  63. {
  64. if (timeout < 0)
  65. {
  66. throw new ArgumentOutOfRangeException("timeout");
  67. }
  68. return NativeMethods.enet_host_service(this.host, null, (uint) timeout);
  69. }
  70. public void Broadcast(byte channelID, ref Packet packet)
  71. {
  72. NativeMethods.enet_host_broadcast(this.host, channelID, packet.PacketPtr);
  73. }
  74. protected void CompressWithRangeCoder()
  75. {
  76. NativeMethods.enet_host_compress_with_range_coder(this.host);
  77. }
  78. public void DoNotCompress()
  79. {
  80. NativeMethods.enet_host_compress(this.host, IntPtr.Zero);
  81. }
  82. public void Flush()
  83. {
  84. NativeMethods.enet_host_flush(this.host);
  85. }
  86. public void SetBandwidthLimit(uint incomingBandwidth, uint outgoingBandwidth)
  87. {
  88. NativeMethods.enet_host_bandwidth_limit(this.host, incomingBandwidth, outgoingBandwidth);
  89. }
  90. public void SetChannelLimit(uint channelLimit)
  91. {
  92. CheckChannelLimit(channelLimit);
  93. NativeMethods.enet_host_channel_limit(this.host, channelLimit);
  94. }
  95. public event Action Events
  96. {
  97. add
  98. {
  99. lock (this.eventsLock)
  100. {
  101. this.events += value;
  102. }
  103. }
  104. remove
  105. {
  106. lock (this.eventsLock)
  107. {
  108. this.events -= value;
  109. }
  110. }
  111. }
  112. protected void OnEvents()
  113. {
  114. Action local = null;
  115. lock (this.eventsLock)
  116. {
  117. if (this.events == null)
  118. {
  119. return;
  120. }
  121. local = this.events;
  122. this.events = null;
  123. }
  124. local();
  125. }
  126. public void Stop()
  127. {
  128. this.isRunning = false;
  129. }
  130. }
  131. }