Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
debug_server.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014, 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
33
34// Custom logger
36
38
39#include <iostream>
40
44
47 typedef debug_asio base;
48
49 typedef base::concurrency_type concurrency_type;
50
51 typedef base::request_type request_type;
52 typedef base::response_type response_type;
53
54 typedef base::message_type message_type;
55 typedef base::con_msg_manager_type con_msg_manager_type;
56 typedef base::endpoint_msg_manager_type endpoint_msg_manager_type;
57
59 /*typedef websocketpp::log::syslog<concurrency_type,
60 websocketpp::log::elevel> elog_type;
61 typedef websocketpp::log::syslog<concurrency_type,
62 websocketpp::log::alevel> alog_type;
63 */
64 typedef base::alog_type alog_type;
65 typedef base::elog_type elog_type;
66
67 typedef base::rng_type rng_type;
68
78
81
82 static const long timeout_open_handshake = 0;
83};
84
86
88
89using websocketpp::lib::placeholders::_1;
90using websocketpp::lib::placeholders::_2;
91using websocketpp::lib::bind;
92
93// pull out the type of messages sent by our config
95
97 //sleep(6);
98 return true;
99}
100
103
104 std::string res = con->get_request_body();
105
106 std::stringstream ss;
107 ss << "got HTTP request with " << res.size() << " bytes of body data.";
108
109 con->set_body(ss.str());
110 con->set_status(websocketpp::http::status_code::ok);
111}
112
114 server::connection_ptr con = s->get_con_from_hdl(hdl);
115
116 std::cout << "Fail handler: " << con->get_ec() << " " << con->get_ec().message() << std::endl;
117}
118
120 std::cout << "Close handler" << std::endl;
121}
122
123// Define a callback to handle incoming messages
125 std::cout << "on_message called with hdl: " << hdl.lock().get()
126 << " and message: " << msg->get_payload()
127 << std::endl;
128
129 try {
130 s->send(hdl, msg->get_payload(), msg->get_opcode());
131 } catch (const websocketpp::lib::error_code& e) {
132 std::cout << "Echo failed because: " << e
133 << "(" << e.message() << ")" << std::endl;
134 }
135}
136
137int main() {
138 // Create a server endpoint
139 server echo_server;
140
141 try {
142 // Set logging settings
145
146 // Initialize ASIO
147 echo_server.init_asio();
148 echo_server.set_reuse_addr(true);
149
150 // Register our message handler
151 echo_server.set_message_handler(bind(&on_message,&echo_server,::_1,::_2));
152
153 echo_server.set_http_handler(bind(&on_http,&echo_server,::_1));
154 echo_server.set_fail_handler(bind(&on_fail,&echo_server,::_1));
155 echo_server.set_close_handler(&on_close);
156
157 echo_server.set_validate_handler(bind(&validate,&echo_server,::_1));
158
159 // Listen on port 9012
160 echo_server.listen(9012);
161
162 // Start the server accept loop
163 echo_server.start_accept();
164
165 // Start the ASIO io_service run loop
166 echo_server.run();
167 } catch (const std::exception & e) {
168 std::cout << e.what() << std::endl;
169 } catch (websocketpp::lib::error_code e) {
170 std::cout << e.message() << std::endl;
171 } catch (...) {
172 std::cout << "other exception" << std::endl;
173 }
174}
void set_fail_handler(fail_handler h)
Definition endpoint.hpp:287
connection_ptr get_con_from_hdl(connection_hdl hdl, lib::error_code &ec)
Retrieves a connection_ptr from a connection_hdl (exception free)
Definition endpoint.hpp:643
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 set_http_handler(http_handler h)
Definition endpoint.hpp:312
void set_access_channels(log::level channels)
Set Access logging channel.
Definition endpoint.hpp:220
void set_close_handler(close_handler h)
Definition endpoint.hpp:282
void set_validate_handler(validate_handler h)
Definition endpoint.hpp:317
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)
Basic ASIO endpoint socket component.
Definition none.hpp:314
Asio based endpoint transport component.
Definition endpoint.hpp:53
websocketpp::config::asio_tls_client::message_type::ptr message_ptr
websocketpp::server< debug_custom > server
server::message_ptr message_ptr
void on_fail(server *s, websocketpp::connection_hdl hdl)
bool validate(server *, websocketpp::connection_hdl)
void on_message(server *s, websocketpp::connection_hdl hdl, message_ptr msg)
void on_close(websocketpp::connection_hdl)
int main()
void on_http(server *s, websocketpp::connection_hdl hdl)
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
websocketpp::transport::asio::basic_socket::endpoint socket_type
type::concurrency_type concurrency_type
type::request_type request_type
type::response_type response_type
static const long timeout_open_handshake
base::concurrency_type concurrency_type
base::elog_type elog_type
base::response_type response_type
base::con_msg_manager_type con_msg_manager_type
base::alog_type alog_type
Custom Logging policies.
base::message_type message_type
websocketpp::transport::asio::endpoint< transport_config > transport_type
debug_asio base
base::request_type request_type
base::endpoint_msg_manager_type endpoint_msg_manager_type
debug_custom type
base::rng_type rng_type
Client/Server debug config with asio transport and TLS disabled.
static level const all
Special aggregate value representing "all levels".
Definition levels.hpp:152
static level const frame_payload
One line per frame, includes the full message payload (warning: chatty)
Definition levels.hpp:129
char * s