summaryrefslogtreecommitdiff
path: root/Kernel/Net
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2022-02-24 20:05:10 +0200
committerAndreas Kling <kling@serenityos.org>2022-02-27 20:37:57 +0100
commit24be52b3f53a66f625d8f19fb3465ebf799d2c78 (patch)
tree7cc12462b0c3d7bca19347eb884e4bef25849175 /Kernel/Net
parent74ab8ccde0aaacc8663cea13f96ded69238da7be (diff)
downloadserenity-24be52b3f53a66f625d8f19fb3465ebf799d2c78.zip
Kernel: Add LocalSocket::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/LocalSocket.cpp9
-rw-r--r--Kernel/Net/LocalSocket.h1
2 files changed, 10 insertions, 0 deletions
diff --git a/Kernel/Net/LocalSocket.cpp b/Kernel/Net/LocalSocket.cpp
index de46196624..57b811637a 100644
--- a/Kernel/Net/LocalSocket.cpp
+++ b/Kernel/Net/LocalSocket.cpp
@@ -34,6 +34,15 @@ void LocalSocket::for_each(Function<void(const LocalSocket&)> callback)
});
}
+ErrorOr<void> LocalSocket::try_for_each(Function<ErrorOr<void>(const LocalSocket&)> callback)
+{
+ return all_sockets().with_shared([&](const auto& sockets) -> ErrorOr<void> {
+ for (auto& socket : sockets)
+ TRY(callback(socket));
+ return {};
+ });
+}
+
ErrorOr<NonnullRefPtr<LocalSocket>> LocalSocket::try_create(int type)
{
auto client_buffer = TRY(DoubleBuffer::try_create());
diff --git a/Kernel/Net/LocalSocket.h b/Kernel/Net/LocalSocket.h
index c9643f7310..4bc5c3e4a3 100644
--- a/Kernel/Net/LocalSocket.h
+++ b/Kernel/Net/LocalSocket.h
@@ -30,6 +30,7 @@ public:
ErrorOr<NonnullRefPtr<OpenFileDescription>> recvfd(const OpenFileDescription& socket_description);
static void for_each(Function<void(const LocalSocket&)>);
+ static ErrorOr<void> try_for_each(Function<ErrorOr<void>(const LocalSocket&)>);
StringView socket_path() const;
ErrorOr<NonnullOwnPtr<KString>> pseudo_path(const OpenFileDescription& description) const override;