Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
endpoint_impl.hpp
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 */
27
28#ifndef WEBSOCKETPP_ENDPOINT_IMPL_HPP
29#define WEBSOCKETPP_ENDPOINT_IMPL_HPP
30
31#include <string>
32
33namespace websocketpp {
34
35template <typename connection, typename config>
38 m_alog.write(log::alevel::devel,"create_connection");
39 //scoped_lock_type lock(m_state_lock);
40
41 /*if (m_state == STOPPING || m_state == STOPPED) {
42 return connection_ptr();
43 }*/
44
45 //scoped_lock_type guard(m_mutex);
46 // Create a connection on the heap and manage it using a shared pointer
47 connection_ptr con = lib::make_shared<connection_type>(m_is_server,
48 m_user_agent, lib::ref(m_alog), lib::ref(m_elog), lib::ref(m_rng));
49
51
52 // Create a weak pointer on the heap using that shared_ptr.
53 // Cast that weak pointer to void* and manage it using another shared_ptr
54 // connection_hdl hdl(reinterpret_cast<void*>(new connection_weak_ptr(con)));
55
56 con->set_handle(w);
57
58 // Copy default handlers from the endpoint
59 con->set_open_handler(m_open_handler);
60 con->set_close_handler(m_close_handler);
61 con->set_fail_handler(m_fail_handler);
62 con->set_ping_handler(m_ping_handler);
63 con->set_pong_handler(m_pong_handler);
64 con->set_pong_timeout_handler(m_pong_timeout_handler);
65 con->set_interrupt_handler(m_interrupt_handler);
66 con->set_http_handler(m_http_handler);
67 con->set_validate_handler(m_validate_handler);
68 con->set_message_handler(m_message_handler);
69
70 if (m_open_handshake_timeout_dur != config::timeout_open_handshake) {
71 con->set_open_handshake_timeout(m_open_handshake_timeout_dur);
72 }
73 if (m_close_handshake_timeout_dur != config::timeout_close_handshake) {
74 con->set_close_handshake_timeout(m_close_handshake_timeout_dur);
75 }
76 if (m_pong_timeout_dur != config::timeout_pong) {
77 con->set_pong_timeout(m_pong_timeout_dur);
78 }
79 if (m_max_message_size != config::max_message_size) {
80 con->set_max_message_size(m_max_message_size);
81 }
82 con->set_max_http_body_size(m_max_http_body_size);
83
84 lib::error_code ec;
85
86 ec = transport_type::init(con);
87 if (ec) {
88 m_elog.write(log::elevel::fatal,ec.message());
89 return connection_ptr();
90 }
91
92 return con;
93}
94
95template <typename connection, typename config>
97{
98 connection_ptr con = get_con_from_hdl(hdl,ec);
99 if (ec) {return;}
100
101 m_alog.write(log::alevel::devel,"Interrupting connection");
102
103 ec = con->interrupt();
104}
105
106template <typename connection, typename config>
108 lib::error_code ec;
109 interrupt(hdl,ec);
110 if (ec) { throw exception(ec); }
111}
112
113template <typename connection, typename config>
115{
116 connection_ptr con = get_con_from_hdl(hdl,ec);
117 if (ec) {return;}
118
119 ec = con->pause_reading();
120}
121
122template <typename connection, typename config>
124 lib::error_code ec;
125 pause_reading(hdl,ec);
126 if (ec) { throw exception(ec); }
127}
128
129template <typename connection, typename config>
131{
132 connection_ptr con = get_con_from_hdl(hdl,ec);
133 if (ec) {return;}
134
135 ec = con->resume_reading();
136}
137
138template <typename connection, typename config>
140 lib::error_code ec;
141 resume_reading(hdl,ec);
142 if (ec) { throw exception(ec); }
143}
144
145template <typename connection, typename config>
147 lib::error_code & ec)
148{
149 connection_ptr con = get_con_from_hdl(hdl,ec);
150 if (ec) {return;}
151 con->send_http_response(ec);
152}
153
154template <typename connection, typename config>
156 lib::error_code ec;
157 send_http_response(hdl,ec);
158 if (ec) { throw exception(ec); }
159}
160
161template <typename connection, typename config>
162void endpoint<connection,config>::send(connection_hdl hdl, std::string const & payload,
163 frame::opcode::value op, lib::error_code & ec)
164{
165 connection_ptr con = get_con_from_hdl(hdl,ec);
166 if (ec) {return;}
167
168 ec = con->send(payload,op);
169}
170
171template <typename connection, typename config>
172void endpoint<connection,config>::send(connection_hdl hdl, std::string const & payload,
174{
175 lib::error_code ec;
176 send(hdl,payload,op,ec);
177 if (ec) { throw exception(ec); }
178}
179
180template <typename connection, typename config>
182 size_t len, frame::opcode::value op, lib::error_code & ec)
183{
184 connection_ptr con = get_con_from_hdl(hdl,ec);
185 if (ec) {return;}
186 ec = con->send(payload,len,op);
187}
188
189template <typename connection, typename config>
192{
193 lib::error_code ec;
194 send(hdl,payload,len,op,ec);
195 if (ec) { throw exception(ec); }
196}
197
198template <typename connection, typename config>
200 lib::error_code & ec)
201{
202 connection_ptr con = get_con_from_hdl(hdl,ec);
203 if (ec) {return;}
204 ec = con->send(msg);
205}
206
207template <typename connection, typename config>
209 lib::error_code ec;
210 send(hdl,msg,ec);
211 if (ec) { throw exception(ec); }
212}
213
214template <typename connection, typename config>
216 const code, std::string const & reason,
217 lib::error_code & ec)
218{
219 connection_ptr con = get_con_from_hdl(hdl,ec);
220 if (ec) {return;}
221 con->close(code,reason,ec);
222}
223
224template <typename connection, typename config>
226 const code, std::string const & reason)
227{
228 lib::error_code ec;
229 close(hdl,code,reason,ec);
230 if (ec) { throw exception(ec); }
231}
232
233template <typename connection, typename config>
235 payload, lib::error_code & ec)
236{
237 connection_ptr con = get_con_from_hdl(hdl,ec);
238 if (ec) {return;}
239 con->ping(payload,ec);
240}
241
242template <typename connection, typename config>
243void endpoint<connection,config>::ping(connection_hdl hdl, std::string const & payload)
244{
245 lib::error_code ec;
246 ping(hdl,payload,ec);
247 if (ec) { throw exception(ec); }
248}
249
250template <typename connection, typename config>
251void endpoint<connection,config>::pong(connection_hdl hdl, std::string const & payload,
252 lib::error_code & ec)
253{
254 connection_ptr con = get_con_from_hdl(hdl,ec);
255 if (ec) {return;}
256 con->pong(payload,ec);
257}
258
259template <typename connection, typename config>
260void endpoint<connection,config>::pong(connection_hdl hdl, std::string const & payload)
261{
262 lib::error_code ec;
263 pong(hdl,payload,ec);
264 if (ec) { throw exception(ec); }
265}
266
267} // namespace websocketpp
268
269#endif // WEBSOCKETPP_ENDPOINT_IMPL_HPP
void ping(connection_hdl hdl, std::string const &payload, lib::error_code &ec)
Send a ping to a specific connection.
void send(connection_hdl hdl, std::string const &payload, frame::opcode::value op, lib::error_code &ec)
Create a message and add it to the outgoing send queue (exception free)
void pong(connection_hdl hdl, std::string const &payload, lib::error_code &ec)
Send a pong to a specific connection.
connection_type::weak_ptr connection_weak_ptr
Weak pointer to connection type.
Definition endpoint.hpp:58
connection_type::message_ptr message_ptr
Type of message pointers that this endpoint uses.
Definition endpoint.hpp:70
void resume_reading(connection_hdl hdl, lib::error_code &ec)
Resume reading of new data (exception free)
void close(connection_hdl hdl, close::status::value const code, std::string const &reason, lib::error_code &ec)
connection_type::ptr connection_ptr
Shared pointer to connection_type.
Definition endpoint.hpp:56
void interrupt(connection_hdl hdl, lib::error_code &ec)
connection_ptr create_connection()
void send_http_response(connection_hdl hdl, lib::error_code &ec)
Send deferred HTTP Response.
void pause_reading(connection_hdl hdl, lib::error_code &ec)
Pause reading of new data (exception free)
client::connection_ptr connection_ptr
uint16_t value
The type of a close code value.
Definition close.hpp:49
Namespace for the WebSocket++ project.
Definition base64.hpp:41
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
static level const devel
Development messages (warning: very chatty)
Definition levels.hpp:141
static level const fatal
Definition levels.hpp:78
size_t len