| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- using System;
- using System.Runtime.InteropServices;
- using Common.Network;
- namespace UNet
- {
- internal sealed class UPacket: IDisposable
- {
- private IntPtr packet;
- public UPacket(IntPtr packet)
- {
- this.packet = packet;
- }
- public UPacket(byte[] data, PacketFlags flags = PacketFlags.None)
- {
- if (data == null)
- {
- throw new ArgumentNullException("data");
- }
- this.packet = NativeMethods.ENetPacketCreate(data, (uint) data.Length, flags);
- if (this.packet == IntPtr.Zero)
- {
- throw new UException("Packet creation call failed");
- }
- }
- ~UPacket()
- {
- this.Dispose(false);
- }
- public void Dispose()
- {
- this.Dispose(true);
- GC.SuppressFinalize(this);
- }
- private void Dispose(bool disposing)
- {
- if (this.packet == IntPtr.Zero)
- {
- return;
- }
- NativeMethods.ENetPacketDestroy(this.packet);
- this.packet = IntPtr.Zero;
- }
- private ENetPacket Struct
- {
- get
- {
- return (ENetPacket) Marshal.PtrToStructure(this.packet, typeof (ENetPacket));
- }
- set
- {
- Marshal.StructureToPtr(value, this.packet, false);
- }
- }
- public IntPtr PacketPtr
- {
- get
- {
- return this.packet;
- }
- set
- {
- this.packet = value;
- }
- }
- public byte[] Bytes
- {
- get
- {
- ENetPacket enetPacket = this.Struct;
- var bytes = new byte[(long) enetPacket.DataLength];
- Marshal.Copy(enetPacket.Data, bytes, 0, (int) enetPacket.DataLength);
- return bytes;
- }
- }
- }
- }
|