Skip to content

Commit 9db9eca

Browse files
authored
Added getInitialBytesFromEncodedType and others functions in ArrayUtils (#122)
1 parent 8614009 commit 9db9eca

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

Assets/Plugins/Colyseus/Room.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public async Task Send(byte type, object message)
155155
public async Task Send(string type)
156156
{
157157
byte[] encodedType = System.Text.Encoding.UTF8.GetBytes(type);
158-
byte[] initialBytes = { Protocol.ROOM_DATA, (byte)(encodedType.Length | 0xa0) };
158+
byte[] initialBytes = ArrayUtils.getInitialBytesFromEncodedType(encodedType);
159159

160160
byte[] bytes = new byte[initialBytes.Length + encodedType.Length];
161161
Buffer.BlockCopy(initialBytes, 0, bytes, 0, initialBytes.Length);
@@ -175,7 +175,7 @@ public async Task Send(string type, object message)
175175
MsgPack.Serialize(message, serializationOutput, SerializationOptions.SuppressTypeInformation);
176176

177177
byte[] encodedType = System.Text.Encoding.UTF8.GetBytes(type);
178-
byte[] initialBytes = { Protocol.ROOM_DATA, (byte) (encodedType.Length | 0xa0) };
178+
byte[] initialBytes = ArrayUtils.getInitialBytesFromEncodedType(encodedType);
179179
byte[] encodedMessage = serializationOutput.ToArray();
180180

181181
byte[] bytes = new byte[encodedType.Length + encodedMessage.Length + initialBytes.Length];

Assets/Plugins/Colyseus/Utils/ArrayUtils.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,68 @@ public static byte[] SubArray(byte[] bytes, int index, int length)
88
{
99
return new List<byte>(bytes).GetRange(index, length).ToArray();
1010
}
11+
12+
public static byte[] getInitialBytesFromEncodedType(byte[] encodedType)
13+
{
14+
byte[] initialBytes = { Protocol.ROOM_DATA };
15+
16+
if (encodedType.Length < 0x20)
17+
{
18+
initialBytes = addByteToArray(initialBytes, new byte[] { (byte)(encodedType.Length | 0xa0) });
19+
}
20+
else if (encodedType.Length < 0x100)
21+
{
22+
initialBytes = addByteToArray(initialBytes, new byte[] { 0xd9 });
23+
initialBytes = uint8(initialBytes, encodedType.Length);
24+
}
25+
else if (encodedType.Length < 0x10000)
26+
{
27+
initialBytes = addByteToArray(initialBytes, new byte[] { 0xda });
28+
initialBytes = uint16(initialBytes, encodedType.Length);
29+
}
30+
else if (encodedType.Length < 0x7fffffff)
31+
{
32+
initialBytes = addByteToArray(initialBytes, new byte[] { 0xdb });
33+
initialBytes = uint32(initialBytes, encodedType.Length);
34+
}
35+
else
36+
{
37+
throw new System.Exception("String too long");
38+
}
39+
40+
return initialBytes;
41+
}
42+
43+
private static byte[] addByteToArray(byte[] byteArray, byte[] newBytes)
44+
{
45+
byte[] bytes = new byte[byteArray.Length + newBytes.Length];
46+
System.Buffer.BlockCopy(byteArray, 0, bytes, 0, byteArray.Length);
47+
System.Buffer.BlockCopy(newBytes, 0, bytes, byteArray.Length, newBytes.Length);
48+
return bytes;
49+
}
50+
51+
private static byte[] uint8(byte[] bytes, int value)
52+
{
53+
return addByteToArray(bytes, new byte[] { (byte)(value & 255) });
54+
}
55+
56+
private static byte[] uint16(byte[] bytes, int value)
57+
{
58+
var a1 = addByteToArray(bytes, new byte[] { (byte)(value & 255) });
59+
return addByteToArray(a1, new byte[] { (byte)((value >> 8) & 255) });
60+
}
61+
62+
private static byte[] uint32(byte[] bytes, int value)
63+
{
64+
var b4 = value >> 24;
65+
var b3 = value >> 16;
66+
var b2 = value >> 8;
67+
var b1 = value;
68+
var a1 = addByteToArray(bytes, new byte[] { (byte)(b1 & 255) });
69+
var a2 = addByteToArray(a1, new byte[] { (byte)(b2 & 255) });
70+
var a3 = addByteToArray(a2, new byte[] { (byte)(b3 & 255) });
71+
return addByteToArray(a3, new byte[] { (byte)(b4 & 255) });
72+
}
73+
}
1174
}
1275
}

0 commit comments

Comments
 (0)