aboutsummaryrefslogtreecommitdiff
path: root/src/util/sock.h
diff options
context:
space:
mode:
authorVasil Dimov <vd@FreeBSD.org>2021-04-23 09:43:43 +0200
committerVasil Dimov <vd@FreeBSD.org>2021-12-01 15:22:08 +0100
commitf8bd13f85ae5404adef23a52719d804a5c36b1e8 (patch)
treeeca0fd0670339d479384bbed2ab6d0bbdd1444b9 /src/util/sock.h
parente7507f333bc93047d0baadea4fde19f770dacb56 (diff)
downloadbitcoin-f8bd13f85ae5404adef23a52719d804a5c36b1e8.tar.xz
net: add new method Sock::Accept() that wraps accept()
This will help to increase `Sock` usage and make more code mockable.
Diffstat (limited to 'src/util/sock.h')
-rw-r--r--src/util/sock.h9
1 files changed, 9 insertions, 0 deletions
diff --git a/src/util/sock.h b/src/util/sock.h
index 59cc8c0b1d..7510482857 100644
--- a/src/util/sock.h
+++ b/src/util/sock.h
@@ -10,6 +10,7 @@
#include <util/time.h>
#include <chrono>
+#include <memory>
#include <string>
/**
@@ -97,6 +98,14 @@ public:
[[nodiscard]] virtual int Connect(const sockaddr* addr, socklen_t addr_len) const;
/**
+ * accept(2) wrapper. Equivalent to `std::make_unique<Sock>(accept(this->Get(), addr, addr_len))`.
+ * Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock
+ * implementation.
+ * The returned unique_ptr is empty if `accept()` failed in which case errno will be set.
+ */
+ [[nodiscard]] virtual std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const;
+
+ /**
* getsockopt(2) wrapper. Equivalent to
* `getsockopt(this->Get(), level, opt_name, opt_val, opt_len)`. Code that uses this
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.