mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 23:18:14 +01:00
Merge #20788: net: add RAII socket and use it instead of bare SOCKET
615ba0eb96test: add Sock unit tests (Vasil Dimov)7bd21ce1efstyle: rename hSocket to sock (Vasil Dimov)04ae846904net: use Sock in InterruptibleRecv() and Socks5() (Vasil Dimov)ba9d73268fnet: add RAII socket and use it instead of bare SOCKET (Vasil Dimov)dec9b5e850net: move CloseSocket() from netbase to util/sock (Vasil Dimov)aa17a44551net: 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-ACK615ba0eb96jonatack: re-ACK615ba0eb96Tree-SHA512: 3003e6bc0259295ca0265ccdeb1522ee25b4abe66d32e6ceaa51b55e0a999df7ddee765f86ce558a788c1953ee2009bfa149b09d494593f7d799c0d7d930bee8
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user