From b3a07d097857877f1a0daa1af3d162f0a487695c Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Sun, 10 Mar 2013 08:47:58 -0500 Subject: [PATCH] adds overridable connection base class --- test/connection/connection.cpp | 47 +++++++++++++++++ websocketpp/config/core.hpp | 3 ++ websocketpp/connection.hpp | 94 +++++++++++++++++++++------------ websocketpp/connection_base.hpp | 38 +++++++++++++ 4 files changed, 147 insertions(+), 35 deletions(-) create mode 100644 websocketpp/connection_base.hpp diff --git a/test/connection/connection.cpp b/test/connection/connection.cpp index 9f7f4c543e..ddf428ff4a 100644 --- a/test/connection/connection.cpp +++ b/test/connection/connection.cpp @@ -45,6 +45,53 @@ BOOST_AUTO_TEST_CASE( basic_http_request ) { BOOST_CHECK(o2 == output); } + +struct connection_extension { + connection_extension() : extension_value(5) {} + + int extension_method() { + return extension_value; + } + + bool is_server() const { + return false; + } + + int extension_value; +}; + +struct stub_config : public websocketpp::config::core { + typedef core::concurrency_type concurrency_type; + + typedef core::request_type request_type; + typedef core::response_type response_type; + + typedef core::message_type message_type; + typedef core::con_msg_manager_type con_msg_manager_type; + typedef core::endpoint_msg_manager_type endpoint_msg_manager_type; + + typedef core::alog_type alog_type; + typedef core::elog_type elog_type; + + typedef core::rng_type rng_type; + + typedef core::transport_type transport_type; + + typedef core::endpoint_base endpoint_base; + typedef connection_extension connection_base; +}; + +BOOST_AUTO_TEST_CASE( connection_extensions ) { + stub_config::alog_type alog; + stub_config::elog_type elog; + websocketpp::connection s(true,"",alog,elog); + + BOOST_CHECK( s.extension_value == 5 ); + BOOST_CHECK( s.extension_method() == 5 ); + + BOOST_CHECK( s.is_server() == true ); +} + /* BOOST_AUTO_TEST_CASE( basic_websocket_request ) { std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example.com\r\n\r\n"; diff --git a/websocketpp/config/core.hpp b/websocketpp/config/core.hpp index 8236403532..5794d2fbbd 100644 --- a/websocketpp/config/core.hpp +++ b/websocketpp/config/core.hpp @@ -53,6 +53,7 @@ // User stub base classes #include +#include // Extensions #include @@ -97,6 +98,8 @@ struct core { /// User overridable Endpoint base class typedef websocketpp::endpoint_base endpoint_base; + /// User overridable Connection base class + typedef websocketpp::connection_base connection_base; /// static const size_t connection_read_buffer_size = 512; diff --git a/websocketpp/connection.hpp b/websocketpp/connection.hpp index c642f4027a..292ae02e9e 100644 --- a/websocketpp/connection.hpp +++ b/websocketpp/connection.hpp @@ -113,6 +113,7 @@ namespace internal_state { template class connection : public config::transport_type::transport_con_type + , public config::connection_base , public lib::enable_shared_from_this< connection > { public: @@ -176,33 +177,11 @@ public: } // Public Interface - - /// Set Connection Handle - /** - * The connection handle is a token that can be shared outside the - * WebSocket++ core for the purposes of identifying a connection and - * sending it messages. - * - * @param hdl A connection_hdl that the connection will use to refer - * to itself. - */ - void set_handle(connection_hdl hdl) { - m_connection_hdl = hdl; - transport_con_type::set_handle(hdl); - } - - /// Get Connection Handle - /** - * The connection handle is a token that can be shared outside the - * WebSocket++ core for the purposes of identifying a connection and - * sending it messages. - * - * @return A handle to the connection - */ - connection_hdl get_handle() const { - return m_connection_hdl; - } + /////////////////////////// + // Set Handler Callbacks // + /////////////////////////// + /// Set open handler /** * The open handler is called after the WebSocket handshake is complete and @@ -329,14 +308,9 @@ public: m_message_handler = h; } - /// Return the same origin policy origin value from the opening request. - /** - * This value is available after the HTTP request has been fully read and - * may be called from any thread. - * - * @return The connection's origin value from the opening handshake. - */ - const std::string& get_origin() const; + ////////////////////////////////// + // Uncategorized public methods // + ////////////////////////////////// /// Get the size of the outgoing write buffer (in payload bytes) /** @@ -354,6 +328,10 @@ public: /// DEPRECATED: use get_buffered_amount instead size_t buffered_amount() const {return get_buffered_amount();} + //////////////////// + // Action Methods // + //////////////////// + /// Create a message and then add it to the outgoing send queue /** * Convenience method to send a message given a payload string and @@ -474,7 +452,7 @@ public: /// exception free variant of close void close(const close::status::value code, const std::string & reason, lib::error_code & ec); - + //////////////////////////////////////////////// // Pass-through access to the uri information // //////////////////////////////////////////////// @@ -608,6 +586,38 @@ public: */ void remove_header(const std::string &key); + ///////////////////////////////////////////////////////////// + // Pass-through access to the other connection information // + ///////////////////////////////////////////////////////////// + + /// Get Connection Handle + /** + * The connection handle is a token that can be shared outside the + * WebSocket++ core for the purposes of identifying a connection and + * sending it messages. + * + * @return A handle to the connection + */ + connection_hdl get_handle() const { + return m_connection_hdl; + } + + /// Get whether or not this connection is part of a server or client + /** + * @return whether or not the connection is attached to a server endpoint + */ + bool is_server() const { + return m_is_server; + } + + /// Return the same origin policy origin value from the opening request. + /** + * This value is available after the HTTP request has been fully read and + * may be called from any thread. + * + * @return The connection's origin value from the opening handshake. + */ + const std::string& get_origin() const; //////////////////////////////////////////////////////////////////////// // The remaining public member functions are for internal/policy use // @@ -615,6 +625,20 @@ public: // you are doing. // //////////////////////////////////////////////////////////////////////// + /// Set Connection Handle + /** + * The connection handle is a token that can be shared outside the + * WebSocket++ core for the purposes of identifying a connection and + * sending it messages. + * + * @param hdl A connection_hdl that the connection will use to refer + * to itself. + */ + void set_handle(connection_hdl hdl) { + m_connection_hdl = hdl; + transport_con_type::set_handle(hdl); + } + void start(); void read(size_t num_bytes); diff --git a/websocketpp/connection_base.hpp b/websocketpp/connection_base.hpp new file mode 100644 index 0000000000..c65b6472bd --- /dev/null +++ b/websocketpp/connection_base.hpp @@ -0,0 +1,38 @@ +/* + * 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: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the WebSocket++ Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef WEBSOCKETPP_CONNECTION_BASE_HPP +#define WEBSOCKETPP_CONNECTION_BASE_HPP + +namespace websocketpp { + +/// Stub for user supplied base class. +class connection_base {}; + +} // namespace websocketpp + +#endif // WEBSOCKETPP_CONNECTION_BASE_HPP