浏览代码

Kernel: Add UDPSocket::try_for_each() for fallible iteration

This API will allow users to short circuit iteration and properly
propagate errors.
Idan Horowitz 3 年之前
父节点
当前提交
6682afb5d4
共有 2 个文件被更改,包括 10 次插入0 次删除
  1. 9 0
      Kernel/Net/UDPSocket.cpp
  2. 1 0
      Kernel/Net/UDPSocket.h

+ 9 - 0
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()

+ 1 - 0
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);