Packet.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace ENet
  4. {
  5. public sealed class Packet : IDisposable
  6. {
  7. private IntPtr packet;
  8. public Packet(IntPtr packet)
  9. {
  10. this.packet = packet;
  11. }
  12. public Packet(string data, PacketFlags flags = PacketFlags.None)
  13. {
  14. if (data == null)
  15. {
  16. throw new ArgumentNullException("data");
  17. }
  18. this.packet = Native.enet_packet_create(data, (uint)data.Length, flags);
  19. if (this.packet == IntPtr.Zero)
  20. {
  21. throw new ENetException(0, "Packet creation call failed.");
  22. }
  23. }
  24. ~Packet()
  25. {
  26. Dispose(false);
  27. }
  28. public void Dispose()
  29. {
  30. Dispose(true);
  31. GC.SuppressFinalize(this);
  32. }
  33. private void Dispose(bool disposing)
  34. {
  35. if (this.packet == IntPtr.Zero)
  36. {
  37. return;
  38. }
  39. Native.enet_packet_destroy(this.packet);
  40. this.packet = IntPtr.Zero;
  41. }
  42. public ENetPacket Struct
  43. {
  44. get
  45. {
  46. return (ENetPacket)Marshal.PtrToStructure(this.packet, typeof(ENetPacket));
  47. }
  48. set
  49. {
  50. Marshal.StructureToPtr(value, this.packet, false);
  51. }
  52. }
  53. public IntPtr NativePtr
  54. {
  55. get
  56. {
  57. return this.packet;
  58. }
  59. }
  60. }
  61. }