Skip to content

Commit 4f7216d

Browse files
committed
bug fix
1 parent d60b43b commit 4f7216d

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sanctumterra/raknet",
3-
"version": "1.3.66",
3+
"version": "1.3.67",
44
"description": "",
55
"main": "dist/index.js",
66
"scripts": {

src/client/client.ts

+28-7
Original file line numberDiff line numberDiff line change
@@ -81,45 +81,66 @@ export class Client extends Emitter<ClientEvents> {
8181
this.status = Status.Connecting;
8282
this.initSocket();
8383

84-
this.timer = setInterval(() => this.emit("tick"), 20);
84+
this.timer = setInterval(() => this.emit("tick"), 50);
8585

8686
try {
8787
const advertisement = await this.ping();
8888
if (!advertisement) throw new Error("Failed to get server advertisement");
8989

9090
return new Promise((resolve, reject) => {
9191
let isResolved = false;
92+
let shouldContinueSending = true;
9293

93-
const connectionTimeout = setTimeout(() => {
94+
const cleanup = () => {
95+
clearTimeout(connectionTimeout);
96+
clearInterval(requestInterval);
9497
if (!isResolved) {
98+
Logger.error("Could not resolve connection.");
9599
this.cleanup();
96-
reject(new Error("Connection timed out"));
97100
}
101+
};
102+
103+
const connectionTimeout = setTimeout(() => {
104+
cleanup();
105+
reject(new Error("Connection timed out"));
98106
}, this.options.timeout);
99107

100108
const request = new OpenConnectionRequestOne();
101109
request.mtu = this.options.mtuSize;
102110
request.protocol = this.options.protocolVersion;
103111

112+
this.on("open-connection-reply-two", (packet) => {
113+
const mtu = packet.mtu;
114+
if (mtu > 400 && mtu < 1500) {
115+
shouldContinueSending = false;
116+
} else {
117+
cleanup();
118+
reject(new Error(`Invalid MTU size: ${mtu}`));
119+
}
120+
});
121+
104122
this.emit("open-connection-request-one", request);
105123
this.send(request.serialize());
106124

107125
const requestInterval = setInterval(() => {
108-
if (!isResolved) {
126+
if (!isResolved && shouldContinueSending) {
109127
this.send(request.serialize());
110128
}
111-
}, 20);
129+
}, 50);
112130

113131
this.onceAfter("new-incoming-connection", () => {
114132
if (!isResolved) {
115-
clearTimeout(connectionTimeout);
116-
clearInterval(requestInterval);
117133
isResolved = true;
118134
this.emit("connect");
119135
this.status = Status.Connected;
120136
resolve(advertisement);
121137
}
122138
});
139+
140+
this.once("error", (error) => {
141+
cleanup();
142+
reject(error);
143+
});
123144
});
124145
} catch (error) {
125146
this.status = Status.Disconnected;

src/client/framer.ts

-27
Original file line numberDiff line numberDiff line change
@@ -135,40 +135,13 @@ export class Framer {
135135
const packet = new OpenConnectionReplyTwo(payload).deserialize();
136136
this.client.emit("open-connection-reply-two", packet);
137137
this.client.options.mtuSize = packet.mtu;
138-
139138
const conReq = new ConnectionRequest();
140139
conReq.clientGuid = this.client.options.clientId;
141140
conReq.timestamp = BigInt(Date.now());
142141
conReq.useSecurity = false;
143142

144143
this.client.emit("connection-request", conReq);
145144
this.client.framer.frameAndSend(conReq.serialize(), Priority.Immediate);
146-
147-
let connectionAttempts = 1;
148-
const maxAttempts = 3;
149-
150-
const connectionInterval = setInterval(() => {
151-
if (connectionAttempts >= maxAttempts) {
152-
clearInterval(connectionInterval);
153-
this.client.cleanup();
154-
this.client.emit(
155-
"error",
156-
new Error("Connection request timed out"),
157-
);
158-
return;
159-
}
160-
161-
this.client.framer.frameAndSend(
162-
conReq.serialize(),
163-
Priority.Immediate,
164-
);
165-
connectionAttempts++;
166-
}, 30);
167-
168-
this.client.once("new-incoming-connection", () => {
169-
clearInterval(connectionInterval);
170-
});
171-
172145
break;
173146
}
174147
case Packet.FrameSet: {

0 commit comments

Comments
 (0)