-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathTcpReceiver.cs
378 lines (320 loc) · 11.2 KB
/
TcpReceiver.cs
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Iviz.Msgs;
using Iviz.MsgsGen.Dynamic;
using Iviz.Roslib.Utils;
using Iviz.Tools;
namespace Iviz.Roslib;
internal sealed class TcpReceiver<TMessage> : IProtocolReceiver, ILoopbackReceiver<TMessage>, ITcpReceiver
where TMessage : IMessage
{
const int DisposeTimeoutInMs = 2000;
readonly ReceiverManager<TMessage> manager;
readonly bool requestNoDelay;
readonly CancellationTokenSource runningTs = new();
readonly Task task;
readonly int connectionTimeoutInMs;
int receiveBufferSize = 8192;
TopicInfo topicInfo;
bool disposed;
long numReceived;
long bytesReceived;
bool KeepRunning => !runningTs.IsCancellationRequested;
public string Topic => topicInfo.Topic;
public string Type => topicInfo.Type;
public Endpoint RemoteEndpoint { get; }
public Endpoint Endpoint { get; private set; }
public IReadOnlyCollection<string> RosHeader { get; private set; } = Array.Empty<string>();
public bool IsPaused { get; set; }
public Uri RemoteUri { get; }
public bool IsAlive => !task.IsCompleted;
public bool IsConnected => TcpClient is { Connected: true };
public ReceiverStatus Status { get; private set; }
public ErrorMessage? ErrorDescription { get; private set; }
public TcpClient? TcpClient { get; private set; }
public string? RemoteId { get; private set; }
public TcpReceiver(ReceiverManager<TMessage> manager,
Uri remoteUri, Endpoint remoteEndpoint, TopicInfo topicInfo,
bool requestNoDelay, int timeoutInMs)
{
RemoteUri = remoteUri;
RemoteEndpoint = remoteEndpoint;
this.topicInfo = topicInfo;
this.manager = manager;
this.requestNoDelay = requestNoDelay;
connectionTimeoutInMs = timeoutInMs;
Status = ReceiverStatus.ConnectingTcp;
task = TaskUtils.Run(async () => await StartSession().AwaitNoThrow(this), runningTs.Token);
}
public SubscriberReceiverState State => new TcpReceiverState(RemoteUri)
{
RemoteId = RemoteId,
Status = Status,
RequestNoDelay = requestNoDelay,
EndPoint = Endpoint,
RemoteEndpoint = RemoteEndpoint,
NumReceived = numReceived,
BytesReceived = bytesReceived,
ErrorDescription = ErrorDescription,
IsAlive = IsAlive,
};
async ValueTask StartSession()
{
bool shouldRetry = false;
var newTcpClient = await StartTcpConnection();
if (newTcpClient != null)
{
Logger.LogDebugFormat("{0}: Connected!", this);
var ipEndpoint = (IPEndPoint)newTcpClient.Client.LocalEndPoint!;
Endpoint = new Endpoint(ipEndpoint);
TcpClient = newTcpClient;
try
{
await ProcessLoop(newTcpClient);
}
catch (Exception e)
{
switch (e)
{
case ObjectDisposedException:
case OperationCanceledException:
break;
case IOException:
case SocketException:
case TimeoutException:
ErrorDescription = new ErrorMessage(e);
Logger.LogDebugFormat(BaseUtils.GenericExceptionFormat, this, e);
shouldRetry = true;
break;
case RoslibException:
ErrorDescription = new ErrorMessage(e);
Logger.LogErrorFormat(BaseUtils.GenericExceptionFormat, this, e);
break;
default:
Logger.LogFormat(BaseUtils.GenericExceptionFormat, this, e);
break;
}
}
}
else
{
Logger.LogDebugFormat("{0}: Connection to the TCP listener failed!", this);
ErrorDescription = new ErrorMessage("Could not connect to the TCP listener");
shouldRetry = true;
}
Status = ReceiverStatus.Dead;
TcpClient?.Dispose();
TcpClient = null;
runningTs.Cancel();
Logger.LogDebugFormat("{0}: Stopped!", this);
if (shouldRetry)
{
try
{
await Task.Delay(2000, runningTs.Token);
manager.RetryConnection(RemoteUri);
}
catch (OperationCanceledException)
{
}
}
}
async ValueTask<TcpClient?> StartTcpConnection()
{
Logger.LogDebugFormat("{0}: Trying to connect!", this);
var newTcpClient = new TcpClient(AddressFamily.InterNetworkV6)
{
Client = { DualMode = true },
NoDelay = requestNoDelay
};
(string hostname, int port) = RemoteEndpoint;
try
{
await newTcpClient.TryConnectAsync(hostname, port, runningTs.Token, connectionTimeoutInMs);
}
catch (Exception e)
{
if (e is not (IOException or SocketException or OperationCanceledException))
{
Logger.LogFormat(BaseUtils.GenericExceptionFormat, this, e);
}
newTcpClient.Dispose();
ErrorDescription = new ErrorMessage("Could not connect to the TCP listener");
return null;
}
if (newTcpClient.Client is { RemoteEndPoint: { }, LocalEndPoint: { } })
{
return newTcpClient;
}
newTcpClient.Dispose();
ErrorDescription = new ErrorMessage("Could not connect to the TCP listener");
return null;
}
async ValueTask<int> ReceivePacket(TcpClient client, ResizableRent readBuffer)
{
byte[] array = readBuffer.Array;
if (!await client.ReadChunkAsync(array, 4, runningTs.Token))
{
return -1;
}
int length = array.ReadInt();
if (length == 0)
{
return 0;
}
const int maxMessageLength = 64 * 1024 * 1024;
if (length is < 0 or > maxMessageLength)
{
throw new RosInvalidPackageSizeException($"Invalid packet size {length}. Disconnecting.");
}
readBuffer.EnsureCapacity(length);
if (!await client.ReadChunkAsync(readBuffer.Array, length, runningTs.Token))
{
return -1;
}
return length;
}
async ValueTask<int> ReceiveAndIgnore(TcpClient client, byte[] readBuffer)
{
if (!await client.ReadChunkAsync(readBuffer, 4, runningTs.Token))
{
return -1;
}
int length = readBuffer.ReadInt();
if (length == 0)
{
return 0;
}
if (!await client.ReadAndIgnoreAsync(length, runningTs.Token))
{
return -1;
}
return length;
}
ValueTask SendHeader(TcpClient client)
{
string[] contents =
{
$"message_definition={topicInfo.MessageDependencies}",
$"callerid={topicInfo.CallerId}",
$"topic={topicInfo.Topic}",
$"md5sum={topicInfo.Md5Sum}",
$"type={topicInfo.Type}",
requestNoDelay ? "tcp_nodelay=1" : "tcp_nodelay=0"
};
return client.WriteHeaderAsync(contents, runningTs.Token);
}
async ValueTask ProcessHandshake(TcpClient client)
{
await SendHeader(client);
using var readBuffer = new ResizableRent();
int receivedLength = await ReceivePacket(client, readBuffer);
if (receivedLength == -1)
{
throw new IOException("Connection closed during handshake");
}
var responseHeader = RosUtils.ParseHeader(readBuffer, receivedLength);
var dictionary = RosUtils.CreateHeaderDictionary(responseHeader);
if (dictionary.TryGetValue("callerid", out string? remoteCallerId))
{
RemoteId = remoteCallerId;
}
if (dictionary.TryGetValue("error", out string? message)) // TODO: improve error handling here
{
throw new RosHandshakeException($"Partner sent error message: [{message}]");
}
RosHeader = responseHeader.ToArray();
if (DynamicMessage.IsGeneric(typeof(TMessage)))
{
//bool allowDirectLookup = DynamicMessage.IsDynamic(typeof(TMessage));
const bool allowDirectLookup = false;
topicInfo = RosUtils.GenerateDynamicTopicInfo(topicInfo.CallerId, topicInfo.Topic, RosHeader,
allowDirectLookup);
}
}
async ValueTask ProcessMessages(TcpClient client)
{
if (topicInfo.Generator is not IDeserializable<TMessage> generator)
{
throw new InvalidOperationException("Invalid generator!"); // shouldn'T happen
}
using var readBuffer = new ResizableRent();
while (KeepRunning)
{
bool isPaused = IsPaused;
int rcvLength = isPaused
? await ReceiveAndIgnore(client, readBuffer.Array)
: await ReceivePacket(client, readBuffer);
if (rcvLength == -1)
{
Logger.LogDebugFormat("{0}: Partner closed connection", this);
return;
}
numReceived++;
bytesReceived += rcvLength + 4;
if (isPaused)
{
continue;
}
var message = generator.DeserializeFrom(readBuffer[..rcvLength]);
manager.MessageCallback(message, this);
CheckBufferSize(client, rcvLength);
}
}
void CheckBufferSize(TcpClient client, int rcvLength)
{
if (receiveBufferSize >= rcvLength)
{
return;
}
int recommendedSize = RosUtils.GetRecommendedBufferSize(rcvLength, receiveBufferSize);
if (recommendedSize == receiveBufferSize)
{
return;
}
receiveBufferSize = recommendedSize;
client.Client.ReceiveBufferSize = recommendedSize;
Logger.LogDebugFormat("{0}: Large message received. Changing buffer size to {1} kB.", this,
recommendedSize / 1024);
}
async ValueTask ProcessLoop(TcpClient client)
{
await ProcessHandshake(client);
Status = ReceiverStatus.Running;
await ProcessMessages(client);
}
void ILoopbackReceiver<TMessage>.Post(in TMessage message, int rcvLength)
{
if (!IsAlive)
{
return;
}
numReceived++;
bytesReceived += rcvLength + 4;
if (!IsPaused)
{
manager.MessageCallback(message, this);
}
}
public async ValueTask DisposeAsync(CancellationToken token)
{
if (disposed)
{
return;
}
disposed = true;
runningTs.Cancel();
TcpClient?.Dispose();
await task.AwaitNoThrow(DisposeTimeoutInMs, this, token);
}
public override string ToString()
{
return $"[{nameof(TcpReceiver<TMessage>)} for '{Topic}' PartnerUri={RemoteUri} " +
$"PartnerSocket={RemoteEndpoint.Hostname}:{RemoteEndpoint.Port.ToString()}]";
}
}