mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 11:00:32 +00:00
Protocol: removed content length from message header because we can calculate the size from whatever is left in the reader. Saves 1-2 bytes bandwidth, so another 5% on average.
This commit is contained in:
parent
d36b6d926d
commit
00f571c28c
@ -119,7 +119,6 @@ public static bool IsUnreliableQoS(QosType qos)
|
|||||||
// network protocol all in one place, instead of constructing headers in all kinds of different places
|
// network protocol all in one place, instead of constructing headers in all kinds of different places
|
||||||
//
|
//
|
||||||
// MsgType (1-n bytes)
|
// MsgType (1-n bytes)
|
||||||
// ContentSize (1-n bytes)
|
|
||||||
// Content (ContentSize bytes)
|
// Content (ContentSize bytes)
|
||||||
//
|
//
|
||||||
// -> we use varint for headers because most messages will result in 1 byte type/size headers then instead of always
|
// -> we use varint for headers because most messages will result in 1 byte type/size headers then instead of always
|
||||||
@ -136,14 +135,6 @@ public static byte[] PackMessage(ushort msgType, byte[] content)
|
|||||||
|
|
||||||
NetworkWriter writer = new NetworkWriter();
|
NetworkWriter writer = new NetworkWriter();
|
||||||
|
|
||||||
// message content size (varint)
|
|
||||||
if (content.Length > UInt16.MaxValue)
|
|
||||||
{
|
|
||||||
if (LogFilter.logError) { Debug.LogError("PackMessage: size is too large (" + content.Length + ") bytes. The maximum buffer size is " + UInt16.MaxValue + " bytes."); }
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
writer.WritePackedUInt32((uint)content.Length);
|
|
||||||
|
|
||||||
// message type (varint)
|
// message type (varint)
|
||||||
writer.WritePackedUInt32(msgType);
|
writer.WritePackedUInt32(msgType);
|
||||||
|
|
||||||
@ -158,16 +149,13 @@ public static bool UnpackMessage(byte[] message, out ushort msgType, out byte[]
|
|||||||
{
|
{
|
||||||
NetworkReader reader = new NetworkReader(message);
|
NetworkReader reader = new NetworkReader(message);
|
||||||
|
|
||||||
// read content size (varint)
|
|
||||||
UInt16 size = (UInt16)reader.ReadPackedUInt32();
|
|
||||||
|
|
||||||
// read message type (varint)
|
// read message type (varint)
|
||||||
msgType = (UInt16)reader.ReadPackedUInt32();
|
msgType = (UInt16)reader.ReadPackedUInt32();
|
||||||
|
|
||||||
// read content (if any)
|
// read content (remaining data in message)
|
||||||
content = reader.ReadBytes(size);
|
content = reader.ReadBytes(reader.Length - reader.Position);
|
||||||
|
|
||||||
return content.Length == size;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user