From 17d3592cabffe3bc413df45cb4b166b55d600f44 Mon Sep 17 00:00:00 2001 From: sin-ack Date: Sun, 6 Feb 2022 16:40:12 +0000 Subject: [PATCH] LibCore: Add a timeout option to UDPSocket::connect This allows us to set a timeout during connection and during receive and send operations. I didn't add this to the other connect calls as it's not used anywhere else for the time being. --- Userland/Libraries/LibCore/Stream.cpp | 15 ++++++++++++--- Userland/Libraries/LibCore/Stream.h | 6 ++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibCore/Stream.cpp b/Userland/Libraries/LibCore/Stream.cpp index 21988042b3..0498ab3f46 100644 --- a/Userland/Libraries/LibCore/Stream.cpp +++ b/Userland/Libraries/LibCore/Stream.cpp @@ -390,6 +390,12 @@ ErrorOr PosixSocketHelper::set_close_on_exec(bool enabled) return {}; } +ErrorOr PosixSocketHelper::set_receive_timeout(Time timeout) +{ + auto timeout_spec = timeout.to_timespec(); + return System::setsockopt(m_fd, SOL_SOCKET, SO_RCVTIMEO, &timeout_spec, sizeof(timeout_spec)); +} + void PosixSocketHelper::setup_notifier() { if (!m_notifier) @@ -438,18 +444,21 @@ ErrorOr PosixSocketHelper::pending_bytes() const return static_cast(value); } -ErrorOr> UDPSocket::connect(String const& host, u16 port) +ErrorOr> UDPSocket::connect(String const& host, u16 port, Optional