-
Notifications
You must be signed in to change notification settings - Fork 323
/
SocketCommunicationManager.cs
411 lines (364 loc) · 15.3 KB
/
SocketCommunicationManager.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.VisualStudio.TestPlatform.CommunicationUtilities
{
using System;
using System.Diagnostics;
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.CoreUtilities.Helpers;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions;
/// <summary>
/// Facilitates communication using sockets
/// </summary>
public class SocketCommunicationManager : ICommunicationManager
{
/// <summary>
/// The server stream read timeout constant (in microseconds).
/// </summary>
private const int STREAMREADTIMEOUT = 1000 * 1000;
/// <summary>
/// TCP Listener to host TCP channel and listen
/// </summary>
private TcpListener tcpListener;
/// <summary>
/// TCP Client that can connect to a TCP listener
/// </summary>
private TcpClient tcpClient;
/// <summary>
/// Binary Writer to write to channel stream
/// </summary>
private BinaryWriter binaryWriter;
/// <summary>
/// Binary reader to read from channel stream
/// </summary>
private BinaryReader binaryReader;
/// <summary>
/// Serializer for the data objects
/// </summary>
private IDataSerializer dataSerializer;
/// <summary>
/// Event used to maintain client connection state
/// </summary>
private ManualResetEvent clientConnectedEvent = new ManualResetEvent(false);
/// <summary>
/// Event used to maintain client connection state
/// </summary>
private ManualResetEvent clientConnectionAcceptedEvent = new ManualResetEvent(false);
/// <summary>
/// Sync object for sending messages
/// SendMessage over socket channel is NOT thread-safe
/// </summary>
private object sendSyncObject = new object();
/// <summary>
/// Sync object for receiving messages
/// </summary>
private object receiveSyncObject = new object();
private Socket socket;
/// <summary>
/// Initializes a new instance of the <see cref="SocketCommunicationManager"/> class.
/// </summary>
public SocketCommunicationManager()
: this(JsonDataSerializer.Instance)
{
}
internal SocketCommunicationManager(IDataSerializer dataSerializer)
{
this.dataSerializer = dataSerializer;
}
#region ServerMethods
/// <summary>
/// Host TCP Socket Server and start listening
/// </summary>
/// <param name="endpoint">End point where server is hosted</param>
/// <returns>Port of the listener</returns>
public IPEndPoint HostServer(IPEndPoint endpoint)
{
this.tcpListener = new TcpListener(endpoint);
this.tcpListener.Start();
EqtTrace.Info("Listening on Endpoint : {0}", (IPEndPoint)this.tcpListener.LocalEndpoint);
return (IPEndPoint)this.tcpListener.LocalEndpoint;
}
/// <summary>
/// Accepts client async
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task AcceptClientAsync()
{
if (this.tcpListener != null)
{
this.clientConnectedEvent.Reset();
var client = await this.tcpListener.AcceptTcpClientAsync();
this.socket = client.Client;
this.socket.NoDelay = true;
// Using Buffered stream only in case of write, and Network stream in case of read.
var bufferedStream = new PlatformStream().CreateBufferedStream(client.GetStream(), SocketConstants.BufferSize);
var networkStream = client.GetStream();
this.binaryReader = new BinaryReader(networkStream);
this.binaryWriter = new BinaryWriter(bufferedStream);
this.clientConnectedEvent.Set();
if (EqtTrace.IsInfoEnabled)
{
EqtTrace.Info("Using the buffer size of {0} bytes", SocketConstants.BufferSize);
EqtTrace.Info("Accepted Client request and set the flag");
}
}
}
/// <summary>
/// Waits for Client Connection
/// </summary>
/// <param name="clientConnectionTimeout">Time to Wait for the connection</param>
/// <returns>True if Client is connected, false otherwise</returns>
public bool WaitForClientConnection(int clientConnectionTimeout)
{
return this.clientConnectedEvent.WaitOne(clientConnectionTimeout);
}
/// <summary>
/// Stop Listener
/// </summary>
public void StopServer()
{
this.tcpListener?.Stop();
this.tcpListener = null;
this.binaryReader?.Dispose();
this.binaryWriter?.Dispose();
}
#endregion
#region ClientMethods
/// <summary>
/// Connects to server async
/// </summary>
/// <param name="endpoint">EndPointAddress for client to connect</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task SetupClientAsync(IPEndPoint endpoint)
{
// ToDo: pass cancellationtoken, if user cancels the operation, so we don't wait 50 secs to connect
// for now added a check for validation of this.tcpclient
this.clientConnectionAcceptedEvent.Reset();
EqtTrace.Info("Trying to connect to server on socket : {0} ", endpoint);
this.tcpClient = new TcpClient { NoDelay = true };
this.socket = this.tcpClient.Client;
Stopwatch watch = new Stopwatch();
watch.Start();
var connectionTimeout = EnvironmentHelper.GetConnectionTimeout() * 1000;
do
{
try
{
EqtTrace.Verbose("SocketCommunicationManager : SetupClientAsync : Attempting to connect to the server.");
await this.tcpClient.ConnectAsync(endpoint.Address, endpoint.Port);
if (this.tcpClient.Connected)
{
// Using Buffered stream only in case of write, and Network stream in case of read.
var bufferedStream = new PlatformStream().CreateBufferedStream(this.tcpClient.GetStream(), SocketConstants.BufferSize);
var networkStream = this.tcpClient.GetStream();
this.binaryReader = new BinaryReader(networkStream);
this.binaryWriter = new BinaryWriter(bufferedStream);
if (EqtTrace.IsInfoEnabled)
{
EqtTrace.Info("Connected to the server successfully ");
EqtTrace.Info("Using the buffer size of {0} bytes", SocketConstants.BufferSize);
}
this.clientConnectionAcceptedEvent.Set();
}
}
catch (Exception ex)
{
EqtTrace.Error("Connection Failed with error {0}, retrying", ex.ToString());
}
}
while ((this.tcpClient != null) && !this.tcpClient.Connected && watch.ElapsedMilliseconds < connectionTimeout);
}
/// <summary>
/// Waits for server to be connected
/// Whoever creating the client and trying to connect to a server
/// should use this method to wait for connection to be established with server
/// </summary>
/// <param name="connectionTimeout">Time to wait for the connection</param>
/// <returns>True, if Server got a connection from client</returns>
public bool WaitForServerConnection(int connectionTimeout)
{
return this.clientConnectionAcceptedEvent.WaitOne(connectionTimeout);
}
/// <summary>
/// Stop Listener
/// </summary>
public void StopClient()
{
#if NET451
// tcpClient.Close() calls tcpClient.Dispose().
this.tcpClient?.Close();
#else
// tcpClient.Close() not available for netstandard1.5.
this.tcpClient?.Dispose();
#endif
this.tcpClient = null;
this.binaryReader?.Dispose();
this.binaryWriter?.Dispose();
}
#endregion
/// <summary>
/// Writes message to the binary writer.
/// </summary>
/// <param name="messageType">Type of Message to be sent, for instance TestSessionStart</param>
public void SendMessage(string messageType)
{
var serializedObject = this.dataSerializer.SerializeMessage(messageType);
this.WriteAndFlushToChannel(serializedObject);
}
/// <summary>
/// Writes message to the binary writer with payload
/// </summary>
/// <param name="messageType">Type of Message to be sent, for instance TestSessionStart</param>
/// <param name="payload">payload to be sent</param>
public void SendMessage(string messageType, object payload)
{
var rawMessage = this.dataSerializer.SerializePayload(messageType, payload);
this.WriteAndFlushToChannel(rawMessage);
}
/// <summary>
/// Writes message to the binary writer with payload
/// </summary>
/// <param name="messageType">Type of Message to be sent, for instance TestSessionStart</param>
/// <param name="payload">payload to be sent</param>
/// <param name="version">version to be sent</param>
public void SendMessage(string messageType, object payload, int version)
{
var rawMessage = this.dataSerializer.SerializePayload(messageType, payload, version);
this.WriteAndFlushToChannel(rawMessage);
}
/// <summary>
/// Send serialized raw message
/// </summary>
/// <param name="rawMessage">serialized message</param>
public void SendRawMessage(string rawMessage)
{
this.WriteAndFlushToChannel(rawMessage);
}
/// <summary>
/// Reads message from the binary reader
/// </summary>
/// <returns>Returns message read from the binary reader</returns>
public Message ReceiveMessage()
{
var rawMessage = this.ReceiveRawMessage();
return this.dataSerializer.DeserializeMessage(rawMessage);
}
/// <summary>
/// Reads message from the binary reader using read timeout
/// </summary>
/// <param name="cancellationToken">
/// The cancellation Token.
/// </param>
/// <returns>
/// Returns message read from the binary reader
/// </returns>
public async Task<Message> ReceiveMessageAsync(CancellationToken cancellationToken)
{
var rawMessage = await this.ReceiveRawMessageAsync(cancellationToken);
if (!string.IsNullOrEmpty(rawMessage))
{
return this.dataSerializer.DeserializeMessage(rawMessage);
}
return null;
}
/// <summary>
/// Reads message from the binary reader
/// </summary>
/// <returns> Raw message string </returns>
public string ReceiveRawMessage()
{
lock (this.receiveSyncObject)
{
// Reading message on binaryreader is not thread-safe
return this.binaryReader.ReadString();
}
}
/// <summary>
/// Reads message from the binary reader using read timeout
/// </summary>
/// <param name="cancellationToken">
/// The cancellation Token.
/// </param>
/// <returns>
/// Raw message string
/// </returns>
public async Task<string> ReceiveRawMessageAsync(CancellationToken cancellationToken)
{
var str = await Task.Run(() => this.TryReceiveRawMessage(cancellationToken));
return str;
}
/// <summary>
/// Deserializes the Message into actual TestPlatform objects
/// </summary>
/// <typeparam name="T"> The type of object to deserialize to. </typeparam>
/// <param name="message"> Message object </param>
/// <returns> TestPlatform object </returns>
public T DeserializePayload<T>(Message message)
{
return this.dataSerializer.DeserializePayload<T>(message);
}
private string TryReceiveRawMessage(CancellationToken cancellationToken)
{
string str = null;
bool success = false;
// Set read timeout to avoid blocking receive raw message
while (!cancellationToken.IsCancellationRequested && !success)
{
try
{
if (this.socket.Poll(STREAMREADTIMEOUT, SelectMode.SelectRead))
{
str = this.ReceiveRawMessage();
success = true;
}
}
catch (IOException ioException)
{
var socketException = ioException.InnerException as SocketException;
if (socketException != null
&& socketException.SocketErrorCode == SocketError.TimedOut)
{
EqtTrace.Info(
"SocketCommunicationManager ReceiveMessage: failed to receive message because read timeout {0}",
ioException);
}
else
{
EqtTrace.Error(
"SocketCommunicationManager ReceiveMessage: failed to receive message {0}",
ioException);
break;
}
}
catch (Exception exception)
{
EqtTrace.Error(
"SocketCommunicationManager ReceiveMessage: failed to receive message {0}",
exception);
break;
}
}
return str;
}
/// <summary>
/// Writes the data on socket and flushes the buffer
/// </summary>
/// <param name="rawMessage">message to write</param>
private void WriteAndFlushToChannel(string rawMessage)
{
// Writing Message on binarywriter is not Thread-Safe
// Need to sync one by one to avoid buffer corruption
lock (this.sendSyncObject)
{
this.binaryWriter?.Write(rawMessage);
this.binaryWriter?.Flush();
}
}
}
}