UPoller.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Threading.Tasks;
  5. using Common.Base;
  6. namespace UNet
  7. {
  8. internal sealed class UPoller: IDisposable
  9. {
  10. static UPoller()
  11. {
  12. Library.Initialize();
  13. }
  14. private readonly USocketManager uSocketManager = new USocketManager();
  15. private readonly QueueDictionary<IntPtr, ENetEvent> connQueue =
  16. new QueueDictionary<IntPtr, ENetEvent>();
  17. private IntPtr host;
  18. private readonly USocket acceptor;
  19. // 线程同步队列,发送接收socket回调都放到该队列,由poll线程统一执行
  20. private readonly ConcurrentQueue<Action> concurrentQueue = new ConcurrentQueue<Action>();
  21. private readonly Queue<Action> localQueue = new Queue<Action>();
  22. private ENetEvent eNetEventCache;
  23. public UPoller(string hostName, ushort port)
  24. {
  25. this.acceptor = new USocket(IntPtr.Zero, this);
  26. UAddress address = new UAddress(hostName, port);
  27. ENetAddress nativeAddress = address.Struct;
  28. this.host = NativeMethods.ENetHostCreate(ref nativeAddress,
  29. NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID, 0, 0, 0);
  30. if (this.host == IntPtr.Zero)
  31. {
  32. throw new UException("Host creation call failed.");
  33. }
  34. NativeMethods.ENetHostCompressWithRangeCoder(this.host);
  35. }
  36. public UPoller()
  37. {
  38. this.host = NativeMethods.ENetHostCreate(IntPtr.Zero, NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID,
  39. 0, 0, 0);
  40. if (this.host == IntPtr.Zero)
  41. {
  42. throw new UException("Host creation call failed.");
  43. }
  44. }
  45. ~UPoller()
  46. {
  47. this.Dispose(false);
  48. }
  49. public void Dispose()
  50. {
  51. this.Dispose(true);
  52. GC.SuppressFinalize(this);
  53. }
  54. private void Dispose(bool disposing)
  55. {
  56. if (this.host == IntPtr.Zero)
  57. {
  58. return;
  59. }
  60. NativeMethods.ENetHostDestroy(this.host);
  61. this.host = IntPtr.Zero;
  62. }
  63. public USocketManager USocketManager
  64. {
  65. get
  66. {
  67. return this.uSocketManager;
  68. }
  69. }
  70. public IntPtr Host
  71. {
  72. get
  73. {
  74. return this.host;
  75. }
  76. }
  77. public Task<USocket> AcceptAsync()
  78. {
  79. if (this.uSocketManager.ContainsKey(IntPtr.Zero))
  80. {
  81. throw new UException("do not accept twice!");
  82. }
  83. var tcs = new TaskCompletionSource<USocket>();
  84. // 如果有请求连接缓存的包,从缓存中取
  85. if (this.connQueue.Count > 0)
  86. {
  87. IntPtr ptr = this.connQueue.FirstKey;
  88. this.connQueue.Remove(ptr);
  89. USocket socket = new USocket(ptr, this);
  90. this.uSocketManager.Add(ptr, socket);
  91. tcs.TrySetResult(socket);
  92. }
  93. else
  94. {
  95. this.uSocketManager.Add(this.acceptor.PeerPtr, this.acceptor);
  96. this.acceptor.Connected = eEvent =>
  97. {
  98. if (eEvent.Type == EventType.Disconnect)
  99. {
  100. tcs.TrySetException(new UException("socket disconnected in accpet"));
  101. }
  102. this.uSocketManager.Remove(IntPtr.Zero);
  103. USocket socket = new USocket(eEvent.Peer, this);
  104. this.uSocketManager.Add(socket.PeerPtr, socket);
  105. tcs.TrySetResult(socket);
  106. };
  107. }
  108. return tcs.Task;
  109. }
  110. private ENetEvent GetEvent()
  111. {
  112. if (this.eNetEventCache == null)
  113. {
  114. this.eNetEventCache = new ENetEvent();
  115. }
  116. if (NativeMethods.ENetHostCheckEvents(this.host, this.eNetEventCache) <= 0)
  117. {
  118. return null;
  119. }
  120. ENetEvent eNetEvent = this.eNetEventCache;
  121. this.eNetEventCache = null;
  122. return eNetEvent;
  123. }
  124. public void Flush()
  125. {
  126. NativeMethods.ENetHostFlush(this.host);
  127. }
  128. public void Add(Action action)
  129. {
  130. this.concurrentQueue.Enqueue(action);
  131. }
  132. private void OnEvents()
  133. {
  134. while (true)
  135. {
  136. Action action;
  137. if (!this.concurrentQueue.TryDequeue(out action))
  138. {
  139. break;
  140. }
  141. this.localQueue.Enqueue(action);
  142. }
  143. while (this.localQueue.Count > 0)
  144. {
  145. Action a = this.localQueue.Dequeue();
  146. a();
  147. }
  148. }
  149. private int Service()
  150. {
  151. int ret = NativeMethods.ENetHostService(this.host, null, 0);
  152. return ret;
  153. }
  154. public void Update()
  155. {
  156. this.OnEvents();
  157. if (this.Service() < 0)
  158. {
  159. return;
  160. }
  161. while (true)
  162. {
  163. ENetEvent eNetEvent = this.GetEvent();
  164. if (eNetEvent == null)
  165. {
  166. return;
  167. }
  168. switch (eNetEvent.Type)
  169. {
  170. case EventType.Connect:
  171. {
  172. // 这是一个connect peer
  173. if (this.uSocketManager.ContainsKey(eNetEvent.Peer))
  174. {
  175. USocket uSocket = this.uSocketManager[eNetEvent.Peer];
  176. uSocket.OnConnected(eNetEvent);
  177. break;
  178. }
  179. // 这是accept peer
  180. if (this.uSocketManager.ContainsKey(IntPtr.Zero))
  181. {
  182. USocket uSocket = this.uSocketManager[IntPtr.Zero];
  183. uSocket.OnConnected(eNetEvent);
  184. break;
  185. }
  186. // 如果server端没有acceptasync,则请求放入队列
  187. this.connQueue.Add(eNetEvent.Peer, eNetEvent);
  188. break;
  189. }
  190. case EventType.Receive:
  191. {
  192. USocket uSocket = this.uSocketManager[eNetEvent.Peer];
  193. uSocket.OnReceived(eNetEvent);
  194. break;
  195. }
  196. case EventType.Disconnect:
  197. {
  198. // 如果链接还在缓存中,则删除
  199. if (this.connQueue.Remove(eNetEvent.Peer))
  200. {
  201. break;
  202. }
  203. // 链接已经被应用层接收
  204. USocket uSocket = this.uSocketManager[eNetEvent.Peer];
  205. this.uSocketManager.Remove(eNetEvent.Peer);
  206. // 等待的task将抛出异常
  207. if (uSocket.Connected != null)
  208. {
  209. uSocket.OnConnected(eNetEvent);
  210. break;
  211. }
  212. if (uSocket.Received != null)
  213. {
  214. uSocket.OnReceived(eNetEvent);
  215. break;
  216. }
  217. if (uSocket.Disconnect != null)
  218. {
  219. uSocket.OnDisconnect(eNetEvent);
  220. break;
  221. }
  222. break;
  223. }
  224. }
  225. }
  226. }
  227. }
  228. }