diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2022-02-24 20:05:24 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-02-27 20:37:57 +0100 |
commit | aabc8d348b677b409162dccc4f85f5fc9fc66ccc (patch) | |
tree | 344876ac8b48875563642a0adcfb41cb0d6e37ad /Kernel/Net | |
parent | 24be52b3f53a66f625d8f19fb3465ebf799d2c78 (diff) | |
download | serenity-aabc8d348b677b409162dccc4f85f5fc9fc66ccc.zip |
Kernel: Add TCPSocket::try_for_each() for fallible iteration
This API will allow users to short circuit iteration and properly
propagate errors.
Diffstat (limited to 'Kernel/Net')
-rw-r--r-- | Kernel/Net/TCPSocket.cpp | 9 | ||||
-rw-r--r-- | Kernel/Net/TCPSocket.h | 1 |
2 files changed, 10 insertions, 0 deletions
diff --git a/Kernel/Net/TCPSocket.cpp b/Kernel/Net/TCPSocket.cpp index 2fe8161a41..1b6de7df24 100644 --- a/Kernel/Net/TCPSocket.cpp +++ b/Kernel/Net/TCPSocket.cpp @@ -29,6 +29,15 @@ void TCPSocket::for_each(Function<void(const TCPSocket&)> callback) }); } +ErrorOr<void> TCPSocket::try_for_each(Function<ErrorOr<void>(const TCPSocket&)> callback) +{ + return sockets_by_tuple().with_shared([&](const auto& sockets) -> ErrorOr<void> { + for (auto& it : sockets) + TRY(callback(*it.value)); + return {}; + }); +} + bool TCPSocket::unref() const { bool did_hit_zero = sockets_by_tuple().with_exclusive([&](auto& table) { diff --git a/Kernel/Net/TCPSocket.h b/Kernel/Net/TCPSocket.h index 607f0c628e..9c30d1ebd5 100644 --- a/Kernel/Net/TCPSocket.h +++ b/Kernel/Net/TCPSocket.h @@ -19,6 +19,7 @@ namespace Kernel { class TCPSocket final : public IPv4Socket { public: static void for_each(Function<void(const TCPSocket&)>); + static ErrorOr<void> try_for_each(Function<ErrorOr<void>(const TCPSocket&)>); static ErrorOr<NonnullRefPtr<TCPSocket>> try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer); virtual ~TCPSocket() override; |