2 * Copyright (c) 2014, Peter Thorson. All rights reserved.
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.
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.
28 #ifndef WEBSOCKETPP_ENDPOINT_IMPL_HPP
29 #define WEBSOCKETPP_ENDPOINT_IMPL_HPP
33 namespace websocketpp {
35 template <typename connection, typename config>
36 typename endpoint<connection,config>::connection_ptr
37 endpoint<connection,config>::create_connection() {
38 m_alog.write(log::alevel::devel,"create_connection");
39 //scoped_lock_type lock(m_state_lock);
41 /*if (m_state == STOPPING || m_state == STOPPED) {
42 return connection_ptr();
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));
50 connection_weak_ptr w(con);
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)));
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);
70 if (m_open_handshake_timeout_dur != config::timeout_open_handshake) {
71 con->set_open_handshake_timeout(m_open_handshake_timeout_dur);
73 if (m_close_handshake_timeout_dur != config::timeout_close_handshake) {
74 con->set_close_handshake_timeout(m_close_handshake_timeout_dur);
76 if (m_pong_timeout_dur != config::timeout_pong) {
77 con->set_pong_timeout(m_pong_timeout_dur);
79 if (m_max_message_size != config::max_message_size) {
80 con->set_max_message_size(m_max_message_size);
82 con->set_max_http_body_size(m_max_http_body_size);
86 ec = transport_type::init(con);
88 m_elog.write(log::elevel::fatal,ec.message());
89 return connection_ptr();
95 template <typename connection, typename config>
96 void endpoint<connection,config>::interrupt(connection_hdl hdl, lib::error_code & ec)
98 connection_ptr con = get_con_from_hdl(hdl,ec);
101 m_alog.write(log::alevel::devel,"Interrupting connection");
103 ec = con->interrupt();
106 template <typename connection, typename config>
107 void endpoint<connection,config>::interrupt(connection_hdl hdl) {
110 if (ec) { throw exception(ec); }
113 template <typename connection, typename config>
114 void endpoint<connection,config>::pause_reading(connection_hdl hdl, lib::error_code & ec)
116 connection_ptr con = get_con_from_hdl(hdl,ec);
119 ec = con->pause_reading();
122 template <typename connection, typename config>
123 void endpoint<connection,config>::pause_reading(connection_hdl hdl) {
125 pause_reading(hdl,ec);
126 if (ec) { throw exception(ec); }
129 template <typename connection, typename config>
130 void endpoint<connection,config>::resume_reading(connection_hdl hdl, lib::error_code & ec)
132 connection_ptr con = get_con_from_hdl(hdl,ec);
135 ec = con->resume_reading();
138 template <typename connection, typename config>
139 void endpoint<connection,config>::resume_reading(connection_hdl hdl) {
141 resume_reading(hdl,ec);
142 if (ec) { throw exception(ec); }
145 template <typename connection, typename config>
146 void endpoint<connection,config>::send_http_response(connection_hdl hdl,
147 lib::error_code & ec)
149 connection_ptr con = get_con_from_hdl(hdl,ec);
151 con->send_http_response(ec);
154 template <typename connection, typename config>
155 void endpoint<connection,config>::send_http_response(connection_hdl hdl) {
157 send_http_response(hdl,ec);
158 if (ec) { throw exception(ec); }
161 template <typename connection, typename config>
162 void endpoint<connection,config>::send(connection_hdl hdl, std::string const & payload,
163 frame::opcode::value op, lib::error_code & ec)
165 connection_ptr con = get_con_from_hdl(hdl,ec);
168 ec = con->send(payload,op);
171 template <typename connection, typename config>
172 void endpoint<connection,config>::send(connection_hdl hdl, std::string const & payload,
173 frame::opcode::value op)
176 send(hdl,payload,op,ec);
177 if (ec) { throw exception(ec); }
180 template <typename connection, typename config>
181 void endpoint<connection,config>::send(connection_hdl hdl, void const * payload,
182 size_t len, frame::opcode::value op, lib::error_code & ec)
184 connection_ptr con = get_con_from_hdl(hdl,ec);
186 ec = con->send(payload,len,op);
189 template <typename connection, typename config>
190 void endpoint<connection,config>::send(connection_hdl hdl, void const * payload,
191 size_t len, frame::opcode::value op)
194 send(hdl,payload,len,op,ec);
195 if (ec) { throw exception(ec); }
198 template <typename connection, typename config>
199 void endpoint<connection,config>::send(connection_hdl hdl, message_ptr msg,
200 lib::error_code & ec)
202 connection_ptr con = get_con_from_hdl(hdl,ec);
207 template <typename connection, typename config>
208 void endpoint<connection,config>::send(connection_hdl hdl, message_ptr msg) {
211 if (ec) { throw exception(ec); }
214 template <typename connection, typename config>
215 void endpoint<connection,config>::close(connection_hdl hdl, close::status::value
216 const code, std::string const & reason,
217 lib::error_code & ec)
219 connection_ptr con = get_con_from_hdl(hdl,ec);
221 con->close(code,reason,ec);
224 template <typename connection, typename config>
225 void endpoint<connection,config>::close(connection_hdl hdl, close::status::value
226 const code, std::string const & reason)
229 close(hdl,code,reason,ec);
230 if (ec) { throw exception(ec); }
233 template <typename connection, typename config>
234 void endpoint<connection,config>::ping(connection_hdl hdl, std::string const &
235 payload, lib::error_code & ec)
237 connection_ptr con = get_con_from_hdl(hdl,ec);
239 con->ping(payload,ec);
242 template <typename connection, typename config>
243 void endpoint<connection,config>::ping(connection_hdl hdl, std::string const & payload)
246 ping(hdl,payload,ec);
247 if (ec) { throw exception(ec); }
250 template <typename connection, typename config>
251 void endpoint<connection,config>::pong(connection_hdl hdl, std::string const & payload,
252 lib::error_code & ec)
254 connection_ptr con = get_con_from_hdl(hdl,ec);
256 con->pong(payload,ec);
259 template <typename connection, typename config>
260 void endpoint<connection,config>::pong(connection_hdl hdl, std::string const & payload)
263 pong(hdl,payload,ec);
264 if (ec) { throw exception(ec); }
267 } // namespace websocketpp
269 #endif // WEBSOCKETPP_ENDPOINT_IMPL_HPP