adds async_shutdown interface to transport connections

This commit is contained in:
Peter Thorson
2013-05-07 09:35:13 -05:00
parent 7d8f1ea163
commit 42ca501f76
6 changed files with 60 additions and 20 deletions

View File

@@ -28,8 +28,11 @@
#ifndef WEBSOCKETPP_TRANSPORT_ASIO_BASE_HPP
#define WEBSOCKETPP_TRANSPORT_ASIO_BASE_HPP
#include <websocketpp/common/system_error.hpp>
#include <websocketpp/common/cpp11.hpp>
#include <websocketpp/common/functional.hpp>
#include <websocketpp/common/system_error.hpp>
#include <boost/system/error_code.hpp>
#include <string>
@@ -37,6 +40,9 @@ namespace websocketpp {
namespace transport {
namespace asio {
typedef lib::function<void(const boost::system::error_code &)>
socket_shutdown_handler;
/**
* This policy uses a single boost::asio io_service to provide transport
* services to a WebSocket++ endpoint.

View File

@@ -503,9 +503,12 @@ protected:
boost::system::error_code& ec)
{
m_bufs.clear();
// TODO: translate this better
if (ec) {
handler(make_error_code(error::pass_through));
std::stringstream s;
s << "asio async_write error: " << ec
<< " (" << ec.message() << ")";
m_elog.write(log::elevel::info,s.str());
handler(make_error_code(transport::error::pass_through));
} else {
handler(lib::error_code());
}
@@ -544,8 +547,37 @@ protected:
}*/
/// close and clean up the underlying socket
void shutdown() {
socket_con_type::shutdown();
void async_shutdown(shutdown_handler h) {
if (m_alog.static_test(log::alevel::devel)) {
m_alog.write(log::alevel::devel,"asio connection async_shutdown");
}
socket_con_type::async_shutdown(
lib::bind(
&type::handle_async_shutdown,
this,
h,
lib::placeholders::_1
)
);
}
void handle_async_shutdown(shutdown_handler h, const
boost::system::error_code & ec)
{
if (m_alog.static_test(log::alevel::devel)) {
m_alog.write(log::alevel::devel,"asio con handle_async_shutdown");
}
if (ec) {
std::stringstream s;
s << "asio async_shutdown error: " << ec
<< " (" << ec.message() << ")";
m_elog.write(log::elevel::info,s.str());
h(make_error_code(transport::error::pass_through));
} else {
h(lib::error_code());
}
}
typedef lib::shared_ptr<boost::asio::deadline_timer> timer_ptr;
@@ -563,9 +595,8 @@ protected:
h(make_error_code(transport::error::operation_aborted));
} else if (ec) {
std::stringstream s;
s << "asio async_wait error::pass_through"
<< "Original Error: " << ec << " (" << ec.message() << ")";
m_elog.write(log::elevel::devel,s.str());
s << "asio async_wait error: " << ec << " (" << ec.message() << ")";
m_elog.write(log::elevel::info,s.str());
h(make_error_code(transport::error::pass_through));
} else {
h(lib::error_code());

View File

@@ -205,11 +205,10 @@ protected:
m_hdl = hdl;
}
void shutdown() {
void async_shutdown(socket_shutdown_handler h) {
boost::system::error_code ec;
m_socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both,ec);
// TODO: handle errors
h(ec);
}
private:
enum state {

View File

@@ -291,20 +291,23 @@ protected:
callback(lib::error_code());
}
void handle_shutdown(socket_ptr s, const boost::system::error_code& ec) {
// TODO: error handling?
}
void shutdown() {
void async_shutdown(socket_shutdown_handler h) {
m_socket->async_shutdown(
lib::bind(
&type::handle_shutdown,
lib::bind(
&type::handle_async_shutdown,
this,
m_socket,
h,
lib::placeholders::_1
)
);
}
void handle_async_shutdown(socket_ptr s, socket_shutdown_handler h, const
boost::system::error_code& ec)
{
h(ec);
}
private:
socket_type::handshake_type get_handshake_type() {
if (m_is_server) {

View File

@@ -70,6 +70,7 @@ typedef lib::function<void(const lib::error_code&)> init_handler;
typedef lib::function<void(const lib::error_code&,size_t)> read_handler;
typedef lib::function<void(const lib::error_code&)> write_handler;
typedef lib::function<void(const lib::error_code&)> timer_handler;
typedef lib::function<void(const lib::error_code&)> shutdown_handler;
typedef lib::function<void()> inturrupt_handler;
typedef lib::function<void()> dispatch_handler;

View File

@@ -313,8 +313,8 @@ protected:
return lib::error_code();
}
void shutdown() {
// TODO:
void async_shutdown(shutdown_handler h) {
h(lib::error_code());
}
private:
void read(std::istream &in) {