Skip to content

Commit 9fe246c

Browse files
committed
Added keepalive timeout, interval and retry count options #219
1 parent eab5be7 commit 9fe246c

File tree

7 files changed

+121
-1
lines changed

7 files changed

+121
-1
lines changed

source/NetCoreServer/NetCoreServer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
5-
<Version>6.5.0.0</Version>
5+
<Version>6.6.0.0</Version>
66
<Authors>Ivan Shynkarenka</Authors>
77
<Copyright>Copyright (c) 2019-2022 Ivan Shynkarenka</Copyright>
88
<RepositoryUrl>https://github.com/chronoxor/NetCoreServer</RepositoryUrl>

source/NetCoreServer/SslClient.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,27 @@ private SslClient(SslContext context, EndPoint endpoint, string address, int por
115115
/// </remarks>
116116
public bool OptionKeepAlive { get; set; }
117117
/// <summary>
118+
/// Option: TCP keep alive time
119+
/// </summary>
120+
/// <remarks>
121+
/// The number of seconds a TCP connection will remain alive/idle before keepalive probes are sent to the remote
122+
/// </remarks>
123+
public int OptionTcpKeepAliveTime { get; set; } = -1;
124+
/// <summary>
125+
/// Option: TCP keep alive interval
126+
/// </summary>
127+
/// <remarks>
128+
/// The number of seconds a TCP connection will wait for a keepalive response before sending another keepalive probe
129+
/// </remarks>
130+
public int OptionTcpKeepAliveInterval { get; set; } = -1;
131+
/// <summary>
132+
/// Option: TCP keep alive retry count
133+
/// </summary>
134+
/// <remarks>
135+
/// The number of TCP keep alive probes that will be sent before the connection is terminated
136+
/// </remarks>
137+
public int OptionTcpKeepAliveRetryCount { get; set; } = -1;
138+
/// <summary>
118139
/// Option: no delay
119140
/// </summary>
120141
/// <remarks>
@@ -244,6 +265,12 @@ public virtual bool Connect()
244265
// Apply the option: keep alive
245266
if (OptionKeepAlive)
246267
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
268+
if (OptionTcpKeepAliveTime >= 0)
269+
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, OptionTcpKeepAliveTime);
270+
if (OptionTcpKeepAliveInterval >= 0)
271+
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, OptionTcpKeepAliveInterval);
272+
if (OptionTcpKeepAliveRetryCount >= 0)
273+
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, OptionTcpKeepAliveRetryCount);
247274
// Apply the option: no delay
248275
if (OptionNoDelay)
249276
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
@@ -813,6 +840,12 @@ private void ProcessConnect(SocketAsyncEventArgs e)
813840
// Apply the option: keep alive
814841
if (OptionKeepAlive)
815842
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
843+
if (OptionTcpKeepAliveTime >= 0)
844+
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, OptionTcpKeepAliveTime);
845+
if (OptionTcpKeepAliveInterval >= 0)
846+
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, OptionTcpKeepAliveInterval);
847+
if (OptionTcpKeepAliveRetryCount >= 0)
848+
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, OptionTcpKeepAliveRetryCount);
816849
// Apply the option: no delay
817850
if (OptionNoDelay)
818851
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);

source/NetCoreServer/SslServer.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,27 @@ private SslServer(SslContext context, EndPoint endpoint, string address, int por
118118
/// </remarks>
119119
public bool OptionKeepAlive { get; set; }
120120
/// <summary>
121+
/// Option: TCP keep alive time
122+
/// </summary>
123+
/// <remarks>
124+
/// The number of seconds a TCP connection will remain alive/idle before keepalive probes are sent to the remote
125+
/// </remarks>
126+
public int OptionTcpKeepAliveTime { get; set; } = -1;
127+
/// <summary>
128+
/// Option: TCP keep alive interval
129+
/// </summary>
130+
/// <remarks>
131+
/// The number of seconds a TCP connection will wait for a keepalive response before sending another keepalive probe
132+
/// </remarks>
133+
public int OptionTcpKeepAliveInterval { get; set; } = -1;
134+
/// <summary>
135+
/// Option: TCP keep alive retry count
136+
/// </summary>
137+
/// <remarks>
138+
/// The number of TCP keep alive probes that will be sent before the connection is terminated
139+
/// </remarks>
140+
public int OptionTcpKeepAliveRetryCount { get; set; } = -1;
141+
/// <summary>
121142
/// Option: no delay
122143
/// </summary>
123144
/// <remarks>

source/NetCoreServer/SslSession.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ internal void Connect(Socket socket)
106106
// Apply the option: keep alive
107107
if (Server.OptionKeepAlive)
108108
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
109+
if (Server.OptionTcpKeepAliveTime >= 0)
110+
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, Server.OptionTcpKeepAliveTime);
111+
if (Server.OptionTcpKeepAliveInterval >= 0)
112+
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, Server.OptionTcpKeepAliveInterval);
113+
if (Server.OptionTcpKeepAliveRetryCount >= 0)
114+
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, Server.OptionTcpKeepAliveRetryCount);
109115
// Apply the option: no delay
110116
if (Server.OptionNoDelay)
111117
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);

