mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2025-05-15 09:18:27 +00:00
Adjust the way to check if the sockt is still alive.
This commit is contained in:
parent
fb6c4cdfb4
commit
6c673b21e5
1 changed files with 14 additions and 12 deletions
26
httplib.h
26
httplib.h
|
@ -2337,14 +2337,6 @@ inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool is_socket_alive(socket_t sock) {
|
|
||||||
if (detail::select_read(sock, 0, 0) == 0) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
char buf[1];
|
|
||||||
return recv(sock, &buf[0], sizeof(buf), MSG_PEEK) > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
class SocketStream : public Stream {
|
class SocketStream : public Stream {
|
||||||
public:
|
public:
|
||||||
SocketStream(socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec,
|
SocketStream(socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec,
|
||||||
|
@ -5119,9 +5111,7 @@ Server::create_server_socket(const char *host, int port, int socket_flags,
|
||||||
if (::bind(sock, ai.ai_addr, static_cast<socklen_t>(ai.ai_addrlen))) {
|
if (::bind(sock, ai.ai_addr, static_cast<socklen_t>(ai.ai_addrlen))) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (::listen(sock, CPPHTTPLIB_LISTEN_BACKLOG)) {
|
if (::listen(sock, CPPHTTPLIB_LISTEN_BACKLOG)) { return false; }
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -5738,7 +5728,19 @@ inline bool ClientImpl::send(Request &req, Response &res, Error &error) {
|
||||||
|
|
||||||
auto is_alive = false;
|
auto is_alive = false;
|
||||||
if (socket_.is_open()) {
|
if (socket_.is_open()) {
|
||||||
is_alive = detail::is_socket_alive(socket_.sock);
|
// Check if the socket is still alive.
|
||||||
|
{
|
||||||
|
auto val = detail::select_read(socket_.sock, 0, 0);
|
||||||
|
if (val < 0) {
|
||||||
|
return false;
|
||||||
|
} else if (val == 0) {
|
||||||
|
is_alive = true;
|
||||||
|
}
|
||||||
|
char buf[1];
|
||||||
|
is_alive =
|
||||||
|
detail::read_socket(socket_.sock, buf, sizeof(buf), MSG_PEEK) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (!is_alive) {
|
if (!is_alive) {
|
||||||
// Attempt to avoid sigpipe by shutting down nongracefully if it seems
|
// Attempt to avoid sigpipe by shutting down nongracefully if it seems
|
||||||
// like the other side has already closed the connection Also, there
|
// like the other side has already closed the connection Also, there
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue