feat: packet queue (and retry)

This commit is contained in:
cha0s 2019-10-01 22:43:10 -05:00
parent 0cdc7ed4e5
commit 995bbac48c

View File

@ -8,8 +8,6 @@ import {Trait} from '@avocado/entity';
import {Rectangle, Vector} from '@avocado/math'; import {Rectangle, Vector} from '@avocado/math';
import {BundlePacket, ServerSynchronizer} from '@avocado/net'; import {BundlePacket, ServerSynchronizer} from '@avocado/net';
import {SelfEntityPacket} from '../../common/packets/self-entity.packet';
const decorate = compose( const decorate = compose(
); );
@ -21,12 +19,15 @@ export class Informed extends decorate(Trait) {
constructor(entity, params, state) { constructor(entity, params, state) {
super(entity, params, state); super(entity, params, state);
this._triedPackets = [];
this._queuedPackets = [];
this._sentSelfEntityPacket = false; this._sentSelfEntityPacket = false;
this._socket = undefined; this._socket = undefined;
this._synchronizer = new ServerSynchronizer(); this._synchronizer = new ServerSynchronizer();
} }
destroy() { destroy() {
this._queuedPackets = [];
if (this._socket) { if (this._socket) {
delete this._socket.entity; delete this._socket.entity;
delete this._socket; delete this._socket;
@ -59,16 +60,17 @@ export class Informed extends decorate(Trait) {
return { return {
inform: () => { inform: () => {
const payload = this._synchronizer.packetsFor(this.entity); const synchronizerPackets = this._synchronizer.packetsFor(this.entity);
if (0 === payload.length) { for (let i = 0; i < synchronizerPackets.length; i++) {
return; this._triedPackets.push(synchronizerPackets[i]);
} }
if (!this._sentSelfEntityPacket) { for (let i = 0; i < this._queuedPackets.length; i++) {
this._sentSelfEntityPacket = true; this._triedPackets.push(this._queuedPackets[i]);
payload.push(new SelfEntityPacket(this.entity.numericUid));
} }
if (this._socket) { this._queuedPackets = [];
this._socket.send(new BundlePacket(payload)); 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); this._synchronizer.addSynchronized(synchronized);
}, },
queuePacket: (packet) => {
this._queuedPackets.push(packet);
},
removeSynchronized: (synchronized) => { removeSynchronized: (synchronized) => {
this._synchronizer.removeSynchronized(synchronized); this._synchronizer.removeSynchronized(synchronized);
}, },