Skip to content
Open
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
33 changes: 20 additions & 13 deletions src/Simplex/Messaging/Transport/KeepAlive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,46 @@ _SOL_TCP = 6

#if defined(mingw32_HOST_OS)
-- Windows

-- The values are copied from windows::Win32::Networking::WinSock
-- https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/Networking/WinSock/index.html

_TCP_KEEPIDLE :: CInt
_TCP_KEEPIDLE = 3

_TCP_KEEPINTVL :: CInt
_TCP_KEEPINTVL = 17

_TCP_KEEPCNT :: CInt
_TCP_KEEPCNT = 16

#else
-- Mac/Linux

#if defined(darwin_HOST_OS)
#elif defined(darwin_HOST_OS)
-- macOS (Darwin)
foreign import capi "netinet/tcp.h value TCP_KEEPALIVE" _TCP_KEEPIDLE :: CInt
foreign import capi "netinet/tcp.h value TCP_KEEPINTVL" _TCP_KEEPINTVL :: CInt
foreign import capi "netinet/tcp.h value TCP_KEEPCNT" _TCP_KEEPCNT :: CInt

#elif defined(openbsd_HOST_OS)
-- OpenBSD: These constants are not directly available via setsockopt in the same way.
-- We define dummy values to satisfy compilation, but note that these options may be ignored or unsupported.
_TCP_KEEPIDLE :: CInt
_TCP_KEEPIDLE = 0 -- Dummy value; OpenBSD does not support per-socket keepalive tuning via TCP_* options
_TCP_KEEPINTVL :: CInt
_TCP_KEEPINTVL = 0
_TCP_KEEPCNT :: CInt
_TCP_KEEPCNT = 0

#else
-- Linux and other Unix-like systems (FreeBSD, NetBSD, etc.)
foreign import capi "netinet/tcp.h value TCP_KEEPIDLE" _TCP_KEEPIDLE :: CInt
#endif

foreign import capi "netinet/tcp.h value TCP_KEEPINTVL" _TCP_KEEPINTVL :: CInt

foreign import capi "netinet/tcp.h value TCP_KEEPCNT" _TCP_KEEPCNT :: CInt

#endif

setSocketKeepAlive :: Socket -> KeepAliveOpts -> IO ()
setSocketKeepAlive sock KeepAliveOpts {keepCnt, keepIdle, keepIntvl} = do
setSocketOption sock KeepAlive 1
#if defined(openbsd_HOST_OS)
-- On OpenBSD, these options are typically ignored. We skip them to avoid errors.
#else
setSocketOption sock (SockOpt _SOL_TCP _TCP_KEEPIDLE) keepIdle
setSocketOption sock (SockOpt _SOL_TCP _TCP_KEEPINTVL) keepIntvl
setSocketOption sock (SockOpt _SOL_TCP _TCP_KEEPCNT) keepCnt
#endif

$(J.deriveJSON defaultJSON ''KeepAliveOpts)