Skip to content

Commit

Permalink
Added keepalive timeout, interval and retry count options #219
Browse files Browse the repository at this point in the history
  • Loading branch information
chronoxor committed Sep 2, 2022
1 parent eab5be7 commit 9fe246c
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 1 deletion.
2 changes: 1 addition & 1 deletion source/NetCoreServer/NetCoreServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Version>6.5.0.0</Version>
<Version>6.6.0.0</Version>
<Authors>Ivan Shynkarenka</Authors>
<Copyright>Copyright (c) 2019-2022 Ivan Shynkarenka</Copyright>
<RepositoryUrl>https://github.com/chronoxor/NetCoreServer</RepositoryUrl>
Expand Down
33 changes: 33 additions & 0 deletions source/NetCoreServer/SslClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,27 @@ private SslClient(SslContext context, EndPoint endpoint, string address, int por
/// </remarks>
public bool OptionKeepAlive { get; set; }
/// <summary>
/// Option: TCP keep alive time
/// </summary>
/// <remarks>
/// The number of seconds a TCP connection will remain alive/idle before keepalive probes are sent to the remote
/// </remarks>
public int OptionTcpKeepAliveTime { get; set; } = -1;
/// <summary>
/// Option: TCP keep alive interval
/// </summary>
/// <remarks>
/// The number of seconds a TCP connection will wait for a keepalive response before sending another keepalive probe
/// </remarks>
public int OptionTcpKeepAliveInterval { get; set; } = -1;
/// <summary>
/// Option: TCP keep alive retry count
/// </summary>
/// <remarks>
/// The number of TCP keep alive probes that will be sent before the connection is terminated
/// </remarks>
public int OptionTcpKeepAliveRetryCount { get; set; } = -1;
/// <summary>
/// Option: no delay
/// </summary>
/// <remarks>
Expand Down Expand Up @@ -244,6 +265,12 @@ public virtual bool Connect()
// Apply the option: keep alive
if (OptionKeepAlive)
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
if (OptionTcpKeepAliveTime >= 0)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, OptionTcpKeepAliveTime);
if (OptionTcpKeepAliveInterval >= 0)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, OptionTcpKeepAliveInterval);
if (OptionTcpKeepAliveRetryCount >= 0)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, OptionTcpKeepAliveRetryCount);
// Apply the option: no delay
if (OptionNoDelay)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
Expand Down Expand Up @@ -813,6 +840,12 @@ private void ProcessConnect(SocketAsyncEventArgs e)
// Apply the option: keep alive
if (OptionKeepAlive)
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
if (OptionTcpKeepAliveTime >= 0)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, OptionTcpKeepAliveTime);
if (OptionTcpKeepAliveInterval >= 0)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, OptionTcpKeepAliveInterval);
if (OptionTcpKeepAliveRetryCount >= 0)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, OptionTcpKeepAliveRetryCount);
// Apply the option: no delay
if (OptionNoDelay)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
Expand Down
21 changes: 21 additions & 0 deletions source/NetCoreServer/SslServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,27 @@ private SslServer(SslContext context, EndPoint endpoint, string address, int por
/// </remarks>
public bool OptionKeepAlive { get; set; }
/// <summary>
/// Option: TCP keep alive time
/// </summary>
/// <remarks>
/// The number of seconds a TCP connection will remain alive/idle before keepalive probes are sent to the remote
/// </remarks>
public int OptionTcpKeepAliveTime { get; set; } = -1;
/// <summary>
/// Option: TCP keep alive interval
/// </summary>
/// <remarks>
/// The number of seconds a TCP connection will wait for a keepalive response before sending another keepalive probe
/// </remarks>
public int OptionTcpKeepAliveInterval { get; set; } = -1;
/// <summary>
/// Option: TCP keep alive retry count
/// </summary>
/// <remarks>
/// The number of TCP keep alive probes that will be sent before the connection is terminated
/// </remarks>
public int OptionTcpKeepAliveRetryCount { get; set; } = -1;
/// <summary>
/// Option: no delay
/// </summary>
/// <remarks>
Expand Down
6 changes: 6 additions & 0 deletions source/NetCoreServer/SslSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ internal void Connect(Socket socket)
// Apply the option: keep alive
if (Server.OptionKeepAlive)
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
if (Server.OptionTcpKeepAliveTime >= 0)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, Server.OptionTcpKeepAliveTime);
if (Server.OptionTcpKeepAliveInterval >= 0)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, Server.OptionTcpKeepAliveInterval);
if (Server.OptionTcpKeepAliveRetryCount >= 0)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, Server.OptionTcpKeepAliveRetryCount);
// Apply the option: no delay
if (Server.OptionNoDelay)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
Expand Down
33 changes: 33 additions & 0 deletions source/NetCoreServer/TcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,27 @@ private TcpClient(EndPoint endpoint, string address, int port)
/// </remarks>
public bool OptionKeepAlive { get; set; }
/// <summary>
/// Option: TCP keep alive time
/// </summary>
/// <remarks>
/// The number of seconds a TCP connection will remain alive/idle before keepalive probes are sent to the remote
/// </remarks>
public int OptionTcpKeepAliveTime { get; set; } = -1;
/// <summary>
/// Option: TCP keep alive interval
/// </summary>
/// <remarks>
/// The number of seconds a TCP connection will wait for a keepalive response before sending another keepalive probe
/// </remarks>
public int OptionTcpKeepAliveInterval { get; set; } = -1;
/// <summary>
/// Option: TCP keep alive retry count
/// </summary>
/// <remarks>
/// The number of TCP keep alive probes that will be sent before the connection is terminated
/// </remarks>
public int OptionTcpKeepAliveRetryCount { get; set; } = -1;
/// <summary>
/// Option: no delay
/// </summary>
/// <remarks>
Expand Down Expand Up @@ -229,6 +250,12 @@ public virtual bool Connect()
// Apply the option: keep alive
if (OptionKeepAlive)
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
if (OptionTcpKeepAliveTime >= 0)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, OptionTcpKeepAliveTime);
if (OptionTcpKeepAliveInterval >= 0)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, OptionTcpKeepAliveInterval);
if (OptionTcpKeepAliveRetryCount >= 0)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, OptionTcpKeepAliveRetryCount);
// Apply the option: no delay
if (OptionNoDelay)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
Expand Down Expand Up @@ -764,6 +791,12 @@ private void ProcessConnect(SocketAsyncEventArgs e)
// Apply the option: keep alive
if (OptionKeepAlive)
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
if (OptionTcpKeepAliveTime >= 0)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, OptionTcpKeepAliveTime);
if (OptionTcpKeepAliveInterval >= 0)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, OptionTcpKeepAliveInterval);
if (OptionTcpKeepAliveRetryCount >= 0)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, OptionTcpKeepAliveRetryCount);
// Apply the option: no delay
if (OptionNoDelay)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
Expand Down
21 changes: 21 additions & 0 deletions source/NetCoreServer/TcpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,27 @@ private TcpServer(EndPoint endpoint, string address, int port)
/// </remarks>
public bool OptionKeepAlive { get; set; }
/// <summary>
/// Option: TCP keep alive time
/// </summary>
/// <remarks>
/// The number of seconds a TCP connection will remain alive/idle before keepalive probes are sent to the remote
/// </remarks>
public int OptionTcpKeepAliveTime { get; set; } = -1;
/// <summary>
/// Option: TCP keep alive interval
/// </summary>
/// <remarks>
/// The number of seconds a TCP connection will wait for a keepalive response before sending another keepalive probe
/// </remarks>
public int OptionTcpKeepAliveInterval { get; set; } = -1;
/// <summary>
/// Option: TCP keep alive retry count
/// </summary>
/// <remarks>
/// The number of TCP keep alive probes that will be sent before the connection is terminated
/// </remarks>
public int OptionTcpKeepAliveRetryCount { get; set; } = -1;
/// <summary>
/// Option: no delay
/// </summary>
/// <remarks>
Expand Down
6 changes: 6 additions & 0 deletions source/NetCoreServer/TcpSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ internal void Connect(Socket socket)
// Apply the option: keep alive
if (Server.OptionKeepAlive)
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
if (Server.OptionTcpKeepAliveTime >= 0)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, Server.OptionTcpKeepAliveTime);
if (Server.OptionTcpKeepAliveInterval >= 0)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, Server.OptionTcpKeepAliveInterval);
if (Server.OptionTcpKeepAliveRetryCount >= 0)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, Server.OptionTcpKeepAliveRetryCount);
// Apply the option: no delay
if (Server.OptionNoDelay)
Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
Expand Down

0 comments on commit 9fe246c

Please sign in to comment.