@@ -22,7 +22,7 @@ You should have received a copy of the GNU Lesser General Public License
22
22
using System ;
23
23
using System . Diagnostics ;
24
24
using System . Net . Sockets ;
25
- #if NETSTANDARD2_0 || NETSTANDARD2_1
25
+ #if NET8_0_OR_GREATER || NETSTANDARD2_0 || NETSTANDARD2_1
26
26
using System . Runtime . InteropServices ;
27
27
#endif
28
28
using AsyncIO ;
@@ -45,12 +45,12 @@ internal class TcpListener : Own, IProactorEvents
45
45
/// </summary>
46
46
private AsyncSocket ? m_handle ;
47
47
48
- /*
49
- /// <summary>
50
- /// socket being accepted
51
- /// </summary>
52
- private AsyncSocket m_acceptedSocket;
53
- */
48
+ /*
49
+ /// <summary>
50
+ /// socket being accepted
51
+ /// </summary>
52
+ private AsyncSocket m_acceptedSocket;
53
+ */
54
54
55
55
/// <summary>
56
56
/// Socket the listener belongs to.
@@ -107,7 +107,7 @@ protected override void ProcessPlug()
107
107
protected override void ProcessTerm ( int linger )
108
108
{
109
109
Assumes . NotNull ( m_handle ) ;
110
-
110
+
111
111
m_ioObject . SetHandler ( this ) ;
112
112
m_ioObject . RemoveSocket ( m_handle ) ;
113
113
Close ( ) ;
@@ -141,7 +141,7 @@ public virtual void SetAddress(string addr)
141
141
}
142
142
}
143
143
144
- #if NETSTANDARD2_0 || NETSTANDARD2_1
144
+ #if NET8_0_OR_GREATER || NETSTANDARD2_0 || NETSTANDARD2_1
145
145
// This command is failing on linux
146
146
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
147
147
m_handle . ExclusiveAddressUse = false ;
@@ -194,68 +194,76 @@ public void InCompleted(SocketError socketError, int bytesTransferred)
194
194
switch ( socketError )
195
195
{
196
196
case SocketError . Success :
197
- {
198
- // TODO: check TcpFilters
199
- var acceptedSocket = m_handle . GetAcceptedSocket ( ) ;
197
+ {
198
+ // TODO: check TcpFilters
199
+ var acceptedSocket = m_handle . GetAcceptedSocket ( ) ;
200
200
201
201
acceptedSocket . NoDelay = true ;
202
202
203
- if ( m_options . TcpKeepalive != - 1 )
204
- {
205
- acceptedSocket . SetSocketOption ( SocketOptionLevel . Socket , SocketOptionName . KeepAlive , m_options . TcpKeepalive ) ;
206
-
207
- if ( m_options . TcpKeepaliveIdle != - 1 && m_options . TcpKeepaliveIntvl != - 1 )
203
+ if ( m_options . TcpKeepalive != - 1 )
208
204
{
209
- var bytes = new ByteArraySegment ( new byte [ 12 ] ) ;
205
+ acceptedSocket . SetSocketOption ( SocketOptionLevel . Socket , SocketOptionName . KeepAlive , m_options . TcpKeepalive ) ;
206
+ #if NET8_0_OR_GREATER
207
+ if ( m_options . TcpKeepaliveIdle != - 1 )
208
+ acceptedSocket . SetSocketOption ( SocketOptionLevel . Tcp , SocketOptionName . TcpKeepAliveTime , m_options . TcpKeepaliveIdle / 1000 ) ;
209
+ if ( m_options . TcpKeepaliveIntvl != - 1 )
210
+ acceptedSocket . SetSocketOption ( SocketOptionLevel . Tcp , SocketOptionName . TcpKeepAliveInterval , m_options . TcpKeepaliveIntvl / 1000 ) ;
211
+ #else
212
+
213
+ if ( m_options . TcpKeepaliveIdle != - 1 && m_options . TcpKeepaliveIntvl != - 1 )
214
+ {
215
+ var bytes = new ByteArraySegment ( new byte [ 12 ] ) ;
216
+
217
+ Endianness endian = BitConverter . IsLittleEndian ? Endianness . Little : Endianness . Big ;
210
218
211
- Endianness endian = BitConverter . IsLittleEndian ? Endianness . Little : Endianness . Big ;
219
+ bytes . PutInteger ( endian , m_options . TcpKeepalive , 0 ) ;
220
+ bytes . PutInteger ( endian , m_options . TcpKeepaliveIdle , 4 ) ;
221
+ bytes . PutInteger ( endian , m_options . TcpKeepaliveIntvl , 8 ) ;
212
222
213
- bytes . PutInteger ( endian , m_options . TcpKeepalive , 0 ) ;
214
- bytes . PutInteger ( endian , m_options . TcpKeepaliveIdle , 4 ) ;
215
- bytes . PutInteger ( endian , m_options . TcpKeepaliveIntvl , 8 ) ;
223
+ acceptedSocket . IOControl ( IOControlCode . KeepAliveValues , ( byte [ ] ) bytes , null ) ;
224
+ }
225
+ #endif
216
226
217
- acceptedSocket . IOControl ( IOControlCode . KeepAliveValues , ( byte [ ] ) bytes , null ) ;
218
227
}
219
- }
220
228
221
- // Create the engine object for this connection.
222
- var engine = new StreamEngine ( acceptedSocket , m_options , m_endpoint ) ;
229
+ // Create the engine object for this connection.
230
+ var engine = new StreamEngine ( acceptedSocket , m_options , m_endpoint ) ;
223
231
224
- // Choose I/O thread to run connector in. Given that we are already
225
- // running in an I/O thread, there must be at least one available.
226
- IOThread ? ioThread = ChooseIOThread ( m_options . Affinity ) ;
232
+ // Choose I/O thread to run connector in. Given that we are already
233
+ // running in an I/O thread, there must be at least one available.
234
+ IOThread ? ioThread = ChooseIOThread ( m_options . Affinity ) ;
227
235
228
- Assumes . NotNull ( ioThread ) ;
236
+ Assumes . NotNull ( ioThread ) ;
229
237
230
- // Create and launch a session object.
231
- // TODO: send null in address parameter, is unneeded in this case
232
- SessionBase session = SessionBase . Create ( ioThread , false , m_socket , m_options , new Address ( m_handle . LocalEndPoint ) ) ;
233
- session . IncSeqnum ( ) ;
234
- LaunchChild ( session ) ;
238
+ // Create and launch a session object.
239
+ // TODO: send null in address parameter, is unneeded in this case
240
+ SessionBase session = SessionBase . Create ( ioThread , false , m_socket , m_options , new Address ( m_handle . LocalEndPoint ) ) ;
241
+ session . IncSeqnum ( ) ;
242
+ LaunchChild ( session ) ;
235
243
236
- SendAttach ( session , engine , false ) ;
244
+ SendAttach ( session , engine , false ) ;
237
245
238
- m_socket . EventAccepted ( m_endpoint , acceptedSocket ) ;
246
+ m_socket . EventAccepted ( m_endpoint , acceptedSocket ) ;
239
247
240
- Accept ( ) ;
241
- break ;
242
- }
248
+ Accept ( ) ;
249
+ break ;
250
+ }
243
251
case SocketError . ConnectionReset :
244
252
case SocketError . NoBufferSpaceAvailable :
245
253
case SocketError . TooManyOpenSockets :
246
- {
247
- m_socket . EventAcceptFailed ( m_endpoint , socketError . ToErrorCode ( ) ) ;
254
+ {
255
+ m_socket . EventAcceptFailed ( m_endpoint , socketError . ToErrorCode ( ) ) ;
248
256
249
- Accept ( ) ;
250
- break ;
251
- }
257
+ Accept ( ) ;
258
+ break ;
259
+ }
252
260
default :
253
- {
254
- NetMQException exception = NetMQException . Create ( socketError ) ;
261
+ {
262
+ NetMQException exception = NetMQException . Create ( socketError ) ;
255
263
256
- m_socket . EventAcceptFailed ( m_endpoint , exception . ErrorCode ) ;
257
- throw exception ;
258
- }
264
+ m_socket . EventAcceptFailed ( m_endpoint , exception . ErrorCode ) ;
265
+ throw exception ;
266
+ }
259
267
}
260
268
}
261
269
0 commit comments