/
TcpConnection.ts
317 lines (270 loc) · 9.81 KB
/
TcpConnection.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
import { MessageDefinition } from "@foxglove/message-definition";
import { parse as parseMessageDefinition } from "@foxglove/rosmsg";
import { MessageReader } from "@foxglove/rosmsg-serialization";
import { EventEmitter } from "eventemitter3";
import { Connection, ConnectionStats } from "./Connection";
import { LoggerService } from "./LoggerService";
import { RosTcpMessageStream } from "./RosTcpMessageStream";
import { TcpAddress, TcpSocket } from "./TcpTypes";
import { backoff } from "./backoff";
export interface TcpConnectionEvents {
header: (
header: Map<string, string>,
messageDefinition: MessageDefinition[],
messageReader: MessageReader,
) => void;
message: (msg: unknown, msgData: Uint8Array) => void;
error: (err: Error) => void;
}
// Implements a subscriber for the TCPROS transport. The actual TCP transport is
// implemented in the passed in `socket` (TcpSocket). A transform stream is used
// internally for parsing the TCPROS message format (4 byte length followed by
// message payload) so "message" events represent one full message each without
// the length prefix. A transform class that meets this requirements is
// implemented in `RosTcpMessageStream`.
export class TcpConnection extends EventEmitter<TcpConnectionEvents> implements Connection {
retries = 0;
private _socket: TcpSocket;
private _address: string;
private _port: number;
private _connected = false;
private _shutdown = false;
private _transportInfo = "TCPROS not connected [socket -1]";
private _readingHeader = true;
private _requestHeader: Map<string, string>;
private _header = new Map<string, string>();
private _stats = {
bytesSent: 0,
bytesReceived: 0,
messagesSent: 0,
messagesReceived: 0,
dropEstimate: -1,
};
private _transformer = new RosTcpMessageStream();
private _msgDefinition: MessageDefinition[] = [];
private _msgReader: MessageReader | undefined;
private _log?: LoggerService;
constructor(
socket: TcpSocket,
address: string,
port: number,
requestHeader: Map<string, string>,
log?: LoggerService,
) {
super();
this._socket = socket;
this._address = address;
this._port = port;
this._requestHeader = requestHeader;
this._log = log;
// eslint-disable-next-line @typescript-eslint/no-misused-promises
socket.on("connect", this._handleConnect);
socket.on("close", this._handleClose);
socket.on("error", this._handleError);
socket.on("data", this._handleData);
this._transformer.on("message", this._handleMessage);
}
transportType(): string {
return "TCPROS";
}
async remoteAddress(): Promise<TcpAddress | undefined> {
return await this._socket.remoteAddress();
}
async connect(): Promise<void> {
if (this._shutdown) {
return;
}
this._log?.debug?.(`connecting to ${this.toString()} (attempt ${this.retries})`);
try {
await this._socket.connect();
this._log?.debug?.(`connected to ${this.toString()}`);
} catch (err) {
this._log?.warn?.(`${this.toString()} connection failed: ${err}`);
// _handleClose() will be called, triggering a reconnect attempt
}
}
private _retryConnection(): void {
if (!this._shutdown) {
backoff(++this.retries)
// eslint-disable-next-line @typescript-eslint/promise-function-async
.then(() => this.connect())
.catch((err) => {
// This should never be called, this.connect() is not expected to throw
this._log?.warn?.(`${this.toString()} unexpected retry failure: ${err}`);
});
}
}
connected(): boolean {
return this._connected;
}
header(): Map<string, string> {
return new Map<string, string>(this._header);
}
stats(): ConnectionStats {
return this._stats;
}
messageDefinition(): MessageDefinition[] {
return this._msgDefinition;
}
messageReader(): MessageReader | undefined {
return this._msgReader;
}
close(): void {
this._log?.debug?.(`closing connection to ${this.toString()}`);
this._shutdown = true;
this._connected = false;
this.removeAllListeners();
this._socket.close().catch((err) => {
this._log?.warn?.(`${this.toString()} close failed: ${err}`);
});
}
async writeHeader(): Promise<void> {
const serializedHeader = TcpConnection.SerializeHeader(this._requestHeader);
const totalLen = 4 + serializedHeader.byteLength;
this._stats.bytesSent += totalLen;
const data = new Uint8Array(totalLen);
// Write the 4-byte length
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
view.setUint32(0, serializedHeader.byteLength, true);
// Copy the serialized header into the final buffer
data.set(serializedHeader, 4);
// Write the length and serialized header payload
return await this._socket.write(data);
}
// e.g. "TCPROS connection on port 59746 to [host:34318 on socket 11]"
getTransportInfo(): string {
return this._transportInfo;
}
override toString(): string {
return TcpConnection.Uri(this._address, this._port);
}
private _getTransportInfo = async (): Promise<string> => {
const localPort = (await this._socket.localAddress())?.port ?? -1;
const addr = await this._socket.remoteAddress();
const fd = (await this._socket.fd()) ?? -1;
if (addr != null) {
const { address, port } = addr;
const host = address.includes(":") ? `[${address}]` : address;
return `TCPROS connection on port ${localPort} to [${host}:${port} on socket ${fd}]`;
}
return `TCPROS not connected [socket ${fd}]`;
};
private _handleConnect = async (): Promise<void> => {
if (this._shutdown) {
this.close();
return;
}
this._connected = true;
this.retries = 0;
this._transportInfo = await this._getTransportInfo();
try {
// Write the initial request header. This prompts the publisher to respond
// with its own header then start streaming messages
await this.writeHeader();
} catch (err) {
this._log?.warn?.(`${this.toString()} failed to write header. reconnecting: ${err}`);
this.emit("error", new Error(`Header write failed: ${err}`));
this._retryConnection();
}
};
private _handleClose = (): void => {
this._connected = false;
if (!this._shutdown) {
this._log?.warn?.(`${this.toString()} closed unexpectedly. reconnecting`);
this.emit("error", new Error("Connection closed unexpectedly"));
this._retryConnection();
}
};
private _handleError = (err: Error): void => {
if (!this._shutdown) {
this._log?.warn?.(`${this.toString()} error: ${err}`);
this.emit("error", err);
}
};
private _handleData = (chunk: Uint8Array): void => {
if (this._shutdown) {
return;
}
try {
this._transformer.addData(chunk);
} catch (unk) {
const err = unk instanceof Error ? unk : new Error(unk as string);
this._log?.warn?.(
`failed to decode ${chunk.length} byte chunk from tcp publisher ${this.toString()}: ${err}`,
);
// Close the socket, the stream is now corrupt
this._socket.close().catch((closeErr) => {
this._log?.warn?.(`${this.toString()} close failed: ${closeErr}`);
});
this.emit("error", err);
}
};
private _handleMessage = (msgData: Uint8Array): void => {
if (this._shutdown) {
this.close();
return;
}
this._stats.bytesReceived += msgData.byteLength;
if (this._readingHeader) {
this._readingHeader = false;
this._header = TcpConnection.ParseHeader(msgData);
this._msgDefinition = parseMessageDefinition(this._header.get("message_definition") ?? "");
this._msgReader = new MessageReader(this._msgDefinition);
this.emit("header", this._header, this._msgDefinition, this._msgReader);
} else {
this._stats.messagesReceived++;
if (this._msgReader != null) {
try {
const bytes = new Uint8Array(msgData.buffer, msgData.byteOffset, msgData.length);
const msg = this._msgReader.readMessage(bytes);
this.emit("message", msg, msgData);
} catch (unk) {
const err = unk instanceof Error ? unk : new Error(unk as string);
this.emit("error", err);
}
}
}
};
static Uri(address: string, port: number): string {
// RFC2732 requires IPv6 addresses that include ":" characters to be wrapped in "[]" brackets
// when used in a URI
const host = address.includes(":") ? `[${address}]` : address;
return `tcpros://${host}:${port}`;
}
static SerializeHeader(header: Map<string, string>): Uint8Array {
const encoder = new TextEncoder();
const encoded = Array.from(header).map(([key, value]) => encoder.encode(`${key}=${value}`));
const payloadLen = encoded.reduce((sum, str) => sum + str.length + 4, 0);
const buffer = new ArrayBuffer(payloadLen);
const array = new Uint8Array(buffer);
const view = new DataView(buffer);
let idx = 0;
encoded.forEach((strData) => {
view.setUint32(idx, strData.length, true);
idx += 4;
array.set(strData, idx);
idx += strData.length;
});
return new Uint8Array(buffer);
}
static ParseHeader(data: Uint8Array): Map<string, string> {
const decoder = new TextDecoder();
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
const result = new Map<string, string>();
let idx = 0;
while (idx + 4 < data.length) {
const len = Math.min(view.getUint32(idx, true), data.length - idx - 4);
idx += 4;
const str = decoder.decode(new Uint8Array(data.buffer, data.byteOffset + idx, len));
let equalIdx = str.indexOf("=");
if (equalIdx < 0) {
equalIdx = str.length;
}
const key = str.substr(0, equalIdx);
const value = str.substr(equalIdx + 1);
result.set(key, value);
idx += len;
}
return result;
}
}