protected object ReadStruct(NetworkStream stream, Type type)
{
byte[] buffer = new byte[Marshal.SizeOf(type)];
stream.Read(buffer, 0, Marshal.SizeOf(type));
GCHandle
handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
object value = Marshal.PtrToStructure(
handle.AddrOfPinnedObject(), type);
handle.Free();
return value;
}
protected void WriteStruct(NetworkStream stream, Type type, object value)
{
byte[] buffer = new byte[Marshal.SizeOf(type)];
GCHandle
handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
Marshal.StructureToPtr(value,
handle.AddrOfPinnedObject(), false);
stream.Write(buffer, 0, Marshal.SizeOf(type));
handle.Free();
}