0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-26 07:23:53 +01:00

ircd::net: Tentative future-based socket resolve->connect->handshake.

This commit is contained in:
Jason Volk 2018-01-04 15:14:13 -08:00
parent fcb2660f0a
commit ebad745a3f
2 changed files with 57 additions and 0 deletions

View file

@ -82,6 +82,9 @@ namespace ircd::net
std::shared_ptr<socket> connect(const remote &, const milliseconds &timeout = 30000ms);
bool disconnect(socket &, const dc &type = dc::SSL_NOTIFY) noexcept;
ctx::future<std::shared_ptr<socket>> open(const ipport &, const milliseconds &timeout = 30000ms);
ctx::future<std::shared_ptr<socket>> open(const hostport &, const milliseconds &timeout = 30000ms);
}
struct ircd::net::init

View file

@ -80,6 +80,60 @@ catch(const std::exception &e)
return false;
}
ircd::ctx::future<std::shared_ptr<ircd::net::socket>>
ircd::net::open(const hostport &hostport,
const milliseconds &timeout)
{
ctx::promise<std::shared_ptr<ircd::net::socket>> p;
ctx::future<std::shared_ptr<ircd::net::socket>> f(p);
resolve(hostport, [p(std::move(p)), timeout]
(auto eptr, const ipport &ipport)
mutable
{
if(eptr)
return p.set_exception(std::move(eptr));
const auto ep{make_endpoint(ipport)};
const auto s(std::make_shared<socket>());
s->open(ep, timeout, [p(std::move(p)), s]
(const error_code &ec)
mutable
{
if(ec)
{
disconnect(*s, dc::RST);
p.set_exception(make_eptr(ec));
}
else p.set_value(s);
});
});
return f;
}
ircd::ctx::future<std::shared_ptr<ircd::net::socket>>
ircd::net::open(const ipport &ipport,
const milliseconds &timeout)
{
ctx::promise<std::shared_ptr<ircd::net::socket>> p;
ctx::future<std::shared_ptr<ircd::net::socket>> f(p);
const auto ep{make_endpoint(ipport)};
const auto s(std::make_shared<socket>());
s->open(ep, timeout, [p(std::move(p)), s]
(const error_code &ec)
mutable
{
if(ec)
{
disconnect(*s, dc::RST);
p.set_exception(make_eptr(ec));
}
else p.set_value(s);
});
return f;
}
std::shared_ptr<ircd::net::socket>
ircd::net::connect(const net::remote &remote,
const milliseconds &timeout)