diff --git a/source/NetCoreServer/NetCoreServer.csproj b/source/NetCoreServer/NetCoreServer.csproj index b17b36a4..bd978d8a 100644 --- a/source/NetCoreServer/NetCoreServer.csproj +++ b/source/NetCoreServer/NetCoreServer.csproj @@ -2,7 +2,7 @@ net6.0 - 6.5.0.0 + 6.6.0.0 Ivan Shynkarenka Copyright (c) 2019-2022 Ivan Shynkarenka https://github.com/chronoxor/NetCoreServer diff --git a/source/NetCoreServer/SslClient.cs b/source/NetCoreServer/SslClient.cs index fac1dffd..648d63df 100644 --- a/source/NetCoreServer/SslClient.cs +++ b/source/NetCoreServer/SslClient.cs @@ -115,6 +115,27 @@ private SslClient(SslContext context, EndPoint endpoint, string address, int por /// public bool OptionKeepAlive { get; set; } /// + /// Option: TCP keep alive time + /// + /// + /// The number of seconds a TCP connection will remain alive/idle before keepalive probes are sent to the remote + /// + public int OptionTcpKeepAliveTime { get; set; } = -1; + /// + /// Option: TCP keep alive interval + /// + /// + /// The number of seconds a TCP connection will wait for a keepalive response before sending another keepalive probe + /// + public int OptionTcpKeepAliveInterval { get; set; } = -1; + /// + /// Option: TCP keep alive retry count + /// + /// + /// The number of TCP keep alive probes that will be sent before the connection is terminated + /// + public int OptionTcpKeepAliveRetryCount { get; set; } = -1; + /// /// Option: no delay /// /// @@ -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); @@ -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); diff --git a/source/NetCoreServer/SslServer.cs b/source/NetCoreServer/SslServer.cs index 435f9a5c..737c9040 100644 --- a/source/NetCoreServer/SslServer.cs +++ b/source/NetCoreServer/SslServer.cs @@ -118,6 +118,27 @@ private SslServer(SslContext context, EndPoint endpoint, string address, int por /// public bool OptionKeepAlive { get; set; } /// + /// Option: TCP keep alive time + /// + /// + /// The number of seconds a TCP connection will remain alive/idle before keepalive probes are sent to the remote + /// + public int OptionTcpKeepAliveTime { get; set; } = -1; + /// + /// Option: TCP keep alive interval + /// + /// + /// The number of seconds a TCP connection will wait for a keepalive response before sending another keepalive probe + /// + public int OptionTcpKeepAliveInterval { get; set; } = -1; + /// + /// Option: TCP keep alive retry count + /// + /// + /// The number of TCP keep alive probes that will be sent before the connection is terminated + /// + public int OptionTcpKeepAliveRetryCount { get; set; } = -1; + /// /// Option: no delay /// /// diff --git a/source/NetCoreServer/SslSession.cs b/source/NetCoreServer/SslSession.cs index 4f7b6434..2f234ad6 100644 --- a/source/NetCoreServer/SslSession.cs +++ b/source/NetCoreServer/SslSession.cs @@ -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); diff --git a/source/NetCoreServer/TcpClient.cs b/source/NetCoreServer/TcpClient.cs index 85cc1d5a..66f5fd90 100644 --- a/source/NetCoreServer/TcpClient.cs +++ b/source/NetCoreServer/TcpClient.cs @@ -103,6 +103,27 @@ private TcpClient(EndPoint endpoint, string address, int port) /// public bool OptionKeepAlive { get; set; } /// + /// Option: TCP keep alive time + /// + /// + /// The number of seconds a TCP connection will remain alive/idle before keepalive probes are sent to the remote + /// + public int OptionTcpKeepAliveTime { get; set; } = -1; + /// + /// Option: TCP keep alive interval + /// + /// + /// The number of seconds a TCP connection will wait for a keepalive response before sending another keepalive probe + /// + public int OptionTcpKeepAliveInterval { get; set; } = -1; + /// + /// Option: TCP keep alive retry count + /// + /// + /// The number of TCP keep alive probes that will be sent before the connection is terminated + /// + public int OptionTcpKeepAliveRetryCount { get; set; } = -1; + /// /// Option: no delay /// /// @@ -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); @@ -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); diff --git a/source/NetCoreServer/TcpServer.cs b/source/NetCoreServer/TcpServer.cs index 5a9c07fd..6ae13b1b 100644 --- a/source/NetCoreServer/TcpServer.cs +++ b/source/NetCoreServer/TcpServer.cs @@ -108,6 +108,27 @@ private TcpServer(EndPoint endpoint, string address, int port) /// public bool OptionKeepAlive { get; set; } /// + /// Option: TCP keep alive time + /// + /// + /// The number of seconds a TCP connection will remain alive/idle before keepalive probes are sent to the remote + /// + public int OptionTcpKeepAliveTime { get; set; } = -1; + /// + /// Option: TCP keep alive interval + /// + /// + /// The number of seconds a TCP connection will wait for a keepalive response before sending another keepalive probe + /// + public int OptionTcpKeepAliveInterval { get; set; } = -1; + /// + /// Option: TCP keep alive retry count + /// + /// + /// The number of TCP keep alive probes that will be sent before the connection is terminated + /// + public int OptionTcpKeepAliveRetryCount { get; set; } = -1; + /// /// Option: no delay /// /// diff --git a/source/NetCoreServer/TcpSession.cs b/source/NetCoreServer/TcpSession.cs index 17260183..6eb65a71 100644 --- a/source/NetCoreServer/TcpSession.cs +++ b/source/NetCoreServer/TcpSession.cs @@ -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);