What is more efficient to use?
Pointers or MemoryStream?
Pointers or MemoryStream?
public unsafe struct WalkPacket
{
public uint Direction;
public uint Id;
public uint Mode;
public SystemTime Timestamp;
public uint Unknown1;
public static WalkPacket Create(uint id, uint direction, uint mode)
{
var packet = new WalkPacket();
packet.Direction = direction;
packet.Id = id;
packet.Mode = mode;
packet.Timestamp = SystemTime.Now;
return packet;
}
public static implicit operator WalkPacket(byte* ptr)
{
var packet = new WalkPacket();
packet.Direction = *((uint*) (ptr + 4));
packet.Id = *((uint*) (ptr + 8));
packet.Mode = *((uint*) (ptr + 12));
packet.Timestamp = *((SystemTime*) (ptr + 16));
packet.Unknown1 = *((uint*) (ptr + 20));
return packet;
}
public static implicit operator byte[](WalkPacket packet)
{
var buffer = new byte[24 + 8];
fixed (byte* ptr = buffer)
{
PacketBuilder.AppendHeader(ptr, buffer.Length, 10005);
*((uint*) (ptr + 4)) = packet.Direction;
*((uint*) (ptr + 8)) = packet.Id;
*((uint*) (ptr + 12)) = packet.Mode;
*((SystemTime*) (ptr + 16)) = packet.Timestamp;
*((uint*) (ptr + 20)) = packet.Unknown1;
}
return buffer;
}
}