Peer.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.Threading.Tasks;
  19. namespace ENet
  20. {
  21. public unsafe class Peer : IDisposable
  22. {
  23. private Native.ENetPeer* peer;
  24. public Peer(Native.ENetPeer* peer)
  25. {
  26. if (peer == null)
  27. {
  28. throw new InvalidOperationException("No native peer.");
  29. }
  30. this.peer = peer;
  31. }
  32. ~Peer()
  33. {
  34. this.Dispose(false);
  35. }
  36. public void Dispose()
  37. {
  38. Dispose(true);
  39. GC.SuppressFinalize(this);
  40. }
  41. protected void Dispose(bool disposing)
  42. {
  43. if (this.peer == null)
  44. {
  45. return;
  46. }
  47. if (disposing)
  48. {
  49. Native.enet_peer_reset(this.peer);
  50. this.peer = null;
  51. }
  52. }
  53. public void ConfigureThrottle(uint interval, uint acceleration, uint deceleration)
  54. {
  55. Native.enet_peer_throttle_configure(this.peer, interval, acceleration, deceleration);
  56. }
  57. public void Disconnect(uint data)
  58. {
  59. Native.enet_peer_disconnect(this.peer, data);
  60. }
  61. public void DisconnectLater(uint data)
  62. {
  63. Native.enet_peer_disconnect_later(this.peer, data);
  64. }
  65. public void DisconnectNow(uint data)
  66. {
  67. Native.enet_peer_disconnect_now(this.peer, data);
  68. }
  69. public void Ping()
  70. {
  71. Native.enet_peer_ping(this.peer);
  72. }
  73. public bool Receive(out byte channelID, out Packet packet)
  74. {
  75. Native.ENetPacket* nativePacket = Native.enet_peer_receive(this.peer, out channelID);
  76. if (nativePacket == null)
  77. {
  78. packet = new Packet();
  79. return false;
  80. }
  81. packet = new Packet(nativePacket);
  82. return true;
  83. }
  84. public void Send(byte channelID, byte[] data)
  85. {
  86. this.Send(channelID, data, 0, data.Length);
  87. }
  88. public void Send(byte channelID, byte[] data, int offset, int length)
  89. {
  90. using (var packet = new Packet(data, offset, length))
  91. {
  92. this.Send(channelID, packet);
  93. }
  94. }
  95. public void Send(byte channelID, Packet packet)
  96. {
  97. Native.enet_peer_send(this.peer, channelID, packet.NativeData);
  98. }
  99. public Native.ENetPeer* NativeData
  100. {
  101. get
  102. {
  103. return this.peer;
  104. }
  105. set
  106. {
  107. this.peer = value;
  108. }
  109. }
  110. public PeerState State
  111. {
  112. get
  113. {
  114. return this.peer->state;
  115. }
  116. }
  117. public IntPtr Data
  118. {
  119. get
  120. {
  121. return this.peer->data;
  122. }
  123. set
  124. {
  125. this.peer->data = value;
  126. }
  127. }
  128. public uint ConnectID
  129. {
  130. get
  131. {
  132. return this.peer->connectID;
  133. }
  134. }
  135. }
  136. }