@baltica/raknet 0.0.1 → 0.0.2

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.
@@ -17,4 +17,5 @@ export declare class Client extends Emitter<ClientEvents> {
17
17
  connect(): Promise<void>;
18
18
  sendReliable(data: Buffer, priority?: Priority): void;
19
19
  close(): void;
20
+ disconnect(): void;
20
21
  }
@@ -111,12 +111,36 @@ class Client extends utils_1.Emitter {
111
111
  this.network.frameAndSend(data, priority);
112
112
  }
113
113
  close() {
114
- if (this.tickInterval)
114
+ if (this.tickInterval) {
115
115
  clearInterval(this.tickInterval);
116
- if (this.relay)
117
- this.relay.close();
118
- else
119
- this.socket.close();
116
+ this.tickInterval = undefined;
117
+ }
118
+ this.network.status = shared_1.Status.Disconnected;
119
+ this.network.send = () => { };
120
+ this.network.removeAllListeners();
121
+ try {
122
+ if (this.relay) {
123
+ this.relay.close();
124
+ this.relay = undefined;
125
+ }
126
+ else if (this.socket) {
127
+ this.socket.close();
128
+ this.socket = undefined;
129
+ }
130
+ }
131
+ catch { }
132
+ }
133
+ disconnect() {
134
+ const packet = new shared_1.Disconnect();
135
+ try {
136
+ this.sendReliable(packet.serialize(), shared_1.Priority.High);
137
+ }
138
+ catch { }
139
+ // Give UDP time to actually send the packet before closing
140
+ setTimeout(() => {
141
+ this.close();
142
+ this.emit("disconnect");
143
+ }, 150);
120
144
  }
121
145
  }
122
146
  exports.Client = Client;
@@ -221,6 +221,8 @@ class NetworkSession extends utils_1.Emitter {
221
221
  }
222
222
  }
223
223
  tick() {
224
+ if (this.status === proto_1.Status.Disconnected)
225
+ return;
224
226
  const now = Date.now();
225
227
  if (this.offlineRetry) {
226
228
  const r = this.offlineRetry;
@@ -43,17 +43,34 @@ async function createSocks5Relay(proxy, family = "udp4") {
43
43
  if (messageHandler)
44
44
  messageHandler(payload, addr, port);
45
45
  });
46
- tcp.on("close", () => udp.close());
46
+ let closed = false;
47
+ tcp.on("close", () => {
48
+ if (closed)
49
+ return;
50
+ closed = true;
51
+ try {
52
+ udp.close();
53
+ }
54
+ catch { }
55
+ });
47
56
  return {
48
57
  socket: udp,
49
58
  tcp,
50
59
  send(data, targetAddress, targetPort) {
60
+ if (closed)
61
+ return;
51
62
  const header = buildUdpHeader(targetAddress, targetPort);
52
63
  const packet = Buffer.concat([header, data]);
53
64
  udp.send(packet, relay.port, relay.address);
54
65
  },
55
66
  close() {
56
- udp.close();
67
+ if (closed)
68
+ return;
69
+ closed = true;
70
+ try {
71
+ udp.close();
72
+ }
73
+ catch { }
57
74
  tcp.destroy();
58
75
  },
59
76
  onMessage(handler) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@baltica/raknet",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "RakNet implementation for Baltica",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",