mirror of
https://github.com/matrix-construct/construct
synced 2024-11-29 02:02:38 +01:00
ircd::net: Move scope_timeout from socket:: to net:: w/ exposure.
This commit is contained in:
parent
6d88ee339d
commit
5be18475df
4 changed files with 126 additions and 89 deletions
|
@ -45,6 +45,7 @@ namespace ircd::net
|
|||
#include "wait.h"
|
||||
#include "read.h"
|
||||
#include "write.h"
|
||||
#include "scope_timeout.h"
|
||||
|
||||
namespace ircd::net
|
||||
{
|
||||
|
|
37
include/ircd/net/scope_timeout.h
Normal file
37
include/ircd/net/scope_timeout.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
// Matrix Construct
|
||||
//
|
||||
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
||||
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice is present in all copies. The
|
||||
// full license for this software is available in the LICENSE file.
|
||||
|
||||
#pragma once
|
||||
#define HAVE_IRCD_NET_SCOPE_TIMEOUT_H
|
||||
|
||||
namespace ircd::net
|
||||
{
|
||||
struct scope_timeout;
|
||||
}
|
||||
|
||||
class ircd::net::scope_timeout
|
||||
{
|
||||
socket *s {nullptr};
|
||||
|
||||
public:
|
||||
using handler = std::function<void (const bool &)>; // true = TO; false = canceled
|
||||
|
||||
bool cancel() noexcept; // invoke timer.cancel() before dtor
|
||||
bool release(); // cancels the cancel;
|
||||
|
||||
scope_timeout(socket &, const milliseconds &timeout, handler callback);
|
||||
scope_timeout(socket &, const milliseconds &timeout);
|
||||
scope_timeout() = default;
|
||||
scope_timeout(scope_timeout &&) noexcept;
|
||||
scope_timeout(const scope_timeout &) = delete;
|
||||
scope_timeout &operator=(scope_timeout &&) noexcept;
|
||||
scope_timeout &operator=(const scope_timeout &) = delete;
|
||||
~scope_timeout() noexcept;
|
||||
};
|
|
@ -29,7 +29,6 @@ struct ircd::net::socket
|
|||
struct io;
|
||||
struct stat;
|
||||
struct xfer;
|
||||
struct scope_timeout;
|
||||
|
||||
using endpoint = ip::tcp::endpoint;
|
||||
using wait_type = ip::tcp::socket::wait_type;
|
||||
|
@ -113,24 +112,6 @@ struct ircd::net::socket
|
|||
~socket() noexcept;
|
||||
};
|
||||
|
||||
class ircd::net::socket::scope_timeout
|
||||
{
|
||||
socket *s {nullptr};
|
||||
|
||||
public:
|
||||
bool cancel() noexcept; // invoke timer.cancel() before dtor
|
||||
bool release(); // cancels the cancel;
|
||||
|
||||
scope_timeout(socket &, const milliseconds &timeout, socket::ec_handler handler);
|
||||
scope_timeout(socket &, const milliseconds &timeout);
|
||||
scope_timeout() = default;
|
||||
scope_timeout(scope_timeout &&) noexcept;
|
||||
scope_timeout(const scope_timeout &) = delete;
|
||||
scope_timeout &operator=(scope_timeout &&) noexcept;
|
||||
scope_timeout &operator=(const scope_timeout &) = delete;
|
||||
~scope_timeout() noexcept;
|
||||
};
|
||||
|
||||
template<class... args>
|
||||
auto
|
||||
ircd::net::socket::operator()(args&&... a)
|
||||
|
|
158
ircd/net.cc
158
ircd/net.cc
|
@ -1305,6 +1305,94 @@ const
|
|||
};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// net/scope_timeout.h
|
||||
//
|
||||
|
||||
ircd::net::scope_timeout::scope_timeout(socket &socket,
|
||||
const milliseconds &timeout)
|
||||
:s
|
||||
{
|
||||
timeout < 0ms? nullptr : &socket
|
||||
}
|
||||
{
|
||||
if(timeout < 0ms)
|
||||
return;
|
||||
|
||||
socket.set_timeout(timeout);
|
||||
}
|
||||
|
||||
ircd::net::scope_timeout::scope_timeout(socket &socket,
|
||||
const milliseconds &timeout,
|
||||
handler callback)
|
||||
:s
|
||||
{
|
||||
timeout < 0ms? nullptr : &socket
|
||||
}
|
||||
{
|
||||
if(timeout < 0ms)
|
||||
return;
|
||||
|
||||
socket.set_timeout(timeout, [callback(std::move(callback))]
|
||||
(const error_code &ec)
|
||||
{
|
||||
const bool &timed_out{!ec};
|
||||
callback(timed_out);
|
||||
});
|
||||
}
|
||||
|
||||
ircd::net::scope_timeout::scope_timeout(scope_timeout &&other)
|
||||
noexcept
|
||||
:s{std::move(other.s)}
|
||||
{
|
||||
other.s = nullptr;
|
||||
}
|
||||
|
||||
ircd::net::scope_timeout &
|
||||
ircd::net::scope_timeout::operator=(scope_timeout &&other)
|
||||
noexcept
|
||||
{
|
||||
this->~scope_timeout();
|
||||
s = std::move(other.s);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ircd::net::scope_timeout::~scope_timeout()
|
||||
noexcept
|
||||
{
|
||||
cancel();
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::net::scope_timeout::cancel()
|
||||
noexcept try
|
||||
{
|
||||
if(!this->s)
|
||||
return false;
|
||||
|
||||
auto *const s{this->s};
|
||||
this->s = nullptr;
|
||||
s->cancel_timeout();
|
||||
return true;
|
||||
}
|
||||
catch(const std::exception &e)
|
||||
{
|
||||
log.error("socket(%p) scope_timeout::cancel: %s",
|
||||
(const void *)s,
|
||||
e.what());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::net::scope_timeout::release()
|
||||
{
|
||||
const auto s{this->s};
|
||||
this->s = nullptr;
|
||||
return s != nullptr;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// net/socket.h
|
||||
|
@ -2206,76 +2294,6 @@ const
|
|||
return *ssl.native_handle();
|
||||
}
|
||||
|
||||
//
|
||||
// socket::scope_timeout
|
||||
//
|
||||
|
||||
ircd::net::socket::scope_timeout::scope_timeout(socket &socket,
|
||||
const milliseconds &timeout)
|
||||
:s{&socket}
|
||||
{
|
||||
socket.set_timeout(timeout);
|
||||
}
|
||||
|
||||
ircd::net::socket::scope_timeout::scope_timeout(socket &socket,
|
||||
const milliseconds &timeout,
|
||||
socket::ec_handler handler)
|
||||
:s{&socket}
|
||||
{
|
||||
socket.set_timeout(timeout, std::move(handler));
|
||||
}
|
||||
|
||||
ircd::net::socket::scope_timeout::scope_timeout(scope_timeout &&other)
|
||||
noexcept
|
||||
:s{std::move(other.s)}
|
||||
{
|
||||
other.s = nullptr;
|
||||
}
|
||||
|
||||
ircd::net::socket::scope_timeout &
|
||||
ircd::net::socket::scope_timeout::operator=(scope_timeout &&other)
|
||||
noexcept
|
||||
{
|
||||
this->~scope_timeout();
|
||||
s = std::move(other.s);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ircd::net::socket::scope_timeout::~scope_timeout()
|
||||
noexcept
|
||||
{
|
||||
cancel();
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::net::socket::scope_timeout::cancel()
|
||||
noexcept try
|
||||
{
|
||||
if(!this->s)
|
||||
return false;
|
||||
|
||||
auto *const s{this->s};
|
||||
this->s = nullptr;
|
||||
s->cancel_timeout();
|
||||
return true;
|
||||
}
|
||||
catch(const std::exception &e)
|
||||
{
|
||||
log.error("socket(%p) scope_timeout::cancel: %s",
|
||||
(const void *)s,
|
||||
e.what());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::net::socket::scope_timeout::release()
|
||||
{
|
||||
const auto s{this->s};
|
||||
this->s = nullptr;
|
||||
return s != nullptr;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// net/dns.h
|
||||
|
|
Loading…
Reference in a new issue