diff options
author | Vasil Dimov <vd@FreeBSD.org> | 2021-04-13 12:31:49 +0200 |
---|---|---|
committer | Vasil Dimov <vd@FreeBSD.org> | 2022-04-15 09:14:49 +0200 |
commit | 184e56d6683d05fc84f5153cfff83a2e32883556 (patch) | |
tree | 33fe88e870ac0196ecf245be57a02e98afb0d561 /src/util | |
parent | e14f0fa6a346afecbb1d5470aef5226a8cc33e57 (diff) | |
download | bitcoin-184e56d6683d05fc84f5153cfff83a2e32883556.tar.xz |
net: add new method Sock::SetSockOpt() that wraps setsockopt()
This will help to increase `Sock` usage and make more code mockable.
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/sock.cpp | 5 | ||||
-rw-r--r-- | src/util/sock.h | 10 |
2 files changed, 15 insertions, 0 deletions
diff --git a/src/util/sock.cpp b/src/util/sock.cpp index 2029d70a37..b5c1e28294 100644 --- a/src/util/sock.cpp +++ b/src/util/sock.cpp @@ -105,6 +105,11 @@ int Sock::GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) return getsockopt(m_socket, level, opt_name, static_cast<char*>(opt_val), opt_len); } +int Sock::SetSockOpt(int level, int opt_name, const void* opt_val, socklen_t opt_len) const +{ + return setsockopt(m_socket, level, opt_name, static_cast<const char*>(opt_val), opt_len); +} + bool Sock::Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred) const { #ifdef USE_POLL diff --git a/src/util/sock.h b/src/util/sock.h index 7510482857..dd2913a66c 100644 --- a/src/util/sock.h +++ b/src/util/sock.h @@ -115,6 +115,16 @@ public: void* opt_val, socklen_t* opt_len) const; + /** + * setsockopt(2) wrapper. Equivalent to + * `setsockopt(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. + */ + [[nodiscard]] virtual int SetSockOpt(int level, + int opt_name, + const void* opt_val, + socklen_t opt_len) const; + using Event = uint8_t; /** |