Skip to content

Support Tcp KeepAlive under Linux (.net8 only) #1132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/NetMQ.Tests/SocketOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public void GetAndSetAllProperties()
socket.Options.TcpKeepalive = true;
Assert.True(socket.Options.TcpKeepalive);

// socket.Options.TcpKeepaliveCnt = 100;
// Assert.Equal(100, socket.Options.TcpKeepaliveCnt);
socket.Options.TcpKeepaliveCnt = 100;
Assert.Equal(100, socket.Options.TcpKeepaliveCnt);

socket.Options.TcpKeepaliveIdle = TimeSpan.FromMilliseconds(100);
Assert.Equal(TimeSpan.FromMilliseconds(100), socket.Options.TcpKeepaliveIdle);
Expand Down
7 changes: 7 additions & 0 deletions src/NetMQ/Core/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,10 @@ public void SetSocketOption(ZmqSocketOption option, object? optionValue)
TcpKeepalive = tcpKeepalive;
break;

case ZmqSocketOption.TcpKeepaliveCnt:
TcpKeepaliveCnt = Get<int>();
break;

case ZmqSocketOption.DelayAttachOnConnect:
DelayAttachOnConnect = Get<bool>();
break;
Expand Down Expand Up @@ -649,6 +653,9 @@ public void SetSocketOption(ZmqSocketOption option, object? optionValue)
case ZmqSocketOption.TcpKeepalive:
return TcpKeepalive;

case ZmqSocketOption.TcpKeepaliveCnt:
return TcpKeepaliveCnt;

case ZmqSocketOption.DelayAttachOnConnect:
return DelayAttachOnConnect;

Expand Down
17 changes: 9 additions & 8 deletions src/NetMQ/Core/Transports/Tcp/TcpConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ public void OutCompleted(SocketError socketError, int bytesTransferred)
// Set the TCP keep-alive option values to the underlying socket.
m_s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, m_options.TcpKeepalive);

#if NET
if (m_options.TcpKeepaliveIdle != -1)
m_s.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, m_options.TcpKeepaliveIdle / 1000);
if (m_options.TcpKeepaliveIntvl != -1)
m_s.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, m_options.TcpKeepaliveIntvl / 1000);
if (m_options.TcpKeepaliveCnt != -1)
m_s.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, m_options.TcpKeepaliveCnt);
#else
if (m_options.TcpKeepaliveIdle != -1 && m_options.TcpKeepaliveIntvl != -1)
{
// Write the TCP keep-alive options to a byte-array, to feed to the IOControl method..
Expand All @@ -259,16 +267,9 @@ public void OutCompleted(SocketError socketError, int bytesTransferred)
bytes.PutInteger(endian, m_options.TcpKeepaliveIdle, 4);
bytes.PutInteger(endian, m_options.TcpKeepaliveIntvl, 8);

#if NET
if (!OperatingSystem.IsWindows())
{
throw new InvalidOperationException("Not supported on you platform"); // There is a pull request for .net8.0

}
#endif
m_s.IOControl(IOControlCode.KeepAliveValues, (byte[])bytes, null);

}
#endif
}

// Create the engine object for this connection.
Expand Down
16 changes: 10 additions & 6 deletions src/NetMQ/Core/Transports/Tcp/TcpListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ public void InCompleted(SocketError socketError, int bytesTransferred)
if (m_options.TcpKeepalive != -1)
{
acceptedSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, m_options.TcpKeepalive);
#if NET
if (m_options.TcpKeepaliveIdle != -1)
acceptedSocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, m_options.TcpKeepaliveIdle / 1000);
if (m_options.TcpKeepaliveIntvl != -1)
acceptedSocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, m_options.TcpKeepaliveIntvl / 1000);
if (m_options.TcpKeepaliveCnt != -1)
acceptedSocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, m_options.TcpKeepaliveCnt);
#else

if (m_options.TcpKeepaliveIdle != -1 && m_options.TcpKeepaliveIntvl != -1)
{
Expand All @@ -214,15 +222,11 @@ public void InCompleted(SocketError socketError, int bytesTransferred)
bytes.PutInteger(endian, m_options.TcpKeepalive, 0);
bytes.PutInteger(endian, m_options.TcpKeepaliveIdle, 4);
bytes.PutInteger(endian, m_options.TcpKeepaliveIntvl, 8);
#if NET
if (!OperatingSystem.IsWindows())
{
throw new InvalidOperationException("Not supported on you platform"); // There is a pull request for .net8.0

}
#endif

acceptedSocket.IOControl(IOControlCode.KeepAliveValues, (byte[])bytes, null);
}
#endif
}

// Create the engine object for this connection.
Expand Down
9 changes: 9 additions & 0 deletions src/NetMQ/Core/ZmqSocketOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ internal enum ZmqSocketOption
/// </summary>
TcpKeepalive = 34,

/// <summary>
/// The maximum number of keepalive probes TCP should send before dropping the connection.
/// </summary>
/// <remarks>
/// This setting controls how many unacknowledged probes are sent before the connection is considered dead.
/// A value of -1 (the default) means to use the OS default setting.
/// </remarks>
TcpKeepaliveCnt = 35,

/// <summary>
/// The keep-alive time - the duration between two keepalive transmissions in idle condition.
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions src/NetMQ/SocketOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,21 @@ public bool TcpKeepalive
// See http://api.zeromq.org/3-2:zmq-getsockopt
}


/// <summary>
/// Get or set the maximum number of TCP keepalive probes to send before dropping the connection.
/// </summary>
/// <remarks>
/// This value determines how many unacknowledged keepalive probes TCP should send before assuming the connection is dead.
/// A lower value means faster detection of dead peers, while a higher value allows more tolerance for temporary disruptions.
/// A value of -1 (the default) means to use the OS default setting.
/// </remarks>
public int TcpKeepaliveCnt
{
get => m_socket.GetSocketOption(ZmqSocketOption.TcpKeepaliveCnt);
set => m_socket.SetSocketOption(ZmqSocketOption.TcpKeepaliveCnt, value);
}

/// <summary>
/// Get or set the keep-alive time - the duration between two keepalive transmissions in idle condition.
/// The TCP keepalive period is required by socket implementers to be configurable and by default is
Expand Down