UPoller.cs 6.3 KB

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