From 9db9ecaa2f04496900fd343811df7892e5d27a5a Mon Sep 17 00:00:00 2001 From: Jimmy DJABALI Date: Mon, 1 Jun 2020 04:29:07 +0200 Subject: [PATCH] Added getInitialBytesFromEncodedType and others functions in ArrayUtils (#122) --- Assets/Plugins/Colyseus/Room.cs | 4 +- Assets/Plugins/Colyseus/Utils/ArrayUtils.cs | 63 +++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/Assets/Plugins/Colyseus/Room.cs b/Assets/Plugins/Colyseus/Room.cs index 3767725c..95682b4e 100644 --- a/Assets/Plugins/Colyseus/Room.cs +++ b/Assets/Plugins/Colyseus/Room.cs @@ -155,7 +155,7 @@ public async Task Send(byte type, object message) public async Task Send(string type) { byte[] encodedType = System.Text.Encoding.UTF8.GetBytes(type); - byte[] initialBytes = { Protocol.ROOM_DATA, (byte)(encodedType.Length | 0xa0) }; + byte[] initialBytes = ArrayUtils.getInitialBytesFromEncodedType(encodedType); byte[] bytes = new byte[initialBytes.Length + encodedType.Length]; Buffer.BlockCopy(initialBytes, 0, bytes, 0, initialBytes.Length); @@ -175,7 +175,7 @@ public async Task Send(string type, object message) MsgPack.Serialize(message, serializationOutput, SerializationOptions.SuppressTypeInformation); byte[] encodedType = System.Text.Encoding.UTF8.GetBytes(type); - byte[] initialBytes = { Protocol.ROOM_DATA, (byte) (encodedType.Length | 0xa0) }; + byte[] initialBytes = ArrayUtils.getInitialBytesFromEncodedType(encodedType); byte[] encodedMessage = serializationOutput.ToArray(); byte[] bytes = new byte[encodedType.Length + encodedMessage.Length + initialBytes.Length]; diff --git a/Assets/Plugins/Colyseus/Utils/ArrayUtils.cs b/Assets/Plugins/Colyseus/Utils/ArrayUtils.cs index 6a430657..d36368bb 100644 --- a/Assets/Plugins/Colyseus/Utils/ArrayUtils.cs +++ b/Assets/Plugins/Colyseus/Utils/ArrayUtils.cs @@ -8,5 +8,68 @@ public static byte[] SubArray(byte[] bytes, int index, int length) { return new List(bytes).GetRange(index, length).ToArray(); } + + public static byte[] getInitialBytesFromEncodedType(byte[] encodedType) + { + byte[] initialBytes = { Protocol.ROOM_DATA }; + + if (encodedType.Length < 0x20) + { + initialBytes = addByteToArray(initialBytes, new byte[] { (byte)(encodedType.Length | 0xa0) }); + } + else if (encodedType.Length < 0x100) + { + initialBytes = addByteToArray(initialBytes, new byte[] { 0xd9 }); + initialBytes = uint8(initialBytes, encodedType.Length); + } + else if (encodedType.Length < 0x10000) + { + initialBytes = addByteToArray(initialBytes, new byte[] { 0xda }); + initialBytes = uint16(initialBytes, encodedType.Length); + } + else if (encodedType.Length < 0x7fffffff) + { + initialBytes = addByteToArray(initialBytes, new byte[] { 0xdb }); + initialBytes = uint32(initialBytes, encodedType.Length); + } + else + { + throw new System.Exception("String too long"); + } + + return initialBytes; + } + + private static byte[] addByteToArray(byte[] byteArray, byte[] newBytes) + { + byte[] bytes = new byte[byteArray.Length + newBytes.Length]; + System.Buffer.BlockCopy(byteArray, 0, bytes, 0, byteArray.Length); + System.Buffer.BlockCopy(newBytes, 0, bytes, byteArray.Length, newBytes.Length); + return bytes; + } + + private static byte[] uint8(byte[] bytes, int value) + { + return addByteToArray(bytes, new byte[] { (byte)(value & 255) }); + } + + private static byte[] uint16(byte[] bytes, int value) + { + var a1 = addByteToArray(bytes, new byte[] { (byte)(value & 255) }); + return addByteToArray(a1, new byte[] { (byte)((value >> 8) & 255) }); + } + + private static byte[] uint32(byte[] bytes, int value) + { + var b4 = value >> 24; + var b3 = value >> 16; + var b2 = value >> 8; + var b1 = value; + var a1 = addByteToArray(bytes, new byte[] { (byte)(b1 & 255) }); + var a2 = addByteToArray(a1, new byte[] { (byte)(b2 & 255) }); + var a3 = addByteToArray(a2, new byte[] { (byte)(b3 & 255) }); + return addByteToArray(a3, new byte[] { (byte)(b4 & 255) }); + } + } } }