From 8b890d755b7369735f47f622b0ea6fd443c9c4bf Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Mon, 7 Jan 2013 12:22:46 -0600 Subject: [PATCH] Updates asio transport components to new styles --- websocketpp/transport/asio/connection.hpp | 44 +++++++++----- websocketpp/transport/asio/endpoint.hpp | 73 +++++++++++++---------- 2 files changed, 70 insertions(+), 47 deletions(-) diff --git a/websocketpp/transport/asio/connection.hpp b/websocketpp/transport/asio/connection.hpp index 2532363bf1..d6c7ea5ea4 100644 --- a/websocketpp/transport/asio/connection.hpp +++ b/websocketpp/transport/asio/connection.hpp @@ -47,20 +47,34 @@ namespace asio { typedef lib::function tcp_init_handler; -template -class connection : public security { +/// Boost Asio based connection transport component +/** + * transport::asio::connection impliments a connection transport component using + * Boost ASIO that works with the transport::asio::endpoint endpoint transport + * component. + */ +template +class connection : public socket { public: - typedef connection type; - typedef security security_type; - + /// Type of this connection transport component + typedef connection type; + /// Type of a shared pointer to this connection transport component typedef lib::shared_ptr ptr; - class handler_interface : public security_type::handler_interface { + /// Type of the socket connection component + typedef socket socket_con_type; + /// Type of a shared pointer to the socket connection component + typedef typename socket_con_type::ptr socket_con_ptr; + + /// Type of a pointer to the ASIO io_service being used + typedef boost::asio::io_service* io_service_ptr; + + // TODO: clean up the rest of these types + class handler_interface : public socket_con_type::handler_interface { public: }; typedef lib::shared_ptr handler_ptr; - typedef boost::asio::io_service* io_service_ptr; // generate and manage our own io_service explicit connection(bool is_server) @@ -70,7 +84,7 @@ public: } bool is_secure() const { - return security::is_secure(); + return socket_con_type::is_secure(); } /// Finish constructing the transport @@ -88,7 +102,7 @@ public: //m_strand.reset(new boost::asio::strand(*io_service)); - security::init_asio(io_service, m_is_server); + socket_con_type::init_asio(io_service, m_is_server); } void set_tcp_init_handler(tcp_init_handler h) { @@ -108,7 +122,7 @@ protected: void init(init_handler callback) { std::cout << "asio connection init" << std::endl; - security_type::init( + socket_con_type::init( lib::bind( &type::handle_init, this, @@ -144,7 +158,7 @@ protected: } boost::asio::async_read( - security_type::get_socket(), + socket_con_type::get_socket(), boost::asio::buffer(buf,len), boost::asio::transfer_at_least(num_bytes), lib::bind( @@ -177,7 +191,7 @@ protected: m_bufs.push_back(boost::asio::buffer(buf,len)); boost::asio::async_write( - security_type::get_socket(), + socket_con_type::get_socket(), m_bufs, lib::bind( &type::handle_async_write, @@ -196,7 +210,7 @@ protected: } boost::asio::async_write( - security_type::get_socket(), + socket_con_type::get_socket(), m_bufs, lib::bind( &type::handle_async_write, @@ -222,7 +236,7 @@ protected: void set_handler(handler_ptr new_handler) { m_handler = new_handler; - security::set_handler(m_handler); + socket_con_type::set_handler(m_handler); } /// Set Connection Handle @@ -260,7 +274,7 @@ protected: /// close and clean up the underlying socket void shutdown() { - security_type::shutdown(); + socket_con_type::shutdown(); } private: // static settings diff --git a/websocketpp/transport/asio/endpoint.hpp b/websocketpp/transport/asio/endpoint.hpp index 6b5efb7283..5786eb3e07 100644 --- a/websocketpp/transport/asio/endpoint.hpp +++ b/websocketpp/transport/asio/endpoint.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, Peter Thorson. All rights reserved. + * Copyright (c) 2013, Peter Thorson. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -43,30 +43,35 @@ namespace websocketpp { namespace transport { namespace asio { +/// Boost Asio based endpoint transport component /** - * - * - * - * - * -From Endpoint -- get new connection - - -*/ - -template -class endpoint : public security { + * transport::asio::endpoint impliments an endpoint transport component using + * Boost ASIO. + */ +template +class endpoint : public socket { public: - typedef endpoint type; - - typedef asio::connection con_policy; - - //typedef lib::shared_ptr io_service_ptr; + /// Type of this endpoint transport component + typedef endpoint type; + + /// Type of the socket endpoint component + typedef socket socket_type; + /// Type of the socket connection component + typedef typename socket_type::socket_con_type socket_con_type; + /// Type of a shared pointer to the socket connection component + typedef typename socket_con_type::ptr socket_con_ptr; + + /// Type of the connection transport component associated with this + /// endpoint transport component + typedef asio::connection transport_con_type; + /// Type of a shared pointer to the connection transport component + /// associated with this endpoint transport component + typedef typename transport_con_type::ptr transport_con_ptr; + + /// Type of a pointer to the ASIO io_service being used typedef boost::asio::io_service* io_service_ptr; + /// Type of a shared pointer to the acceptor being used typedef lib::shared_ptr acceptor_ptr; - - typedef typename con_policy::ptr trans_connection_ptr; // generate and manage our own io_service explicit endpoint() @@ -191,10 +196,11 @@ public: // Accept the next connection attempt via m_acceptor and assign it to con. // callback is called - void async_accept(trans_connection_ptr tcon, accept_handler callback) { + void async_accept(transport_con_ptr tcon, accept_handler callback) { if (m_state != LISTENING) { // TODO: throw invalid state - std::cout << "asio::async_accept called from the wrong state" << std::endl; + std::cout << "asio::async_accept called from the wrong state" + << std::endl; throw; } @@ -274,16 +280,19 @@ protected: /// Initialize a connection /** - * init(connection_ptr) is called by an endpoint once for each newly created - * connection. It's purpose is to give the transport policy the chance to - * perform any transport specific initialization that couldn't be done via - * the default constructor. + * init is called by an endpoint once for each newly created connection. It's + * purpose is to give the transport policy the chance to perform any transport + * specific initialization that couldn't be done via the default constructor. + * + * @param tcon A pointer to the transport portion of the connection. */ - template - void init(connection_ptr con) { - // init tls? - con->init_asio(m_io_service); - con->set_tcp_init_handler(m_tcp_init_handler); + void init(transport_con_ptr tcon) { + tcon->init_asio(m_io_service); + tcon->set_tcp_init_handler(m_tcp_init_handler); + + // Initialize the connection socket component + socket_type::init(lib::static_pointer_cast(tcon)); } private: enum state {