Skip to content

Commit b61fb49

Browse files
authored
Moved decoding functions to Encoder.cs (#123)
* Moved functions to Encoder.cs * fixed indentation * fixed ArrayUtils whitespaces
1 parent 9db9eca commit b61fb49

File tree

3 files changed

+88
-65
lines changed

3 files changed

+88
-65
lines changed

Assets/Plugins/Colyseus/Room.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public class Room<T> : IRoom
5656

5757
protected Dictionary<string, IMessageHandler> OnMessageHandlers = new Dictionary<string, IMessageHandler>();
5858

59+
private Schema.Encoder Encode = Schema.Encoder.GetInstance();
5960
private Schema.Decoder Decode = Schema.Decoder.GetInstance();
6061

6162
/// <summary>
@@ -155,7 +156,7 @@ public async Task Send(byte type, object message)
155156
public async Task Send(string type)
156157
{
157158
byte[] encodedType = System.Text.Encoding.UTF8.GetBytes(type);
158-
byte[] initialBytes = ArrayUtils.getInitialBytesFromEncodedType(encodedType);
159+
byte[] initialBytes = Encode.getInitialBytesFromEncodedType(encodedType);
159160

160161
byte[] bytes = new byte[initialBytes.Length + encodedType.Length];
161162
Buffer.BlockCopy(initialBytes, 0, bytes, 0, initialBytes.Length);
@@ -175,7 +176,7 @@ public async Task Send(string type, object message)
175176
MsgPack.Serialize(message, serializationOutput, SerializationOptions.SuppressTypeInformation);
176177

177178
byte[] encodedType = System.Text.Encoding.UTF8.GetBytes(type);
178-
byte[] initialBytes = ArrayUtils.getInitialBytesFromEncodedType(encodedType);
179+
byte[] initialBytes = Encode.getInitialBytesFromEncodedType(encodedType);
179180
byte[] encodedMessage = serializationOutput.ToArray();
180181

181182
byte[] bytes = new byte[encodedType.Length + encodedMessage.Length + initialBytes.Length];
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using MiscUtil.Conversion;
3+
4+
namespace Colyseus.Schema
5+
{
6+
public class Encoder
7+
{
8+
/*
9+
* Singleton
10+
*/
11+
protected static Encoder Instance = new Encoder();
12+
13+
public static Encoder GetInstance()
14+
{
15+
return Instance;
16+
}
17+
18+
public Encoder()
19+
{
20+
21+
}
22+
23+
public byte[] getInitialBytesFromEncodedType(byte[] encodedType)
24+
{
25+
byte[] initialBytes = { Protocol.ROOM_DATA };
26+
27+
if (encodedType.Length < 0x20)
28+
{
29+
initialBytes = addByteToArray(initialBytes, new byte[] { (byte)(encodedType.Length | 0xa0) });
30+
}
31+
else if (encodedType.Length < 0x100)
32+
{
33+
initialBytes = addByteToArray(initialBytes, new byte[] { 0xd9 });
34+
initialBytes = uint8(initialBytes, encodedType.Length);
35+
}
36+
else if (encodedType.Length < 0x10000)
37+
{
38+
initialBytes = addByteToArray(initialBytes, new byte[] { 0xda });
39+
initialBytes = uint16(initialBytes, encodedType.Length);
40+
}
41+
else if (encodedType.Length < 0x7fffffff)
42+
{
43+
initialBytes = addByteToArray(initialBytes, new byte[] { 0xdb });
44+
initialBytes = uint32(initialBytes, encodedType.Length);
45+
}
46+
else
47+
{
48+
throw new System.Exception("String too long");
49+
}
50+
51+
return initialBytes;
52+
}
53+
54+
private byte[] addByteToArray(byte[] byteArray, byte[] newBytes)
55+
{
56+
byte[] bytes = new byte[byteArray.Length + newBytes.Length];
57+
System.Buffer.BlockCopy(byteArray, 0, bytes, 0, byteArray.Length);
58+
System.Buffer.BlockCopy(newBytes, 0, bytes, byteArray.Length, newBytes.Length);
59+
return bytes;
60+
}
61+
62+
private byte[] uint8(byte[] bytes, int value)
63+
{
64+
return addByteToArray(bytes, new byte[] { (byte)(value & 255) });
65+
}
66+
67+
private byte[] uint16(byte[] bytes, int value)
68+
{
69+
var a1 = addByteToArray(bytes, new byte[] { (byte)(value & 255) });
70+
return addByteToArray(a1, new byte[] { (byte)((value >> 8) & 255) });
71+
}
72+
73+
private byte[] uint32(byte[] bytes, int value)
74+
{
75+
var b4 = value >> 24;
76+
var b3 = value >> 16;
77+
var b2 = value >> 8;
78+
var b1 = value;
79+
var a1 = addByteToArray(bytes, new byte[] { (byte)(b1 & 255) });
80+
var a2 = addByteToArray(a1, new byte[] { (byte)(b2 & 255) });
81+
var a3 = addByteToArray(a2, new byte[] { (byte)(b3 & 255) });
82+
return addByteToArray(a3, new byte[] { (byte)(b4 & 255) });
83+
}
84+
}
85+
}

Assets/Plugins/Colyseus/Utils/ArrayUtils.cs

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,68 +8,5 @@ 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-
}
7411
}
7512
}

0 commit comments

Comments
 (0)