This commit is contained in:
Peter Thorson
2013-05-03 08:33:43 -05:00
parent 0cba06bd67
commit 14e80a4ff1
2 changed files with 234 additions and 234 deletions

View File

@@ -51,28 +51,28 @@ typedef lib::function<void(connection_hdl,boost::asio::ip::tcp::socket&)>
class connection {
public:
/// Type of this connection socket component
typedef connection type;
typedef connection type;
/// Type of a shared pointer to this connection socket component
typedef lib::shared_ptr<type> ptr;
/// Type of a pointer to the ASIO io_service being used
typedef boost::asio::io_service* io_service_ptr;
typedef boost::asio::io_service* io_service_ptr;
/// Type of a shared pointer to the socket being used.
typedef lib::shared_ptr<boost::asio::ip::tcp::socket> socket_ptr;
explicit connection() : m_state(UNINITIALIZED) {
//std::cout << "transport::asio::basic_socket::connection constructor"
explicit connection() : m_state(UNINITIALIZED) {
//std::cout << "transport::asio::basic_socket::connection constructor"
// << std::endl;
}
}
/// Check whether or not this connection is secure
/**
* @return Wether or not this connection is secure
*/
bool is_secure() const {
return false;
}
bool is_secure() const {
return false;
}
/// Set the socket initialization handler
/**
* The socket initialization handler is called after the socket object is
@@ -85,87 +85,87 @@ public:
m_socket_init_handler = h;
}
/// Retrieve a pointer to the underlying socket
/**
* This is used internally. It can also be used to set socket options, etc
*/
/// Retrieve a pointer to the underlying socket
/**
* This is used internally. It can also be used to set socket options, etc
*/
boost::asio::ip::tcp::socket& get_socket() {
return *m_socket;
return *m_socket;
}
/// Retrieve a pointer to the underlying socket
/**
* This is used internally. It can also be used to set socket options, etc
*/
/**
* This is used internally. It can also be used to set socket options, etc
*/
boost::asio::ip::tcp::socket& get_raw_socket() {
return *m_socket;
return *m_socket;
}
/// Get the remote endpoint address
/**
* The iostream transport has no information about the ultimate remote
* endpoint. It will return the string "iostream transport". To indicate
* this.
*
* TODO: allow user settable remote endpoint addresses if this seems useful
*
* @return A string identifying the address of the remote endpoint
*/
std::string get_remote_endpoint(lib::error_code &ec) const {
std::stringstream s;
boost::system::error_code bec;
boost::asio::ip::tcp::endpoint ep = m_socket->remote_endpoint(bec);
if (bec) {
ec = error::make_error_code(error::pass_through);
s << "Error getting remote endpoint: " << bec
<< " (" << bec.message() << ")";
return s.str();
} else {
ec = lib::error_code();
s << ep;
return s.str();
}
}
protected:
/// Perform one time initializations
/**
* init_asio is called once immediately after construction to initialize
* boost::asio components to the io_service
*
* @param service A pointer to the endpoint's io_service
* @param strand A shared pointer to the connection's asio strand
* @param is_server Whether or not the endpoint is a server or not.
*/
lib::error_code init_asio (io_service_ptr service, bool is_server) {
if (m_state != UNINITIALIZED) {
return socket::make_error(socket::error::invalid_state);
}
m_socket.reset(new boost::asio::ip::tcp::socket(*service));
m_state = READY;
return lib::error_code();
/**
* The iostream transport has no information about the ultimate remote
* endpoint. It will return the string "iostream transport". To indicate
* this.
*
* TODO: allow user settable remote endpoint addresses if this seems useful
*
* @return A string identifying the address of the remote endpoint
*/
std::string get_remote_endpoint(lib::error_code &ec) const {
std::stringstream s;
boost::system::error_code bec;
boost::asio::ip::tcp::endpoint ep = m_socket->remote_endpoint(bec);
if (bec) {
ec = error::make_error_code(error::pass_through);
s << "Error getting remote endpoint: " << bec
<< " (" << bec.message() << ")";
return s.str();
} else {
ec = lib::error_code();
s << ep;
return s.str();
}
}
/// Initialize security policy for reading
void init(init_handler callback) {
if (m_state != READY) {
callback(socket::make_error(socket::error::invalid_state));
return;
}
protected:
/// Perform one time initializations
/**
* init_asio is called once immediately after construction to initialize
* boost::asio components to the io_service
*
* @param service A pointer to the endpoint's io_service
* @param strand A shared pointer to the connection's asio strand
* @param is_server Whether or not the endpoint is a server or not.
*/
lib::error_code init_asio (io_service_ptr service, bool is_server) {
if (m_state != UNINITIALIZED) {
return socket::make_error(socket::error::invalid_state);
}
m_socket.reset(new boost::asio::ip::tcp::socket(*service));
m_state = READY;
return lib::error_code();
}
/// Initialize security policy for reading
void init(init_handler callback) {
if (m_state != READY) {
callback(socket::make_error(socket::error::invalid_state));
return;
}
if (m_socket_init_handler) {
m_socket_init_handler(m_hdl,*m_socket);
}
m_state = READING;
callback(lib::error_code());
}
m_state = READING;
callback(lib::error_code());
}
/// Sets the connection handle
/**
* The connection handle is passed to any handlers to identify the
@@ -184,14 +184,14 @@ protected:
// TODO: handle errors
}
private:
enum state {
UNINITIALIZED = 0,
READY = 1,
READING = 2
};
socket_ptr m_socket;
state m_state;
enum state {
UNINITIALIZED = 0,
READY = 1,
READING = 2
};
socket_ptr m_socket;
state m_state;
connection_hdl m_hdl;
socket_init_handler m_socket_init_handler;
@@ -213,7 +213,7 @@ public:
/// component.
typedef socket_con_type::ptr socket_con_ptr;
explicit endpoint() {}
explicit endpoint() {}
/// Checks whether the endpoint creates secure connections
/**

View File

@@ -58,51 +58,51 @@ typedef lib::function<lib::shared_ptr<boost::asio::ssl::context>(connection_hdl)
class connection {
public:
/// Type of this connection socket component
typedef connection type;
/// Type of a shared pointer to this connection socket component
typedef connection type;
/// Type of a shared pointer to this connection socket component
typedef lib::shared_ptr<type> ptr;
/// Type of the ASIO socket being used
typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket_type;
typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket_type;
/// Type of a shared pointer to the ASIO socket being used
typedef lib::shared_ptr<socket_type> socket_ptr;
/// Type of a pointer to the ASIO io_service being used
typedef boost::asio::io_service* io_service_ptr;
typedef boost::asio::io_service* io_service_ptr;
/// Type of a shared pointer to the ASIO TLS context being used
typedef lib::shared_ptr<boost::asio::ssl::context> context_ptr;
typedef lib::shared_ptr<boost::asio::ssl::context> context_ptr;
/// Type of a shared pointer to the ASIO timer being used
typedef lib::shared_ptr<boost::asio::deadline_timer> timer_ptr;
typedef boost::system::error_code boost_error;
explicit connection() {
//std::cout << "transport::asio::tls_socket::connection constructor"
typedef lib::shared_ptr<boost::asio::deadline_timer> timer_ptr;
typedef boost::system::error_code boost_error;
explicit connection() {
//std::cout << "transport::asio::tls_socket::connection constructor"
// << std::endl;
}
}
/// Check whether or not this connection is secure
/**
* @return Wether or not this connection is secure
*/
bool is_secure() const {
return true;
}
/// Retrieve a pointer to the underlying socket
/**
* This is used internally. It can also be used to set socket options, etc
*/
socket_type::lowest_layer_type& get_raw_socket() {
return m_socket->lowest_layer();
}
/// Retrieve a pointer to the wrapped socket
/**
* This is used internally.
*/
socket_type& get_socket() {
return *m_socket;
}
bool is_secure() const {
return true;
}
/// Retrieve a pointer to the underlying socket
/**
* This is used internally. It can also be used to set socket options, etc
*/
socket_type::lowest_layer_type& get_raw_socket() {
return m_socket->lowest_layer();
}
/// Retrieve a pointer to the wrapped socket
/**
* This is used internally.
*/
socket_type& get_socket() {
return *m_socket;
}
/// Set the socket initialization handler
/**
@@ -130,75 +130,75 @@ public:
}
/// Get the remote endpoint address
/**
* The iostream transport has no information about the ultimate remote
* endpoint. It will return the string "iostream transport". To indicate
* this.
*
* TODO: allow user settable remote endpoint addresses if this seems useful
*
* @return A string identifying the address of the remote endpoint
*/
std::string get_remote_endpoint(lib::error_code &ec) const {
std::stringstream s;
boost::system::error_code bec;
boost::asio::ip::tcp::endpoint ep = m_socket->lowest_layer().remote_endpoint(bec);
if (bec) {
ec = error::make_error_code(error::pass_through);
s << "Error getting remote endpoint: " << bec
<< " (" << bec.message() << ")";
return s.str();
} else {
ec = lib::error_code();
s << ep;
return s.str();
}
}
/**
* The iostream transport has no information about the ultimate remote
* endpoint. It will return the string "iostream transport". To indicate
* this.
*
* TODO: allow user settable remote endpoint addresses if this seems useful
*
* @return A string identifying the address of the remote endpoint
*/
std::string get_remote_endpoint(lib::error_code &ec) const {
std::stringstream s;
boost::system::error_code bec;
boost::asio::ip::tcp::endpoint ep = m_socket->lowest_layer().remote_endpoint(bec);
if (bec) {
ec = error::make_error_code(error::pass_through);
s << "Error getting remote endpoint: " << bec
<< " (" << bec.message() << ")";
return s.str();
} else {
ec = lib::error_code();
s << ep;
return s.str();
}
}
protected:
/// Perform one time initializations
/**
* init_asio is called once immediately after construction to initialize
* boost::asio components to the io_service
*
* @param service A pointer to the endpoint's io_service
* @param strand A shared pointer to the connection's asio strand
* @param is_server Whether or not the endpoint is a server or not.
*/
/// Perform one time initializations
/**
* init_asio is called once immediately after construction to initialize
* boost::asio components to the io_service
*
* @param service A pointer to the endpoint's io_service
* @param strand A shared pointer to the connection's asio strand
* @param is_server Whether or not the endpoint is a server or not.
*/
lib::error_code init_asio (io_service_ptr service, bool is_server) {
if (!m_tls_init_handler) {
return socket::make_error(socket::error::missing_tls_init_handler);
return socket::make_error(socket::error::missing_tls_init_handler);
}
m_context = m_tls_init_handler(m_hdl);
if (!m_context) {
return socket::make_error(socket::error::invalid_tls_context);
}
m_socket.reset(new socket_type(*service,*m_context));
m_timer.reset(new boost::asio::deadline_timer(
*service,
boost::posix_time::seconds(0))
);
m_io_service = service;
m_is_server = is_server;
return lib::error_code();
if (!m_context) {
return socket::make_error(socket::error::invalid_tls_context);
}
m_socket.reset(new socket_type(*service,*m_context));
m_timer.reset(new boost::asio::deadline_timer(
*service,
boost::posix_time::seconds(0))
);
m_io_service = service;
m_is_server = is_server;
return lib::error_code();
}
/// Initialize security policy for reading
void init(init_handler callback) {
if (m_socket_init_handler) {
/// Initialize security policy for reading
void init(init_handler callback) {
if (m_socket_init_handler) {
m_socket_init_handler(m_hdl,get_raw_socket());
}
// register timeout
m_timer->expires_from_now(boost::posix_time::milliseconds(5000));
// TEMP
m_timer->async_wait(
// register timeout
m_timer->expires_from_now(boost::posix_time::milliseconds(5000));
// TEMP
m_timer->async_wait(
lib::bind(
&type::handle_timeout,
this,
@@ -206,19 +206,19 @@ protected:
lib::placeholders::_1
)
);
// TLS handshake
m_socket->async_handshake(
get_handshake_type(),
lib::bind(
&type::handle_init,
this,
callback,
lib::placeholders::_1
)
);
}
// TLS handshake
m_socket->async_handshake(
get_handshake_type(),
lib::bind(
&type::handle_init,
this,
callback,
lib::placeholders::_1
)
);
}
/// Sets the connection handle
/**
* The connection handle is passed to any handlers to identify the
@@ -230,39 +230,39 @@ protected:
m_hdl = hdl;
}
void handle_timeout(init_handler callback, const
boost::system::error_code& error)
{
if (error) {
if (error.value() == boost::asio::error::operation_aborted) {
// The timer was cancelled because the handshake succeeded.
return;
}
// Some other ASIO error, pass it through
// TODO: make this error pass through better
callback(socket::make_error(socket::error::pass_through));
return;
}
callback(socket::make_error(socket::error::tls_handshake_timeout));
}
void handle_init(init_handler callback, const
boost::system::error_code& error)
{
/// stop waiting for our handshake timer.
m_timer->cancel();
if (error) {
// TODO: make this error pass through better
callback(socket::make_error(socket::error::pass_through));
return;
}
callback(lib::error_code());
}
void handle_timeout(init_handler callback, const
boost::system::error_code& error)
{
if (error) {
if (error.value() == boost::asio::error::operation_aborted) {
// The timer was cancelled because the handshake succeeded.
return;
}
// Some other ASIO error, pass it through
// TODO: make this error pass through better
callback(socket::make_error(socket::error::pass_through));
return;
}
callback(socket::make_error(socket::error::tls_handshake_timeout));
}
void handle_init(init_handler callback, const
boost::system::error_code& error)
{
/// stop waiting for our handshake timer.
m_timer->cancel();
if (error) {
// TODO: make this error pass through better
callback(socket::make_error(socket::error::pass_through));
return;
}
callback(lib::error_code());
}
void shutdown() {
boost::system::error_code ec;
m_socket->shutdown(ec);
@@ -270,19 +270,19 @@ protected:
// TODO: error handling
}
private:
socket_type::handshake_type get_handshake_type() {
socket_type::handshake_type get_handshake_type() {
if (m_is_server) {
return boost::asio::ssl::stream_base::server;
} else {
return boost::asio::ssl::stream_base::client;
}
}
io_service_ptr m_io_service;
context_ptr m_context;
socket_ptr m_socket;
timer_ptr m_timer;
bool m_is_server;
io_service_ptr m_io_service;
context_ptr m_context;
socket_ptr m_socket;
timer_ptr m_timer;
bool m_is_server;
connection_hdl m_hdl;
socket_init_handler m_socket_init_handler;