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

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