mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 22:50:59 +01:00
net: move the constant maxWait out of InterruptibleRecv()
Move `maxWait` out of `InterruptibleRecv()` and rename it to `MAX_WAIT_FOR_IO` so that it can be reused by other code.
This commit is contained in:
@@ -14,6 +14,7 @@
|
|||||||
#include <util/time.h>
|
#include <util/time.h>
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#include <chrono>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
@@ -360,9 +361,6 @@ static IntrRecvError InterruptibleRecv(uint8_t* data, size_t len, int timeout, c
|
|||||||
{
|
{
|
||||||
int64_t curTime = GetTimeMillis();
|
int64_t curTime = GetTimeMillis();
|
||||||
int64_t endTime = curTime + timeout;
|
int64_t endTime = curTime + timeout;
|
||||||
// Maximum time to wait for I/O readiness. It will take up until this time
|
|
||||||
// (in millis) to break off in case of an interruption.
|
|
||||||
const int64_t maxWait = 1000;
|
|
||||||
while (len > 0 && curTime < endTime) {
|
while (len > 0 && curTime < endTime) {
|
||||||
ssize_t ret = sock.Recv(data, len, 0); // Optimistically try the recv first
|
ssize_t ret = sock.Recv(data, len, 0); // Optimistically try the recv first
|
||||||
if (ret > 0) {
|
if (ret > 0) {
|
||||||
@@ -373,10 +371,11 @@ static IntrRecvError InterruptibleRecv(uint8_t* data, size_t len, int timeout, c
|
|||||||
} else { // Other error or blocking
|
} else { // Other error or blocking
|
||||||
int nErr = WSAGetLastError();
|
int nErr = WSAGetLastError();
|
||||||
if (nErr == WSAEINPROGRESS || nErr == WSAEWOULDBLOCK || nErr == WSAEINVAL) {
|
if (nErr == WSAEINPROGRESS || nErr == WSAEWOULDBLOCK || nErr == WSAEINVAL) {
|
||||||
// Only wait at most maxWait milliseconds at a time, unless
|
// Only wait at most MAX_WAIT_FOR_IO at a time, unless
|
||||||
// we're approaching the end of the specified total timeout
|
// we're approaching the end of the specified total timeout
|
||||||
int timeout_ms = std::min(endTime - curTime, maxWait);
|
const auto remaining = std::chrono::milliseconds{endTime - curTime};
|
||||||
if (!sock.Wait(std::chrono::milliseconds{timeout_ms}, Sock::RECV)) {
|
const auto timeout = std::min(remaining, std::chrono::milliseconds{MAX_WAIT_FOR_IO});
|
||||||
|
if (!sock.Wait(timeout, Sock::RECV)) {
|
||||||
return IntrRecvError::NetworkError;
|
return IntrRecvError::NetworkError;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -6,10 +6,17 @@
|
|||||||
#define BITCOIN_UTIL_SOCK_H
|
#define BITCOIN_UTIL_SOCK_H
|
||||||
|
|
||||||
#include <compat.h>
|
#include <compat.h>
|
||||||
|
#include <util/time.h>
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maximum time to wait for I/O readiness.
|
||||||
|
* It will take up until this time to break off in case of an interruption.
|
||||||
|
*/
|
||||||
|
static constexpr auto MAX_WAIT_FOR_IO = 1s;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* RAII helper class that manages a socket. Mimics `std::unique_ptr`, but instead of a pointer it
|
* RAII helper class that manages a socket. Mimics `std::unique_ptr`, but instead of a pointer it
|
||||||
* contains a socket and closes it automatically when it goes out of scope.
|
* contains a socket and closes it automatically when it goes out of scope.
|
||||||
|
|||||||
Reference in New Issue
Block a user