refactor: packet coding

This commit is contained in:
cha0s 2020-06-28 06:39:19 -05:00
parent d18ed7171e
commit 4bc2d9f50f
13 changed files with 117 additions and 34 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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,
});
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}