From 6682afb5d4d6b7455f85fa3a8f07dd14cae4211e Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Thu, 24 Feb 2022 20:05:49 +0200 Subject: [PATCH] Kernel: Add UDPSocket::try_for_each() for fallible iteration This API will allow users to short circuit iteration and properly propagate errors. --- Kernel/Net/UDPSocket.cpp | 9 +++++++++ Kernel/Net/UDPSocket.h | 1 + 2 files changed, 10 insertions(+) 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 callback) }); } +ErrorOr UDPSocket::try_for_each(Function(const UDPSocket&)> callback) +{ + return sockets_by_port().with_shared([&](const auto& sockets) -> ErrorOr { + for (auto& socket : sockets) + TRY(callback(*socket.value)); + return {}; + }); +} + static Singleton>> s_map; MutexProtected>& 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 from_port(u16); static void for_each(Function); + static ErrorOr try_for_each(Function(const UDPSocket&)>); private: explicit UDPSocket(int protocol, NonnullOwnPtr receive_buffer);