Packet.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.Runtime.InteropServices;
  19. namespace ENet
  20. {
  21. public sealed unsafe class Packet : IDisposable
  22. {
  23. private Native.ENetPacket* packet;
  24. public Packet(Native.ENetPacket* packet)
  25. {
  26. if (packet == null)
  27. {
  28. throw new InvalidOperationException("No native packet.");
  29. }
  30. this.packet = packet;
  31. }
  32. public Packet(): this(new byte[]{})
  33. {
  34. }
  35. public Packet(byte[] data): this(data, 0, data.Length)
  36. {
  37. }
  38. public Packet(byte[] data, int offset, int length, PacketFlags flags = PacketFlags.None)
  39. {
  40. if (data == null)
  41. {
  42. throw new ArgumentNullException("data");
  43. }
  44. if (offset < 0 || length < 0 || length > data.Length - offset)
  45. {
  46. throw new ArgumentOutOfRangeException();
  47. }
  48. fixed (byte* bytes = data)
  49. {
  50. this.packet = Native.enet_packet_create(bytes + offset, (uint)length, flags);
  51. if (this.packet == null)
  52. {
  53. throw new ENetException(0, "Packet creation call failed.");
  54. }
  55. }
  56. }
  57. ~Packet()
  58. {
  59. Dispose(false);
  60. }
  61. public void Dispose()
  62. {
  63. Dispose(true);
  64. GC.SuppressFinalize(this);
  65. }
  66. private void Dispose(bool disposing)
  67. {
  68. if (this.packet == null)
  69. {
  70. return;
  71. }
  72. if (disposing)
  73. {
  74. if (this.packet->referenceCount == IntPtr.Zero)
  75. {
  76. Native.enet_packet_destroy(this.packet);
  77. }
  78. }
  79. this.packet = null;
  80. }
  81. public void CopyTo(byte[] array)
  82. {
  83. if (array == null)
  84. {
  85. throw new ArgumentNullException("array");
  86. }
  87. this.CopyTo(array, 0, array.Length);
  88. }
  89. public void CopyTo(byte[] array, int offset, int length)
  90. {
  91. if (array == null)
  92. {
  93. throw new ArgumentNullException("array");
  94. }
  95. if (offset < 0 || length < 0 || length > array.Length - offset)
  96. {
  97. throw new ArgumentOutOfRangeException();
  98. }
  99. if (length > this.Length - offset)
  100. {
  101. throw new ArgumentOutOfRangeException();
  102. }
  103. if (length > 0)
  104. {
  105. Marshal.Copy((IntPtr) ((byte*) this.Data + offset), array, offset, length);
  106. }
  107. }
  108. public byte[] GetBytes()
  109. {
  110. var array = new byte[this.Length];
  111. this.CopyTo(array);
  112. return array;
  113. }
  114. public void Resize(int length)
  115. {
  116. if (length < 0)
  117. {
  118. throw new ArgumentOutOfRangeException("length");
  119. }
  120. int ret = Native.enet_packet_resize(this.packet, (uint)length);
  121. if (ret < 0)
  122. {
  123. throw new ENetException(ret, "Packet resize call failed.");
  124. }
  125. }
  126. public void* Data
  127. {
  128. get
  129. {
  130. return this.packet->data;
  131. }
  132. }
  133. public int Length
  134. {
  135. get
  136. {
  137. if (this.packet->dataLength.ToPointer() > (void*) int.MaxValue)
  138. {
  139. throw new ENetException(0, "Packet too long!");
  140. }
  141. return (int) this.packet->dataLength;
  142. }
  143. }
  144. public Native.ENetPacket* NativeData
  145. {
  146. get
  147. {
  148. return this.packet;
  149. }
  150. set
  151. {
  152. this.packet = value;
  153. }
  154. }
  155. }
  156. }