Peer.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #region License
  2. /*
  3. ENet for C#
  4. Copyright (c) 2011 James F. Bellinger <jfb@zer7.com>
  5. Permission to use, copy, modify, and/or distribute this software for any
  6. purpose with or without fee is hereby granted, provided that the above
  7. copyright notice and this permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #endregion
  17. using System;
  18. using System.Diagnostics;
  19. using System.Threading.Tasks;
  20. namespace ENet
  21. {
  22. public unsafe class Peer : IDisposable
  23. {
  24. private readonly Host host;
  25. private Native.ENetPeer* peer;
  26. private Action<Event> connected;
  27. private Action<Event> received;
  28. private Action<Event> disconnect;
  29. public Peer(Host host, Native.ENetPeer* peer)
  30. {
  31. if (peer == null)
  32. {
  33. throw new InvalidOperationException("No native peer.");
  34. }
  35. this.peer = peer;
  36. this.host = host;
  37. this.host.Peers.Add(this);
  38. }
  39. ~Peer()
  40. {
  41. this.Dispose(false);
  42. }
  43. public void Dispose()
  44. {
  45. Dispose(true);
  46. GC.SuppressFinalize(this);
  47. }
  48. protected void Dispose(bool disposing)
  49. {
  50. if (this.peer == null)
  51. {
  52. return;
  53. }
  54. if (disposing)
  55. {
  56. Native.enet_peer_reset(this.peer);
  57. }
  58. this.host.Peers.Remove((int)this.peer->data);
  59. this.peer = null;
  60. }
  61. public Native.ENetPeer* NativeData
  62. {
  63. get
  64. {
  65. return this.peer;
  66. }
  67. set
  68. {
  69. this.peer = value;
  70. }
  71. }
  72. public PeerState State
  73. {
  74. get
  75. {
  76. return this.peer->state;
  77. }
  78. }
  79. public int Data
  80. {
  81. get
  82. {
  83. return (int)this.peer->data;
  84. }
  85. set
  86. {
  87. this.peer->data = (IntPtr)value;
  88. }
  89. }
  90. // peer连接上了调用该事件
  91. public event Action<Event> Connected
  92. {
  93. add
  94. {
  95. connected += value;
  96. }
  97. remove
  98. {
  99. connected -= value;
  100. }
  101. }
  102. public event Action<Event> Received
  103. {
  104. add
  105. {
  106. received += value;
  107. }
  108. remove
  109. {
  110. received -= value;
  111. }
  112. }
  113. public event Action<Event> Disconnect
  114. {
  115. add
  116. {
  117. disconnect += value;
  118. }
  119. remove
  120. {
  121. disconnect -= value;
  122. }
  123. }
  124. internal void OnConnected(Event e)
  125. {
  126. if (connected == null)
  127. {
  128. return;
  129. }
  130. connected(e);
  131. }
  132. internal void OnReceived(Event e)
  133. {
  134. if (received == null)
  135. {
  136. return;
  137. }
  138. received(e);
  139. }
  140. internal void OnDisconnect(Event e)
  141. {
  142. if (disconnect == null)
  143. {
  144. return;
  145. }
  146. disconnect(e);
  147. }
  148. public void ConfigureThrottle(uint interval, uint acceleration, uint deceleration)
  149. {
  150. Native.enet_peer_throttle_configure(this.peer, interval, acceleration, deceleration);
  151. }
  152. public void Send(byte channelID, byte[] data)
  153. {
  154. this.Send(channelID, data, 0, data.Length);
  155. }
  156. public void Send(byte channelID, byte[] data, int offset, int length)
  157. {
  158. using (var packet = new Packet(data, offset, length))
  159. {
  160. this.Send(channelID, packet);
  161. }
  162. }
  163. public void Send(byte channelID, Packet packet)
  164. {
  165. Native.enet_peer_send(this.peer, channelID, packet.NativeData);
  166. }
  167. public Task<Packet> ReceiveAsync()
  168. {
  169. var tcs = new TaskCompletionSource<Packet>();
  170. this.Received += e => tcs.TrySetResult(e.Packet);
  171. return tcs.Task;
  172. }
  173. public Task<bool> DisconnectAsync(uint data)
  174. {
  175. var tcs = new TaskCompletionSource<bool>();
  176. this.Disconnect += e => tcs.TrySetResult(true);
  177. Native.enet_peer_disconnect(this.peer, data);
  178. return tcs.Task;
  179. }
  180. public Task<bool> DisconnectLaterAsync(uint data)
  181. {
  182. var tcs = new TaskCompletionSource<bool>();
  183. this.Disconnect += e => tcs.TrySetResult(true);
  184. Native.enet_peer_disconnect_later(this.peer, data);
  185. return tcs.Task;
  186. }
  187. public void DisconnectNow(uint data)
  188. {
  189. Native.enet_peer_disconnect_now(this.peer, data);
  190. }
  191. public void Ping()
  192. {
  193. Native.enet_peer_ping(this.peer);
  194. }
  195. }
  196. }