Packet.cs 3.6 KB

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