@sanctumterra/raknet 1.1.7 → 1.1.10
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.
- package/dist/client/client.js +3 -3
- package/dist/client/receiver.d.ts +13 -19
- package/dist/client/receiver.js +187 -167
- package/dist/client/sender.d.ts +2 -2
- package/dist/client/sender.js +35 -25
- package/dist/tools/test.js +1 -1
- package/package.json +1 -1
package/dist/client/client.js
CHANGED
|
@@ -18,19 +18,18 @@ class Client extends emitter_1.default {
|
|
|
18
18
|
this.status = raknet_1.Status.Disconnected;
|
|
19
19
|
this.options = { ...options_1.defaultOptions, ...options };
|
|
20
20
|
this.socket = options.socket ?? (0, node_dgram_1.createSocket)("udp4");
|
|
21
|
-
this.receiver = new receiver_1.Receiver(this
|
|
21
|
+
this.receiver = new receiver_1.Receiver(this);
|
|
22
22
|
this.sender = new sender_1.Sender(this);
|
|
23
23
|
}
|
|
24
24
|
async connect() {
|
|
25
25
|
this.ticker = setInterval(() => {
|
|
26
26
|
this.tick++;
|
|
27
27
|
this.emit("tick");
|
|
28
|
-
}, 50);
|
|
28
|
+
}, 50).unref();
|
|
29
29
|
this.tick++;
|
|
30
30
|
this.status = raknet_1.Status.Connecting;
|
|
31
31
|
const advertisement = await this.ping();
|
|
32
32
|
await sender_1.Sender.connect(this);
|
|
33
|
-
this.emit("connect");
|
|
34
33
|
return advertisement;
|
|
35
34
|
}
|
|
36
35
|
ping() {
|
|
@@ -42,6 +41,7 @@ class Client extends emitter_1.default {
|
|
|
42
41
|
close() {
|
|
43
42
|
this.emit("close");
|
|
44
43
|
this.socket.close();
|
|
44
|
+
// @ts-ignore
|
|
45
45
|
clearInterval(this.ticker);
|
|
46
46
|
}
|
|
47
47
|
disconnect() {
|
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
2
|
import { Frame } from "@serenityjs/raknet";
|
|
4
|
-
import type { Socket } from "node:dgram";
|
|
5
3
|
import type { Client } from "./client";
|
|
4
|
+
import { NewIncomingConnection } from "../packets";
|
|
6
5
|
declare class Receiver {
|
|
7
|
-
private readonly client;
|
|
8
|
-
private readonly socket;
|
|
9
6
|
private lastInputSequence;
|
|
10
7
|
private receivedFrameSequences;
|
|
11
8
|
private lostFrameSequences;
|
|
@@ -13,26 +10,23 @@ declare class Receiver {
|
|
|
13
10
|
private inputOrderIndex;
|
|
14
11
|
protected inputOrderingQueue: Map<number, Map<number, Frame>>;
|
|
15
12
|
protected readonly fragmentsQueue: Map<number, Map<number, Frame>>;
|
|
16
|
-
|
|
13
|
+
private client;
|
|
14
|
+
constructor(client: Client);
|
|
15
|
+
incomingMessage(buffer: Buffer): void;
|
|
16
|
+
otherPackets(buffer: Buffer): void;
|
|
17
|
+
sendUnconnectedPing(): void;
|
|
17
18
|
private tick;
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
send(buffer: Buffer): void;
|
|
20
|
+
handleFrameSet(buffer: Buffer): void;
|
|
20
21
|
private handleFrame;
|
|
21
|
-
private
|
|
22
|
-
private processOrderedFrames;
|
|
23
|
-
private handleSequenced;
|
|
22
|
+
private processFrame;
|
|
24
23
|
private handleFragment;
|
|
25
24
|
private reassembleAndProcessFragment;
|
|
26
|
-
private
|
|
25
|
+
private handleSequenced;
|
|
26
|
+
private handleOrdered;
|
|
27
|
+
private processOrderedFrames;
|
|
27
28
|
private handleConnectedPing;
|
|
28
|
-
|
|
29
|
-
private handleNackSequences;
|
|
30
|
-
private handleUnconnectedPong;
|
|
31
|
-
private handleOpenConnectionReply1;
|
|
32
|
-
private handleOpenConnectionReply2;
|
|
33
|
-
private handleValidPacket;
|
|
34
|
-
private handleSequenceGap;
|
|
35
|
-
private createReassembledFrame;
|
|
29
|
+
handleConnectionRequestAcceptedTwo(frame: Frame): NewIncomingConnection;
|
|
36
30
|
private handleConnectionRequestAccepted;
|
|
37
31
|
}
|
|
38
32
|
export { Receiver };
|
package/dist/client/receiver.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Receiver = void 0;
|
|
4
|
-
const binarystream_1 = require("@serenityjs/binarystream");
|
|
5
4
|
const raknet_1 = require("@serenityjs/raknet");
|
|
5
|
+
const binarystream_1 = require("@serenityjs/binarystream");
|
|
6
6
|
const packets_1 = require("../packets");
|
|
7
7
|
const sender_1 = require("./sender");
|
|
8
|
+
const magic = Buffer.from("00ffff00fefefefefdfdfdfd12345678", "hex");
|
|
8
9
|
class Receiver {
|
|
9
|
-
constructor(client
|
|
10
|
+
constructor(client) {
|
|
10
11
|
this.lastInputSequence = -1;
|
|
11
12
|
this.receivedFrameSequences = new Set();
|
|
12
13
|
this.lostFrameSequences = new Set();
|
|
@@ -15,55 +16,86 @@ class Receiver {
|
|
|
15
16
|
this.inputOrderingQueue = new Map();
|
|
16
17
|
this.fragmentsQueue = new Map();
|
|
17
18
|
this.client = client;
|
|
18
|
-
this.client.on("tick", () => this.tick());
|
|
19
|
-
this.socket = socket;
|
|
20
|
-
this.socket.on("message", (packet) => this.handle(packet));
|
|
21
19
|
for (let index = 0; index < 64; index++) {
|
|
22
20
|
this.inputOrderingQueue.set(index, new Map());
|
|
23
21
|
}
|
|
22
|
+
this.client.on("tick", () => {
|
|
23
|
+
this.tick();
|
|
24
|
+
});
|
|
25
|
+
this.client.socket.on("message", (...data) => this.incomingMessage(data[0]));
|
|
24
26
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
handle(packet) {
|
|
30
|
-
const packetType = packet[0];
|
|
27
|
+
incomingMessage(buffer) {
|
|
28
|
+
const packetId = buffer.readUint8();
|
|
29
|
+
const ignore = [132, 192, 128];
|
|
31
30
|
if (this.client.options.debug)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
case raknet_1.Packet.OpenConnectionReply1:
|
|
38
|
-
this.handleOpenConnectionReply1(packet);
|
|
31
|
+
if (!ignore.includes(packetId))
|
|
32
|
+
console.debug("Received Packet ID ", packetId);
|
|
33
|
+
switch (packetId) {
|
|
34
|
+
case raknet_1.Bitflags.Valid:
|
|
35
|
+
this.handleFrameSet(buffer);
|
|
39
36
|
break;
|
|
40
|
-
case raknet_1.Packet.
|
|
41
|
-
|
|
37
|
+
case raknet_1.Packet.OpenConnectionReply1: {
|
|
38
|
+
const reply = new packets_1.OpenConnectionFirstReply(buffer);
|
|
39
|
+
const deserialized = reply.deserialize();
|
|
40
|
+
this.client.emit("open-connection-first-reply", deserialized);
|
|
41
|
+
sender_1.Sender.secondRequest(this.client, deserialized);
|
|
42
42
|
break;
|
|
43
|
-
case raknet_1.Bitflags.Valid: {
|
|
44
|
-
if (this.client.options.debug)
|
|
45
|
-
console.log(`Received valid packet: ${packetType}`);
|
|
46
|
-
this.handleValidPacket(packet);
|
|
47
|
-
return;
|
|
48
43
|
}
|
|
49
|
-
case raknet_1.Packet.
|
|
50
|
-
|
|
44
|
+
case raknet_1.Packet.OpenConnectionReply2: {
|
|
45
|
+
const reply = new packets_1.OpenConnectionSecondReply(buffer);
|
|
46
|
+
const deserialized = reply.deserialize();
|
|
47
|
+
this.client.emit("open-connection-second-reply", deserialized);
|
|
48
|
+
this.client.sender.connectionRequest();
|
|
51
49
|
break;
|
|
52
50
|
}
|
|
53
|
-
default:
|
|
54
|
-
|
|
55
|
-
if ((packetId & 0xf0) === raknet_1.Bitflags.Valid) {
|
|
56
|
-
if (this.client.options.debug)
|
|
57
|
-
console.log(`Received valid packet: ${packetType}`);
|
|
58
|
-
this.handleFrameSet(packet);
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
51
|
+
default:
|
|
52
|
+
this.otherPackets(buffer);
|
|
61
53
|
if (this.client.options.debug)
|
|
62
|
-
console.debug(
|
|
54
|
+
console.debug("Received unknown packet ", packetId);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
otherPackets(buffer) {
|
|
58
|
+
const packetId = buffer.readUint8() & 0xf0;
|
|
59
|
+
switch (packetId) {
|
|
60
|
+
case raknet_1.Bitflags.Valid:
|
|
61
|
+
this.handleFrameSet(buffer);
|
|
63
62
|
break;
|
|
64
|
-
|
|
63
|
+
case raknet_1.Packet.Ack:
|
|
64
|
+
//this.ack(buffer);
|
|
65
|
+
break;
|
|
66
|
+
case raknet_1.Packet.Nack:
|
|
67
|
+
//console.debug(new Nack(buffer).deserialize());
|
|
68
|
+
break;
|
|
69
|
+
default:
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
sendUnconnectedPing() {
|
|
74
|
+
const packet = new raknet_1.UnconnectedPing();
|
|
75
|
+
packet.timestamp = BigInt(Date.now());
|
|
76
|
+
packet.magic = magic;
|
|
77
|
+
packet.client = BigInt(this.client.options.guid);
|
|
78
|
+
this.client.send(packet.serialize());
|
|
79
|
+
}
|
|
80
|
+
tick() {
|
|
81
|
+
if (this.receivedFrameSequences.size > 0) {
|
|
82
|
+
const ack = new raknet_1.Ack();
|
|
83
|
+
ack.sequences = [...this.receivedFrameSequences];
|
|
84
|
+
for (const sequence of this.receivedFrameSequences)
|
|
85
|
+
this.receivedFrameSequences.delete(sequence);
|
|
86
|
+
this.send(ack.serialize());
|
|
87
|
+
}
|
|
88
|
+
if (this.lostFrameSequences.size > 0) {
|
|
89
|
+
const nack = new raknet_1.Nack();
|
|
90
|
+
nack.sequences = [...this.lostFrameSequences];
|
|
91
|
+
for (const sequence of this.lostFrameSequences)
|
|
92
|
+
this.lostFrameSequences.delete(sequence);
|
|
93
|
+
this.send(nack.serialize());
|
|
65
94
|
}
|
|
66
95
|
}
|
|
96
|
+
send(buffer) {
|
|
97
|
+
this.client.send(buffer);
|
|
98
|
+
}
|
|
67
99
|
handleFrameSet(buffer) {
|
|
68
100
|
const frameSet = new raknet_1.FrameSet(buffer).deserialize();
|
|
69
101
|
if (this.receivedFrameSequences.has(frameSet.sequence)) {
|
|
@@ -72,22 +104,27 @@ class Receiver {
|
|
|
72
104
|
return;
|
|
73
105
|
}
|
|
74
106
|
this.lostFrameSequences.delete(frameSet.sequence);
|
|
75
|
-
if (frameSet.sequence
|
|
107
|
+
if (frameSet.sequence < this.lastInputSequence ||
|
|
108
|
+
frameSet.sequence === this.lastInputSequence) {
|
|
76
109
|
if (this.client.options.debug)
|
|
77
110
|
console.debug(`Received out of order frameset ${frameSet.sequence}!`);
|
|
78
111
|
return;
|
|
79
112
|
}
|
|
80
113
|
this.receivedFrameSequences.add(frameSet.sequence);
|
|
81
|
-
|
|
114
|
+
const diff = frameSet.sequence - this.lastInputSequence;
|
|
115
|
+
if (diff !== 1) {
|
|
116
|
+
for (let index = this.lastInputSequence + 1; index < frameSet.sequence; index++) {
|
|
117
|
+
if (!this.receivedFrameSequences.has(index)) {
|
|
118
|
+
this.lostFrameSequences.add(index);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
82
122
|
this.lastInputSequence = frameSet.sequence;
|
|
83
123
|
for (const frame of frameSet.frames) {
|
|
84
124
|
this.handleFrame(frame);
|
|
85
125
|
}
|
|
86
126
|
}
|
|
87
127
|
handleFrame(frame) {
|
|
88
|
-
if (this.client.options.debug) {
|
|
89
|
-
console.debug(`Handling frame: reliability=${frame.reliability}, orderIndex=${frame.orderIndex}, sequenceIndex=${frame.sequenceIndex}`);
|
|
90
|
-
}
|
|
91
128
|
if (frame.isSplit()) {
|
|
92
129
|
this.handleFragment(frame);
|
|
93
130
|
}
|
|
@@ -101,47 +138,31 @@ class Receiver {
|
|
|
101
138
|
this.processFrame(frame);
|
|
102
139
|
}
|
|
103
140
|
}
|
|
104
|
-
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
this.processFrame(frame);
|
|
117
|
-
this.inputOrderIndex[frame.orderChannel]++;
|
|
118
|
-
let nextOrderIndex = this.inputOrderIndex[frame.orderChannel];
|
|
119
|
-
while (orderingQueue.has(nextOrderIndex)) {
|
|
120
|
-
const nextFrame = orderingQueue.get(nextOrderIndex);
|
|
121
|
-
if (nextFrame) {
|
|
122
|
-
this.processFrame(nextFrame);
|
|
123
|
-
orderingQueue.delete(nextOrderIndex);
|
|
124
|
-
this.inputOrderIndex[frame.orderChannel]++;
|
|
125
|
-
nextOrderIndex++;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
handleSequenced(frame) {
|
|
130
|
-
const currentHighestSequence = this.inputHighestSequenceIndex[frame.orderChannel];
|
|
131
|
-
if (frame.sequenceIndex > currentHighestSequence) {
|
|
132
|
-
this.inputHighestSequenceIndex[frame.orderChannel] = frame.sequenceIndex;
|
|
133
|
-
this.processFrame(frame);
|
|
134
|
-
}
|
|
135
|
-
else {
|
|
136
|
-
if (this.client.options.debug)
|
|
137
|
-
console.debug(`Discarding old sequenced frame: ${frame.sequenceIndex}`);
|
|
141
|
+
processFrame(frame) {
|
|
142
|
+
const header = frame.payload[0];
|
|
143
|
+
switch (header) {
|
|
144
|
+
case raknet_1.Packet.ConnectionRequestAccepted:
|
|
145
|
+
this.handleConnectionRequestAccepted(frame);
|
|
146
|
+
break;
|
|
147
|
+
case raknet_1.Packet.ConnectedPing:
|
|
148
|
+
this.handleConnectedPing(frame.payload);
|
|
149
|
+
break;
|
|
150
|
+
default:
|
|
151
|
+
this.client.emit("encapsulated", frame);
|
|
152
|
+
break;
|
|
138
153
|
}
|
|
139
154
|
}
|
|
140
155
|
handleFragment(frame) {
|
|
141
|
-
|
|
142
|
-
|
|
156
|
+
if (!this.fragmentsQueue.has(frame.splitId)) {
|
|
157
|
+
this.fragmentsQueue.set(frame.splitId, new Map());
|
|
158
|
+
}
|
|
159
|
+
const fragment = this.fragmentsQueue.get(frame.splitId);
|
|
160
|
+
if (!fragment)
|
|
161
|
+
return;
|
|
143
162
|
fragment.set(frame.splitIndex, frame);
|
|
144
163
|
if (fragment.size === frame.splitSize) {
|
|
164
|
+
if (this.client.options.debug)
|
|
165
|
+
console.debug(`Reassembling complete frame from fragments: splitId=${frame.splitId}`);
|
|
145
166
|
this.reassembleAndProcessFragment(frame, fragment);
|
|
146
167
|
}
|
|
147
168
|
}
|
|
@@ -157,115 +178,114 @@ class Receiver {
|
|
|
157
178
|
return;
|
|
158
179
|
}
|
|
159
180
|
}
|
|
160
|
-
const reassembledFrame =
|
|
181
|
+
const reassembledFrame = new raknet_1.Frame();
|
|
182
|
+
reassembledFrame.reliability = frame.reliability;
|
|
183
|
+
reassembledFrame.reliableIndex = frame.reliableIndex;
|
|
184
|
+
reassembledFrame.sequenceIndex = frame.sequenceIndex;
|
|
185
|
+
reassembledFrame.orderIndex = frame.orderIndex;
|
|
186
|
+
reassembledFrame.orderChannel = frame.orderChannel;
|
|
187
|
+
reassembledFrame.payload = stream.getBuffer();
|
|
161
188
|
this.fragmentsQueue.delete(frame.splitId);
|
|
162
189
|
this.handleFrame(reassembledFrame);
|
|
163
190
|
}
|
|
164
|
-
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
191
|
+
handleSequenced(frame) {
|
|
192
|
+
const currentHighestSequence = this.inputHighestSequenceIndex[frame.orderChannel];
|
|
193
|
+
if (this.client.options.debug)
|
|
194
|
+
console.debug(`Handling sequenced frame: sequenceIndex=${frame.sequenceIndex}, currentHighest=${currentHighestSequence}`);
|
|
195
|
+
if (frame.sequenceIndex > currentHighestSequence) {
|
|
196
|
+
this.inputHighestSequenceIndex[frame.orderChannel] = frame.sequenceIndex;
|
|
197
|
+
this.processFrame(frame);
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
if (this.client.options.debug)
|
|
201
|
+
console.debug(`Discarding old sequenced frame: ${frame.sequenceIndex}`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
handleOrdered(frame) {
|
|
205
|
+
if (this.client.options.debug)
|
|
206
|
+
console.debug(`Handling ordered frame: orderIndex=${frame.orderIndex}, channel=${frame.orderChannel}`);
|
|
207
|
+
const expectedOrderIndex = this.inputOrderIndex[frame.orderChannel];
|
|
208
|
+
const outOfOrderQueue = this.inputOrderingQueue.get(frame.orderChannel);
|
|
209
|
+
if (frame.orderIndex === expectedOrderIndex) {
|
|
210
|
+
this.processOrderedFrames(frame, outOfOrderQueue);
|
|
211
|
+
}
|
|
212
|
+
else if (frame.orderIndex > expectedOrderIndex) {
|
|
213
|
+
if (this.client.options.debug)
|
|
214
|
+
console.debug(`Queuing out-of-order frame: ${frame.orderIndex}`);
|
|
215
|
+
outOfOrderQueue.set(frame.orderIndex, frame);
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
if (this.client.options.debug)
|
|
219
|
+
console.debug(`Discarding old frame: ${frame.orderIndex}`);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
processOrderedFrames(frame, outOfOrderQueue) {
|
|
223
|
+
this.processFrame(frame);
|
|
224
|
+
this.inputOrderIndex[frame.orderChannel]++;
|
|
225
|
+
let nextOrderIndex = this.inputOrderIndex[frame.orderChannel];
|
|
226
|
+
while (outOfOrderQueue.has(nextOrderIndex)) {
|
|
227
|
+
const nextFrame = outOfOrderQueue.get(nextOrderIndex);
|
|
228
|
+
if (nextFrame) {
|
|
184
229
|
if (this.client.options.debug)
|
|
185
|
-
console.debug(`
|
|
186
|
-
|
|
230
|
+
console.debug(`Processing queued frame: ${nextOrderIndex}`);
|
|
231
|
+
this.processFrame(nextFrame);
|
|
232
|
+
outOfOrderQueue.delete(nextOrderIndex);
|
|
233
|
+
this.inputOrderIndex[frame.orderChannel]++;
|
|
234
|
+
nextOrderIndex++;
|
|
187
235
|
}
|
|
188
236
|
}
|
|
189
237
|
}
|
|
190
|
-
handleConnectedPing(
|
|
191
|
-
const packet = new packets_1.ConnectedPing(
|
|
238
|
+
handleConnectedPing(buffer) {
|
|
239
|
+
const packet = new packets_1.ConnectedPing(buffer);
|
|
192
240
|
const deserializedPacket = packet.deserialize();
|
|
193
|
-
const pong = new
|
|
194
|
-
pong.
|
|
195
|
-
pong.
|
|
241
|
+
const pong = new packets_1.ConnectedPong();
|
|
242
|
+
pong.pingTime = deserializedPacket.timestamp;
|
|
243
|
+
pong.pongTime = BigInt(Date.now());
|
|
196
244
|
const frame = new raknet_1.Frame();
|
|
197
245
|
frame.reliability = raknet_1.Reliability.Unreliable;
|
|
198
246
|
frame.orderChannel = 0;
|
|
199
247
|
frame.payload = pong.serialize();
|
|
200
248
|
this.client.sender.sendFrame(frame, raknet_1.Priority.Immediate);
|
|
201
249
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
handleNackSequences() {
|
|
211
|
-
if (this.lostFrameSequences.size > 0) {
|
|
212
|
-
const nack = new raknet_1.Nack();
|
|
213
|
-
nack.sequences = [...this.lostFrameSequences];
|
|
214
|
-
this.lostFrameSequences.clear();
|
|
215
|
-
this.client.send(nack.serialize());
|
|
216
|
-
}
|
|
250
|
+
handleConnectionRequestAcceptedTwo(frame) {
|
|
251
|
+
const des = new packets_1.ConnectionRequestAccepted(frame.payload).deserialize();
|
|
252
|
+
const packet = new packets_1.NewIncomingConnection();
|
|
253
|
+
packet.internalAddresses = new Array(10).fill(new raknet_1.Address("0.0.0.0", 0, 4));
|
|
254
|
+
packet.serverAddress = des.address;
|
|
255
|
+
packet.incomingTimestamp = BigInt(Date.now());
|
|
256
|
+
packet.serverTimestamp = des.timestamp;
|
|
257
|
+
return packet;
|
|
217
258
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
}
|
|
222
|
-
handleOpenConnectionReply1(packet) {
|
|
223
|
-
const reply = new packets_1.OpenConnectionFirstReply(packet);
|
|
224
|
-
const deserialized = reply.deserialize();
|
|
225
|
-
this.client.emit("open-connection-first-reply", deserialized);
|
|
226
|
-
sender_1.Sender.secondRequest(this.client, deserialized);
|
|
227
|
-
}
|
|
228
|
-
handleOpenConnectionReply2(packet) {
|
|
229
|
-
const reply = new packets_1.OpenConnectionSecondReply(packet);
|
|
230
|
-
const deserialized = reply.deserialize();
|
|
231
|
-
this.client.emit("open-connection-second-reply", deserialized);
|
|
232
|
-
this.client.sender.connectionRequest();
|
|
233
|
-
}
|
|
234
|
-
handleValidPacket(packet) {
|
|
259
|
+
handleConnectionRequestAccepted(frame) {
|
|
260
|
+
let des;
|
|
261
|
+
let packet;
|
|
235
262
|
try {
|
|
236
|
-
const
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
263
|
+
const IncomingPacket = new packets_1.ConnectionRequestAccepted(frame.payload);
|
|
264
|
+
des = IncomingPacket.deserialize();
|
|
265
|
+
packet = new packets_1.NewIncomingConnection();
|
|
266
|
+
packet.internalAddresses = new Array(10).fill(new raknet_1.Address("0.0.0.0", 0, 4));
|
|
267
|
+
packet.serverAddress = new raknet_1.Address(des.address.address, des.address.port, 4);
|
|
268
|
+
packet.incomingTimestamp = BigInt(Date.now());
|
|
269
|
+
packet.serverTimestamp = des.timestamp;
|
|
240
270
|
}
|
|
241
271
|
catch (error) {
|
|
242
|
-
|
|
272
|
+
packet = this.handleConnectionRequestAcceptedTwo(frame);
|
|
243
273
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
if (diff !== 1) {
|
|
248
|
-
for (let index = this.lastInputSequence + 1; index < currentSequence; index++) {
|
|
249
|
-
if (!this.receivedFrameSequences.has(index)) {
|
|
250
|
-
this.lostFrameSequences.add(index);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
274
|
+
if (!packet) {
|
|
275
|
+
console.error("Failed to deserialize IncomingPacket!");
|
|
276
|
+
return;
|
|
253
277
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
handleConnectionRequestAccepted(frame) {
|
|
266
|
-
const packet = new packets_1.ConnectionRequestAccepted(frame.payload);
|
|
267
|
-
const deserialized = packet.deserialize();
|
|
268
|
-
this.client.sender.newIncommingConnection(frame.payload, deserialized);
|
|
278
|
+
const sendFrame = new raknet_1.Frame();
|
|
279
|
+
sendFrame.reliability = raknet_1.Reliability.ReliableOrdered;
|
|
280
|
+
sendFrame.orderChannel = 0;
|
|
281
|
+
sendFrame.payload = packet.serialize();
|
|
282
|
+
if (!sendFrame.payload) {
|
|
283
|
+
console.error("Failed to serialize the packet!");
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
this.client.status = raknet_1.Status.Connected;
|
|
287
|
+
this.client.sender.sendFrame(sendFrame, raknet_1.Priority.Immediate);
|
|
288
|
+
void this.client.emit("connect");
|
|
269
289
|
}
|
|
270
290
|
}
|
|
271
291
|
exports.Receiver = Receiver;
|
package/dist/client/sender.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { Frame, FrameSet, Priority } from "@serenityjs/raknet";
|
|
3
3
|
import type { Client } from "./client";
|
|
4
|
-
import { type OpenConnectionFirstReply
|
|
4
|
+
import { type OpenConnectionFirstReply } from "../packets";
|
|
5
5
|
import { type Advertisement } from "./types/Advertisement";
|
|
6
6
|
declare class Sender {
|
|
7
7
|
private readonly client;
|
|
@@ -22,7 +22,7 @@ declare class Sender {
|
|
|
22
22
|
private createSplitFrame;
|
|
23
23
|
private queueFrame;
|
|
24
24
|
sendQueue(amount: number): void;
|
|
25
|
-
newIncommingConnection(payload: Buffer
|
|
25
|
+
newIncommingConnection(payload: Buffer): void;
|
|
26
26
|
connectionRequest(): void;
|
|
27
27
|
static secondRequest(client: Client, reply1: OpenConnectionFirstReply): void;
|
|
28
28
|
static ping(client: Client): Promise<Advertisement>;
|
package/dist/client/sender.js
CHANGED
|
@@ -12,17 +12,19 @@ class Sender {
|
|
|
12
12
|
this.outputReliableIndex = 0;
|
|
13
13
|
this.outputFrames = new Set();
|
|
14
14
|
this.outputBackup = new Map();
|
|
15
|
-
this.outputOrderIndex = Array(32).fill(0);
|
|
16
|
-
this.outputSequenceIndex = Array(32).fill(0);
|
|
17
15
|
this.outputFrameQueue = new raknet_1.FrameSet();
|
|
18
|
-
this.mtu = client.options.mtu;
|
|
19
16
|
this.outputFrameQueue.frames = [];
|
|
17
|
+
this.outputOrderIndex = Array.from({ length: 32 }).fill(0);
|
|
18
|
+
this.outputSequenceIndex = Array.from({ length: 32 }).fill(0);
|
|
19
|
+
this.mtu = client.options.mtu;
|
|
20
20
|
this.client.on("tick", () => this.tick());
|
|
21
21
|
}
|
|
22
22
|
tick() {
|
|
23
23
|
if (this.client.status === raknet_1.Status.Disconnecting ||
|
|
24
|
-
this.client.status === raknet_1.Status.Disconnected)
|
|
24
|
+
this.client.status === raknet_1.Status.Disconnected) {
|
|
25
|
+
console.log("Can not send queue, client is disconnecting or disconnected");
|
|
25
26
|
return;
|
|
27
|
+
}
|
|
26
28
|
this.sendQueue(this.outputFrames.size);
|
|
27
29
|
}
|
|
28
30
|
sendFrame(frame, priority) {
|
|
@@ -98,40 +100,48 @@ class Sender {
|
|
|
98
100
|
this.outputFrames.delete(frame);
|
|
99
101
|
this.client.send(frameset.serialize());
|
|
100
102
|
}
|
|
101
|
-
newIncommingConnection(payload
|
|
102
|
-
|
|
103
|
-
|
|
103
|
+
newIncommingConnection(payload) {
|
|
104
|
+
let des = null;
|
|
105
|
+
let packet;
|
|
106
|
+
try {
|
|
107
|
+
const IncomingPacket = new packets_1.ConnectionRequestAccepted(payload);
|
|
108
|
+
des = IncomingPacket.deserialize();
|
|
109
|
+
packet = new packets_1.NewIncomingConnection();
|
|
104
110
|
packet.internalAddresses = new Array(10).fill(new raknet_1.Address("0.0.0.0", 0, 4));
|
|
105
|
-
packet.serverAddress = new raknet_1.Address(
|
|
111
|
+
packet.serverAddress = new raknet_1.Address(des.address.address, des.address.port, 4);
|
|
106
112
|
packet.incomingTimestamp = BigInt(Date.now());
|
|
107
|
-
packet.serverTimestamp =
|
|
113
|
+
packet.serverTimestamp = des.timestamp;
|
|
108
114
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
packet
|
|
115
|
+
catch (error) {
|
|
116
|
+
const _des = new packets_1.ConnectionRequestAccepted(payload).deserialize();
|
|
117
|
+
packet = new packets_1.NewIncomingConnection();
|
|
118
|
+
packet.internalAddresses = new Array(20).fill(new raknet_1.Address("0.0.0.0", 0, 4));
|
|
119
|
+
packet.serverAddress = new raknet_1.Address("0.0.0.0", 0, 4);
|
|
112
120
|
packet.incomingTimestamp = BigInt(Date.now());
|
|
113
|
-
packet.serverTimestamp =
|
|
121
|
+
packet.serverTimestamp = BigInt(0);
|
|
114
122
|
}
|
|
123
|
+
if (!packet) {
|
|
124
|
+
console.error("Failed to deserialize IncomingPacket!");
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
this.client.status = raknet_1.Status.Connected;
|
|
115
128
|
this.client.sender.frameAndSend(packet.serialize(), raknet_1.Priority.Immediate);
|
|
129
|
+
void this.client.emit("connect");
|
|
116
130
|
}
|
|
117
131
|
connectionRequest() {
|
|
118
132
|
const packet = new packets_1.ConnectionRequest();
|
|
119
133
|
packet.client = BigInt(this.client.options.guid);
|
|
120
134
|
packet.timestamp = BigInt(Date.now());
|
|
121
135
|
packet.security = false;
|
|
122
|
-
|
|
123
|
-
frame.reliability = raknet_1.Reliability.Reliable;
|
|
124
|
-
frame.orderChannel = 0;
|
|
125
|
-
frame.payload = packet.serialize();
|
|
126
|
-
this.sendFrame(frame, raknet_1.Priority.Immediate);
|
|
136
|
+
this.frameAndSend(packet.serialize(), raknet_1.Priority.Immediate);
|
|
127
137
|
}
|
|
128
138
|
static secondRequest(client, reply1) {
|
|
129
|
-
const
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
client.send(
|
|
139
|
+
const pak = new packets_1.OpenConnectionSecondRequest();
|
|
140
|
+
pak.mtu = client.options.mtu;
|
|
141
|
+
pak.client = client.options.guid;
|
|
142
|
+
pak.magic = reply1.magic;
|
|
143
|
+
pak.address = new raknet_1.Address(client.socket.address().address, client.socket.address().port, 4);
|
|
144
|
+
client.send(pak.serialize());
|
|
135
145
|
}
|
|
136
146
|
static async ping(client) {
|
|
137
147
|
return new Promise((resolve, reject) => {
|
|
@@ -161,7 +171,6 @@ class Sender {
|
|
|
161
171
|
packet.magic = new raknet_1.Magic();
|
|
162
172
|
packet.mtu = client.options.mtu;
|
|
163
173
|
packet.protocol = client.options.protocol;
|
|
164
|
-
client.send(packet.serialize());
|
|
165
174
|
const timer = setTimeout(() => {
|
|
166
175
|
reject(new Error(`Connection request has timed out after ${client.options.timeout}ms.`));
|
|
167
176
|
client.socket.off("message", listener);
|
|
@@ -175,6 +184,7 @@ class Sender {
|
|
|
175
184
|
}
|
|
176
185
|
};
|
|
177
186
|
client.socket.on("message", listener);
|
|
187
|
+
client.send(packet.serialize());
|
|
178
188
|
});
|
|
179
189
|
}
|
|
180
190
|
}
|
package/dist/tools/test.js
CHANGED