aboutsummaryrefslogtreecommitdiff
path: root/src/util/time.cpp
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@protonmail.com>2021-02-11 13:21:31 +0100
committerWladimir J. van der Laan <laanwj@protonmail.com>2021-02-11 14:07:33 +0100
commita1be08405d9bbf771a965dc170ce85ea1ad034c7 (patch)
tree3e5cf94f5d6a393f972ae66cffaeaed066154fe8 /src/util/time.cpp
parent685c16fcb2c718154bf3e6114e35fcac351503f2 (diff)
parent615ba0eb96cf131364c1ceca9d3dedf006fa1e1c (diff)
downloadbitcoin-a1be08405d9bbf771a965dc170ce85ea1ad034c7.tar.xz
Merge #20788: net: add RAII socket and use it instead of bare SOCKET
615ba0eb96cf131364c1ceca9d3dedf006fa1e1c test: add Sock unit tests (Vasil Dimov) 7bd21ce1efc363b3e8ea1d51dd1410ccd66820cb style: rename hSocket to sock (Vasil Dimov) 04ae8469049e1f14585aabfb618ae522150240a7 net: use Sock in InterruptibleRecv() and Socks5() (Vasil Dimov) ba9d73268f9585d4b9254adcf54708f88222798b net: add RAII socket and use it instead of bare SOCKET (Vasil Dimov) dec9b5e850c6aad989e814aea5b630b36f55d580 net: move CloseSocket() from netbase to util/sock (Vasil Dimov) aa17a44551c03b00a47854438afe9f2f89b6ea74 net: move MillisToTimeval() from netbase to util/time (Vasil Dimov) Pull request description: Introduce a class to manage the lifetime of a socket - when the object that contains the socket goes out of scope, the underlying socket will be closed. In addition, the new `Sock` class has a `Send()`, `Recv()` and `Wait()` methods that can be overridden by unit tests to mock the socket operations. The `Wait()` method also hides the `#ifdef USE_POLL poll() #else select() #endif` technique from higher level code. ACKs for top commit: laanwj: Re-ACK 615ba0eb96cf131364c1ceca9d3dedf006fa1e1c jonatack: re-ACK 615ba0eb96cf131364c1ceca9d3dedf006fa1e1c Tree-SHA512: 3003e6bc0259295ca0265ccdeb1522ee25b4abe66d32e6ceaa51b55e0a999df7ddee765f86ce558a788c1953ee2009bfa149b09d494593f7d799c0d7d930bee8
Diffstat (limited to 'src/util/time.cpp')
-rw-r--r--src/util/time.cpp14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/util/time.cpp b/src/util/time.cpp
index d130e4e4d4..295806c54a 100644
--- a/src/util/time.cpp
+++ b/src/util/time.cpp
@@ -7,6 +7,7 @@
#include <config/bitcoin-config.h>
#endif
+#include <compat.h>
#include <util/time.h>
#include <util/check.h>
@@ -117,3 +118,16 @@ int64_t ParseISO8601DateTime(const std::string& str)
return 0;
return (ptime - epoch).total_seconds();
}
+
+struct timeval MillisToTimeval(int64_t nTimeout)
+{
+ struct timeval timeout;
+ timeout.tv_sec = nTimeout / 1000;
+ timeout.tv_usec = (nTimeout % 1000) * 1000;
+ return timeout;
+}
+
+struct timeval MillisToTimeval(std::chrono::milliseconds ms)
+{
+ return MillisToTimeval(count_milliseconds(ms));
+}