CppServer  1.0.0.0
C++ Server Library
websocket_ssl_client.h
Go to the documentation of this file.
1 
9 #ifndef CPPSERVER_ASIO_WEBSOCKET_SSL_CLIENT_H
10 #define CPPSERVER_ASIO_WEBSOCKET_SSL_CLIENT_H
11 
12 #include "service.h"
13 #include "websocket.h"
14 
15 #include "system/uuid.h"
16 
17 namespace CppServer {
18 namespace Asio {
19 
21 
26 class WebSocketSSLClient : public std::enable_shared_from_this<WebSocketSSLClient>
27 {
28 public:
30 
35  explicit WebSocketSSLClient(std::shared_ptr<Service> service, std::shared_ptr<asio::ssl::context> context, const std::string& uri);
36  WebSocketSSLClient(const WebSocketSSLClient&) = delete;
38  virtual ~WebSocketSSLClient() = default;
39 
42 
44  const CppCommon::UUID& id() const noexcept { return _id; }
45 
47  std::shared_ptr<Service>& service() noexcept { return _service; }
49  std::shared_ptr<asio::ssl::context>& context() noexcept;
51  const std::string& uri() const noexcept { return _uri; }
53  WebSocketSSLClientCore& core() noexcept { return _core; }
54 
56  uint64_t messages_sent() const noexcept { return _messages_sent; }
58  uint64_t messages_received() const noexcept { return _messages_received; }
60  uint64_t bytes_sent() const noexcept { return _bytes_sent; }
62  uint64_t bytes_received() const noexcept { return _bytes_received; }
63 
65  bool IsConnected() const noexcept { return _connected; }
66 
68 
71  bool Connect();
73 
78  bool Disconnect(websocketpp::close::status::value code = websocketpp::close::status::normal, const std::string& reason = "") { return Disconnect(false, code, reason); }
80 
83  bool Reconnect();
84 
86 
92  size_t Send(const void* buffer, size_t size, websocketpp::frame::opcode::value opcode = websocketpp::frame::opcode::binary);
94 
99  size_t Send(const std::string& text, websocketpp::frame::opcode::value opcode = websocketpp::frame::opcode::text);
101 
105  size_t Send(const WebSocketSSLMessage& message);
106 
107 protected:
109  virtual void onConnected() {}
111  virtual void onDisconnected() {}
112 
114 
117  virtual void onReceived(const WebSocketSSLMessage& message) {}
118 
120 
125  virtual void onError(int error, const std::string& category, const std::string& message) {}
126 
127 private:
128  // Client Id
129  CppCommon::UUID _id;
130  // Asio service
131  std::shared_ptr<Service> _service;
132  // Server SSL context, URI address, client core and client connection
133  std::shared_ptr<asio::ssl::context> _context;
134  std::string _uri;
136  websocketpp::connection_hdl _connection;
137  std::atomic<bool> _initialized;
138  std::atomic<bool> _connected;
139  // Client statistic
140  uint64_t _messages_sent;
141  uint64_t _messages_received;
142  uint64_t _bytes_sent;
143  uint64_t _bytes_received;
144 
146  void InitAsio();
147 
149 
155  bool Disconnect(bool dispatch, websocketpp::close::status::value code = websocketpp::close::status::normal, const std::string& reason = "");
156 
158  void Connected(websocketpp::connection_hdl connection);
160  void Disconnected(websocketpp::connection_hdl connection);
161 
163  void SendError(std::error_code ec);
164 };
165 
168 } // namespace Asio
169 } // namespace CppServer
170 
171 #endif // CPPSERVER_ASIO_WEBSOCKET_SSL_CLIENT_H
virtual void onConnected()
Handle client connected notification.
WebSocketSSLClient(std::shared_ptr< Service > service, std::shared_ptr< asio::ssl::context > context, const std::string &uri)
Initialize WebSocket client with a given Asio service, SSL context and server URI address...
std::shared_ptr< asio::ssl::context > & context() noexcept
Get the client SSL context.
bool Disconnect(websocketpp::close::status::value code=websocketpp::close::status::normal, const std::string &reason="")
Disconnect the client.
uint64_t messages_sent() const noexcept
Get the number messages sent by this client.
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
WebSocketSSLClientCore & core() noexcept
Get the WebSocket client core.
C++ Server project definitions.
Definition: asio.h:24
WebSocketSSLConnection::message_ptr WebSocketSSLMessage
WebSocket SSL message.
Definition: websocket.h:40
virtual void onReceived(const WebSocketSSLMessage &message)
Handle message received notification.
WebSocketSSLClient & operator=(const WebSocketSSLClient &)=delete
const std::string & uri() const noexcept
Get the WebSocket URI address.
uint64_t messages_received() const noexcept
Get the number messages received by this client.
size_t Send(const void *buffer, size_t size, websocketpp::frame::opcode::value opcode=websocketpp::frame::opcode::binary)
Send data to the server.
bool Reconnect()
Reconnect the client.
uint64_t bytes_sent() const noexcept
Get the number of bytes sent by this client.
Asio service definition.
uint64_t bytes_received() const noexcept
Get the number of bytes received by this client.
virtual void onDisconnected()
Handle client disconnected notification.
bool IsConnected() const noexcept
Is the client connected?
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
const CppCommon::UUID & id() const noexcept
Get the client Id.
WebSocket C++ Library definition.
websocketpp::client< websocketpp::config::asio_tls > WebSocketSSLClientCore
WebSocket SSL client core.
Definition: websocket.h:34