UPoller.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Base
  4. {
  5. internal sealed class UPoller : IDisposable
  6. {
  7. static UPoller()
  8. {
  9. Library.Initialize();
  10. }
  11. private IntPtr host;
  12. // 线程同步队列,发送接收socket回调都放到该队列,由poll线程统一执行
  13. private Queue<Action> concurrentQueue = new Queue<Action>();
  14. private Queue<Action> localQueue;
  15. private ENetEvent eNetEventCache;
  16. private readonly object lockObject = new object();
  17. public UPoller()
  18. {
  19. this.USocketManager = new USocketManager();
  20. this.host = NativeMethods.ENetHostCreate(IntPtr.Zero, NativeMethods.ENET_PROTOCOL_MAXIMUM_PEER_ID, 0, 0, 0);
  21. if (this.host == IntPtr.Zero)
  22. {
  23. throw new Exception("Host creation call failed.");
  24. }
  25. }
  26. ~UPoller()
  27. {
  28. this.Dispose(false);
  29. }
  30. public void Dispose()
  31. {
  32. this.Dispose(true);
  33. }
  34. private void Dispose(bool disposing)
  35. {
  36. if (this.host == IntPtr.Zero)
  37. {
  38. return;
  39. }
  40. NativeMethods.ENetHostDestroy(this.host);
  41. this.host = IntPtr.Zero;
  42. }
  43. public USocketManager USocketManager { get; }
  44. public IntPtr Host
  45. {
  46. get
  47. {
  48. return this.host;
  49. }
  50. }
  51. private ENetEvent GetEvent()
  52. {
  53. if (this.eNetEventCache == null)
  54. {
  55. this.eNetEventCache = new ENetEvent();
  56. }
  57. if (NativeMethods.ENetHostCheckEvents(this.host, this.eNetEventCache) <= 0)
  58. {
  59. return null;
  60. }
  61. ENetEvent eNetEvent = this.eNetEventCache;
  62. this.eNetEventCache = null;
  63. return eNetEvent;
  64. }
  65. public void Flush()
  66. {
  67. NativeMethods.ENetHostFlush(this.host);
  68. }
  69. public void Add(Action action)
  70. {
  71. this.concurrentQueue.Enqueue(action);
  72. }
  73. private void OnEvents()
  74. {
  75. lock (lockObject)
  76. {
  77. localQueue = concurrentQueue;
  78. concurrentQueue = new Queue<Action>();
  79. }
  80. while (this.localQueue.Count > 0)
  81. {
  82. Action a = this.localQueue.Dequeue();
  83. a();
  84. }
  85. }
  86. private int Service()
  87. {
  88. int ret = NativeMethods.ENetHostService(this.host, null, 0);
  89. return ret;
  90. }
  91. public void Update()
  92. {
  93. this.OnEvents();
  94. if (this.Service() < 0)
  95. {
  96. return;
  97. }
  98. while (true)
  99. {
  100. ENetEvent eNetEvent = this.GetEvent();
  101. if (eNetEvent == null)
  102. {
  103. return;
  104. }
  105. switch (eNetEvent.Type)
  106. {
  107. case EventType.Connect:
  108. {
  109. // 这是一个connect peer
  110. if (this.USocketManager.ContainsKey(eNetEvent.Peer))
  111. {
  112. USocket uSocket = this.USocketManager[eNetEvent.Peer];
  113. uSocket.OnConnected(eNetEvent);
  114. }
  115. break;
  116. }
  117. case EventType.Receive:
  118. {
  119. USocket uSocket = this.USocketManager[eNetEvent.Peer];
  120. uSocket.OnReceived(eNetEvent);
  121. break;
  122. }
  123. case EventType.Disconnect:
  124. {
  125. USocket uSocket = this.USocketManager[eNetEvent.Peer];
  126. this.USocketManager.Remove(uSocket.PeerPtr);
  127. uSocket.PeerPtr = IntPtr.Zero;
  128. uSocket.OnDisconnect(eNetEvent);
  129. break;
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }