adds overridable connection base class

This commit is contained in:
Peter Thorson
2013-03-10 08:47:58 -05:00
parent 117a76f1f9
commit b3a07d0978
4 changed files with 147 additions and 35 deletions

View File

@@ -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<stub_config> 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";

View File

@@ -53,6 +53,7 @@
// User stub base classes
#include <websocketpp/endpoint_base.hpp>
#include <websocketpp/connection_base.hpp>
// Extensions
#include <websocketpp/extensions/permessage_compress/disabled.hpp>
@@ -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;

View File

@@ -113,6 +113,7 @@ namespace internal_state {
template <typename config>
class connection
: public config::transport_type::transport_con_type
, public config::connection_base
, public lib::enable_shared_from_this< connection<config> >
{
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);

View File

@@ -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