UPoller.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 QueueDictionary<IntPtr, ENetEvent> connQueue = new QueueDictionary<IntPtr, ENetEvent>();
  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. this.USocketManager = new USocketManager();
  24. UAddress address = new UAddress(hostName, port);
  25. ENetAddress nativeAddress = address.Struct;
  26. this.host = NativeMethods.ENetHostCreate(ref nativeAddress,
  27. NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID, 0, 0, 0);
  28. if (this.host == IntPtr.Zero)
  29. {
  30. throw new Exception("Host creation call failed.");
  31. }
  32. NativeMethods.ENetHostCompressWithRangeCoder(this.host);
  33. }
  34. public UPoller()
  35. {
  36. this.USocketManager = new USocketManager();
  37. this.host = NativeMethods.ENetHostCreate(IntPtr.Zero, NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID, 0, 0, 0);
  38. if (this.host == IntPtr.Zero)
  39. {
  40. throw new Exception("Host creation call failed.");
  41. }
  42. }
  43. public void Dispose()
  44. {
  45. if (this.host == IntPtr.Zero)
  46. {
  47. return;
  48. }
  49. NativeMethods.ENetHostDestroy(this.host);
  50. this.host = IntPtr.Zero;
  51. }
  52. public IntPtr Host
  53. {
  54. get
  55. {
  56. return this.host;
  57. }
  58. }
  59. private ENetEvent TryGetEvent()
  60. {
  61. if (this.eNetEventCache == null)
  62. {
  63. this.eNetEventCache = new ENetEvent();
  64. }
  65. if (NativeMethods.ENetHostCheckEvents(this.host, this.eNetEventCache) <= 0)
  66. {
  67. return null;
  68. }
  69. ENetEvent eNetEvent = this.eNetEventCache;
  70. this.eNetEventCache = null;
  71. return eNetEvent;
  72. }
  73. public void Flush()
  74. {
  75. NativeMethods.ENetHostFlush(this.host);
  76. }
  77. public void Add(Action action)
  78. {
  79. lock (lockObject)
  80. {
  81. this.concurrentQueue.Enqueue(action);
  82. }
  83. }
  84. public Task<USocket> AcceptAsync()
  85. {
  86. if (this.AcceptTcs != null)
  87. {
  88. throw new Exception("do not accept twice!");
  89. }
  90. var tcs = new TaskCompletionSource<USocket>();
  91. // 如果有请求连接缓存的包,从缓存中取
  92. if (this.connQueue.Count > 0)
  93. {
  94. IntPtr ptr = this.connQueue.FirstKey;
  95. this.connQueue.Remove(ptr);
  96. USocket socket = new USocket(ptr, this);
  97. this.USocketManager.Add(ptr, socket);
  98. tcs.SetResult(socket);
  99. }
  100. else
  101. {
  102. this.AcceptTcs = tcs;
  103. }
  104. return tcs.Task;
  105. }
  106. private void OnAccepted(ENetEvent eEvent)
  107. {
  108. if (eEvent.Type == EventType.Disconnect)
  109. {
  110. this.AcceptTcs.TrySetException(new Exception("socket disconnected in accpet"));
  111. }
  112. USocket socket = new USocket(eEvent.Peer, this);
  113. this.USocketManager.Add(socket.PeerPtr, socket);
  114. socket.OnAccepted();
  115. var tcs = this.AcceptTcs;
  116. this.AcceptTcs = null;
  117. tcs.SetResult(socket);
  118. }
  119. private void OnEvents()
  120. {
  121. lock (lockObject)
  122. {
  123. localQueue = concurrentQueue;
  124. concurrentQueue = new Queue<Action>();
  125. }
  126. while (this.localQueue.Count > 0)
  127. {
  128. Action a = this.localQueue.Dequeue();
  129. a();
  130. }
  131. }
  132. private int Service()
  133. {
  134. int ret = NativeMethods.ENetHostService(this.host, null, 0);
  135. return ret;
  136. }
  137. public void Update()
  138. {
  139. this.OnEvents();
  140. if (this.Service() < 0)
  141. {
  142. return;
  143. }
  144. while (true)
  145. {
  146. ENetEvent eNetEvent = this.TryGetEvent();
  147. if (eNetEvent == null)
  148. {
  149. return;
  150. }
  151. switch (eNetEvent.Type)
  152. {
  153. case EventType.Connect:
  154. {
  155. // 这是一个connect peer
  156. if (this.USocketManager.ContainsKey(eNetEvent.Peer))
  157. {
  158. USocket uSocket = this.USocketManager[eNetEvent.Peer];
  159. uSocket.OnConnected();
  160. break;
  161. }
  162. // 这是accept peer
  163. if (this.AcceptTcs != null)
  164. {
  165. this.OnAccepted(eNetEvent);
  166. break;
  167. }
  168. // 如果server端没有acceptasync,则请求放入队列
  169. this.connQueue.Add(eNetEvent.Peer, eNetEvent);
  170. break;
  171. }
  172. case EventType.Receive:
  173. {
  174. USocket uSocket = this.USocketManager[eNetEvent.Peer];
  175. uSocket.OnReceived(eNetEvent);
  176. break;
  177. }
  178. case EventType.Disconnect:
  179. {
  180. USocket uSocket = this.USocketManager[eNetEvent.Peer];
  181. this.USocketManager.Remove(uSocket.PeerPtr);
  182. uSocket.PeerPtr = IntPtr.Zero;
  183. uSocket.OnDisconnect(eNetEvent);
  184. break;
  185. }
  186. }
  187. }
  188. }
  189. }
  190. }