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