From 0e699743c42455ee3486aba12d4e04aa3afa4967 Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Wed, 17 Apr 2024 16:39:57 -0600 Subject: [PATCH] LibCore: Move PosixSocketHelper's read EOF handling to a helper We'll call this from upcoming LocalSocket methods directly --- Userland/Libraries/LibCore/Socket.cpp | 14 ++++++++++---- Userland/Libraries/LibCore/Socket.h | 1 + 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibCore/Socket.cpp b/Userland/Libraries/LibCore/Socket.cpp index 85e99f2c2a3..c193ab1689c 100644 --- a/Userland/Libraries/LibCore/Socket.cpp +++ b/Userland/Libraries/LibCore/Socket.cpp @@ -105,15 +105,21 @@ ErrorOr PosixSocketHelper::read(Bytes buffer, int flags) } ssize_t nread = TRY(System::recv(m_fd, buffer.data(), buffer.size(), flags)); - m_last_read_was_eof = nread == 0; + if (nread == 0) + did_reach_eof_on_read(); + + return buffer.trim(nread); +} + +void PosixSocketHelper::did_reach_eof_on_read() +{ + m_last_read_was_eof = true; // If a socket read is EOF, then no more data can be read from it because // the protocol has disconnected. In this case, we can just disable the // notifier if we have one. - if (m_last_read_was_eof && m_notifier) + if (m_notifier) m_notifier->set_enabled(false); - - return buffer.trim(nread); } ErrorOr PosixSocketHelper::write(ReadonlyBytes buffer, int flags) diff --git a/Userland/Libraries/LibCore/Socket.h b/Userland/Libraries/LibCore/Socket.h index 993f2543176..3a6892a9280 100644 --- a/Userland/Libraries/LibCore/Socket.h +++ b/Userland/Libraries/LibCore/Socket.h @@ -134,6 +134,7 @@ public: ErrorOr write(ReadonlyBytes, int flags); bool is_eof() const { return !is_open() || m_last_read_was_eof; } + void did_reach_eof_on_read(); bool is_open() const { return m_fd != -1; } void close();