splitting out policy refactor into final files and namespaces

This commit is contained in:
Peter Thorson
2011-11-16 09:06:28 -06:00
parent dfb30b157a
commit 25504243d2
8 changed files with 804 additions and 89 deletions

136
src/connection.hpp Normal file
View File

@@ -0,0 +1,136 @@
/*
* Copyright (c) 2011, 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_HPP
#define WEBSOCKETPP_CONNECTION_HPP
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <iostream> // temporary?
namespace websocketpp {
template <typename T>
struct connection_traits;
template <
typename endpoint,
template <class> class socket>
class connection
: public socket< connection<endpoint,socket> >,
public boost::enable_shared_from_this< connection<endpoint, socket> >
{
public:
typedef connection_traits< connection<endpoint,socket> > traits;
// get types that we need from our traits class
typedef typename traits::type type;
typedef typename traits::socket_type socket_type;
typedef endpoint endpoint_type;
connection(endpoint_type& e)
: socket_type(e),
m_endpoint(e)
{
std::cout << "setup connection" << std::endl;
}
void websocket_handshake() {
std::cout << "Websocket Handshake" << std::endl;
socket_type::get_socket().async_read_some(
boost::asio::buffer(m_data, 512),
boost::bind(
&type::handle_read,
type::shared_from_this(),
boost::asio::placeholders::error,
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,
size_t bytes_transferred)
{
if (error) {
std::cout << "read error" << std::endl;
} else {
std::cout << "read complete: " << m_data << std::endl;
std::string r = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nbar";
boost::asio::async_write(
socket_type::get_socket(),
boost::asio::buffer(r, r.size()),
boost::bind(
&type::handle_write,
type::shared_from_this(),
boost::asio::placeholders::error
)
);
}
}
void handle_write(const boost::system::error_code& error) {
if (error) {
std::cout << "write error" << std::endl;
} else {
std::cout << "write successful" << std::endl;
// end session
m_endpoint.remove_connection(type::shared_from_this());
}
}
protected:
endpoint_type& m_endpoint;
char m_data[512]; // temporary
};
// connection related types that it and its policy classes need.
template <typename endpoint,template <class> class socket>
struct connection_traits< connection<endpoint, socket> > {
// type of the connection itself
typedef connection<endpoint,socket> type;
// types of the connection policies
typedef endpoint endpoint_type;
typedef socket< type > socket_type;
};
} // namespace websocketpp
#endif // WEBSOCKETPP_CONNECTION_HPP

111
src/endpoint.hpp Normal file
View File

@@ -0,0 +1,111 @@
/*
* Copyright (c) 2011, 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_ENDPOINT_HPP
#define WEBSOCKETPP_ENDPOINT_HPP
#include "connection.hpp"
#include <boost/shared_ptr.hpp>
#include <iostream>
namespace websocketpp {
template <typename T>
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 <template <class> class role,template <class> class socket>
class endpoint
: public endpoint_base,
public role< endpoint<role,socket> >,
public socket< endpoint<role,socket> >
{
public:
typedef endpoint_traits< endpoint<role,socket> > traits;
// get types that we need from our traits class
typedef typename traits::handler_ptr handler_ptr;
typedef typename traits::role_type role_type;
typedef typename traits::socket_type socket_type;
typedef typename traits::connection_type connection_type;
typedef typename traits::connection_ptr connection_ptr;
endpoint(handler_ptr h) : role_type(m_io_service,h),socket_type(m_io_service) {
std::cout << "Setup endpoint" << std::endl;
}
void start() {
std::cout << "Connect" << std::endl;
connection_ptr con = create_connection();
con->security_handshake();
}
connection_ptr create_connection() {
connection_ptr new_connection(new connection_type(*this));
m_connections.insert(new_connection);
return new_connection;
}
void remove_connection(connection_ptr con) {
m_connections.erase(con);
}
private:
std::set<connection_ptr> m_connections;
};
// endpoint related types that it and its policy classes need.
template <template <class> class role,template <class> class socket>
struct endpoint_traits< endpoint<role, socket> > {
// type of the endpoint itself
typedef endpoint<role,socket> type;
// types of the endpoint policies
typedef role< type > role_type;
typedef socket< type > socket_type;
// type of the handler that this endpoint and its connections call back.
typedef typename role_type::handler handler;
typedef typename role_type::handler_ptr handler_ptr;
// types of the connections that this endpoint manages and pointers to them
typedef connection<type,socket< type >::template connection> connection_type;
typedef boost::shared_ptr<connection_type> connection_ptr;
};
} // namespace websocketpp
#endif // WEBSOCKETPP_ENDPOINT_HPP

View File

@@ -10,11 +10,12 @@ SSL:
*/
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <set>
class server_handler {
virtual void on_action() = 0;
@@ -31,10 +32,12 @@ typedef boost::shared_ptr<client_handler> client_handler_ptr;
namespace connection {
template <typename endpoint_type,template <class> class security_policy>
class connection : public security_policy< connection<endpoint_type,security_policy> > {
class connection
: public security_policy< connection<endpoint_type,security_policy> >,
public boost::enable_shared_from_this< connection<endpoint_type, security_policy> > {
public:
//typedef role< connection<role,security_policy> > role_type;
typedef security_policy< connection<endpoint_type,security_policy> > security_policy_type;
typedef connection<endpoint_type,security_policy> type;
typedef security_policy< type > security_policy_type;
//typedef typename role_type::handler_ptr handler_ptr;
@@ -44,13 +47,56 @@ public:
void websocket_handshake() {
std::cout << "Websocket Handshake" << std::endl;
security_policy_type::get_socket().async_read_some(
boost::asio::buffer(m_data, 512),
boost::bind(
&type::handle_read,
type::shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
)
);
this->websocket_messages();
}
void websocket_messages() {
std::cout << "Websocket Messages" << std::endl;
}
void handle_read(const boost::system::error_code& error,size_t bytes_transferred) {
if (error) {
std::cout << "read error" << std::endl;
} else {
std::cout << "read complete: " << m_data << std::endl;
std::string r = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nbar";
boost::asio::async_write(
security_policy_type::get_socket(),
boost::asio::buffer(r, r.size()),
boost::bind(
&type::handle_write,
type::shared_from_this(),
boost::asio::placeholders::error
)
);
}
}
void handle_write(const boost::system::error_code& error) {
if (error) {
std::cout << "write error" << std::endl;
} else {
std::cout << "write successful" << std::endl;
// end session
m_endpoint.remove_connection(type::shared_from_this());
}
}
protected:
endpoint_type& m_endpoint;
char m_data[512];
};
// Connection roles
@@ -63,17 +109,7 @@ public:
std::cout << "setup server connection role" << std::endl;
}
void public_api() {
std::cout << "connection::server::public_api()" << std::endl;
}
protected:
void protected_api() {
std::cout << "connection::server::protected_api()" << std::endl;
}
private:
void private_api() {
std::cout << "connection::server::private_api()" << std::endl;
}
handler_ptr m_handler;
};
@@ -86,17 +122,7 @@ public:
std::cout << "setup client connection role" << std::endl;
}
void public_api() {
std::cout << "connection::client::public_api()" << std::endl;
}
protected:
void protected_api() {
std::cout << "connection::client::protected_api()" << std::endl;
}
private:
void private_api() {
std::cout << "connection::client::private_api()" << std::endl;
}
handler_ptr m_handler;
};
@@ -104,7 +130,6 @@ private:
namespace endpoint {
// test
template <typename derived_t>
struct endpoint_traits;
@@ -141,8 +166,16 @@ public:
}
connection_ptr create_connection() {
return connection_ptr(new connection_type(*this));
connection_ptr new_connection(new connection_type(*this));
m_connections.insert(new_connection);
return new_connection;
}
void remove_connection(connection_ptr con) {
m_connections.erase(con);
}
private:
std::set<connection_ptr> m_connections;
};
// endpoint related types that it's policy classes need.
@@ -174,7 +207,11 @@ public:
std::cout << "performing plain security handshake" << std::endl;
static_cast< connection_type* >(this)->websocket_handshake();
}
boost::asio::ip::tcp::socket& get_raw_socket() {
return m_socket;
}
boost::asio::ip::tcp::socket& get_socket() {
return m_socket;
}
@@ -219,33 +256,31 @@ public:
void security_handshake() {
std::cout << "performing ssl security handshake" << std::endl;
/*m_socket.get_io_service().post(
boost::bind(
&connection<connection_type>::test,
this
)
);*/
m_socket.async_handshake(
boost::asio::ssl::stream_base::server,
boost::bind(
&connection<connection_type>::test,
this
&connection<connection_type>::handle_handshake,
this,
boost::asio::placeholders::error
)
);
}
void test() {
static_cast< connection_type* >(this)->websocket_handshake();
}
void handle_handshake(const boost::system::error_code& error) {
static_cast< connection_type* >(this)->websocket_handshake();
if (error) {
std::cout << "SSL handshake error" << std::endl;
} else {
static_cast< connection_type* >(this)->websocket_handshake();
}
}
ssl_socket::lowest_layer_type& get_socket() {
ssl_socket::lowest_layer_type& get_raw_socket() {
return m_socket.lowest_layer();
}
ssl_socket& get_socket() {
return m_socket;
}
private:
ssl_socket m_socket;
};
@@ -258,9 +293,9 @@ protected:
boost::asio::ssl::context::no_sslv2 |
boost::asio::ssl::context::single_dh_use);
m_context.set_password_callback(boost::bind(&type::get_password, this));
m_context.use_certificate_chain_file("/Users/zaphoyd/Documents/ZS/websocketpp/src/ssl/server.pem");
m_context.use_private_key_file("/Users/zaphoyd/Documents/ZS/websocketpp/src/ssl/server.pem", boost::asio::ssl::context::pem);
m_context.use_tmp_dh_file("/Users/zaphoyd/Documents/ZS/websocketpp/src/ssl/dh512.pem");
m_context.use_certificate_chain_file("/Users/zaphoyd/Documents/websocketpp/src/ssl/server.pem");
m_context.use_private_key_file("/Users/zaphoyd/Documents/websocketpp/src/ssl/server.pem", boost::asio::ssl::context::pem);
m_context.use_tmp_dh_file("/Users/zaphoyd/Documents/websocketpp/src/ssl/dh512.pem");
} catch (std::exception& e) {
std::cout << e.what() << std::endl;
}
@@ -321,7 +356,7 @@ private:
connection_ptr con = static_cast< endpoint_type* >(this)->create_connection();
m_acceptor.async_accept(
con->get_socket(),
con->get_raw_socket(),
boost::bind(
&server_type::handle_accept,
this,
@@ -399,8 +434,15 @@ public:
}
// application
class application_server_handler : public server_handler {
// Application start
#include "endpoint.hpp"
#include "roles/server.hpp"
#include "sockets/ssl.hpp"
typedef websocketpp::endpoint<websocketpp::role::server,websocketpp::socket::ssl> endpoint_type;
// application headers
class application_server_handler : public websocketpp::role::server::handler {
void on_action() {
std::cout << "application_server_handler::on_action()" << std::endl;
}
@@ -412,53 +454,14 @@ class application_client_handler : public client_handler {
}
};
using endpoint::factory;
int main () {
std::cout << "Endpoint 0" << std::endl;
server_handler_ptr h(new application_server_handler());
endpoint::endpoint<endpoint::server,endpoint::ssl> e(h);
websocketpp::endpoint<endpoint::server,endpoint::ssl> e(h);
e.listen(9000);
//e.connect();
//e.public_api();
std::cout << std::endl;
/*std::cout << "Endpoint 1" << std::endl;
server_handler_ptr handler1(new application_server_handler());
factory<endpoint::server,endpoint::plain> ef1;
factory<endpoint::server,endpoint::plain>::endpoint_ptr e1(ef1.create(handler1));
e1->connect();
e1->public_api();
std::cout << std::endl;
std::cout << "Endpoint 2" << std::endl;
server_handler_ptr handler2(new application_server_handler());
factory<endpoint::server,endpoint::ssl> ef2;
factory<endpoint::server,endpoint::ssl>::endpoint_ptr e2(ef2.create(handler2));
e2->connect();
e2->public_api();
std::cout << std::endl;
std::cout << "Endpoint 3" << std::endl;
client_handler_ptr handler3(new application_client_handler());
factory<endpoint::client,endpoint::plain> ef3;
factory<endpoint::client,endpoint::plain>::endpoint_ptr e3(ef3.create(handler3));
e3->connect();
e3->public_api();
std::cout << std::endl;
std::cout << "Endpoint 4" << std::endl;
client_handler_ptr handler4(new application_client_handler());
factory<endpoint::client,endpoint::ssl> ef4;
factory<endpoint::client,endpoint::ssl>::endpoint_ptr e4(ef4.create(handler4));
e4->connect();
e4->public_api();
std::cout << std::endl;*/
//connection::connection<connection::ssl> c;
//c.security_handshake();
return 0;
}

88
src/roles/client.hpp Normal file
View File

@@ -0,0 +1,88 @@
/*
* Copyright (c) 2011, 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_ROLE_CLIENT_HPP
#define WEBSOCKETPP_ROLE_CLIENT_HPP
#include "../endpoint.hpp"
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <iostream>
namespace websocketpp {
namespace role {
template <class endpoint>
class client {
public:
// handler interface callback class
class handler {
virtual void on_action() = 0;
};
typedef boost::shared_ptr<handler> handler_ptr;
// types
typedef client<endpoint> type;
typedef endpoint endpoint_type;
typedef typename endpoint_traits<endpoint>::connection_ptr connection_ptr;
client (boost::asio::io_service& m,handler_ptr h) : m_handler(h),m_io_service(m) {
std::cout << "setup client endpoint role" << std::endl;
}
void connect() {
static_cast< endpoint_type* >(this)->start();
}
void public_api() {
std::cout << "endpoint::client::public_api()" << std::endl;
}
protected:
handler_ptr get_handler() {
return m_handler;
}
void protected_api() {
std::cout << "endpoint::client::protected_api()" << std::endl;
}
private:
void private_api() {
std::cout << "endpoint::client::private_api()" << std::endl;
}
handler_ptr m_handler;
boost::asio::io_service& m_io_service;
};
} // namespace role
} // namespace websocketpp
#endif // WEBSOCKETPP_ROLE_CLIENT_HPP

128
src/roles/server.hpp Normal file
View File

@@ -0,0 +1,128 @@
/*
* Copyright (c) 2011, 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_ROLE_SERVER_HPP
#define WEBSOCKETPP_ROLE_SERVER_HPP
#include "../endpoint.hpp"
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <iostream>
namespace websocketpp {
namespace role {
template <class endpoint>
class server {
public:
// handler interface callback class
class handler {
virtual void on_action() = 0;
};
typedef boost::shared_ptr<handler> handler_ptr;
// types
typedef server<endpoint> type;
typedef endpoint endpoint_type;
typedef typename endpoint_traits<endpoint>::connection_ptr connection_ptr;
server(boost::asio::io_service& m,handler_ptr h)
: m_handler(h),
m_io_service(m),
m_endpoint(),
m_acceptor(m)
{
std::cout << "setup server endpoint role" << std::endl;
}
void listen(unsigned short port) {
m_endpoint.port(port);
m_acceptor.open(m_endpoint.protocol());
m_acceptor.set_option(boost::asio::socket_base::reuse_address(true));
m_acceptor.bind(m_endpoint);
m_acceptor.listen();
this->accept();
m_io_service.run();
}
void public_api() {
std::cout << "role::server::public_api()" << std::endl;
}
protected:
handler_ptr get_handler() {
return m_handler;
}
void protected_api() {
std::cout << "role::server::protected_api()" << std::endl;
}
private:
void accept() {
connection_ptr con = static_cast< endpoint_type* >(this)->create_connection();
m_acceptor.async_accept(
con->get_raw_socket(),
boost::bind(
&type::handle_accept,
this,
con,
boost::asio::placeholders::error
)
);
}
void handle_accept(connection_ptr con, const boost::system::error_code& error) {
if (!error) {
con->security_handshake();
} else {
throw "";
}
this->accept();
}
void private_api() {
std::cout << "role::server::private_api()" << std::endl;
}
handler_ptr m_handler;
boost::asio::io_service& m_io_service;
boost::asio::ip::tcp::endpoint m_endpoint;
boost::asio::ip::tcp::acceptor m_acceptor;
};
} // namespace role
} // namespace websocketpp
#endif // WEBSOCKETPP_ROLE_SERVER_HPP

95
src/sockets/plain.hpp Normal file
View File

@@ -0,0 +1,95 @@
/*
* Copyright (c) 2011, 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_SOCKET_PLAIN_HPP
#define WEBSOCKETPP_SOCKET_PLAIN_HPP
/*
websocketpp::socket API
endpoint policy that provides:
nested connection policy called `connection` that provides:
- constructor that takes a reference to the endpoint_policy
- async_init
- async_read_some
- async_write_some
- get_raw_socket
*/
#include <boost/asio.hpp>
#include <iostream>
namespace websocketpp {
namespace socket {
template <typename endpoint_type>
class plain {
public:
boost::asio::io_service& get_io_service() {
return m_io_service;
}
// Connection specific details
template <typename connection_type>
class connection {
public:
connection(plain<endpoint_type>& e) : m_socket(e.get_io_service()) {
std::cout << "setup plain connection" << std::endl;
}
void security_handshake() {
std::cout << "performing plain security handshake" << std::endl;
static_cast< connection_type* >(this)->websocket_handshake();
}
boost::asio::ip::tcp::socket& get_raw_socket() {
return m_socket;
}
boost::asio::ip::tcp::socket& get_socket() {
return m_socket;
}
private:
boost::asio::ip::tcp::socket m_socket;
};
protected:
plain (boost::asio::io_service& m) : m_io_service(m) {
std::cout << "setup plain endpoint" << std::endl;
}
private:
boost::asio::io_service& m_io_service;
};
} // namespace socket
} // namespace websocketpp
#endif // WEBSOCKETPP_SOCKET_PLAIN_HPP

125
src/sockets/ssl.hpp Normal file
View File

@@ -0,0 +1,125 @@
/*
* Copyright (c) 2011, 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_SOCKET_SSL_HPP
#define WEBSOCKETPP_SOCKET_SSL_HPP
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/bind.hpp>
#include <iostream>
namespace websocketpp {
namespace socket {
template <typename endpoint_type>
class ssl {
public:
typedef ssl<endpoint_type> type;
std::string get_password() const {
return "test";
}
// should be private friended
boost::asio::io_service& get_io_service() {
return m_io_service;
}
// should be private friended
boost::asio::ssl::context& get_context() {
return m_context;
}
// Connection specific details
template <typename connection_type>
class connection {
public:
typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket;
connection(ssl<endpoint_type>& e) : m_socket(e.get_io_service(),e.get_context()) {
std::cout << "setup ssl connection" << std::endl;
}
void security_handshake() {
std::cout << "performing ssl security handshake" << std::endl;
m_socket.async_handshake(
boost::asio::ssl::stream_base::server,
boost::bind(
&connection<connection_type>::handle_handshake,
this,
boost::asio::placeholders::error
)
);
}
void handle_handshake(const boost::system::error_code& error) {
if (error) {
std::cout << "SSL handshake error" << std::endl;
} else {
static_cast< connection_type* >(this)->websocket_handshake();
}
}
ssl_socket::lowest_layer_type& get_raw_socket() {
return m_socket.lowest_layer();
}
ssl_socket& get_socket() {
return m_socket;
}
private:
ssl_socket m_socket;
};
protected:
ssl (boost::asio::io_service& m) : m_io_service(m),m_context(boost::asio::ssl::context::sslv23) {
std::cout << "setup ssl endpoint" << std::endl;
try {
// this should all be in separate init functions
m_context.set_options(boost::asio::ssl::context::default_workarounds |
boost::asio::ssl::context::no_sslv2 |
boost::asio::ssl::context::single_dh_use);
m_context.set_password_callback(boost::bind(&type::get_password, this));
m_context.use_certificate_chain_file("/Users/zaphoyd/Documents/websocketpp/src/ssl/server.pem");
m_context.use_private_key_file("/Users/zaphoyd/Documents/websocketpp/src/ssl/server.pem", boost::asio::ssl::context::pem);
m_context.use_tmp_dh_file("/Users/zaphoyd/Documents/websocketpp/src/ssl/dh512.pem");
} catch (std::exception& e) {
std::cout << e.what() << std::endl;
}
}
private:
boost::asio::io_service& m_io_service;
boost::asio::ssl::context m_context;
};
} // namespace socket
} // namespace websocketpp
#endif // WEBSOCKETPP_SOCKET_SSL_HPP

View File

@@ -160,6 +160,12 @@
B62A5A3214695185005F9EB0 /* frame_parser.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; name = frame_parser.hpp; path = src/interfaces/frame_parser.hpp; sourceTree = "<group>"; };
B62A5A3314695185005F9EB0 /* session.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; name = session.hpp; path = src/interfaces/session.hpp; sourceTree = "<group>"; };
B62A5A34146963FE005F9EB0 /* protocol.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; name = protocol.hpp; path = src/interfaces/protocol.hpp; sourceTree = "<group>"; };
B62A5A511473EBB0005F9EB0 /* connection.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; name = connection.hpp; path = src/connection.hpp; sourceTree = "<group>"; };
B62A5A521473EBB0005F9EB0 /* endpoint.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; name = endpoint.hpp; path = src/endpoint.hpp; sourceTree = "<group>"; };
B62A5A561473EBE8005F9EB0 /* client.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; name = client.hpp; path = src/roles/client.hpp; sourceTree = "<group>"; };
B62A5A571473EBE8005F9EB0 /* server.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; name = server.hpp; path = src/roles/server.hpp; sourceTree = "<group>"; };
B62A5A581473EBF1005F9EB0 /* plain.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; name = plain.hpp; path = src/sockets/plain.hpp; sourceTree = "<group>"; };
B62A5A591473EBF1005F9EB0 /* ssl.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; name = ssl.hpp; path = src/sockets/ssl.hpp; sourceTree = "<group>"; };
B6828875143745DA002BA48B /* chat_client_handler.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = chat_client_handler.cpp; path = examples/chat_client/chat_client_handler.cpp; sourceTree = "<group>"; };
B6828876143745DA002BA48B /* chat_client_handler.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; name = chat_client_handler.hpp; path = examples/chat_client/chat_client_handler.hpp; sourceTree = "<group>"; };
B6828877143745DA002BA48B /* chat_client.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = chat_client.cpp; path = examples/chat_client/chat_client.cpp; sourceTree = "<group>"; };
@@ -215,7 +221,7 @@
B6FE8D2E146C2C9500B32547 /* hybi_legacy_processor.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; name = hybi_legacy_processor.hpp; path = src/hybi_legacy_processor.hpp; sourceTree = "<group>"; };
B6FE8D30146C721200B32547 /* hybi_processor.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; name = hybi_processor.hpp; path = src/hybi_processor.hpp; sourceTree = "<group>"; };
B6FE8D4914730AA600B32547 /* policy.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = policy.cpp; path = src/policy.cpp; sourceTree = "<group>"; };
B6FE8D4F14730AE900B32547 /* Policy Test */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "Policy Test"; sourceTree = BUILT_PRODUCTS_DIR; };
B6FE8D4F14730AE900B32547 /* Policy Test */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; name = "Policy Test"; path = policy_test; sourceTree = BUILT_PRODUCTS_DIR; };
B6FE8D5214730AEA00B32547 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
B6FE8D5414730AEA00B32547 /* Policy_Test.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = Policy_Test.1; sourceTree = "<group>"; };
B6FE8D5B14730B1A00B32547 /* libcrypto.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libcrypto.dylib; path = usr/lib/libcrypto.dylib; sourceTree = SDKROOT; };
@@ -314,6 +320,24 @@
name = interfaces;
sourceTree = "<group>";
};
B62A5A541473EBB9005F9EB0 /* roles */ = {
isa = PBXGroup;
children = (
B62A5A561473EBE8005F9EB0 /* client.hpp */,
B62A5A571473EBE8005F9EB0 /* server.hpp */,
);
name = roles;
sourceTree = "<group>";
};
B62A5A551473EBC4005F9EB0 /* sockets */ = {
isa = PBXGroup;
children = (
B62A5A581473EBF1005F9EB0 /* plain.hpp */,
B62A5A591473EBF1005F9EB0 /* ssl.hpp */,
);
name = sockets;
sourceTree = "<group>";
};
B6CF18121437C370009295BE /* echo_client */ = {
isa = PBXGroup;
children = (
@@ -363,6 +387,8 @@
B6DF1C7F1434ABB70029A1B1 /* src */ = {
isa = PBXGroup;
children = (
B62A5A551473EBC4005F9EB0 /* sockets */,
B62A5A541473EBB9005F9EB0 /* roles */,
B6FE8D30146C721200B32547 /* hybi_processor.hpp */,
B6FE8D2E146C2C9500B32547 /* hybi_legacy_processor.hpp */,
B62A5A2F1469512A005F9EB0 /* interfaces */,
@@ -372,6 +398,8 @@
B6FE8D09145B0F7400B32547 /* rng */,
B6FE8D05145AFF5F00B32547 /* websocket_constants.hpp */,
B6FE8D4914730AA600B32547 /* policy.cpp */,
B62A5A511473EBB0005F9EB0 /* connection.hpp */,
B62A5A521473EBB0005F9EB0 /* endpoint.hpp */,
B6DF1C921434AC470029A1B1 /* websocket_client_session.cpp */,
B6DF1C931434AC470029A1B1 /* websocket_client_session.hpp */,
B6DF1C941434AC470029A1B1 /* websocket_client.cpp */,
@@ -1109,6 +1137,7 @@
B6FE8D5814730AEA00B32547 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};