UPoller.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. var tcs = new TaskCompletionSource<USocket>();
  85. // 如果有请求连接缓存的包,从缓存中取
  86. if (this.connQueue.Count > 0)
  87. {
  88. IntPtr ptr = this.connQueue.Dequeue();
  89. USocket socket = new USocket(ptr, this);
  90. this.USocketManager.Add(ptr, socket);
  91. tcs.SetResult(socket);
  92. }
  93. else
  94. {
  95. this.AcceptTcs = tcs;
  96. }
  97. return tcs.Task;
  98. }
  99. private void OnAccepted(ENetEvent eEvent)
  100. {
  101. if (eEvent.Type == EventType.Disconnect)
  102. {
  103. this.AcceptTcs.TrySetException(new Exception("socket disconnected in accpet"));
  104. }
  105. USocket socket = new USocket(eEvent.Peer, this);
  106. this.USocketManager.Add(socket.PeerPtr, socket);
  107. socket.OnAccepted();
  108. var tcs = this.AcceptTcs;
  109. this.AcceptTcs = null;
  110. tcs.SetResult(socket);
  111. }
  112. private void OnEvents()
  113. {
  114. lock (lockObject)
  115. {
  116. localQueue = concurrentQueue;
  117. concurrentQueue = new Queue<Action>();
  118. }
  119. while (this.localQueue.Count > 0)
  120. {
  121. Action a = this.localQueue.Dequeue();
  122. a();
  123. }
  124. }
  125. private int Service()
  126. {
  127. int ret = NativeMethods.enet_host_service(this.host, IntPtr.Zero, 0);
  128. return ret;
  129. }
  130. public void Update()
  131. {
  132. this.OnEvents();
  133. if (this.Service() < 0)
  134. {
  135. return;
  136. }
  137. while (true)
  138. {
  139. if (NativeMethods.enet_host_check_events(this.host, ref this.eNetEventCache) <= 0)
  140. {
  141. return;
  142. }
  143. switch (this.eNetEventCache.Type)
  144. {
  145. case EventType.Connect:
  146. {
  147. // 这是一个connect peer
  148. if (this.USocketManager.ContainsKey(this.eNetEventCache.Peer))
  149. {
  150. USocket uSocket = this.USocketManager[this.eNetEventCache.Peer];
  151. uSocket.OnConnected();
  152. break;
  153. }
  154. // 这是accept peer
  155. if (this.AcceptTcs != null)
  156. {
  157. this.OnAccepted(this.eNetEventCache);
  158. break;
  159. }
  160. // 如果server端没有acceptasync,则请求放入队列
  161. this.connQueue.Enqueue(this.eNetEventCache.Peer);
  162. break;
  163. }
  164. case EventType.Receive:
  165. {
  166. USocket uSocket = this.USocketManager[this.eNetEventCache.Peer];
  167. uSocket.OnReceived(this.eNetEventCache);
  168. break;
  169. }
  170. case EventType.Disconnect:
  171. {
  172. USocket uSocket = this.USocketManager[this.eNetEventCache.Peer];
  173. this.USocketManager.Remove(uSocket.PeerPtr);
  174. uSocket.PeerPtr = IntPtr.Zero;
  175. uSocket.OnDisconnect(this.eNetEventCache);
  176. break;
  177. }
  178. }
  179. }
  180. }
  181. }
  182. }