mirror of
https://github.com/matrix-construct/construct
synced 2024-12-30 17:34:04 +01:00
ircd::net: Improve the timeout callback related.
This commit is contained in:
parent
d10b9effd0
commit
a1f0075d62
2 changed files with 34 additions and 26 deletions
|
@ -48,8 +48,8 @@ struct ircd::net::socket
|
|||
using wait_type = ip::tcp::socket::wait_type;
|
||||
using message_flags = asio::socket_base::message_flags;
|
||||
using handshake_type = asio::ssl::stream<ip::tcp::socket>::handshake_type;
|
||||
using ec_handler = std::function<void (const error_code &) noexcept>;
|
||||
using eptr_handler = std::function<void (std::exception_ptr) noexcept>;
|
||||
using ec_handler = std::function<void (const error_code &)>;
|
||||
using eptr_handler = std::function<void (std::exception_ptr)>;
|
||||
|
||||
struct stat
|
||||
{
|
||||
|
@ -69,7 +69,7 @@ struct ircd::net::socket
|
|||
void handle_disconnect(std::shared_ptr<socket>, eptr_handler, const error_code &) noexcept;
|
||||
void handle_handshake(std::weak_ptr<socket>, eptr_handler, const error_code &) noexcept;
|
||||
void handle_connect(std::weak_ptr<socket>, const open_opts, eptr_handler, const error_code &) noexcept;
|
||||
void handle_timeout(std::weak_ptr<socket>, const error_code &) noexcept;
|
||||
void handle_timeout(std::weak_ptr<socket>, ec_handler, const error_code &) noexcept;
|
||||
void handle(std::weak_ptr<socket>, ec_handler, const error_code &) noexcept;
|
||||
|
||||
public:
|
||||
|
|
54
ircd/net.cc
54
ircd/net.cc
|
@ -1384,48 +1384,56 @@ catch(const std::exception &e)
|
|||
|
||||
void
|
||||
ircd::net::socket::handle_timeout(const std::weak_ptr<socket> wp,
|
||||
ec_handler callback,
|
||||
const error_code &ec)
|
||||
noexcept try
|
||||
{
|
||||
using namespace boost::system::errc;
|
||||
|
||||
if(!wp.expired()) switch(ec.value())
|
||||
switch(ec.value())
|
||||
{
|
||||
// A 'success' for this handler means there was a timeout on the socket
|
||||
case success:
|
||||
case success: if(likely(!wp.expired()))
|
||||
{
|
||||
assert(timedout == false);
|
||||
timedout = true;
|
||||
sd.cancel();
|
||||
break;
|
||||
}
|
||||
else break;
|
||||
|
||||
// A cancelation means there was no timeout.
|
||||
case operation_canceled:
|
||||
case operation_canceled: if(likely(!wp.expired()))
|
||||
{
|
||||
assert(ec.category() == boost::system::system_category());
|
||||
assert(timedout == false);
|
||||
timedout = false;
|
||||
break;
|
||||
}
|
||||
else break;
|
||||
|
||||
// All other errors are unexpected, logged and ignored here.
|
||||
default:
|
||||
throw boost::system::system_error(ec);
|
||||
{
|
||||
log.critical("socket(%p): handle_timeout: unexpected: %s\n",
|
||||
(const void *)this,
|
||||
string(ec));
|
||||
assert(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(const boost::system::system_error &e)
|
||||
{
|
||||
log.critical("socket(%p): handle_timeout: unexpected: %s\n",
|
||||
(const void *)this,
|
||||
e.what());
|
||||
assert(0);
|
||||
|
||||
if(callback)
|
||||
call_user(callback, ec);
|
||||
}
|
||||
catch(const std::exception &e)
|
||||
{
|
||||
log.error("socket(%p): handle timeout: %s",
|
||||
(const void *)this,
|
||||
e.what());
|
||||
log.critical("socket(%p): handle timeout: %s",
|
||||
(const void *)this,
|
||||
e.what());
|
||||
assert(0);
|
||||
if(callback)
|
||||
call_user(callback, ec);
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -1746,31 +1754,31 @@ noexcept
|
|||
boost::system::error_code ec;
|
||||
timer.cancel(ec);
|
||||
assert(!ec);
|
||||
|
||||
return duration_cast<milliseconds>(ret);
|
||||
}
|
||||
|
||||
void
|
||||
ircd::net::socket::set_timeout(const milliseconds &t)
|
||||
{
|
||||
cancel_timeout();
|
||||
if(t < milliseconds(0))
|
||||
return;
|
||||
|
||||
timer.expires_from_now(t);
|
||||
timer.async_wait(std::bind(&socket::handle_timeout, this, weak_from(*this), ph::_1));
|
||||
set_timeout(t, nullptr);
|
||||
}
|
||||
|
||||
void
|
||||
ircd::net::socket::set_timeout(const milliseconds &t,
|
||||
ec_handler h)
|
||||
ec_handler callback)
|
||||
{
|
||||
cancel_timeout();
|
||||
timedout = false;
|
||||
if(t < milliseconds(0))
|
||||
return;
|
||||
|
||||
auto handler
|
||||
{
|
||||
std::bind(&socket::handle_timeout, this, weak_from(*this), std::move(callback), ph::_1)
|
||||
};
|
||||
|
||||
timer.expires_from_now(t);
|
||||
timer.async_wait(std::move(h));
|
||||
timer.async_wait(std::move(handler));
|
||||
}
|
||||
|
||||
bool
|
||||
|
|
Loading…
Reference in a new issue