-
Notifications
You must be signed in to change notification settings - Fork 325
/
TcpClientExtensions.cs
109 lines (97 loc) · 4.26 KB
/
TcpClientExtensions.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Interfaces;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
namespace Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;
internal static class TcpClientExtensions
{
// Timeout for polling stream in micro seconds.
private const int Streamreadtimeout = 1000 * 1000;
internal static Task MessageLoopAsync(
this TcpClient client,
ICommunicationChannel channel,
Action<Exception?> errorHandler,
CancellationToken cancellationToken)
{
Exception? error = null;
var remoteEndPoint = string.Empty;
var localEndPoint = string.Empty;
try
{
remoteEndPoint = client.Client.RemoteEndPoint?.ToString();
localEndPoint = client.Client.LocalEndPoint?.ToString();
}
catch (SocketException socketException)
{
EqtTrace.Error(
"TcpClientExtensions.MessageLoopAsync: Failed to access the endpoint due to socket error: {0}",
socketException);
}
// Set read timeout to avoid blocking receive raw message
while (channel != null && !cancellationToken.IsCancellationRequested)
{
EqtTrace.Verbose("TcpClientExtensions.MessageLoopAsync: Polling on remoteEndPoint: {0} localEndPoint: {1}", remoteEndPoint, localEndPoint);
try
{
if (client.Client.Poll(Streamreadtimeout, SelectMode.SelectRead))
{
EqtTrace.Verbose("TcpClientExtensions.MessageLoopAsync: NotifyDataAvailable remoteEndPoint: {0} localEndPoint: {1}", remoteEndPoint, localEndPoint);
channel.NotifyDataAvailable();
}
}
catch (IOException ioException)
{
if (ioException.InnerException is SocketException socketException
&& socketException.SocketErrorCode == SocketError.TimedOut)
{
EqtTrace.Info(
"Socket: Message loop: failed to receive message due to read timeout {0}, remoteEndPoint: {1} localEndPoint: {2}",
ioException,
remoteEndPoint,
localEndPoint);
}
else
{
EqtTrace.Error(
"Socket: Message loop: failed to receive message due to socket error {0}, remoteEndPoint: {1} localEndPoint: {2}",
ioException,
remoteEndPoint,
localEndPoint);
error = ioException;
break;
}
}
catch (Exception exception)
{
EqtTrace.Error(
"Socket: Message loop: failed to receive message {0}, remoteEndPoint: {1} localEndPoint: {2}",
exception,
remoteEndPoint,
localEndPoint);
error = exception;
break;
}
}
// Try clean up and raise client disconnected events
errorHandler(error);
EqtTrace.Verbose("TcpClientExtensions.MessageLoopAsync: exiting MessageLoopAsync remoteEndPoint: {0} localEndPoint: {1}", remoteEndPoint, localEndPoint);
return Task.FromResult(0);
}
/// <summary>
/// Converts a given string endpoint address to valid Ipv4, Ipv6 IPEndpoint
/// </summary>
/// <param name="value">Input endpoint address</param>
/// <returns>IPEndpoint from give string, if its not a valid string. It will create endpoint with loop back address with port 0</returns>
internal static IPEndPoint GetIpEndPoint(this string? value)
{
return Uri.TryCreate(string.Concat("tcp://", value), UriKind.Absolute, out Uri? uri)
? new IPEndPoint(IPAddress.Parse(uri.Host), uri.Port < 0 ? 0 : uri.Port)
: new IPEndPoint(IPAddress.Loopback, 0);
}
}