Peer.cs 3.2 KB

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