diff --git a/src/connection.hpp b/src/connection.hpp index 44243a716c..33573e6556 100644 --- a/src/connection.hpp +++ b/src/connection.hpp @@ -28,6 +28,12 @@ #ifndef WEBSOCKETPP_CONNECTION_HPP #define WEBSOCKETPP_CONNECTION_HPP +//#include "endpoint.hpp" +#include "http/parser.hpp" +#include "logger.hpp" + +#include "roles/server.hpp" + #include #include #include @@ -42,26 +48,31 @@ struct connection_traits; template < typename endpoint, + template class role, template class socket> class connection - : public socket< connection >, - public boost::enable_shared_from_this< connection > + : public role< connection >, + public socket< connection >, + public boost::enable_shared_from_this< connection > { public: - typedef connection_traits< connection > traits; + typedef connection_traits< connection > traits; // get types that we need from our traits class typedef typename traits::type type; + typedef typename traits::role_type role_type; typedef typename traits::socket_type socket_type; typedef endpoint endpoint_type; + + // friends (would require C++11) this would enable connection::start to be + // protected instead of public. + // friend typename endpoint_traits::role_type; connection(endpoint_type& e) - : socket_type(e), - m_endpoint(e) - { - std::cout << "setup connection" << std::endl; - } + : role_type(e), + socket_type(e), + m_endpoint(e) {} void start() { // initialize the socket. @@ -73,17 +84,19 @@ public: ) ); } - +protected: void handle_socket_init(const boost::system::error_code& error) { if (error) { - std::cout << "SSL handshake error" << std::endl; - } else { - this->websocket_handshake(); + m_endpoint.elog().at(log::elevel::ERROR) << "Connection initialization failed, error code: " << error << log::endl; + this->terminate(); + return; } + + this->websocket_handshake(); } void websocket_handshake() { - std::cout << "Websocket Handshake" << std::endl; + m_endpoint.alog().at(log::alevel::DEVEL) << "Websocket Handshake" << log::endl; socket_type::get_socket().async_read_some( boost::asio::buffer(m_data, 512), @@ -94,13 +107,6 @@ public: boost::asio::placeholders::bytes_transferred ) ); - - this->websocket_messages(); // temp - } - - // temporary - void websocket_messages() { - std::cout << "Websocket Messages" << std::endl; } void handle_read(const boost::system::error_code& error, @@ -131,22 +137,36 @@ public: } else { std::cout << "write successful" << std::endl; // end session - m_endpoint.remove_connection(type::shared_from_this()); + this->terminate(); } } + + // terminate cleans up a connection and removes it from the endpoint's + // connection list. + void terminate() { + // TODO: close socket cleanly + m_endpoint.remove_connection(type::shared_from_this()); + } protected: endpoint_type& m_endpoint; char m_data[512]; // temporary + + http::parser::request m_request; + http::parser::response m_response; }; // connection related types that it and its policy classes need. -template class socket> -struct connection_traits< connection > { +template < + typename endpoint, + template class role, + template class socket> +struct connection_traits< connection > { // type of the connection itself - typedef connection type; + typedef connection type; // types of the connection policies typedef endpoint endpoint_type; + typedef role< type > role_type; typedef socket< type > socket_type; }; diff --git a/src/endpoint.hpp b/src/endpoint.hpp index dd58f4f2fd..d9322e4714 100644 --- a/src/endpoint.hpp +++ b/src/endpoint.hpp @@ -28,83 +28,123 @@ #ifndef WEBSOCKETPP_ENDPOINT_HPP #define WEBSOCKETPP_ENDPOINT_HPP +#include +#include + +namespace websocketpp { + template + struct endpoint_traits; + + + + class endpoint_base { + protected: + boost::asio::io_service m_io_service; + }; +} + #include "connection.hpp" #include "sockets/plain.hpp" // should this be here? +#include "logger.hpp" + -#include #include #include namespace websocketpp { - -template -struct endpoint_traits; - // endpoint_base provides core functionality used by policy base class constructors -class endpoint_base { -protected: - boost::asio::io_service m_io_service; -}; + // endpoint host class -template