diff --git a/core/backends/lan/lanlinkprovider.cpp b/core/backends/lan/lanlinkprovider.cpp index 6179e8590..be54f5db4 100644 --- a/core/backends/lan/lanlinkprovider.cpp +++ b/core/backends/lan/lanlinkprovider.cpp @@ -12,6 +12,9 @@ #include #include #include +#else +#include +#include #endif #include @@ -602,6 +605,27 @@ void LanLinkProvider::configureSocket(QSslSocket *socket) int count = 3; setsockopt(socket->socketDescriptor(), IPPROTO_TCP, TCP_KEEPCNT, &count, sizeof(count)); #endif + +#if defined(Q_OS_WIN) + int maxIdle = 5 * 60 * 1000; // 5 minutes of idle before sending keep-alives + int interval = 5 * 1000; // 5 seconds interval between probes after 5 minute delay + DWORD nop; + + // see https://learn.microsoft.com/en-us/windows/win32/winsock/sio-keepalive-vals + struct tcp_keepalive keepalive = { + 1 /* true */, + maxIdle, + interval + }; + + int rv = WSAIoctl(socket->socketDescriptor(), SIO_KEEPALIVE_VALS, &keepalive, + sizeof(keepalive), nullptr, 0, &nop, + nullptr, nullptr); + if (!rv) { + int error = WSAGetLastError(); + qCDebug(KDECONNECT_CORE) << "Could not enable TCP Keep-Alive: " << error; + } +#endif } void LanLinkProvider::addLink(const QString &deviceId, QSslSocket *socket, NetworkPacket *receivedPacket, LanDeviceLink::ConnectionStarted connectionOrigin)