Is this a good method to Read\Write packets
using System.Text;
namespace namespace
{
public unsafe class DataPacket
{
public DataPacket(byte[] buffer)
{
_buffer = new byte[buffer.Length];
System.Buffer.BlockCopy(buffer, 0, _buffer, 0, buffer.Length);
}
public DataPacket(ushort size, ushort type)
{
_buffer = new byte[size];
WriteUInt16(size, 0);
WriteUInt16(type, 2);
}
private readonly byte[] _buffer;
protected byte[] Buffer
{
get
{
return _buffer;
}
}
public byte* Ptr
{
get
{
fixed (byte* ptr = Buffer)
return ptr;
}
}
public ushort Size
{
get
{
return ReadUInt16(0);
}
set
{
WriteUInt16(value, 0);
}
}
public ushort Type
{
get
{
return ReadUInt16(2);
}
set
{
WriteUInt16(value, 2);
}
}
public void WriteSByte(sbyte value, int offset)
{
(*(sbyte*)(Ptr + offset)) = value;
}
public void WriteInt16(short value, int offset)
{
(*(short*)(Ptr + offset)) = value;
}
public void WriteInt32(int value, int offset)
{
(*(int*)(Ptr + offset)) = value;
}
public void WriteInt64(long value, int offset)
{
(*(long*)(Ptr + offset)) = value;
}
public void WriteByte(byte value, int offset)
{
(*(Ptr + offset)) = value;
}
public void WriteUInt16(ushort value, int offset)
{
(*(ushort*)(Ptr + offset)) = value;
}
public void WriteUInt32(uint value, int offset)
{
(*(uint*)(Ptr + offset)) = value;
}
public void WriteUInt64(ulong value, int offset)
{
(*(ulong*)(Ptr + offset)) = value;
}
public void WriteStringWithLength(string value, int offset, out int nextoffset)
{
WriteByte((byte)(value.Length > 255 ? 255 : value.Length), offset);
offset++;
foreach (var c in value)
{
WriteByte((byte)c, offset);
offset++;
}
nextoffset = offset;
}
public void WriteString(string value, int offset)
{
foreach (var c in value)
{
WriteByte((byte)c, offset);
offset++;
}
}
public sbyte ReadSByte(int offset)
{
return (*(sbyte*)(Ptr + offset));
}
public short ReadInt16(int offset)
{
return (*(short*)(Ptr + offset));
}
public int ReadInt32(int offset)
{
return (*(int*)(Ptr + offset));
}
public long ReadInt64(int offset)
{
return (*(long*)(Ptr + offset));
}
public byte ReadByte(int offset)
{
return (*(Ptr + offset));
}
public byte[] ReadBytes(int offset, int length)
{
var bytes = new byte[length];
for (var i = offset; i < length; i++)
bytes[i] = (*(Ptr + i));
return bytes;
}
public ushort ReadUInt16(int offset)
{
return (*(ushort*)(Ptr + offset));
}
public uint ReadUInt32(int offset)
{
return (*(uint*)(Ptr + offset));
}
public ulong ReadUInt64(int offset)
{
return (*(ulong*)(Ptr + offset));
}
public string ReadString(int offset, int length)
{
var sb = new StringBuilder();
for (var i = 0; i < length; i++)
sb.Append((char)ReadByte(offset + i));
return sb.ToString().Replace("\0", "").Replace("\r", "");
}
public string ReadString(int offset)
{
var size = ReadByte(offset);
offset++;
return ReadString(offset, size);
}
public byte[] ToArray()
{
var newbuffer = new byte[Buffer.Length];
System.Buffer.BlockCopy(Buffer, 0, newbuffer, 0, Buffer.Length);
return newbuffer;
}
public static implicit operator byte[](DataPacket dPacket)
{
return dPacket.ToArray();
}
public static implicit operator DataPacket(byte[] packet)
{
return new DataPacket(packet);
}
}
}