Skip to content

Commit

Permalink
Added getInitialBytesFromEncodedType and others functions in ArrayUti…
Browse files Browse the repository at this point in the history
…ls (#122)
  • Loading branch information
jimmydjabali authored Jun 1, 2020
1 parent 8614009 commit 9db9eca
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Assets/Plugins/Colyseus/Room.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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];
Expand Down
63 changes: 63 additions & 0 deletions Assets/Plugins/Colyseus/Utils/ArrayUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,68 @@ public static byte[] SubArray(byte[] bytes, int index, int length)
{
return new List<byte>(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) });
}
}
}
}

0 comments on commit 9db9eca

Please sign in to comment.