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:
vis2k 2018-08-07 14:36:20 +02:00
parent d36b6d926d
commit 00f571c28c

View File

@ -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
//
// MsgType (1-n bytes)
// ContentSize (1-n bytes)
// Content (ContentSize bytes)
//
// -> 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();
// 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)
writer.WritePackedUInt32(msgType);
@ -158,16 +149,13 @@ public static bool UnpackMessage(byte[] message, out ushort msgType, out byte[]
{
NetworkReader reader = new NetworkReader(message);
// read content size (varint)
UInt16 size = (UInt16)reader.ReadPackedUInt32();
// read message type (varint)
msgType = (UInt16)reader.ReadPackedUInt32();
// read content (if any)
content = reader.ReadBytes(size);
// read content (remaining data in message)
content = reader.ReadBytes(reader.Length - reader.Position);
return content.Length == size;
return true;
}
}
}