Add close_socket, beast_close_socket customization:

This algorithm allows a socket-like object to be closed in
a generic context. The customization point enables user-defined
types to define the close algorithm to use with `close_socket`.
This commit is contained in:
Vinnie Falco
2019-02-01 14:05:07 -08:00
parent 6a658b5c3a
commit 49deee4a69
15 changed files with 361 additions and 62 deletions
@@ -504,6 +504,15 @@ public:
#endif
};
#if ! BOOST_BEAST_DOXYGEN
inline
void
beast_close_socket(stream& s)
{
s.close();
}
#endif
#if BOOST_BEAST_DOXYGEN
/** Return a new stream connected to the given stream
+96 -10
View File
@@ -11,35 +11,121 @@
#define BOOST_BEAST_CLOSE_SOCKET_HPP
#include <boost/beast/core/detail/config.hpp>
#include <boost/beast/core/detail/static_const.hpp>
#include <boost/asio/basic_socket.hpp>
namespace boost {
namespace beast {
namespace detail {
/** Default socket close function.
template<class Protocol>
This function is not meant to be called directly. Instead, it
is called automatically when using @ref close_socket. To enable
closure of user-defined types or classes derived from a particular
user-defined type, this function should be overloaded in the
corresponding namespace for the type in question.
@see close_socket
*/
template<class Protocol BOOST_ASIO_SVC_TPARAM>
void
beast_close_socket(
net::basic_socket<Protocol>& sock)
net::basic_socket<Protocol BOOST_ASIO_SVC_TPARAM>& sock)
{
boost::system::error_code ec;
sock.close(ec);
}
namespace detail {
struct close_socket_impl
{
template<class T>
void
operator()(T& t) const
{
using beast::beast_close_socket;
beast_close_socket(t);
}
};
} // detail
/** Close a socket.
/** Close a socket or socket-like object.
@param sock The socket to close.
This function attempts to close an object representing a socket.
In this context, a socket is an object for which an unqualified
call to the function `void beast_close_socket(Socket&)` is
well-defined. The function `beast_close_socket` is a
<em>customization point</em>, allowing user-defined types to
provide an algorithm for performing the close operation by
overloading this function for the type in question.
Since the customization point is a function call, the normal
rules for finding the correct overload are applied including
the rules for argument-dependent lookup ("ADL"). This permits
classes derived from a type for which a customization is provided
to inherit the customization point.
An overload for the networking class template `net::basic_socket`
is provided, which implements the close algorithm for all socket-like
objects (hence the name of this customization point). When used
in conjunction with @ref get_lowest_layer, a generic algorithm
operating on a layered stream can perform a closure of the underlying
socket without knowing the exact list of concrete types.
@par Example 1
The following generic function synchronously sends a message
on the stream, then closes the socket.
@code
template <class WriteStream>
void hello_and_close (WriteStream& stream)
{
net::write(stream, net::const_buffer("Hello, world!", 13));
close_socket(get_lowest_layer(stream));
}
@endcode
To enable closure of user defined types, it is necessary to provide
an overload of the function `beast_close_socket` for the type.
@par Example 2
The following code declares a user-defined type which contains a
private socket, and provides an overload of the customization
point which closes the private socket.
@code
class my_socket
{
net::ip::tcp::socket sock_;
public:
my_socket(net::io_context& ioc)
: sock_(ioc)
{
}
friend void beast_close_socket(my_socket& s)
{
error_code ec;
s.sock_.close(ec);
// ignore the error
}
};
@endcode
@param sock The socket to close. If the customization point is not
defined for the type of this object, or one of its base classes,
then a compiler error results.
@see beast_close_socket
*/
#if BOOST_BEAST_DOXYGEN
template<class Socket>
void
close_socket(Socket& sock)
{
using detail::beast_close_socket;
beast_close_socket(sock);
}
close_socket(Socket& sock);
#else
BOOST_BEAST_INLINE_VARIABLE(close_socket, detail::close_socket_impl)
#endif
} // beast
} // boost
@@ -73,4 +73,11 @@ namespace net = boost::asio;
# define BOOST_BEAST_DECL inline
#endif
#define BOOST_BEAST_INLINE_VARIABLE(name, type) \
inline namespace \
{ \
constexpr auto &name = \
::boost::beast::detail::static_const<type>::value; \
}
#endif
@@ -17,46 +17,38 @@ namespace boost {
namespace beast {
namespace detail {
template<class T, class = void>
struct has_next_layer : std::false_type
{
};
template <class T>
std::false_type has_next_layer_impl(void*) {}
template<class T>
struct has_next_layer<T, boost::void_t<
decltype(std::declval<T>().next_layer())>>
: std::true_type
template <class T>
auto has_next_layer_impl(decltype(nullptr)) ->
decltype(std::declval<T&>().next_layer(), std::true_type{})
{
};
}
template<class T, class = void>
template <class T>
using has_next_layer = decltype(has_next_layer_impl<T>(nullptr));
//---
template<class T, bool = has_next_layer<T>::value>
struct lowest_layer_type_impl
{
using type = typename std::remove_reference<T>::type;
};
template<class T>
struct lowest_layer_type_impl<T, boost::void_t<
decltype(std::declval<T>().next_layer())>>
struct lowest_layer_type_impl<T, true>
{
using type = typename lowest_layer_type_impl<
decltype(std::declval<T>().next_layer())>::type;
decltype(std::declval<T&>().next_layer())>::type;
};
template<class T>
using lowest_layer_type = typename
lowest_layer_type_impl<T>::type;
template<class T>
lowest_layer_type<T>&
get_lowest_layer_impl(
T& t, std::true_type) noexcept
{
using type = typename std::decay<
decltype(t.next_layer())>::type;
return get_lowest_layer_impl(t.next_layer(),
has_next_layer<type>{});
}
//---
template<class T>
T&
@@ -66,6 +58,16 @@ get_lowest_layer_impl(
return t;
}
template<class T>
lowest_layer_type<T>&
get_lowest_layer_impl(
T& t, std::true_type) noexcept
{
return get_lowest_layer_impl(t.next_layer(),
has_next_layer<typename std::decay<
decltype(t.next_layer())>::type>{});
}
} // detail
} // beast
} // boost
@@ -0,0 +1,42 @@
//
// Copyright (c) 2019 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_DETAIL_STATIC_CONST_HPP
#define BOOST_BEAST_DETAIL_STATIC_CONST_HPP
/* This is a derivative work, original copyright:
Copyright Eric Niebler 2013-present
Use, modification and distribution is subject to the
Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
Project home: https://github.com/ericniebler/range-v3
*/
namespace boost {
namespace beast {
namespace detail {
template<typename T>
struct static_const
{
static constexpr T value {};
};
template<typename T>
constexpr T static_const<T>::value;
} // detail
} // beast
} // boost
#endif
@@ -73,6 +73,8 @@ using lowest_layer_type = detail::lowest_layer_type<T>;
@endcode
@param t The layer in a stack of layered objects for which the lowest layer is returned.
@see @ref close_socket, @ref lowest_layer_type
*/
template<class T>
lowest_layer_type<T>&
+3 -2
View File
@@ -289,9 +289,10 @@ struct is_sync_write_stream : std::false_type {};
template<class T>
struct is_sync_write_stream<T, detail::void_t<decltype(
(
std::declval<std::size_t&>() = std::declval<T&>().write_some(
std::declval<detail::ConstBufferSequence>()),
std::declval<std::size_t&>() = std::declval<T&>().write_some(
std::declval<detail::ConstBufferSequence>()))
,std::declval<std::size_t&>() = std::declval<T&>().write_some(
std::declval<detail::ConstBufferSequence>(),
std::declval<boost::system::error_code&>())
)>> : std::true_type {};