Skip to content

Commit

Permalink
fixes msgpack decoder to support decoding 'undefined' and 'Date'. closes
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Oct 5, 2020
1 parent b8ca903 commit 9084228
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 8 deletions.
4 changes: 2 additions & 2 deletions haxelib.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"license": "MIT",
"tags": ["multiplayer", "networking", "websockets", "netcode"],
"description": "Multiplayer Game Client for Haxe",
"version": "0.13.2",
"version": "0.13.3",
"classPath": "src/",
"releasenote": "Support for version 0.13",
"releasenote": "Fixes msgpack decoder to support decoding 'undefined' and 'Date'.",
"contributors": ["endel"],
"dependencies": {
"colyseus-websocket": "1.0.7"
Expand Down
37 changes: 32 additions & 5 deletions src/org/msgpack/Decoder.hx
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ class Decoder {
var o:Dynamic;

public function new(b:Bytes, option:DecodeOption) {
var i = new BytesInput(b);
var i = new BytesInput(b);
i.bigEndian = true;
o = decode(i, option);
o = decode(i, option);
}

function decode(i:BytesInput, option:DecodeOption):Dynamic {
Expand All @@ -51,6 +51,33 @@ class Decoder {
case 0xc5: return i.read(i.readUInt16());
case 0xc6: return i.read(i.readInt32 ());

// ext: undefined
case 0xd4: {
var type = i.readByte();
if (type == 0x00) {
// "undefined"
i.readByte();
return null;
}
throw "MsgPack - unsupported extension type: " + type;
}

// ext: Date
case 0xd7: {
var type = i.readByte();
if (type == 0x00) {
var high = i.readInt32() * Math.pow(2, 32);
var low: UInt = (i.readInt32());

// hi = this._view.getInt32(this._offset) * Math.pow(2, 32);
// lo = this._view.getUint32(this._offset + 4);
// this._offset += 8;

return Date.fromTime(high + low);
}
throw "MsgPack - unsupported extension type: " + type;
}

// floating point
case 0xca: return i.readFloat ();
case 0xcb: return i.readDouble();
Expand Down Expand Up @@ -81,11 +108,11 @@ class Decoder {
case 0xdf: return readMap(i, i.readInt32 (), option);

default : {
if (b < 0x80) { return b; } else // positive fix num
if (b < 0x80) { return b; } else // positive fix num
if (b < 0x90) { return readMap (i, (0xf & b), option); } else // fix map
if (b < 0xa0) { return readArray(i, (0xf & b), option); } else // fix array
if (b < 0xc0) { return i.readString(0x1f & b); } else // fix string
if (b > 0xdf) { return 0xffffff00 | b; } // negative fix num
if (b < 0xc0) { return i.readString(0x1f & b); } else // fix string
if (b > 0xdf) { return 0xffffff00 | b; } // negative fix num
}
}
} catch (e:Eof) {}
Expand Down
44 changes: 44 additions & 0 deletions tests/MsgpackTestCase.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import haxe.io.Bytes;
import haxe.io.BytesOutput;
import org.msgpack.MsgPack;

class MsgpackTestCase extends haxe.unit.TestCase {

public function testDate() {
var decoded = MsgPack.decode(this.getBytes([
130, 164, 114, 111, 111, 109, 129, 169, 99, 114, 101, 97, 116, 101, 100, 65, 116, 215, 0, 0, 0, 1, 116, 249, 113, 223, 56, 169, 115, 101, 115,
115, 105, 111, 110, 73, 100, 169, 54, 106, 113, 120, 100, 55, 80, 55, 95
]));

assertEquals("6jqxd7P7_", decoded.sessionId);
assertEquals(true, Std.isOfType(decoded.room.createdAt, Date));
assertEquals("2020-10-05 12:47:03", decoded.room.createdAt.toString());
}

public function testUndefined() {
var decoded = MsgPack.decode(this.getBytes([
130, 164, 114, 111, 111, 109, 129, 165, 117, 110, 100, 101, 102, 212, 0, 0, 169, 115, 101, 115, 115, 105, 111, 110, 73, 100, 169, 54, 106, 113,
120, 100, 55, 80, 55, 95
]));

assertEquals("6jqxd7P7_", decoded.sessionId);
assertEquals(null, decoded.room.undef);
}

public function testInfinity() {
var decoded = MsgPack.decode(this.getBytes([
130, 164, 114, 111, 111, 109, 129, 170, 109, 97, 120, 67, 108, 105, 101, 110, 116, 115, 203, 127, 240, 0, 0, 0, 0, 0, 0, 169, 115, 101, 115, 115,
105, 111, 110, 73, 100, 169, 54, 106, 113, 120, 100, 55, 80, 55, 95
]));

assertEquals(Math.POSITIVE_INFINITY, decoded.room.maxClients);
assertEquals("6jqxd7P7_", decoded.sessionId);
}

private function getBytes(bytes: Array<Int>) {
var builder = new BytesOutput();
for (byte in bytes) { builder.writeByte(byte); }
return builder.getBytes();
}

}
2 changes: 1 addition & 1 deletion tests/TestMain.hx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

class TestMain {

static function main() {
var r = new haxe.unit.TestRunner();
r.add(new MsgpackTestCase());
r.add(new ClientTestCase());
r.add(new StateContainerTestCase());
r.add(new SchemaSerializerTestCase());
Expand Down

0 comments on commit 9084228

Please sign in to comment.