fixed adding file problem
[c11concurrency-benchmarks.git] / gdax-orderbook-hpp / demo / dependencies / websocketpp-0.7.0 / examples / testee_server / testee_server.cpp
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
28 #include <websocketpp/config/asio_no_tls.hpp>
29 #include <websocketpp/server.hpp>
30 #include <websocketpp/extensions/permessage_deflate/enabled.hpp>
31 #include <iostream>
32
33 struct testee_config : public websocketpp::config::asio {
34     // pull default settings from our core config
35     typedef websocketpp::config::asio core;
36
37     typedef core::concurrency_type concurrency_type;
38     typedef core::request_type request_type;
39     typedef core::response_type response_type;
40     typedef core::message_type message_type;
41     typedef core::con_msg_manager_type con_msg_manager_type;
42     typedef core::endpoint_msg_manager_type endpoint_msg_manager_type;
43
44     typedef core::alog_type alog_type;
45     typedef core::elog_type elog_type;
46     typedef core::rng_type rng_type;
47     typedef core::endpoint_base endpoint_base;
48
49     static bool const enable_multithreading = true;
50
51     struct transport_config : public core::transport_config {
52         typedef core::concurrency_type concurrency_type;
53         typedef core::elog_type elog_type;
54         typedef core::alog_type alog_type;
55         typedef core::request_type request_type;
56         typedef core::response_type response_type;
57
58         static bool const enable_multithreading = true;
59     };
60
61     typedef websocketpp::transport::asio::endpoint<transport_config>
62         transport_type;
63
64     static const websocketpp::log::level elog_level =
65         websocketpp::log::elevel::none;
66     static const websocketpp::log::level alog_level =
67         websocketpp::log::alevel::none;
68         
69     /// permessage_compress extension
70     struct permessage_deflate_config {};
71
72     typedef websocketpp::extensions::permessage_deflate::enabled
73         <permessage_deflate_config> permessage_deflate_type;
74 };
75
76 typedef websocketpp::server<testee_config> server;
77
78 using websocketpp::lib::placeholders::_1;
79 using websocketpp::lib::placeholders::_2;
80 using websocketpp::lib::bind;
81
82 // pull out the type of messages sent by our config
83 typedef server::message_ptr message_ptr;
84
85 // Define a callback to handle incoming messages
86 void on_message(server* s, websocketpp::connection_hdl hdl, message_ptr msg) {
87     s->send(hdl, msg->get_payload(), msg->get_opcode());
88 }
89
90 void 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
95 int 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
109         testee_server.clear_access_channels(websocketpp::log::alevel::all);
110         testee_server.clear_error_channels(websocketpp::log::alevel::all);
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 }