From 4bc2d9f50fe350734596890b7226861da7b37df0 Mon Sep 17 00:00:00 2001 From: cha0s Date: Sun, 28 Jun 2020 06:39:19 -0500 Subject: [PATCH] refactor: packet coding --- .../entity-list-update-entity.packet.js | 5 +- .../packets/entity-update-trait.packet.js | 5 +- ...trait-update-positioned-position.packet.js | 5 +- .../packets/trait-update-visible.packet.js | 5 +- packages/input/packet/input.packet.js | 5 +- packages/net/packet/bundle.packet.js | 3 +- packages/net/packet/packet.js | 9 +- packages/net/packet/socket.io-parser.js | 89 ++++++++++++++++--- .../net/s13n/synchronized-create.packet.js | 5 +- .../packets/layers-update-layer.packet.js | 5 +- .../packets/room-update-layers.packet.js | 5 +- .../packets/room-update-size.packet.js | 5 +- .../topdown/packets/tiles-update.packet.js | 5 +- 13 files changed, 117 insertions(+), 34 deletions(-) diff --git a/packages/entity/packets/entity-list-update-entity.packet.js b/packages/entity/packets/entity-list-update-entity.packet.js index 42f504a..67903cf 100644 --- a/packages/entity/packets/entity-list-update-entity.packet.js +++ b/packages/entity/packets/entity-list-update-entity.packet.js @@ -25,11 +25,12 @@ export default class EntityListUpdateEntityPacket extends Packet { } static unpack(packet) { - const data = super.unpack(packet); + const unpacked = super.unpack(packet); + const {data} = unpacked; for (let i = 0; i < data.length; i++) { data[i].packets = BundlePacket.unpack(data[i].packets); } - return data; + return unpacked; } } diff --git a/packages/entity/packets/entity-update-trait.packet.js b/packages/entity/packets/entity-update-trait.packet.js index 9419db9..d8ac9b9 100644 --- a/packages/entity/packets/entity-update-trait.packet.js +++ b/packages/entity/packets/entity-update-trait.packet.js @@ -29,13 +29,14 @@ export default class EntityUpdateTraitPacket extends SynchronizedUpdatePacket { } static unpack(packet) { - const data = super.unpack(packet); + const unpacked = super.unpack(packet); + const {data} = unpacked; for (let i = 0; i < data.traits.length; i++) { const {default: Trait} = fromId(data.traits[i].type); data.traits[i].type = Trait.type(); data.traits[i].packets = BundlePacket.unpack(data.traits[i].packets); } - return data; + return unpacked; } } diff --git a/packages/entity/packets/trait-update-positioned-position.packet.js b/packages/entity/packets/trait-update-positioned-position.packet.js index e9cab70..74b7dde 100644 --- a/packages/entity/packets/trait-update-positioned-position.packet.js +++ b/packages/entity/packets/trait-update-positioned-position.packet.js @@ -19,9 +19,10 @@ export default class TraitUpdatePositionedPositionPacket extends Packet { } static unpack(packet) { - const data = super.unpack(packet); + const unpacked = super.unpack(packet); + const {data} = unpacked; data.position = Vector.unpackFromUint32(data.position); - return data; + return unpacked; } } diff --git a/packages/graphics/packets/trait-update-visible.packet.js b/packages/graphics/packets/trait-update-visible.packet.js index 7162c57..41016f2 100644 --- a/packages/graphics/packets/trait-update-visible.packet.js +++ b/packages/graphics/packets/trait-update-visible.packet.js @@ -19,9 +19,10 @@ export default class TraitUpdateVisiblePacket extends Packet { } static unpack(packet) { - const data = super.unpack(packet); + const unpacked = super.unpack(packet); + const {data} = unpacked; data.opacity = data.opacity / 255; - return data; + return unpacked; } } diff --git a/packages/input/packet/input.packet.js b/packages/input/packet/input.packet.js index 78a031f..8284b9b 100644 --- a/packages/input/packet/input.packet.js +++ b/packages/input/packet/input.packet.js @@ -30,7 +30,8 @@ export default class InputPacket extends Packet { } static unpack(packet) { - const data = super.unpack(packet); + const unpacked = super.unpack(packet); + const {data} = unpacked; for (let i = 0; i < data.length; i++) { const actionId = data[i] >> 24; const value = data[i] & 0xFFFFFF; @@ -40,7 +41,7 @@ export default class InputPacket extends Packet { value: 16777214 === value ? -1 : value, }; } - return data; + return unpacked; } } diff --git a/packages/net/packet/bundle.packet.js b/packages/net/packet/bundle.packet.js index e89ede8..a1a4fd4 100644 --- a/packages/net/packet/bundle.packet.js +++ b/packages/net/packet/bundle.packet.js @@ -44,7 +44,8 @@ export default class BundlePacket extends Packet { } static unpack(packet) { - const data = super.unpack(packet); + const unpacked = super.unpack(packet); + const {data} = unpacked; const {fromId} = require('./packets.scwp'); const packets = []; let caret = 0; diff --git a/packages/net/packet/packet.js b/packages/net/packet/packet.js index c2016b7..9319a1e 100644 --- a/packages/net/packet/packet.js +++ b/packages/net/packet/packet.js @@ -22,6 +22,9 @@ export class Packet { static pack(packet) { return this.builder.encode({ + id: packet.id, + nsp: packet.nsp, + type: packet.type, _id: packet.data[0], data: packet.data[1], }); @@ -38,12 +41,14 @@ export class Packet { static get schema() { return { _id: 'uint8', + id: 'uint32', + nsp: 'string', + type: 'uint8', }; } static unpack(packet) { - const {data} = this.builder.decode(packet); - return data; + return this.builder.decode(packet); } } diff --git a/packages/net/packet/socket.io-parser.js b/packages/net/packet/socket.io-parser.js index 1ea8284..3297dbd 100644 --- a/packages/net/packet/socket.io-parser.js +++ b/packages/net/packet/socket.io-parser.js @@ -1,20 +1,85 @@ import {compose, deflate, EventEmitter, inflate} from '@avocado/core'; +export const types = [ + 'CONNECT', + 'DISCONNECT', + 'EVENT', + 'ACK', + 'ERROR', + 'BINARY_EVENT', + 'BINARY_ACK' +]; + /** - * Packet types (see https://github.com/socketio/socket.io-protocol) + * Packet type `connect`. + * + * @api public */ -const TYPES = { - EVENT: 2, - ERROR: 4, - BINARY_EVENT: 5, -}; + +export const CONNECT = 0; + +/** + * Packet type `disconnect`. + * + * @api public + */ + +export const DISCONNECT = 1; + +/** + * Packet type `event`. + * + * @api public + */ + +export const EVENT = 2; + +/** + * Packet type `ack`. + * + * @api public + */ + +export const ACK = 3; + +/** + * Packet type `error`. + * + * @api public + */ + +export const ERROR = 4; + +/** + * Packet type 'binary event' + * + * @api public + */ + +export const BINARY_EVENT = 5; + +/** + * Packet type `binary ack`. For acks with binary arguments. + * + * @api public + */ + +export const BINARY_ACK = 6; + +// /** +// * Packet types (see https://github.com/socketio/socket.io-protocol) +// */ +// const TYPES = { +// // EVENT: 2, +// BINARY_EVENT: 5, +// }; class Encoder { encode(packet, callback) { switch (packet.type) { - case TYPES.EVENT: - case TYPES.BINARY_EVENT: + case BINARY_ACK: + case BINARY_EVENT: return callback([this.pack(packet)]); default: return callback([JSON.stringify(packet)]); @@ -53,11 +118,13 @@ class Decoder extends decorateDecoder(class {}) { const view = new Uint8Array(packet); const packetId = view[0]; const {default: Packet} = fromId(packetId); - const data = Packet.unpack(packet); + const unpacked = Packet.unpack(packet); + const {data} = unpacked; this.emit('decoded', { - type: TYPES.EVENT, + type: unpacked.type, data: [packetId, data], - nsp: '/', + id: unpacked.id, + nsp: unpacked.nsp, }); } diff --git a/packages/net/s13n/synchronized-create.packet.js b/packages/net/s13n/synchronized-create.packet.js index 84efffe..787464a 100644 --- a/packages/net/s13n/synchronized-create.packet.js +++ b/packages/net/s13n/synchronized-create.packet.js @@ -17,9 +17,10 @@ export default class SynchronizedCreatePacket extends SynchronizedPacket { } static unpack(packet) { - const data = super.unpack(packet); + const unpacked = super.unpack(packet); + const {data} = unpacked; data.spec = msgpack.decode(data.spec); - return data; + return unpacked; } } diff --git a/packages/topdown/packets/layers-update-layer.packet.js b/packages/topdown/packets/layers-update-layer.packet.js index ee650a7..24fc58e 100644 --- a/packages/topdown/packets/layers-update-layer.packet.js +++ b/packages/topdown/packets/layers-update-layer.packet.js @@ -25,13 +25,14 @@ export default class LayersUpdateLayerPacket extends Packet { } static unpack(packet) { - const data = super.unpack(packet); + const unpacked = super.unpack(packet); + const {data} = unpacked; for (let i = 0; i < data.length; i++) { data[i].layerPackets = BundlePacket.unpack( data[i].layerPackets ); } - return data; + return unpacked; } } diff --git a/packages/topdown/packets/room-update-layers.packet.js b/packages/topdown/packets/room-update-layers.packet.js index 395c5a5..33f7268 100644 --- a/packages/topdown/packets/room-update-layers.packet.js +++ b/packages/topdown/packets/room-update-layers.packet.js @@ -18,9 +18,10 @@ export default class RoomUpdateLayersPacket extends SynchronizedUpdatePacket { } static unpack(packet) { - const data = super.unpack(packet); + const unpacked = super.unpack(packet); + const {data} = unpacked; data.layersPackets = BundlePacket.unpack(data.layersPackets); - return data; + return unpacked; } } diff --git a/packages/topdown/packets/room-update-size.packet.js b/packages/topdown/packets/room-update-size.packet.js index a93fa23..e142f56 100644 --- a/packages/topdown/packets/room-update-size.packet.js +++ b/packages/topdown/packets/room-update-size.packet.js @@ -16,9 +16,10 @@ export default class RoomUpdateSizePacket extends SynchronizedUpdatePacket { } static unpack(packet) { - const data = super.unpack(packet); + const unpacked = super.unpack(packet); + const {data} = unpacked; data.size = Vector.unpackFromUint32(data.size); - return data; + return unpacked; } } diff --git a/packages/topdown/packets/tiles-update.packet.js b/packages/topdown/packets/tiles-update.packet.js index 3177b3d..15fba18 100644 --- a/packages/topdown/packets/tiles-update.packet.js +++ b/packages/topdown/packets/tiles-update.packet.js @@ -20,9 +20,10 @@ export default class TilesUpdatePacket extends Packet { } static unpack(packet) { - const data = super.unpack(packet); + const unpacked = super.unpack(packet); + const {data} = unpacked; data.position = Vector.unpackFromUint32(data.position); - return data; + return unpacked; } }