From 44e47267e92b894dded0bfa451678ee3b38d8ec3 Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Sun, 20 Jan 2013 11:05:30 -0600 Subject: [PATCH] initial work on broadcast_server example --- examples/broadcast_server/SConscript | 23 ++++ .../broadcast_server/broadcast_server.cpp | 100 ++++++++++++++++++ init.txt | 10 +- 3 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 examples/broadcast_server/SConscript create mode 100644 examples/broadcast_server/broadcast_server.cpp diff --git a/examples/broadcast_server/SConscript b/examples/broadcast_server/SConscript new file mode 100644 index 0000000000..5b25973de6 --- /dev/null +++ b/examples/broadcast_server/SConscript @@ -0,0 +1,23 @@ +## Broadcast Server example +## + +Import('env') +Import('env_cpp11') +Import('boostlibs') +Import('platform_libs') +Import('polyfill_libs') + +env = env.Clone () +env_cpp11 = env_cpp11.Clone () + +prgs = [] + +# if a C++11 environment is avaliable build using that, otherwise use boost +if env_cpp11.has_key('WSPP_CPP11_ENABLED'): + ALL_LIBS = boostlibs(['system'],env_cpp11) + [platform_libs] + [polyfill_libs] + prgs += env_cpp11.Program('broadcast_server', ["broadcast_server.cpp"], LIBS = ALL_LIBS) +else: + ALL_LIBS = boostlibs(['system','regex'],env) + [platform_libs] + [polyfill_libs] + prgs += env.Program('broadcast_server', ["broadcast_server.cpp"], LIBS = ALL_LIBS) + +Return('prgs') diff --git a/examples/broadcast_server/broadcast_server.cpp b/examples/broadcast_server/broadcast_server.cpp new file mode 100644 index 0000000000..9eed749efe --- /dev/null +++ b/examples/broadcast_server/broadcast_server.cpp @@ -0,0 +1,100 @@ +#include + +#include + +#include + +typedef websocketpp::server server; + +using websocketpp::lib::placeholders::_1; +using websocketpp::lib::placeholders::_2; +using websocketpp::lib::bind; + +/* on_open insert connection_hdl into channel + * on_close remove connection_hdl from channel + * on_message queue send to all channels + */ + +class broadcast_server { +public: + broadcast_server() { + // Initialize Asio Transport + m_server.init_asio(); + + // Register handler callbacks + m_server.set_open_handler(bind(&on_open,this,::_1)); + m_server.set_close_handler(bind(&on_message,this,::_1)); + m_server.set_message_handler(bind(&on_message,this,::_1,::_2)); + } + + void run(uint16_t port) { + // listen on specified port + m_server.listen(port); + + // Start the server accept loop + echo_server.start_accept(); + + // Start the ASIO io_service run loop + try { + echo_server.run(); + } catch (const std::exception & e) { + std::cout << e.what() << std::endl; + } catch (websocketpp::lib::error_code e) { + std::cout << e.message() << std::endl; + } catch (...) { + std::cout << "other exception" << std::endl; + } + } + + void on_open(connection_hdl hdl) { + boost::unique_lock lock(m_lock); + m_connections.insert(hdl); + } + + void on_close(connection_hdl hdl) { + boost::unique_lock lock(m_lock); + m_connections.remove(hdl); + } + + void on_message(connection_hdl hdl, server::message_ptr msg) { + // queue message up for sending by processing thread + boost::unique_lock lock(m_lock); + m_message_queue.push(msg); + lock.unlock(); + m_cond.notify_one(); + } + + void process_messages() { + while(1) { + boost::unique_lock lock(m_lock); + + while(m_message_queue.empty()) { + m_cond.wait(m_lock); + } + + message_ptr msg = m_message_queue.front(); + m_message_queue.pop(); + + lock.unlock(); + + + } + } +private: + server m_server; + std::set m_connections; + std::queue m_message_queue; + + boost::mutex m_mutex; + boost::condition_variable m_cond; +} + +int main() { + broadcast_server server; + + // Start a thread to run the processing loop + boost::thread(bind(&broadcast_server::process_messages,&server)); + + // Run the asio loop with the main thread + server.run(9002); +} diff --git a/init.txt b/init.txt index d1c7e9e0f9..efc3c59a25 100644 --- a/init.txt +++ b/init.txt @@ -94,6 +94,14 @@ start_accept -#### connection api #### + +############################# +# Endpoint API (Thread Safe # +############################# + + +################## +# Connection API # +################## send message