@baltica/raknet 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -48,6 +48,7 @@ class Client extends utils_1.Emitter {
48
48
  }
49
49
  initDirect() {
50
50
  this.socket = (0, node_dgram_1.createSocket)(this.options.family);
51
+ this.socket.ref();
51
52
  this.network.send = (data) => this.socket.send(data, this.options.port, this.options.address);
52
53
  return new Promise((resolve) => {
53
54
  try {
@@ -44,7 +44,9 @@ export declare class NetworkSession extends Emitter<NetworkEvents> {
44
44
  private retransmitTimer?;
45
45
  private cleanupTimer?;
46
46
  private lastCleanup;
47
+ private keepaliveTimer?;
47
48
  constructor(mtu: number, client: boolean);
49
+ private startKeepalive;
48
50
  sendOfflineWithRetry(data: Buffer): void;
49
51
  private clearOfflineRetry;
50
52
  destroy(): void;
@@ -46,18 +46,32 @@ class NetworkSession extends utils_1.Emitter {
46
46
  retransmitTimer;
47
47
  cleanupTimer;
48
48
  lastCleanup = 0;
49
+ keepaliveTimer;
49
50
  constructor(mtu, client) {
50
51
  super();
51
52
  this.mtu = mtu;
52
53
  this.client = client;
53
54
  for (let i = 0; i < 32; i++)
54
55
  this.inputOrderingQueue.set(i, new Map());
56
+ this.startKeepalive();
57
+ }
58
+ startKeepalive() {
59
+ if (this.keepaliveTimer)
60
+ clearTimeout(this.keepaliveTimer);
61
+ const interval = this.status === proto_1.Status.Connected ? 10 :
62
+ this.status === proto_1.Status.Connecting ? 10 : 100;
63
+ this.keepaliveTimer = setTimeout(() => {
64
+ this.tick();
65
+ this.startKeepalive();
66
+ }, interval);
67
+ this.keepaliveTimer.ref();
55
68
  }
56
69
  sendOfflineWithRetry(data) {
57
70
  this.offlineRetry = { data, attempts: 1, maxAttempts: this.maxRetransmit, lastSent: Date.now(), interval: this.retransmitInterval };
58
71
  this.send(data);
59
72
  if (!this.retransmitTimer) {
60
73
  this.retransmitTimer = setTimeout(() => this.tick(), this.retransmitInterval);
74
+ this.retransmitTimer.ref();
61
75
  }
62
76
  }
63
77
  clearOfflineRetry() {
@@ -68,12 +82,15 @@ class NetworkSession extends utils_1.Emitter {
68
82
  }
69
83
  }
70
84
  destroy() {
85
+ this.status = proto_1.Status.Disconnected;
71
86
  if (this.ackTimer)
72
87
  clearTimeout(this.ackTimer);
73
88
  if (this.retransmitTimer)
74
89
  clearTimeout(this.retransmitTimer);
75
90
  if (this.cleanupTimer)
76
91
  clearTimeout(this.cleanupTimer);
92
+ if (this.keepaliveTimer)
93
+ clearTimeout(this.keepaliveTimer);
77
94
  this.removeAllListeners();
78
95
  }
79
96
  receive(buffer) {
@@ -241,7 +258,7 @@ class NetworkSession extends utils_1.Emitter {
241
258
  }
242
259
  }
243
260
  tick() {
244
- if (this.status === proto_1.Status.Disconnected)
261
+ if (this.status === proto_1.Status.Disconnected || !this.send)
245
262
  return;
246
263
  const now = Date.now();
247
264
  if (this.offlineRetry) {
@@ -259,6 +276,7 @@ class NetworkSession extends utils_1.Emitter {
259
276
  if (this.retransmitTimer)
260
277
  clearTimeout(this.retransmitTimer);
261
278
  this.retransmitTimer = setTimeout(() => this.tick(), r.interval);
279
+ this.retransmitTimer.ref();
262
280
  return;
263
281
  }
264
282
  }
@@ -325,6 +343,7 @@ class NetworkSession extends utils_1.Emitter {
325
343
  if (this.retransmitTimer)
326
344
  clearTimeout(this.retransmitTimer);
327
345
  this.retransmitTimer = setTimeout(() => this.tick(), nextRetransmit);
346
+ this.retransmitTimer.ref();
328
347
  }
329
348
  }
330
349
  onAck(ack) {
@@ -405,7 +424,7 @@ class NetworkSession extends utils_1.Emitter {
405
424
  this.flush();
406
425
  }
407
426
  flush() {
408
- if (this.outputFrames.size === 0)
427
+ if (this.outputFrames.size === 0 || !this.send)
409
428
  return;
410
429
  const fs = new proto_1.FrameSet();
411
430
  fs.sequence = this.outputSequence++;
@@ -416,6 +435,7 @@ class NetworkSession extends utils_1.Emitter {
416
435
  this.send(fs.serialize());
417
436
  if (!this.retransmitTimer) {
418
437
  this.retransmitTimer = setTimeout(() => this.tick(), this.retransmitInterval);
438
+ this.retransmitTimer.ref();
419
439
  }
420
440
  }
421
441
  onFrameSet(fs) {
@@ -438,6 +458,7 @@ class NetworkSession extends utils_1.Emitter {
438
458
  this.ackTimer = undefined;
439
459
  this.tick();
440
460
  }, 10);
461
+ this.ackTimer.ref();
441
462
  }
442
463
  }
443
464
  handleFrame(frame) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@baltica/raknet",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "RakNet implementation for Baltica",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",