UPoller.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. private readonly USocketManager uSocketManager = new USocketManager();
  13. private readonly QueueDictionary<IntPtr, ENetEvent> connQueue = new QueueDictionary<IntPtr, ENetEvent>();
  14. private IntPtr host;
  15. private readonly USocket acceptor;
  16. // 线程同步队列,发送接收socket回调都放到该队列,由poll线程统一执行
  17. private Queue<Action> concurrentQueue = new Queue<Action>();
  18. private Queue<Action> localQueue;
  19. private ENetEvent eNetEventCache;
  20. private readonly object lockObject = new object();
  21. public UPoller(string hostName, ushort port)
  22. {
  23. this.acceptor = new USocket(IntPtr.Zero, this);
  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 USocketManager USocketManager
  53. {
  54. get
  55. {
  56. return this.uSocketManager;
  57. }
  58. }
  59. public IntPtr Host
  60. {
  61. get
  62. {
  63. return this.host;
  64. }
  65. }
  66. private ENetEvent TryGetEvent()
  67. {
  68. if (this.eNetEventCache == null)
  69. {
  70. this.eNetEventCache = new ENetEvent();
  71. }
  72. if (NativeMethods.ENetHostCheckEvents(this.host, this.eNetEventCache) <= 0)
  73. {
  74. return null;
  75. }
  76. ENetEvent eNetEvent = this.eNetEventCache;
  77. this.eNetEventCache = null;
  78. return eNetEvent;
  79. }
  80. public void Flush()
  81. {
  82. NativeMethods.ENetHostFlush(this.host);
  83. }
  84. public void Add(Action action)
  85. {
  86. lock (lockObject)
  87. {
  88. this.concurrentQueue.Enqueue(action);
  89. }
  90. }
  91. public Task<USocket> AcceptAsync()
  92. {
  93. if (this.uSocketManager.ContainsKey(IntPtr.Zero))
  94. {
  95. throw new Exception("do not accept twice!");
  96. }
  97. var tcs = new TaskCompletionSource<USocket>();
  98. // 如果有请求连接缓存的包,从缓存中取
  99. if (this.connQueue.Count > 0)
  100. {
  101. IntPtr ptr = this.connQueue.FirstKey;
  102. this.connQueue.Remove(ptr);
  103. USocket socket = new USocket(ptr, this);
  104. this.uSocketManager.Add(ptr, socket);
  105. tcs.TrySetResult(socket);
  106. }
  107. else
  108. {
  109. this.uSocketManager.Add(this.acceptor.PeerPtr, this.acceptor);
  110. this.acceptor.AcceptTcs = tcs;
  111. }
  112. return tcs.Task;
  113. }
  114. private void OnEvents()
  115. {
  116. lock (lockObject)
  117. {
  118. localQueue = concurrentQueue;
  119. concurrentQueue = new Queue<Action>();
  120. }
  121. while (this.localQueue.Count > 0)
  122. {
  123. Action a = this.localQueue.Dequeue();
  124. a();
  125. }
  126. }
  127. private int Service()
  128. {
  129. int ret = NativeMethods.ENetHostService(this.host, null, 0);
  130. return ret;
  131. }
  132. public void Update()
  133. {
  134. this.OnEvents();
  135. if (this.Service() < 0)
  136. {
  137. return;
  138. }
  139. while (true)
  140. {
  141. ENetEvent eNetEvent = this.TryGetEvent();
  142. if (eNetEvent == null)
  143. {
  144. return;
  145. }
  146. switch (eNetEvent.Type)
  147. {
  148. case EventType.Connect:
  149. {
  150. // 这是一个connect peer
  151. if (this.uSocketManager.ContainsKey(eNetEvent.Peer))
  152. {
  153. USocket uSocket = this.uSocketManager[eNetEvent.Peer];
  154. uSocket.OnConnected();
  155. break;
  156. }
  157. // 这是accept peer
  158. if (this.uSocketManager.ContainsKey(IntPtr.Zero))
  159. {
  160. USocket uSocket = this.uSocketManager[IntPtr.Zero];
  161. uSocket.OnAccepted(eNetEvent);
  162. break;
  163. }
  164. // 如果server端没有acceptasync,则请求放入队列
  165. this.connQueue.Add(eNetEvent.Peer, eNetEvent);
  166. break;
  167. }
  168. case EventType.Receive:
  169. {
  170. USocket uSocket = this.USocketManager[eNetEvent.Peer];
  171. uSocket.OnReceived(eNetEvent);
  172. break;
  173. }
  174. case EventType.Disconnect:
  175. {
  176. USocket uSocket = this.USocketManager[eNetEvent.Peer];
  177. this.USocketManager.Remove(uSocket.PeerPtr);
  178. uSocket.PeerPtr = IntPtr.Zero;
  179. uSocket.OnDisconnect(eNetEvent);
  180. break;
  181. }
  182. }
  183. }
  184. }
  185. }
  186. }