diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2022-02-24 20:05:49 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-02-27 20:37:57 +0100 |
commit | 6682afb5d4d6b7455f85fa3a8f07dd14cae4211e (patch) | |
tree | 28d40230b88c0b51e36006ba9b13e4d1686b7115 | |
parent | aabc8d348b677b409162dccc4f85f5fc9fc66ccc (diff) | |
download | serenity-6682afb5d4d6b7455f85fa3a8f07dd14cae4211e.zip |
Kernel: Add UDPSocket::try_for_each() for fallible iteration
This API will allow users to short circuit iteration and properly
propagate errors.
-rw-r--r-- | Kernel/Net/UDPSocket.cpp | 9 | ||||
-rw-r--r-- | Kernel/Net/UDPSocket.h | 1 |
2 files changed, 10 insertions, 0 deletions
diff --git a/Kernel/Net/UDPSocket.cpp b/Kernel/Net/UDPSocket.cpp index 75a93c0de7..b9ba5f45eb 100644 --- a/Kernel/Net/UDPSocket.cpp +++ b/Kernel/Net/UDPSocket.cpp @@ -22,6 +22,15 @@ void UDPSocket::for_each(Function<void(const UDPSocket&)> callback) }); } +ErrorOr<void> UDPSocket::try_for_each(Function<ErrorOr<void>(const UDPSocket&)> callback) +{ + return sockets_by_port().with_shared([&](const auto& sockets) -> ErrorOr<void> { + for (auto& socket : sockets) + TRY(callback(*socket.value)); + return {}; + }); +} + static Singleton<MutexProtected<HashMap<u16, UDPSocket*>>> s_map; MutexProtected<HashMap<u16, UDPSocket*>>& UDPSocket::sockets_by_port() diff --git a/Kernel/Net/UDPSocket.h b/Kernel/Net/UDPSocket.h index 2e1dcd0496..40bfef5779 100644 --- a/Kernel/Net/UDPSocket.h +++ b/Kernel/Net/UDPSocket.h @@ -19,6 +19,7 @@ public: static RefPtr<UDPSocket> from_port(u16); static void for_each(Function<void(const UDPSocket&)>); + static ErrorOr<void> try_for_each(Function<ErrorOr<void>(const UDPSocket&)>); private: explicit UDPSocket(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer); |