mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-22 20:11:46 +00:00
Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a8d58085cb | ||
|
|
00e2fd89e8 | ||
|
|
530afcc12e | ||
|
|
da42c375ca | ||
|
|
a694b8c306 |
No files matched your search
@@ -4034,7 +4034,8 @@ private:
|
|||||||
|
|
||||||
PollerWaker waker_;
|
PollerWaker waker_;
|
||||||
|
|
||||||
// fds_[0] is the waker's, always armed; tokens_[i] belongs to fds_[i].
|
// fds_[0] is the waker's, which is always armed; tokens_[i] belongs to
|
||||||
|
// fds_[i].
|
||||||
std::vector<struct pollfd> fds_;
|
std::vector<struct pollfd> fds_;
|
||||||
std::vector<Token> tokens_;
|
std::vector<Token> tokens_;
|
||||||
std::unordered_map<socket_t, size_t> index_;
|
std::unordered_map<socket_t, size_t> index_;
|
||||||
@@ -4087,11 +4088,6 @@ private:
|
|||||||
// The best poller for this platform, or nullptr when it cannot be created.
|
// The best poller for this platform, or nullptr when it cannot be created.
|
||||||
std::unique_ptr<Poller> make_poller();
|
std::unique_ptr<Poller> make_poller();
|
||||||
|
|
||||||
// The Poller::wait() timeout until `deadline`: -1 for time_point::max(),
|
|
||||||
// otherwise whole milliseconds, rounded up and capped at INT_MAX.
|
|
||||||
int poll_timeout_until(std::chrono::steady_clock::time_point deadline,
|
|
||||||
std::chrono::steady_clock::time_point now);
|
|
||||||
|
|
||||||
class BufferStream final : public Stream {
|
class BufferStream final : public Stream {
|
||||||
public:
|
public:
|
||||||
BufferStream() = default;
|
BufferStream() = default;
|
||||||
@@ -7383,7 +7379,7 @@ inline void drain_and_close_socket(socket_t sock,
|
|||||||
const auto n = read_socket(sock, buf, sizeof(buf), CPPHTTPLIB_RECV_FLAGS);
|
const auto n = read_socket(sock, buf, sizeof(buf), CPPHTTPLIB_RECV_FLAGS);
|
||||||
if (n <= 0) { break; }
|
if (n <= 0) { break; }
|
||||||
total += static_cast<size_t>(n);
|
total += static_cast<size_t>(n);
|
||||||
} while (total < size_t(1024u * 1024u) && usec_left() > 0); // bound #2
|
} while (usec_left() > 0 && total < size_t(1024u * 1024u)); // bound #2
|
||||||
|
|
||||||
shutdown_socket(sock);
|
shutdown_socket(sock);
|
||||||
close_socket(sock);
|
close_socket(sock);
|
||||||
@@ -8266,18 +8262,6 @@ inline std::unique_ptr<Poller> make_poller() {
|
|||||||
return PollPoller::create();
|
return PollPoller::create();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline int poll_timeout_until(std::chrono::steady_clock::time_point deadline,
|
|
||||||
std::chrono::steady_clock::time_point now) {
|
|
||||||
using namespace std::chrono;
|
|
||||||
if (deadline == (steady_clock::time_point::max)()) { return -1; }
|
|
||||||
if (deadline <= now) { return 0; }
|
|
||||||
const auto left = deadline - now;
|
|
||||||
auto msec = duration_cast<milliseconds>(left);
|
|
||||||
if (msec < left) { msec += milliseconds(1); }
|
|
||||||
const auto cap = milliseconds((std::numeric_limits<int>::max)());
|
|
||||||
return static_cast<int>((std::min)(msec, cap).count());
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool bind_ip_address(socket_t sock, const std::string &host) {
|
inline bool bind_ip_address(socket_t sock, const std::string &host) {
|
||||||
struct addrinfo hints;
|
struct addrinfo hints;
|
||||||
struct addrinfo *result;
|
struct addrinfo *result;
|
||||||
@@ -16592,10 +16576,16 @@ inline bool Server::parking_loop(TaskQueue &task_queue) {
|
|||||||
poller.arm(listen_sock, listen_token) ? never : now + check_interval;
|
poller.arm(listen_sock, listen_token) ? never : now + check_interval;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto until =
|
// At most a minute, which keeps the timeout below within an int.
|
||||||
(std::min)({st.next_deadline(), listen_rearm_at, next_idle,
|
auto until = (std::min)({st.next_deadline(), listen_rearm_at, next_idle,
|
||||||
st.backlog.empty() ? never : now + check_interval});
|
st.backlog.empty() ? never : now + check_interval,
|
||||||
poller.wait(detail::poll_timeout_until(until, now), ready);
|
now + std::chrono::seconds(60)});
|
||||||
|
// Rounded up, so that the wait does not end just short of the deadline.
|
||||||
|
auto timeout = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||||
|
until - now + std::chrono::microseconds(999));
|
||||||
|
poller.wait(static_cast<int>((std::max)(timeout.count(),
|
||||||
|
std::chrono::milliseconds::rep(0))),
|
||||||
|
ready);
|
||||||
if (svr_sock_ != listen_sock) { break; }
|
if (svr_sock_ != listen_sock) { break; }
|
||||||
now = Clock::now();
|
now = Clock::now();
|
||||||
|
|
||||||
|
|||||||
@@ -34049,20 +34049,6 @@ TEST_P(PollerTest, WaitHonoursItsTimeout) {
|
|||||||
EXPECT_LT(elapsed, std::chrono::seconds(1));
|
EXPECT_LT(elapsed, std::chrono::seconds(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_P(PollerTest, NegativeTimeoutWaitsUntilReady) {
|
|
||||||
TestSocketPair sp;
|
|
||||||
ASSERT_TRUE(sp.ok());
|
|
||||||
ASSERT_TRUE(poller_->arm(sp.reader(), 3));
|
|
||||||
std::thread writer([&] {
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(20));
|
|
||||||
sp.make_readable();
|
|
||||||
});
|
|
||||||
|
|
||||||
auto ready = wait_for(-1);
|
|
||||||
writer.join();
|
|
||||||
EXPECT_EQ(Tokens{3}, ready);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_P(PollerTest, OnlyTheReadyDescriptorsAreDelivered) {
|
TEST_P(PollerTest, OnlyTheReadyDescriptorsAreDelivered) {
|
||||||
std::vector<std::unique_ptr<TestSocketPair>> sps;
|
std::vector<std::unique_ptr<TestSocketPair>> sps;
|
||||||
for (detail::Poller::Token i = 0; i < 8; i++) {
|
for (detail::Poller::Token i = 0; i < 8; i++) {
|
||||||
@@ -34181,20 +34167,6 @@ TEST(PollerFactoryTest, MakePollerWorks) {
|
|||||||
EXPECT_TRUE(poller->wait(1000, ready));
|
EXPECT_TRUE(poller->wait(1000, ready));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(PollTimeoutTest, LastsUntilTheDeadline) {
|
|
||||||
using namespace std::chrono;
|
|
||||||
const auto now = steady_clock::now();
|
|
||||||
auto timeout = [&](steady_clock::time_point deadline) {
|
|
||||||
return detail::poll_timeout_until(deadline, now);
|
|
||||||
};
|
|
||||||
EXPECT_EQ(-1, timeout((steady_clock::time_point::max)()));
|
|
||||||
EXPECT_EQ(0, timeout(now - milliseconds(5)));
|
|
||||||
EXPECT_EQ(1, timeout(now + nanoseconds(1)));
|
|
||||||
EXPECT_EQ(1, timeout(now + milliseconds(1)));
|
|
||||||
EXPECT_EQ(2, timeout(now + microseconds(1001)));
|
|
||||||
EXPECT_EQ((std::numeric_limits<int>::max)(), timeout(now + hours(1000)));
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Keep-alive parking
|
// Keep-alive parking
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|||||||
Reference in new issue
Block a user