updates echo_server example

This commit is contained in:
Peter Thorson
2011-11-19 01:18:28 -06:00
parent 221693f975
commit 7a0f9f3073
4 changed files with 72 additions and 33 deletions

View File

@@ -25,48 +25,89 @@
*
*/
#include "echo.hpp"
#include "../../src/endpoint.hpp"
#include "../../src/roles/server.hpp"
#include "../../src/sockets/ssl.hpp"
#include "../../src/websocketpp.hpp"
#include <boost/asio.hpp>
#include <cstring>
#include <iostream>
typedef websocketpp::endpoint<websocketpp::role::server,websocketpp::socket::plain> plain_endpoint_type;
typedef websocketpp::endpoint<websocketpp::role::server,websocketpp::socket::ssl> ssl_endpoint_type;
typedef websocketpp::role::server<plain_endpoint_type>::handler_ptr plain_handler_ptr;
typedef websocketpp::role::server<ssl_endpoint_type>::handler_ptr ssl_handler_ptr;
using boost::asio::ip::tcp;
template <typename T>
class echo_server_handler : public T::handler {
public:
typedef typename websocketpp::role::server<T>::handler handler_type;
typedef typename websocketpp::role::server<T>::handler_ptr handler_ptr;
typedef typename handler_type::connection_ptr connection_ptr;
void validate(connection_ptr connection) {
//std::cout << "state: " << connection->get_state() << std::endl;
}
void on_open(connection_ptr connection) {
//std::cout << "connection opened" << std::endl;
}
void on_close(connection_ptr connection) {
//std::cout << "connection closed" << std::endl;
}
void on_message(connection_ptr connection,websocketpp::utf8_string_ptr msg) {
//std::cout << "got message: " << *msg << std::endl;
connection->send(*msg);
}
void on_message(connection_ptr connection,websocketpp::binary_string_ptr data) {
//std::cout << "got binary message of length: " << data->size() << std::endl;
connection->send(*data);
}
void http(connection_ptr connection) {
connection->set_body("HTTP Response!!");
}
void on_fail(connection_ptr connection) {
std::cout << "connection failed" << std::endl;
}
};
int main(int argc, char* argv[]) {
short port = 9002;
bool ssl = false;
if (argc == 2) {
// TODO: input validation?
port = atoi(argv[2]);
}
if (argc == 3) {
// TODO: input validation?
port = atoi(argv[2]);
ssl = (strcmp(argv[3],"-ssl"));
}
try {
// create an instance of our handler
server_handler_ptr default_handler(new websocketecho::echo_server_handler());
// create a server that listens on port `port` and uses our handler
websocketpp::basic_server_ptr server(new websocketpp::basic_server(port,default_handler));
if (!ssl) {
plain_handler_ptr h(new echo_server_handler<plain_endpoint_type>());
plain_endpoint_type e(h);
e.alog().set_level(websocketpp::log::alevel::CONNECT & websocketpp::log::alevel::DISCONNECT);
e.elog().set_levels(websocketpp::log::elevel::ERROR,websocketpp::log::elevel::FATAL);
e.listen(port);
} else {
ssl_handler_ptr h(new echo_server_handler<ssl_endpoint_type>());
ssl_endpoint_type e(h);
e.alog().set_level(websocketpp::log::alevel::CONNECT & websocketpp::log::alevel::DISCONNECT);
e.elog().set_levels(websocketpp::log::elevel::ERROR,websocketpp::log::elevel::FATAL);
e.listen(port);
}
server->elog().set_levels(websocketpp::log::elevel::DEVEL,websocketpp::log::elevel::FATAL);
server->alog().set_level(websocketpp::log::alevel::ALL);
//server->parse_command_line(argc, argv);
// setup server settings
//server->set_alog_level(websocketpp::ALOG_OFF);
//server->set_elog_level(websocketpp::LOG_OFF);
// bump up max message size to maximum since we may be using the echo
// server to test performance and protocol extremes.
//server->set_max_message_size(websocketpp::frame::limits::PAYLOAD_SIZE_JUMBO);
std::cout << "Starting echo server on port " << port << std::endl;
// start the server
server->run();
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}

View File

@@ -70,8 +70,6 @@ int main () {
e.listen(9002);
//e.connect();
//e.public_api();
std::cout << std::endl;
return 0;

View File

@@ -84,8 +84,6 @@ public:
connection(ssl<endpoint_type>& e) : m_socket(e.get_io_service(),e.get_context()),m_endpoint(e) {}
void async_init(boost::function<void(const boost::system::error_code&)> callback) {
std::cout << "performing ssl security handshake" << std::endl;
m_socket.async_handshake(
m_endpoint.get_handshake_type(),
boost::bind(
@@ -123,8 +121,6 @@ public:
};
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 |

View File

@@ -10,6 +10,8 @@
B613878F145CA61300ED9B19 /* libboost_date_time.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = B6DF1CBE1434AF6A0029A1B1 /* libboost_date_time.dylib */; };
B6138790145CA61C00ED9B19 /* libboost_program_options.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = B6FE8CEB145A0F1900B32547 /* libboost_program_options.dylib */; };
B62A5A7214775ECF005F9EB0 /* libwebsocketpp.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = B6DF1C721434A8280029A1B1 /* libwebsocketpp.dylib */; };
B62A5A7314778E5C005F9EB0 /* libssl.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = B6FE8D5D14730B2200B32547 /* libssl.dylib */; };
B62A5A7414778E65005F9EB0 /* libcrypto.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = B6FE8D5B14730B1A00B32547 /* libcrypto.dylib */; };
B68288871437460E002BA48B /* chat_client_handler.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B6828875143745DA002BA48B /* chat_client_handler.cpp */; };
B68288881437460E002BA48B /* chat_client.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B6828877143745DA002BA48B /* chat_client.cpp */; };
B682888914374617002BA48B /* libwebsocketpp.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = B6DF1C721434A8280029A1B1 /* libwebsocketpp.dylib */; };
@@ -261,6 +263,8 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
B62A5A7414778E65005F9EB0 /* libcrypto.dylib in Frameworks */,
B62A5A7314778E5C005F9EB0 /* libssl.dylib in Frameworks */,
B6138790145CA61C00ED9B19 /* libboost_program_options.dylib in Frameworks */,
B613878F145CA61300ED9B19 /* libboost_date_time.dylib in Frameworks */,
B6DF1CE41435F8250029A1B1 /* Foundation.framework in Frameworks */,