ESocket.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Threading.Tasks;
  5. namespace ENet
  6. {
  7. public sealed class ESocket: IDisposable
  8. {
  9. private IntPtr peerPtr = IntPtr.Zero;
  10. private readonly EService service;
  11. private readonly LinkedList<byte[]> recvBuffer = new LinkedList<byte[]>();
  12. public Action<EEvent> Connected { get; set; }
  13. public Action<EEvent> Received { get; set; }
  14. public Action<EEvent> Disconnect { get; set; }
  15. public Action<int> Error { get; set; }
  16. public ESocket(EService service)
  17. {
  18. this.service = service;
  19. }
  20. public void Dispose()
  21. {
  22. if (this.peerPtr == IntPtr.Zero)
  23. {
  24. return;
  25. }
  26. NativeMethods.EnetPeerReset(this.peerPtr);
  27. this.peerPtr = IntPtr.Zero;
  28. }
  29. public IntPtr PeerPtr
  30. {
  31. get
  32. {
  33. return this.peerPtr;
  34. }
  35. set
  36. {
  37. this.peerPtr = value;
  38. }
  39. }
  40. private ENetPeer Struct
  41. {
  42. get
  43. {
  44. if (this.peerPtr == IntPtr.Zero)
  45. {
  46. return new ENetPeer();
  47. }
  48. return (ENetPeer) Marshal.PtrToStructure(this.peerPtr, typeof (ENetPeer));
  49. }
  50. set
  51. {
  52. Marshal.StructureToPtr(value, this.peerPtr, false);
  53. }
  54. }
  55. public PeerState State
  56. {
  57. get
  58. {
  59. if (this.peerPtr == IntPtr.Zero)
  60. {
  61. return PeerState.Uninitialized;
  62. }
  63. return this.Struct.State;
  64. }
  65. }
  66. public void Ping()
  67. {
  68. NativeMethods.EnetPeerPing(this.peerPtr);
  69. }
  70. public void ConfigureThrottle(uint interval, uint acceleration, uint deceleration)
  71. {
  72. NativeMethods.EnetPeerThrottleConfigure(this.peerPtr, interval, acceleration,
  73. deceleration);
  74. }
  75. public Task<bool> ConnectAsync(
  76. string hostName, ushort port,
  77. uint channelLimit = NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT, uint data = 0)
  78. {
  79. if (channelLimit > NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT)
  80. {
  81. throw new ArgumentOutOfRangeException("channelLimit");
  82. }
  83. var tcs = new TaskCompletionSource<bool>();
  84. var address = new Address { HostName = hostName, Port = port };
  85. ENetAddress nativeAddress = address.Struct;
  86. this.peerPtr = NativeMethods.EnetHostConnect(this.service.HostPtr, ref nativeAddress,
  87. channelLimit, data);
  88. if (this.peerPtr == IntPtr.Zero)
  89. {
  90. throw new EException("host connect call failed.");
  91. }
  92. this.service.PeersManager.Add(this.peerPtr, this);
  93. this.Connected = eEvent =>
  94. {
  95. if (eEvent.EventState == EventState.DISCONNECTED)
  96. {
  97. tcs.TrySetException(new EException("socket disconnected in connect"));
  98. }
  99. tcs.TrySetResult(true);
  100. };
  101. return tcs.Task;
  102. }
  103. public Task<bool> AcceptAsync()
  104. {
  105. if (this.service.PeersManager.ContainsKey(IntPtr.Zero))
  106. {
  107. throw new EException("do not accept twice!");
  108. }
  109. var tcs = new TaskCompletionSource<bool>();
  110. // 如果有请求连接缓存的包,从缓存中取
  111. if (this.service.ConnEEvents.Count > 0)
  112. {
  113. EEvent eEvent = this.service.ConnEEvents.First.Value;
  114. this.service.ConnEEvents.RemoveFirst();
  115. this.PeerPtr = eEvent.PeerPtr;
  116. this.service.PeersManager.Add(this.PeerPtr, this);
  117. tcs.TrySetResult(true);
  118. }
  119. else
  120. {
  121. this.service.PeersManager.Add(this.PeerPtr, this);
  122. this.Connected = eEvent =>
  123. {
  124. if (eEvent.EventState == EventState.DISCONNECTED)
  125. {
  126. tcs.TrySetException(new EException("socket disconnected in accpet"));
  127. }
  128. this.service.PeersManager.Remove(IntPtr.Zero);
  129. this.PeerPtr = eEvent.PeerPtr;
  130. this.service.PeersManager.Add(this.PeerPtr, this);
  131. tcs.TrySetResult(true);
  132. };
  133. }
  134. return tcs.Task;
  135. }
  136. public void WriteAsync(
  137. byte[] data, byte channelID = 0, PacketFlags flags = PacketFlags.Reliable)
  138. {
  139. var packet = new EPacket(data, flags);
  140. NativeMethods.EnetPeerSend(this.peerPtr, channelID, packet.PacketPtr);
  141. // enet_peer_send函数会自动删除packet,设置为0,防止Dispose或者析构函数再次删除
  142. packet.PacketPtr = IntPtr.Zero;
  143. }
  144. public Task<byte[]> ReadAsync()
  145. {
  146. var tcs = new TaskCompletionSource<byte[]>();
  147. // 如果有缓存的包,从缓存中取
  148. if (this.recvBuffer.Count > 0)
  149. {
  150. var bytes = this.recvBuffer.First.Value;
  151. this.recvBuffer.RemoveFirst();
  152. tcs.TrySetResult(bytes);
  153. }
  154. // 没有缓存封包,设置回调等待
  155. else
  156. {
  157. this.Received = eEvent =>
  158. {
  159. if (eEvent.EventState == EventState.DISCONNECTED)
  160. {
  161. tcs.TrySetException(new EException("socket disconnected in receive"));
  162. }
  163. using (var packet = new EPacket(eEvent.PacketPtr))
  164. {
  165. var bytes = packet.Bytes;
  166. tcs.TrySetResult(bytes);
  167. }
  168. };
  169. }
  170. return tcs.Task;
  171. }
  172. public void WriteAsync(
  173. Action<int> action, byte[] data, byte channelID = 0,
  174. PacketFlags flags = PacketFlags.Reliable)
  175. {
  176. var packet = new EPacket(data, flags);
  177. int ret = NativeMethods.EnetPeerSend(this.peerPtr, channelID, packet.PacketPtr);
  178. // enet_peer_send函数会自动删除packet,设置为0,防止Dispose或者析构函数再次删除
  179. packet.PacketPtr = IntPtr.Zero;
  180. action(ret);
  181. }
  182. public void ReadAsync(Action<byte[], int> action)
  183. {
  184. // 如果有缓存的包,从缓存中取
  185. if (this.recvBuffer.Count > 0)
  186. {
  187. var bytes = this.recvBuffer.First.Value;
  188. this.recvBuffer.RemoveFirst();
  189. action(bytes, 0);
  190. }
  191. // 没有缓存封包,设置回调等待
  192. else
  193. {
  194. this.Received = eEvent =>
  195. {
  196. if (eEvent.EventState == EventState.DISCONNECTED)
  197. {
  198. action(new byte[0], -1);
  199. }
  200. using (var packet = new EPacket(eEvent.PacketPtr))
  201. {
  202. var bytes = packet.Bytes;
  203. action(bytes, 0);
  204. }
  205. };
  206. }
  207. }
  208. public Task<bool> DisconnectAsync(uint data = 0)
  209. {
  210. NativeMethods.EnetPeerDisconnect(this.peerPtr, data);
  211. // EnetPeerDisconnect会reset Peer,这里设置为0,防止再次Dispose
  212. this.PeerPtr = IntPtr.Zero;
  213. var tcs = new TaskCompletionSource<bool>();
  214. this.Disconnect = eEvent => tcs.TrySetResult(true);
  215. return tcs.Task;
  216. }
  217. public Task<bool> DisconnectLaterAsync(uint data = 0)
  218. {
  219. NativeMethods.EnetPeerDisconnectLater(this.peerPtr, data);
  220. // EnetPeerDisconnect会reset Peer,这里设置为0,防止再次Dispose
  221. this.PeerPtr = IntPtr.Zero;
  222. var tcs = new TaskCompletionSource<bool>();
  223. this.Disconnect = eEvent => tcs.TrySetResult(true);
  224. return tcs.Task;
  225. }
  226. public void DisconnectNow(uint data)
  227. {
  228. NativeMethods.EnetPeerDisconnectNow(this.peerPtr, data);
  229. // EnetPeerDisconnect会reset Peer,这里设置为0,防止再次Dispose
  230. this.PeerPtr = IntPtr.Zero;
  231. }
  232. internal void OnConnected(EEvent eEvent)
  233. {
  234. if (this.Connected == null)
  235. {
  236. return;
  237. }
  238. Action<EEvent> localConnected = this.Connected;
  239. this.Connected = null;
  240. // 此调用将让await ConnectAsync返回,所以null必须在此之前设置
  241. localConnected(eEvent);
  242. }
  243. internal void OnReceived(EEvent eEvent)
  244. {
  245. // 如果应用层还未调用readasync则将包放到缓存队列
  246. if (this.Received == null)
  247. {
  248. using (var packet = new EPacket(eEvent.PacketPtr))
  249. {
  250. var bytes = packet.Bytes;
  251. this.recvBuffer.AddLast(bytes);
  252. }
  253. }
  254. else
  255. {
  256. Action<EEvent> localReceived = this.Received;
  257. this.Received = null;
  258. // 此调用将让await ReadAsync返回,所以null必须在此之前设置
  259. localReceived(eEvent);
  260. }
  261. }
  262. internal void OnDisconnect(EEvent eEvent)
  263. {
  264. if (this.Disconnect == null)
  265. {
  266. return;
  267. }
  268. this.Disconnect(eEvent);
  269. }
  270. internal void OnError(int errorCode)
  271. {
  272. if (this.Error == null)
  273. {
  274. return;
  275. }
  276. this.Error(errorCode);
  277. }
  278. }
  279. }