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