CppServer  1.0.0.0
C++ Server Library
websocket_session.inl
Go to the documentation of this file.
1 
9 namespace CppServer {
10 namespace Asio {
11 
12 template <class TServer, class TSession>
14  : _id(CppCommon::UUID::Generate()),
15  _server(server),
16  _connected(false),
17  _messages_sent(0),
18  _messages_received(0),
19  _bytes_sent(0),
20  _bytes_received(0)
21 {
22 }
23 
24 template <class TServer, class TSession>
25 inline void WebSocketSession<TServer, TSession>::Connect(websocketpp::connection_hdl connection)
26 {
27  // Setup WebSocket session handlers
28  WebSocketServerCore::connection_ptr con = _server->core().get_con_from_hdl(connection);
29  con->set_message_handler([this](websocketpp::connection_hdl connection, WebSocketMessage message)
30  {
31  size_t size = message->get_raw_payload().size();
32 
33  // Update statistic
34  ++_messages_received;
35  ++_server->_messages_received;
36  _bytes_received += size;
37  _server->_bytes_received += size;
38 
39  // Call the message received handler
40  onReceived(message);
41  });
42  con->set_fail_handler([this](websocketpp::connection_hdl connection)
43  {
44  WebSocketServerCore::connection_ptr con = _server->core().get_con_from_hdl(connection);
45  websocketpp::lib::error_code ec = con->get_ec();
46  SendError(ec);
47  Disconnected();
48  });
49 
50  // Assign new WebSocket connection
51  _connection = connection;
52 
53  // Reset statistic
54  _messages_sent = 0;
55  _messages_received = 0;
56  _bytes_sent = 0;
57  _bytes_received = 0;
58 
59  // Update the connected flag
60  _connected = true;
61 
62  // Call the session connected handler
63  onConnected();
64 }
65 
66 template <class TServer, class TSession>
67 inline bool WebSocketSession<TServer, TSession>::Disconnect(bool dispatch, websocketpp::close::status::value code, const std::string& reason)
68 {
69  if (!IsConnected())
70  return false;
71 
72  auto self(this->shared_from_this());
73  auto disconnect = [this, self, code, reason]()
74  {
75  // Close the session connection
76  websocketpp::lib::error_code ec;
77  _server->core().close(_connection, code, reason, ec);
78  if (ec)
79  SendError(ec);
80  };
81 
82  // Dispatch or post the disconnect routine
83  if (dispatch)
84  service()->Dispatch(disconnect);
85  else
86  service()->Post(disconnect);
87 
88  return true;
89 }
90 
91 template <class TServer, class TSession>
92 inline void WebSocketSession<TServer, TSession>::Disconnected()
93 {
94  // Update the connected flag
95  _connected = false;
96 
97  // Call the session disconnected handler
98  onDisconnected();
99 
100  // Unregister the session
101  _server->UnregisterSession(id());
102 }
103 
104 template <class TServer, class TSession>
105 inline size_t WebSocketSession<TServer, TSession>::Send(const void* buffer, size_t size, websocketpp::frame::opcode::value opcode)
106 {
107  assert((buffer != nullptr) && "Pointer to the buffer should not be equal to 'nullptr'!");
108  assert((size > 0) && "Buffer size should be greater than zero!");
109  if ((buffer == nullptr) || (size == 0))
110  return 0;
111 
112  if (!IsConnected())
113  return 0;
114 
115  websocketpp::lib::error_code ec;
116  _server->core().send(_connection, buffer, size, opcode, ec);
117  if (ec)
118  {
119  SendError(ec);
120  return 0;
121  }
122 
123  // Update statistic
124  ++_messages_sent;
125  ++_server->_messages_sent;
126  _bytes_sent += size;
127  _server->_bytes_sent += size;
128 
129  return size;
130 }
131 
132 template <class TServer, class TSession>
133 inline size_t WebSocketSession<TServer, TSession>::Send(const std::string& text, websocketpp::frame::opcode::value opcode)
134 {
135  if (!IsConnected())
136  return 0;
137 
138  websocketpp::lib::error_code ec;
139  _server->core().send(_connection, text, opcode, ec);
140  if (ec)
141  {
142  SendError(ec);
143  return 0;
144  }
145 
146  size_t size = text.size();
147 
148  // Update statistic
149  ++_messages_sent;
150  ++_server->_messages_sent;
151  _bytes_sent += size;
152  _server->_bytes_sent += size;
153 
154  return size;
155 }
156 
157 template <class TServer, class TSession>
159 {
160  if (!IsConnected())
161  return 0;
162 
163  websocketpp::lib::error_code ec;
164  _server->core().send(_connection, message, ec);
165  if (ec)
166  {
167  SendError(ec);
168  return 0;
169  }
170 
171  size_t size = message->get_raw_payload().size();
172 
173  // Update statistic
174  ++_messages_sent;
175  ++_server->_messages_sent;
176  _bytes_sent += size;
177  _server->_bytes_sent += size;
178 
179  return size;
180 }
181 
182 template <class TServer, class TSession>
183 inline void WebSocketSession<TServer, TSession>::SendError(std::error_code ec)
184 {
185  onError(ec.value(), ec.category().name(), ec.message());
186 }
187 
188 } // namespace Asio
189 } // namespace CppServer
WebSocketSession(std::shared_ptr< WebSocketServer< TServer, TSession >> server)
Initialize the session with a given server.
C++ Server project definitions.
Definition: asio.h:24
size_t Send(const void *buffer, size_t size, websocketpp::frame::opcode::value opcode=websocketpp::frame::opcode::binary)
Send data into the session.
bool Disconnect(websocketpp::close::status::value code=websocketpp::close::status::normal, const std::string &reason="")
Disconnect the session.
WebSocketConnection::message_ptr WebSocketMessage
WebSocket message.
Definition: websocket.h:31