3
3
using System . Runtime . CompilerServices ;
4
4
using System . Runtime . InteropServices ;
5
5
using Microsoft . Extensions . Logging ;
6
- using NATS . Client . Core . Internal ;
7
6
8
- namespace NATS . Client . Core ;
7
+ namespace NATS . Client . Core . Internal ;
9
8
10
- public sealed class SocketClosedException : Exception
9
+ internal sealed class SocketClosedException : Exception
11
10
{
12
11
public SocketClosedException ( Exception ? innerException )
13
12
: base ( "Socket has been closed." , innerException )
14
13
{
15
14
}
16
15
}
17
16
18
- public class TcpConnection : ISocketConnection
17
+ internal sealed class TcpConnection : ISocketConnection
19
18
{
19
+ private readonly ILogger _logger ;
20
+ private readonly Socket _socket ;
20
21
private readonly TaskCompletionSource < Exception > _waitForClosedSource = new ( ) ;
21
22
private int _disposed ;
22
23
23
24
public TcpConnection ( ILogger logger )
24
25
{
25
- Logger = logger ;
26
- Socket = new Socket ( Socket . OSSupportsIPv6 ? AddressFamily . InterNetworkV6 : AddressFamily . InterNetwork , SocketType . Stream , ProtocolType . Tcp ) ;
26
+ _logger = logger ;
27
+ _socket = new Socket ( Socket . OSSupportsIPv6 ? AddressFamily . InterNetworkV6 : AddressFamily . InterNetwork , SocketType . Stream , ProtocolType . Tcp ) ;
27
28
if ( Socket . OSSupportsIPv6 )
28
29
{
29
- Socket . DualMode = true ;
30
+ _socket . DualMode = true ;
30
31
}
31
32
32
- Socket . NoDelay = true ;
33
+ _socket . NoDelay = true ;
33
34
}
34
35
35
- public ILogger Logger { get ; }
36
-
37
- public Socket Socket { get ; }
38
-
39
36
public Task < Exception > WaitForClosed => _waitForClosedSource . Task ;
40
37
41
38
// CancellationToken is not used, operation lifetime is completely same as socket.
@@ -51,9 +48,9 @@ public TcpConnection(ILogger logger)
51
48
public ValueTask ConnectAsync ( string host , int port , CancellationToken cancellationToken )
52
49
{
53
50
#if NETSTANDARD
54
- return new ValueTask ( Socket . ConnectAsync ( host , port ) . WaitAsync ( Timeout . InfiniteTimeSpan , cancellationToken ) ) ;
51
+ return new ValueTask ( _socket . ConnectAsync ( host , port ) . WaitAsync ( Timeout . InfiniteTimeSpan , cancellationToken ) ) ;
55
52
#else
56
- return Socket . ConnectAsync ( host , port , cancellationToken ) ;
53
+ return _socket . ConnectAsync ( host , port , cancellationToken ) ;
57
54
#endif
58
55
}
59
56
@@ -66,9 +63,9 @@ public async ValueTask ConnectAsync(string host, int port, TimeSpan timeout)
66
63
try
67
64
{
68
65
#if NETSTANDARD
69
- await Socket . ConnectAsync ( host , port ) . WaitAsync ( timeout , cts . Token ) . ConfigureAwait ( false ) ;
66
+ await _socket . ConnectAsync ( host , port ) . WaitAsync ( timeout , cts . Token ) . ConfigureAwait ( false ) ;
70
67
#else
71
- await Socket . ConnectAsync ( host , port , cts . Token ) . ConfigureAwait ( false ) ;
68
+ await _socket . ConnectAsync ( host , port , cts . Token ) . ConfigureAwait ( false ) ;
72
69
#endif
73
70
}
74
71
catch ( Exception ex )
@@ -94,9 +91,9 @@ public ValueTask<int> SendAsync(ReadOnlyMemory<byte> buffer)
94
91
segment = new ArraySegment < byte > ( buffer . ToArray ( ) ) ;
95
92
}
96
93
97
- return new ValueTask < int > ( Socket . SendAsync ( segment , SocketFlags . None ) ) ;
94
+ return new ValueTask < int > ( _socket . SendAsync ( segment , SocketFlags . None ) ) ;
98
95
#else
99
- return Socket . SendAsync ( buffer , SocketFlags . None , CancellationToken . None ) ;
96
+ return _socket . SendAsync ( buffer , SocketFlags . None , CancellationToken . None ) ;
100
97
#endif
101
98
}
102
99
@@ -109,19 +106,19 @@ public ValueTask<int> ReceiveAsync(Memory<byte> buffer)
109
106
ThrowHelper . ThrowInvalidOperationException ( "Can't get underlying array" ) ;
110
107
}
111
108
112
- return new ValueTask < int > ( Socket . ReceiveAsync ( segment , SocketFlags . None ) ) ;
109
+ return new ValueTask < int > ( _socket . ReceiveAsync ( segment , SocketFlags . None ) ) ;
113
110
#else
114
- return Socket . ReceiveAsync ( buffer , SocketFlags . None , CancellationToken . None ) ;
111
+ return _socket . ReceiveAsync ( buffer , SocketFlags . None , CancellationToken . None ) ;
115
112
#endif
116
113
}
117
114
118
115
public ValueTask AbortConnectionAsync ( CancellationToken cancellationToken )
119
116
{
120
117
#if NETSTANDARD
121
- Socket . Disconnect ( false ) ;
118
+ _socket . Disconnect ( false ) ;
122
119
return default ;
123
120
#else
124
- return Socket . DisconnectAsync ( false , cancellationToken ) ;
121
+ return _socket . DisconnectAsync ( false , cancellationToken ) ;
125
122
#endif
126
123
}
127
124
@@ -135,19 +132,17 @@ public ValueTask DisposeAsync()
135
132
}
136
133
catch
137
134
{
138
- // ignored
139
135
}
140
136
141
137
try
142
138
{
143
- Socket . Shutdown ( SocketShutdown . Both ) ;
139
+ _socket . Shutdown ( SocketShutdown . Both ) ;
144
140
}
145
141
catch
146
142
{
147
- // ignored
148
143
}
149
144
150
- Socket . Dispose ( ) ;
145
+ _socket . Dispose ( ) ;
151
146
}
152
147
153
148
return default ;
@@ -161,13 +156,13 @@ public void SignalDisconnected(Exception exception)
161
156
162
157
// NetworkStream will own the Socket, so mark as disposed
163
158
// in order to skip socket.Dispose() in DisposeAsync
164
- internal SslStreamConnection UpgradeToSslStreamConnection ( NatsTlsOpts tlsOpts )
159
+ public SslStreamConnection UpgradeToSslStreamConnection ( NatsTlsOpts tlsOpts )
165
160
{
166
161
if ( Interlocked . Increment ( ref _disposed ) == 1 )
167
162
{
168
163
return new SslStreamConnection (
169
- Logger ,
170
- Socket ,
164
+ _logger ,
165
+ _socket ,
171
166
tlsOpts ,
172
167
_waitForClosedSource ) ;
173
168
}
0 commit comments