From 995bbac48c1203fbcf6040ffef9983b56a6947fb Mon Sep 17 00:00:00 2001 From: cha0s Date: Tue, 1 Oct 2019 22:43:10 -0500 Subject: [PATCH] feat: packet queue (and retry) --- common/traits/informed.trait.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/common/traits/informed.trait.js b/common/traits/informed.trait.js index 144b49e..8a34d03 100644 --- a/common/traits/informed.trait.js +++ b/common/traits/informed.trait.js @@ -8,8 +8,6 @@ import {Trait} from '@avocado/entity'; import {Rectangle, Vector} from '@avocado/math'; import {BundlePacket, ServerSynchronizer} from '@avocado/net'; -import {SelfEntityPacket} from '../../common/packets/self-entity.packet'; - const decorate = compose( ); @@ -21,12 +19,15 @@ export class Informed extends decorate(Trait) { constructor(entity, params, state) { super(entity, params, state); + this._triedPackets = []; + this._queuedPackets = []; this._sentSelfEntityPacket = false; this._socket = undefined; this._synchronizer = new ServerSynchronizer(); } destroy() { + this._queuedPackets = []; if (this._socket) { delete this._socket.entity; delete this._socket; @@ -59,16 +60,17 @@ export class Informed extends decorate(Trait) { return { inform: () => { - const payload = this._synchronizer.packetsFor(this.entity); - if (0 === payload.length) { - return; + const synchronizerPackets = this._synchronizer.packetsFor(this.entity); + for (let i = 0; i < synchronizerPackets.length; i++) { + this._triedPackets.push(synchronizerPackets[i]); } - if (!this._sentSelfEntityPacket) { - this._sentSelfEntityPacket = true; - payload.push(new SelfEntityPacket(this.entity.numericUid)); + for (let i = 0; i < this._queuedPackets.length; i++) { + this._triedPackets.push(this._queuedPackets[i]); } - if (this._socket) { - this._socket.send(new BundlePacket(payload)); + this._queuedPackets = []; + if (this._socket && this._triedPackets.length > 0) { + this._socket.send(new BundlePacket(this._triedPackets)); + this._triedPackets = []; } }, @@ -76,6 +78,10 @@ export class Informed extends decorate(Trait) { this._synchronizer.addSynchronized(synchronized); }, + queuePacket: (packet) => { + this._queuedPackets.push(packet); + }, + removeSynchronized: (synchronized) => { this._synchronizer.removeSynchronized(synchronized); },