-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsocket.ts
336 lines (260 loc) · 9.89 KB
/
socket.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
namespace multiplayer {
export enum SocketState {
LookingForPlayer,
Connecting,
Connected,
Disconnected,
}
export enum SocketMessages {
Status = 0,
ConnectRequest = 1,
ConnectResponse = 2,
Custom = 3
}
export class Session {
constructor(public readonly id: number, public readonly playerNumber: number) { }
}
export class SocketPacket {
public pointer:number = 9;
constructor(public data?: Buffer) {
if (!this.data) this.data = control.createBuffer(9);
}
get toString(): string {
return this.data.toHex();
}
public add8(val: number) {
const buff = control.createBuffer(1);
buff.setNumber(NumberFormat.Int8LE, 0, val)
this.data = this.data.concat(buff)
}
public add16( val:number ){
const buff = control.createBuffer(2);
buff.setNumber(NumberFormat.Int16LE, 0, val)
this.data = this.data.concat(buff)
}
public add32(val: number) {
const buff = control.createBuffer(4);
buff.setNumber(NumberFormat.Int32LE, 0, val)
this.data = this.data.concat(buff)
}
public get8(){
const v = this.data.getNumber(NumberFormat.Int8LE, this.pointer);
this.pointer+=1;
return v;
}
public get16() {
const v = this.data.getNumber(NumberFormat.Int16LE, this.pointer);
this.pointer += 2;
return v;
}
public get32() {
const v = this.data.getNumber(NumberFormat.Int32LE, this.pointer);
this.pointer += 4;
return v;
}
get messageType(): SocketMessages {
return this.data[0];
}
set messageType(msgType: SocketMessages) {
this.data[0] = msgType;
}
get senderState(): SocketState {
return this.data[1];
}
set senderState(state: SocketState) {
this.data[1] = state;
}
get senderPlayerNumber() {
return this.data[2];
}
set senderPlayerNumber(num: number) {
this.data[2] = num;
}
get sessionId() {
return this.data.getNumber(NumberFormat.UInt32LE, 3);
}
set sessionId(id: number) {
this.data.setNumber(NumberFormat.UInt32LE, 3, id);
}
get arg1() {
return this.data.getNumber(NumberFormat.Int16LE, 7);
}
set arg1(val: number) {
this.data.setNumber(NumberFormat.Int16LE, 7, val);
}
get arg2() {
return this.data.getNumber(NumberFormat.Int16LE, 9)
}
set arg2(val: number) {
this.data.setNumber(NumberFormat.Int16LE, 9, val);
}
get arg3() {
return this.data.getNumber(NumberFormat.Int16LE, 11)
}
set arg3(val: number) {
this.data.setNumber(NumberFormat.Int16LE, 11, val);
}
get arg4() {
return this.data.getNumber(NumberFormat.Int16LE, 13)
}
set arg4(val: number) {
this.data.setNumber(NumberFormat.Int16LE, 13, val);
}
get arg5() {
return this.data.getNumber(NumberFormat.Int16LE, 15)
}
set arg5(val: number) {
this.data.setNumber(NumberFormat.Int16LE, 15, val);
}
get arg6() {
return this.data.getNumber(NumberFormat.Int16LE, 17)
}
set arg6(val: number) {
this.data.setNumber(NumberFormat.Int16LE, 17, val);
}
get arg7() {
return this.data.getNumber(NumberFormat.Int16LE, 19)
}
set arg7(val: number) {
this.data.setNumber(NumberFormat.Int16LE, 19, val);
}
get arg8() {
return this.data.getNumber(NumberFormat.Int16LE, 21)
}
set arg8(val: number) {
this.data.setNumber(NumberFormat.Int16LE, 21, val);
}
get arg9_32() {
return this.data.getNumber(NumberFormat.UInt32LE, 23)
}
set arg9_32(val: number) {
this.data.setNumber(NumberFormat.UInt32LE, 23, val);
}
get arg10_32() {
return this.data.getNumber(NumberFormat.UInt32LE, 27)
}
set arg10_32(val: number) {
this.data.setNumber(NumberFormat.UInt32LE, 27, val);
}
}
export class Socket extends jacdac.Broadcast {
private static instance: Socket;
public static getInstance() {
if (!Socket.instance) Socket.instance = new Socket();
return Socket.instance;
}
public session: Session;
protected state: SocketState;
protected serverStartTime: number;
protected lastReceivedTime: number;
protected intervalRef: number;
protected messageListener: (pkt: SocketPacket) => void;
protected connectListener: () => void;
protected disconnectListener: () => void;
private constructor(protected pingInterval = 500, protected timeout = 1000) {
super("socket", jacdac.CONTROLLER_DEVICE_CLASS, 5);
this.state = SocketState.LookingForPlayer;
this.restartPingInterval();
}
handlePacket(jd: jacdac.JDPacket): number {
this.handleMessage(getDevice(jd), new SocketPacket(jd.data));
return jacdac.DEVICE_OK;
}
setPingInteval(millis: number) {
this.pingInterval = millis;
this.restartPingInterval();
}
protected sendMessage(messageType: SocketMessages, message?: SocketPacket) {
message = message || new SocketPacket();
message.messageType = messageType;
message.senderState = this.state;
if (this.session) {
message.sessionId = this.session.id;
message.senderPlayerNumber = this.session.playerNumber;
}
this.sendPacket(message.data);
}
onConnect(handler: () => void) {
this.connectListener = handler;
if (this.state === SocketState.Connected) this.connectListener();
}
onDisconnect(handler: () => void) {
this.disconnectListener = handler;
}
onMessage(handler: (pkt: SocketPacket) => void) {
this.messageListener = handler;
}
serverTime() {
return control.millis() - this.serverStartTime;
}
sendCustomMessage(message: SocketPacket) {
this.sendMessage(SocketMessages.Custom, message);
}
protected restartPingInterval() {
if (this.intervalRef !== undefined) clearInterval(this.intervalRef);
this.intervalRef = setInterval(() => {
if (this.state === SocketState.Connected && control.millis() - this.lastReceivedTime > (this.timeout)*2 ) {
this.state = SocketState.Disconnected;
if (this.disconnectListener) this.disconnectListener();
}
this.sendMessage(SocketMessages.Status);
if (this.state === SocketState.Connecting) {
this.sendMessage(SocketMessages.ConnectRequest);
}
}, this.pingInterval);
}
protected handleMessage(sender: jacdac.JDDevice, packet: SocketPacket) {
if (this.session && packet.sessionId != this.session.id && this.state != SocketState.Connecting) {
console.log("Ignoring packet")
return;
}
console.log("Got Packet: " + packet.arg1);
this.lastReceivedTime = control.millis();
if (this.state === SocketState.Disconnected) {
this.startConnection();
}
else if (this.state === SocketState.LookingForPlayer && packet.senderState === SocketState.LookingForPlayer) {
if (sender && sender.device_address < this.device.device_address) {
this.tryToConnect();
}
else {
console.log(`skip conn ${sender.udidl}<${this.device.udidl}|${sender.udidh}<${this.device.udidh}`)
}
}
switch (packet.messageType) {
case SocketMessages.ConnectResponse:
this.startConnection();
break;
case SocketMessages.ConnectRequest:
this.handleConnectionRequest(packet);
break;
case SocketMessages.Custom:
if (this.messageListener) this.messageListener(packet);
}
}
protected tryToConnect() {
console.log("Sending Connect Request")
this.state = SocketState.Connecting;
// Generate a new session
this.session = new Session(Math.randomRange(1, 9999999) | 0, Math.randomRange(1, 2));
this.sendMessage(SocketMessages.ConnectRequest);
}
protected handleConnectionRequest(request: SocketPacket) {
console.log("Got Connect Request")
if (this.state !== SocketState.LookingForPlayer) return;
this.session = new Session(request.sessionId, request.senderPlayerNumber === 1 ? 2 : 1);
this.sendMessage(SocketMessages.ConnectResponse);
this.startConnection();
}
protected startConnection() {
if (this.state === SocketState.Connected) return;
console.log("Connected")
this.serverStartTime = control.millis();
this.state = SocketState.Connected;
if (this.connectListener) this.connectListener();
}
}
function getDevice(packet: jacdac.JDPacket) {
return jacdac.devices().find(d => d.device_address === packet.device_address);
}
}