-
Notifications
You must be signed in to change notification settings - Fork 0
/
RakClient.ts
101 lines (80 loc) · 2.5 KB
/
RakClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import {ReliabilityLayer} from "./ReliabilityLayer";
import * as data from 'dgram';
import * as events from "events";
import BitStream from "./structures/BitStream";
import RakMessages from "./RakMessages";
export default class RakClient extends events.EventEmitter {
#ip : string;
#port : number;
#connection : ReliabilityLayer;
#password : string;
readonly #client : data.Socket;
#startTime : number;
constructor(ip : string, port : number, password : string) {
super();
this.#ip = ip;
this.#port = port;
this.#password = password;
this.#client = data.createSocket('udp4');
this.#startTime = Date.now();
this.#client.on('error', (err) => {
this.onError(err);
});
this.#client.on('message', (msg, senderInfo) => {
let data = new BitStream(msg);
try {
this.onMessage(data, senderInfo)
}
catch(e) {
console.warn(`Something went wrong while handling packet! ${e.message}`);
console.warn(e.stack);
}
});
this.#connection = new ReliabilityLayer(this.#client, {port: this.#port, address: this.#ip});
}
onError(error) {
console.log(`client error:\n${error.stack}`);
}
onMessage(data, senderInfo) {
if(data.length() !== 1) {
const packets = this.#connection.handle_data(data);
let finished = false;
while(!finished) {
let next = packets.next();
if(next.value !== undefined) {
let packet = next.value;
this.onPacket(packet, senderInfo);
}
if(next.done) {
finished = true;
}
}
}
}
onPacket(packet : BitStream, senderInfo : Object) : void {
let type = packet.readByte();
if(this.listenerCount(String(type)) > 0) {
this.emit(String(type), packet, senderInfo);
} else {
console.log(`No listeners found for ID: ${RakMessages.key(type)} (${type})`);
}
}
getServer() {
return this.#connection;
}
get client() {
return this.#client;
}
get port() : number {
return this.#port;
}
get ip() : string {
return this.#ip;
}
get password() : string {
return this.#password;
}
get startTime() : number {
return this.#startTime;
}
}