-
Notifications
You must be signed in to change notification settings - Fork 33
/
SimConnectPacketBuilder.ts
89 lines (73 loc) · 2.43 KB
/
SimConnectPacketBuilder.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
import { RawBuffer } from './RawBuffer';
import { Protocol } from './enums/Protocol';
export class SimConnectPacketBuilder {
private readonly packetContent: RawBuffer;
constructor(packetTypeId: number, protocol: Protocol, packetDataBuffer?: RawBuffer) {
packetDataBuffer?.clear(); // Prepare for new message
/**
* Packet header content (16 bytes):
* 0-3 packet size (set later)
* 4-7 protocol
* 8-11 packet type / SimConnect function
* 12-15 packet id (set later)
*/
this.packetContent = packetDataBuffer || new RawBuffer(256);
this.packetContent.writeInt32(protocol, 4);
this.packetContent.writeInt32(0xf0000000 | packetTypeId, 8);
this.packetContent.setOffset(16);
}
putFloat32(value: number) {
this.packetContent.writeFloat32(value);
return this;
}
putFloat64(value: number) {
this.packetContent.writeFloat64(value);
return this;
}
putString(value: string, fixedLength?: number) {
this.packetContent.writeString(value, fixedLength);
return this;
}
putString256(value: string | null) {
this.packetContent.writeString256(value);
return this;
}
putInt16(value: number, offset?: number) {
this.packetContent.writeInt16(value, offset);
return this;
}
putInt32(value: number, offset?: number) {
this.packetContent.writeInt32(value, offset);
return this;
}
putUint32(value: number, offset?: number) {
this.packetContent.writeUint32(value, offset);
return this;
}
putUint64(value: Long, offset?: number) {
this.packetContent.writeUint64(value, offset);
return this;
}
putByte(value: number) {
this.packetContent.writeByte(value);
return this;
}
putBytes(value: Buffer) {
this.packetContent.write(value);
return this;
}
getRawBuffer(): RawBuffer {
return this.packetContent;
}
/**
* Finalize
* @param sendId - of packet (can be used to identify packet when exception event occurs)
*/
build(sendId: number): Buffer {
const packetSize = this.packetContent.getOffset();
// Finish packet header
this.packetContent.writeInt32(packetSize, 0);
this.packetContent.writeInt32(sendId, 12);
return this.packetContent.getBuffer();
}
}