source/NetCoreServer/TcpClient.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,27 @@ private TcpClient(EndPoint endpoint, string address, int port)
103103
/// </remarks>
104104
public bool OptionKeepAlive { get; set; }
105105
/// <summary>
106+
/// Option: TCP keep alive time
107+
/// </summary>
108+
/// <remarks>
109+
/// The number of seconds a TCP connection will remain alive/idle before keepalive probes are sent to the remote
110+
/// </remarks>
111+
public int OptionTcpKeepAliveTime { get; set; } = -1;
112+
/// <summary>
113+
/// Option: TCP keep alive interval
114+
/// </summary>
115+
/// <remarks>
116+
/// The number of seconds a TCP connection will wait for a keepalive response before sending another keepalive probe
117+
/// </remarks>
118+
public int OptionTcpKeepAliveInterval { get; set; } = -1;
119+
/// <summary>
120+
/// Option: TCP keep alive retry count
121+
/// </summary>
122+
/// <remarks>
123+
/// The number of TCP keep alive probes that will be sent before the connection is terminated
124+
/// </remarks>
125+
public int OptionTcpKeepAliveRetryCount { get; set; } = -1;
126+
/// <summary>
106127
/// Option: no delay
107128
/// </summary>
108129
/// <remarks>
@@ -229,6 +250,12 @@ public virtual bool Connect()
229250
// Apply the option: keep alive
230251
if (OptionKeepAlive)
231252
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
253+
if (OptionTcpKeepAliveTime >= 0)
254+
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, OptionTcpKeepAliveTime);
255+
if (OptionTcpKeepAliveInterval >= 0)
256+
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, OptionTcpKeepAliveInterval);
257+
if (OptionTcpKeepAliveRetryCount >= 0)
258+
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, OptionTcpKeepAliveRetryCount);
232259
// Apply the option: no delay
233260
if (OptionNoDelay)
234261
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
@@ -764,6 +791,12 @@ private void ProcessConnect(SocketAsyncEventArgs e)
764791
// Apply the option: keep alive
765792
if (OptionKeepAlive)
766793
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
794+
if (OptionTcpKeepAliveTime >= 0)
795+
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, OptionTcpKeepAliveTime);
796+
if (OptionTcpKeepAliveInterval >= 0)
797+
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, OptionTcpKeepAliveInterval);
798+
if (OptionTcpKeepAliveRetryCount >= 0)
799+
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, OptionTcpKeepAliveRetryCount);
767800
// Apply the option: no delay
768801
if (OptionNoDelay)
769802
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);

source/NetCoreServer/TcpServer.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,27 @@ private TcpServer(EndPoint endpoint, string address, int port)
108108
/// </remarks>
109109
public bool OptionKeepAlive { get; set; }
110110
/// <summary>
111+
/// Option: TCP keep alive time
112+
/// </summary>
113+
/// <remarks>
114+
/// The number of seconds a TCP connection will remain alive/idle before keepalive probes are sent to the remote
115+
/// </remarks>
116+
public int OptionTcpKeepAliveTime { get; set; } = -1;
117+
/// <summary>
118+
/// Option: TCP keep alive interval
119+
/// </summary>
120+
/// <remarks>
121+
/// The number of seconds a TCP connection will wait for a keepalive response before sending another keepalive probe
122+
/// </remarks>
123+
public int OptionTcpKeepAliveInterval { get; set; } = -1;
124+
/// <summary>
125+
/// Option: TCP keep alive retry count
126+
/// </summary>
127+
/// <remarks>
128+
/// The number of TCP keep alive probes that will be sent before the connection is terminated
129+
/// </remarks>
130+
public int OptionTcpKeepAliveRetryCount { get; set; } = -1;
131+
/// <summary>
111132
/// Option: no delay
112133
/// </summary>
113134
/// <remarks>

source/NetCoreServer/TcpSession.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ internal void Connect(Socket socket)
103103
// Apply the option: keep alive
104104
if (Server.OptionKeepAlive)
105105
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
106+
if (Server.OptionTcpKeepAliveTime >= 0)
107+
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, Server.OptionTcpKeepAliveTime);
108+
if (Server.OptionTcpKeepAliveInterval >= 0)
109+
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, Server.OptionTcpKeepAliveInterval);
110+
if (Server.OptionTcpKeepAliveRetryCount >= 0)
111+
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, Server.OptionTcpKeepAliveRetryCount);
106112
// Apply the option: no delay
107113
if (Server.OptionNoDelay)
108114
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);

0 commit comments

Comments
 (0)