UPoller.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. namespace Base
  5. {
  6. internal sealed class UPoller : IDisposable
  7. {
  8. static UPoller()
  9. {
  10. Library.Initialize();
  11. }
  12. public USocketManager USocketManager { get; }
  13. private readonly Queue<IntPtr> connQueue = new Queue<IntPtr>();
  14. private IntPtr host;
  15. // 线程同步队列,发送接收socket回调都放到该队列,由poll线程统一执行
  16. private Queue<Action> concurrentQueue = new Queue<Action>();
  17. private Queue<Action> localQueue;
  18. private readonly object lockObject = new object();
  19. private ENetEvent eNetEventCache;
  20. private TaskCompletionSource<USocket> AcceptTcs { get; set; }
  21. public UPoller(string hostName, ushort port)
  22. {
  23. try
  24. {
  25. this.USocketManager = new USocketManager();
  26. UAddress address = new UAddress(hostName, port);
  27. ENetAddress nativeAddress = address.Struct;
  28. this.host = NativeMethods.enet_host_create(ref nativeAddress,
  29. NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID, 0, 0, 0);
  30. if (this.host == IntPtr.Zero)
  31. {
  32. throw new Exception("Host creation call failed.");
  33. }
  34. NativeMethods.enet_host_compress_with_range_coder(this.host);
  35. }
  36. catch (Exception e)
  37. {
  38. throw new Exception($"UPoll construct error, address: {hostName}:{port}", e);
  39. }
  40. }
  41. public UPoller()
  42. {
  43. this.USocketManager = new USocketManager();
  44. this.host = NativeMethods.enet_host_create(IntPtr.Zero, NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID, 0, 0, 0);
  45. if (this.host == IntPtr.Zero)
  46. {
  47. throw new Exception("Host creation call failed.");
  48. }
  49. NativeMethods.enet_host_compress_with_range_coder(this.host);
  50. }
  51. public void Dispose()
  52. {
  53. if (this.host == IntPtr.Zero)
  54. {
  55. return;
  56. }
  57. NativeMethods.enet_host_destroy(this.host);
  58. this.host = IntPtr.Zero;
  59. }
  60. public IntPtr Host
  61. {
  62. get
  63. {
  64. return this.host;
  65. }
  66. }
  67. public void Flush()
  68. {
  69. NativeMethods.enet_host_flush(this.host);
  70. }
  71. public void Add(Action action)
  72. {
  73. lock (lockObject)
  74. {
  75. this.concurrentQueue.Enqueue(action);
  76. }
  77. }
  78. public Task<USocket> AcceptAsync()
  79. {
  80. if (this.AcceptTcs != null)
  81. {
  82. throw new Exception("do not accept twice!");
  83. }
  84. // 如果有请求连接缓存的包,从缓存中取
  85. if (this.connQueue.Count > 0)
  86. {
  87. IntPtr ptr = this.connQueue.Dequeue();
  88. USocket socket = new USocket(ptr, this);
  89. this.USocketManager.Add(ptr, socket);
  90. return Task.FromResult(socket);
  91. }
  92. this.AcceptTcs = new TaskCompletionSource<USocket>();
  93. return this.AcceptTcs.Task;
  94. }
  95. private void OnAccepted(ENetEvent eEvent)
  96. {
  97. if (eEvent.Type == EventType.Disconnect)
  98. {
  99. this.AcceptTcs.TrySetException(new Exception("socket disconnected in accpet"));
  100. }
  101. USocket socket = new USocket(eEvent.Peer, this);
  102. this.USocketManager.Add(socket.PeerPtr, socket);
  103. socket.OnAccepted();
  104. var tcs = this.AcceptTcs;
  105. this.AcceptTcs = null;
  106. tcs.SetResult(socket);
  107. }
  108. private void OnEvents()
  109. {
  110. lock (lockObject)
  111. {
  112. localQueue = concurrentQueue;
  113. concurrentQueue = new Queue<Action>();
  114. }
  115. while (this.localQueue.Count > 0)
  116. {
  117. Action a = this.localQueue.Dequeue();
  118. a();
  119. }
  120. }
  121. private int Service()
  122. {
  123. int ret = NativeMethods.enet_host_service(this.host, IntPtr.Zero, 0);
  124. return ret;
  125. }
  126. public void Update()
  127. {
  128. this.OnEvents();
  129. if (this.Service() < 0)
  130. {
  131. return;
  132. }
  133. while (true)
  134. {
  135. if (NativeMethods.enet_host_check_events(this.host, ref this.eNetEventCache) <= 0)
  136. {
  137. return;
  138. }
  139. switch (this.eNetEventCache.Type)
  140. {
  141. case EventType.Connect:
  142. {
  143. // 这是一个connect peer
  144. if (this.USocketManager.ContainsKey(this.eNetEventCache.Peer))
  145. {
  146. USocket uSocket = this.USocketManager[this.eNetEventCache.Peer];
  147. uSocket.OnConnected();
  148. break;
  149. }
  150. // 这是accept peer
  151. if (this.AcceptTcs != null)
  152. {
  153. this.OnAccepted(this.eNetEventCache);
  154. break;
  155. }
  156. // 如果server端没有acceptasync,则请求放入队列
  157. this.connQueue.Enqueue(this.eNetEventCache.Peer);
  158. break;
  159. }
  160. case EventType.Receive:
  161. {
  162. USocket uSocket = this.USocketManager[this.eNetEventCache.Peer];
  163. uSocket.OnReceived(this.eNetEventCache);
  164. break;
  165. }
  166. case EventType.Disconnect:
  167. {
  168. USocket uSocket = this.USocketManager[this.eNetEventCache.Peer];
  169. this.USocketManager.Remove(uSocket.PeerPtr);
  170. uSocket.PeerPtr = IntPtr.Zero;
  171. uSocket.OnDisconnect(this.eNetEventCache);
  172. break;
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }