From 800581b2bf5dacb2134dfda751be08cbf78df978 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat, 26 Mar 2022 13:11:29 +0000 Subject: [PATCH] core: tcp keep-alive parameters for windows (#336) --- src/Simplex/Messaging/Client.hs | 2 +- src/Simplex/Messaging/Transport/KeepAlive.hs | 46 ++++++++++++++------ 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/Simplex/Messaging/Client.hs b/src/Simplex/Messaging/Client.hs index 407bfd40c..e6e87337f 100644 --- a/src/Simplex/Messaging/Client.hs +++ b/src/Simplex/Messaging/Client.hs @@ -114,7 +114,7 @@ smpDefaultConfig = defaultTransport = ("5223", transport @TLS), tcpTimeout = 4_000_000, tcpKeepAlive = Just defaultKeepAliveOpts, - smpPing = 1_200_000_000 -- 20min + smpPing = 600_000_000 -- 10min } data Request = Request diff --git a/src/Simplex/Messaging/Transport/KeepAlive.hs b/src/Simplex/Messaging/Transport/KeepAlive.hs index 3af71c45e..aa308492d 100644 --- a/src/Simplex/Messaging/Transport/KeepAlive.hs +++ b/src/Simplex/Messaging/Transport/KeepAlive.hs @@ -7,19 +7,6 @@ module Simplex.Messaging.Transport.KeepAlive where import Foreign.C (CInt (..)) import Network.Socket -_SOL_TCP :: CInt -_SOL_TCP = 6 - -#if defined(darwin_HOST_OS) -foreign import capi "netinet/tcp.h value TCP_KEEPALIVE" _TCP_KEEPIDLE :: CInt -#else -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 - data KeepAliveOpts = KeepAliveOpts { keepIdle :: Int, keepIntvl :: Int, @@ -34,6 +21,39 @@ defaultKeepAliveOpts = keepCnt = 4 } +_SOL_TCP :: CInt +_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) +foreign import capi "netinet/tcp.h value TCP_KEEPALIVE" _TCP_KEEPIDLE :: CInt +#else +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