Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
testee_server.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, Peter Thorson. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * * Neither the name of the WebSocket++ Project nor the
12 * names of its contributors may be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 */
27
31#include <iostream>
32
75
77
78using websocketpp::lib::placeholders::_1;
79using websocketpp::lib::placeholders::_2;
80using websocketpp::lib::bind;
81
82// pull out the type of messages sent by our config
84
85// Define a callback to handle incoming messages
87 s->send(hdl, msg->get_payload(), msg->get_opcode());
88}
89
90void on_socket_init(websocketpp::connection_hdl, boost::asio::ip::tcp::socket & s) {
91 boost::asio::ip::tcp::no_delay option(true);
92 s.set_option(option);
93}
94
95int main(int argc, char * argv[]) {
96 // Create a server endpoint
97 server testee_server;
98
99 short port = 9002;
100 size_t num_threads = 1;
101
102 if (argc == 3) {
103 port = atoi(argv[1]);
104 num_threads = atoi(argv[2]);
105 }
106
107 try {
108 // Total silence
111
112 // Initialize ASIO
113 testee_server.init_asio();
114 testee_server.set_reuse_addr(true);
115
116 // Register our message handler
117 testee_server.set_message_handler(bind(&on_message,&testee_server,::_1,::_2));
118 testee_server.set_socket_init_handler(bind(&on_socket_init,::_1,::_2));
119
120 // Listen on specified port with extended listen backlog
121 testee_server.set_listen_backlog(8192);
122 testee_server.listen(port);
123
124 // Start the server accept loop
125 testee_server.start_accept();
126
127 // Start the ASIO io_service run loop
128 if (num_threads == 1) {
129 testee_server.run();
130 } else {
131 typedef websocketpp::lib::shared_ptr<websocketpp::lib::thread> thread_ptr;
132 std::vector<thread_ptr> ts;
133 for (size_t i = 0; i < num_threads; i++) {
134 ts.push_back(websocketpp::lib::make_shared<websocketpp::lib::thread>(&server::run, &testee_server));
135 }
136
137 for (size_t i = 0; i < num_threads; i++) {
138 ts[i]->join();
139 }
140 }
141
142 } catch (websocketpp::exception const & e) {
143 std::cout << "exception: " << e.what() << std::endl;
144 }
145}
Concurrency policy that uses std::mutex / boost::mutex.
Definition basic.hpp:37
Stub for user supplied base class.
void set_message_handler(message_handler h)
Definition endpoint.hpp:322
void clear_access_channels(log::level channels)
Clear Access logging channels.
Definition endpoint.hpp:231
void clear_error_channels(log::level channels)
Clear Error logging channels.
Definition endpoint.hpp:253
virtual char const * what() const
Definition error.hpp:263
Stores, parses, and manipulates HTTP requests.
Definition request.hpp:50
Stores, parses, and manipulates HTTP responses.
Definition response.hpp:57
Basic logger that outputs to an ostream.
Definition basic.hpp:59
Represents a buffer for a single WebSocket message.
Definition pool.hpp:101
Thread safe stub "random" integer generator.
Definition none.hpp:46
Server endpoint role based on the given config.
void start_accept(lib::error_code &ec)
Starts the server's async connection acceptance loop (exception free)
Asio based endpoint transport component.
Definition endpoint.hpp:53
websocketpp::config::asio_tls_client::message_type::ptr message_ptr
char ** argv
uint32_t level
Type of a channel package.
Definition levels.hpp:37
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
permessage_compress extension
static bool const enable_multithreading
core::response_type response_type
core::concurrency_type concurrency_type
core::concurrency_type concurrency_type
core::message_type message_type
core::elog_type elog_type
static bool const enable_multithreading
websocketpp::extensions::permessage_deflate::enabled< permessage_deflate_config > permessage_deflate_type
core::con_msg_manager_type con_msg_manager_type
core::response_type response_type
core::alog_type alog_type
core::endpoint_msg_manager_type endpoint_msg_manager_type
websocketpp::transport::asio::endpoint< transport_config > transport_type
websocketpp::config::asio core
core::request_type request_type
static const websocketpp::log::level elog_level
core::endpoint_base endpoint_base
static const websocketpp::log::level alog_level
core::rng_type rng_type
Server config with asio transport and TLS disabled.
static level const none
Special aggregate value representing "no levels".
Definition levels.hpp:114
static level const all
Special aggregate value representing "all levels".
Definition levels.hpp:152
static level const none
Special aggregate value representing "no levels".
Definition levels.hpp:61
websocketpp::server< testee_config > server
void on_socket_init(websocketpp::connection_hdl, boost::asio::ip::tcp::socket &s)
void on_message(server *s, websocketpp::connection_hdl hdl, message_ptr msg)
yh_option option
Definition yubihsm.h:685
char * s