Dreamine.Communication.Sockets
1.0.2
Dreamine.Communication.Sockets 통신 기능과 관련 API를 제공합니다.
Toggle main menu visibility
로딩중...
검색중...
일치하는것 없음
TcpClientTransport.cs
이 파일의 문서화 페이지로 가기
1
using
System.IO;
2
using
System.Net.Sockets;
3
using
Dreamine.Communication.Abstractions.Enums;
4
using
Dreamine.Communication.Abstractions.Interfaces;
5
using
Dreamine.Communication.Abstractions.Models;
6
using
Dreamine.Communication.Core.Framing;
7
using
Dreamine.Communication.Core.Protocols;
8
using
Dreamine.Communication.Sockets.Options
;
9
10
namespace
Dreamine.Communication.Sockets.Clients
;
11
20
public
sealed
class
TcpClientTransport
: IMessageTransport
21
{
30
private
readonly
TcpClientTransportOptions
_options
;
39
private
readonly IMessageProtocolAdapter
_protocolAdapter
;
48
private
readonly IMessageFrameCodec
_frameCodec
;
49
58
private
TcpClient?
_client
;
67
private
CancellationTokenSource?
_receiveLoopCts
;
76
private
Task?
_receiveLoopTask
;
85
private
int
_state
= (int)ConnectionState.Disconnected;
86
103
public
TcpClientTransport
(
TcpClientTransportOptions
options)
104
: this(
105
options,
106
new DreamineEnvelopeProtocolAdapter(),
107
new LengthPrefixedMessageFrameCodec())
108
{
109
}
110
151
public
TcpClientTransport
(
152
TcpClientTransportOptions
options,
153
IMessageProtocolAdapter protocolAdapter,
154
IMessageFrameCodec frameCodec)
155
{
156
_options
= options ??
throw
new
ArgumentNullException(nameof(options));
157
_protocolAdapter
= protocolAdapter ??
throw
new
ArgumentNullException(nameof(protocolAdapter));
158
_frameCodec
= frameCodec ??
throw
new
ArgumentNullException(nameof(frameCodec));
159
160
ValidateOptions
(
_options
);
161
}
162
171
public
ConnectionState
State
=> (ConnectionState)Volatile.Read(ref
_state
);
172
181
public
TransportKind
Kind
=> TransportKind.Tcp;
182
191
public
event
EventHandler<MessageEnvelope>?
MessageReceived
;
192
225
public
async Task
ConnectAsync
(CancellationToken cancellationToken =
default
)
226
{
227
if
(
State
is ConnectionState.Connected or ConnectionState.Connecting)
228
{
229
return
;
230
}
231
232
SetState
(ConnectionState.Connecting);
233
234
try
235
{
236
CleanupClient
();
237
238
_client
=
new
TcpClient
239
{
240
ReceiveBufferSize =
_options
.ReceiveBufferSize,
241
SendBufferSize = _options.SendBufferSize
242
};
243
244
using
var timeoutCts =
new
CancellationTokenSource(
_options
.ConnectTimeoutMs);
245
using
var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(
246
cancellationToken,
247
timeoutCts.Token);
248
249
await
_client
.ConnectAsync(
_options
.Host,
_options
.Port, linkedCts.Token)
250
.ConfigureAwait(
false
);
251
252
SetState
(ConnectionState.Connected);
253
254
_receiveLoopCts
=
new
CancellationTokenSource();
255
_receiveLoopTask
= Task.Run(
256
() =>
ReceiveLoopAsync
(
_receiveLoopCts
.Token),
257
_receiveLoopCts
.Token);
258
}
259
catch
260
{
261
SetState
(ConnectionState.Faulted);
262
CleanupClient
();
263
264
throw
;
265
}
266
}
267
292
public
async Task
DisconnectAsync
(CancellationToken cancellationToken =
default
)
293
{
294
if
(
State
== ConnectionState.Disconnected)
295
{
296
return
;
297
}
298
299
SetState
(ConnectionState.Disconnecting);
300
301
_receiveLoopCts
?.Cancel();
302
303
CleanupClient
();
304
305
if
(
_receiveLoopTask
is not
null
)
306
{
307
try
308
{
309
await
_receiveLoopTask
.ConfigureAwait(
false
);
310
}
311
catch
(OperationCanceledException)
312
{
313
}
314
catch
(ObjectDisposedException)
315
{
316
}
317
catch
(SocketException)
318
{
319
}
320
catch
(IOException)
321
{
322
}
323
}
324
325
_receiveLoopCts
?.Dispose();
326
_receiveLoopCts
=
null
;
327
_receiveLoopTask
=
null
;
328
329
cancellationToken.ThrowIfCancellationRequested();
330
331
SetState
(ConnectionState.Disconnected);
332
}
333
382
public
async Task
SendAsync
(
383
MessageEnvelope message,
384
CancellationToken cancellationToken =
default
)
385
{
386
ArgumentNullException.ThrowIfNull(message);
387
388
if
(
_client
is
null
||
State
!= ConnectionState.Connected)
389
{
390
throw
new
InvalidOperationException(
"TCP client is not connected."
);
391
}
392
393
try
394
{
395
var stream =
_client
.GetStream();
396
var payload =
_protocolAdapter
.Encode(message);
397
398
await
_frameCodec
.WriteFrameAsync(stream, payload, cancellationToken)
399
.ConfigureAwait(
false
);
400
}
401
catch
402
{
403
SetState
(ConnectionState.Faulted);
404
CleanupClient
();
405
406
throw
;
407
}
408
}
409
426
public
async ValueTask
DisposeAsync
()
427
{
428
await
DisconnectAsync
().ConfigureAwait(
false
);
429
}
430
455
private
async Task
ReceiveLoopAsync
(CancellationToken cancellationToken)
456
{
457
if
(
_client
is
null
)
458
{
459
return
;
460
}
461
462
try
463
{
464
var stream =
_client
.GetStream();
465
466
while
(!cancellationToken.IsCancellationRequested &&
467
State
== ConnectionState.Connected)
468
{
469
var payload = await
_frameCodec
.ReadFrameAsync(stream, cancellationToken)
470
.ConfigureAwait(
false
);
471
472
if
(payload is
null
)
473
{
474
break
;
475
}
476
477
var message =
_protocolAdapter
.Decode(payload);
478
MessageReceived
?.Invoke(
this
, message);
479
}
480
481
if
(!cancellationToken.IsCancellationRequested &&
482
State
== ConnectionState.Connected)
483
{
484
SetState
(ConnectionState.Disconnected);
485
CleanupClient
();
486
}
487
}
488
catch
(OperationCanceledException)
489
{
490
}
491
catch
(ObjectDisposedException)
492
{
493
}
494
catch
(SocketException)
495
{
496
if
(!cancellationToken.IsCancellationRequested)
497
{
498
SetState
(ConnectionState.Faulted);
499
CleanupClient
();
500
}
501
}
502
catch
(IOException)
503
{
504
if
(!cancellationToken.IsCancellationRequested)
505
{
506
SetState
(ConnectionState.Faulted);
507
CleanupClient
();
508
}
509
}
510
catch
511
{
512
if
(!cancellationToken.IsCancellationRequested)
513
{
514
SetState
(ConnectionState.Faulted);
515
CleanupClient
();
516
}
517
}
518
}
519
528
private
void
CleanupClient
()
529
{
530
try
531
{
532
_client
?.Close();
533
}
534
catch
535
{
536
}
537
538
try
539
{
540
_client
?.Dispose();
541
}
542
catch
543
{
544
}
545
546
_client
=
null
;
547
}
548
565
private
void
SetState
(ConnectionState state)
566
{
567
Interlocked.Exchange(ref
_state
, (
int
)state);
568
}
569
602
private
static
void
ValidateOptions
(
TcpClientTransportOptions
options)
603
{
604
ArgumentException.ThrowIfNullOrWhiteSpace(options.
Host
);
605
606
if
(options.
Port
<= 0 || options.
Port
> 65535)
607
{
608
throw
new
ArgumentOutOfRangeException(nameof(options.
Port
));
609
}
610
611
if
(options.
ReceiveBufferSize
<= 0)
612
{
613
throw
new
ArgumentOutOfRangeException(nameof(options.
ReceiveBufferSize
));
614
}
615
616
if
(options.
SendBufferSize
<= 0)
617
{
618
throw
new
ArgumentOutOfRangeException(nameof(options.
SendBufferSize
));
619
}
620
621
if
(options.
ConnectTimeoutMs
<= 0)
622
{
623
throw
new
ArgumentOutOfRangeException(nameof(options.
ConnectTimeoutMs
));
624
}
625
}
626
}
Dreamine.Communication.Sockets.Clients
Definition
TcpClientTransport.cs:10
Dreamine.Communication.Sockets.Options
Definition
TcpClientTransportOptions.cs:1
Dreamine.Communication.Sockets.Clients.TcpClientTransport._state
int _state
Definition
TcpClientTransport.cs:85
Dreamine.Communication.Sockets.Clients.TcpClientTransport.ReceiveLoopAsync
async Task ReceiveLoopAsync(CancellationToken cancellationToken)
Definition
TcpClientTransport.cs:455
Dreamine.Communication.Sockets.Clients.TcpClientTransport._receiveLoopCts
CancellationTokenSource? _receiveLoopCts
Definition
TcpClientTransport.cs:67
Dreamine.Communication.Sockets.Clients.TcpClientTransport.SendAsync
async Task SendAsync(MessageEnvelope message, CancellationToken cancellationToken=default)
Definition
TcpClientTransport.cs:382
Dreamine.Communication.Sockets.Clients.TcpClientTransport.CleanupClient
void CleanupClient()
Definition
TcpClientTransport.cs:528
Dreamine.Communication.Sockets.Clients.TcpClientTransport._options
readonly TcpClientTransportOptions _options
Definition
TcpClientTransport.cs:30
Dreamine.Communication.Sockets.Clients.TcpClientTransport.Kind
TransportKind Kind
Definition
TcpClientTransport.cs:181
Dreamine.Communication.Sockets.Clients.TcpClientTransport.TcpClientTransport
TcpClientTransport(TcpClientTransportOptions options, IMessageProtocolAdapter protocolAdapter, IMessageFrameCodec frameCodec)
Definition
TcpClientTransport.cs:151
Dreamine.Communication.Sockets.Clients.TcpClientTransport.ValidateOptions
static void ValidateOptions(TcpClientTransportOptions options)
Definition
TcpClientTransport.cs:602
Dreamine.Communication.Sockets.Clients.TcpClientTransport.MessageReceived
EventHandler< MessageEnvelope >? MessageReceived
Definition
TcpClientTransport.cs:191
Dreamine.Communication.Sockets.Clients.TcpClientTransport._frameCodec
readonly IMessageFrameCodec _frameCodec
Definition
TcpClientTransport.cs:48
Dreamine.Communication.Sockets.Clients.TcpClientTransport.TcpClientTransport
TcpClientTransport(TcpClientTransportOptions options)
Definition
TcpClientTransport.cs:103
Dreamine.Communication.Sockets.Clients.TcpClientTransport._receiveLoopTask
Task? _receiveLoopTask
Definition
TcpClientTransport.cs:76
Dreamine.Communication.Sockets.Clients.TcpClientTransport.DisconnectAsync
async Task DisconnectAsync(CancellationToken cancellationToken=default)
Definition
TcpClientTransport.cs:292
Dreamine.Communication.Sockets.Clients.TcpClientTransport.SetState
void SetState(ConnectionState state)
Definition
TcpClientTransport.cs:565
Dreamine.Communication.Sockets.Clients.TcpClientTransport._protocolAdapter
readonly IMessageProtocolAdapter _protocolAdapter
Definition
TcpClientTransport.cs:39
Dreamine.Communication.Sockets.Clients.TcpClientTransport._client
TcpClient? _client
Definition
TcpClientTransport.cs:58
Dreamine.Communication.Sockets.Clients.TcpClientTransport.State
ConnectionState State
Definition
TcpClientTransport.cs:171
Dreamine.Communication.Sockets.Clients.TcpClientTransport.ConnectAsync
async Task ConnectAsync(CancellationToken cancellationToken=default)
Definition
TcpClientTransport.cs:225
Dreamine.Communication.Sockets.Clients.TcpClientTransport.DisposeAsync
async ValueTask DisposeAsync()
Definition
TcpClientTransport.cs:426
Dreamine.Communication.Sockets.Options.TcpClientTransportOptions
Definition
TcpClientTransportOptions.cs:12
Dreamine.Communication.Sockets.Options.TcpClientTransportOptions.Host
string Host
Definition
TcpClientTransportOptions.cs:21
Dreamine.Communication.Sockets.Options.TcpClientTransportOptions.Port
int Port
Definition
TcpClientTransportOptions.cs:31
Dreamine.Communication.Sockets.Options.TcpClientTransportOptions.ConnectTimeoutMs
int ConnectTimeoutMs
Definition
TcpClientTransportOptions.cs:61
Dreamine.Communication.Sockets.Options.TcpClientTransportOptions.SendBufferSize
int SendBufferSize
Definition
TcpClientTransportOptions.cs:51
Dreamine.Communication.Sockets.Options.TcpClientTransportOptions.ReceiveBufferSize
int ReceiveBufferSize
Definition
TcpClientTransportOptions.cs:41
Clients
TcpClientTransport.cs
다음에 의해 생성됨 :
1.17.0