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
+58 -1
View File
@@ -11,9 +11,12 @@
#include <boost/beast/core/close_socket.hpp>
#include <boost/beast/_experimental/unit_test/suite.hpp>
#include <boost/beast/_experimental/test/stream.hpp>
#include <boost/beast/core/get_lowest_layer.hpp>
#include <boost/beast/core/string.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/write.hpp>
#include <utility>
namespace boost {
@@ -52,18 +55,72 @@ public:
BEAST_EXPECT(! sock.is_open());
}
{
layer<net::ip::tcp::socket> layer(ioc);
using type = layer<net::ip::tcp::socket>;
type layer(ioc);
layer.next_layer().open(net::ip::tcp::v4());
BEAST_EXPECT(layer.next_layer().is_open());
BOOST_STATIC_ASSERT(detail::has_next_layer<type>::value);
BOOST_STATIC_ASSERT((std::is_same<
typename std::decay<decltype(get_lowest_layer(layer))>::type,
lowest_layer_type<decltype(layer)>>::value));
BOOST_STATIC_ASSERT(std::is_same<net::ip::tcp::socket&,
decltype(get_lowest_layer(layer))>::value);
BOOST_STATIC_ASSERT(std::is_same<net::ip::tcp::socket,
lowest_layer_type<decltype(layer)>>::value);
close_socket(get_lowest_layer(layer));
BEAST_EXPECT(! layer.next_layer().is_open());
}
{
test::stream ts(ioc);
close_socket(ts);
}
}
//--------------------------------------------------------------------------
template <class WriteStream>
void hello_and_close (WriteStream& stream)
{
net::write(stream, net::const_buffer("Hello, world!", 13));
close_socket(get_lowest_layer(stream));
}
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
}
};
void
testJavadocs()
{
BEAST_EXPECT(&close_socket_test::template hello_and_close<net::ip::tcp::socket>);
{
net::io_context ioc;
my_socket s(ioc);
close_socket(s);
}
}
//--------------------------------------------------------------------------
void
run() override
{
testClose();
testJavadocs();
}
};