CppServer  1.0.4.0
C++ Server Library
wss_session.h
Go to the documentation of this file.
1 
9 #ifndef CPPSERVER_HTTP_WSS_SESSION_H
10 #define CPPSERVER_HTTP_WSS_SESSION_H
11 
13 #include "server/ws/ws.h"
14 
15 namespace CppServer {
16 namespace WS {
17 
18 class WSSServer;
19 
21 
26 class WSSSession : public HTTP::HTTPSSession, protected WebSocket
27 {
28  friend class WSSServer;
29 
30 public:
31  explicit WSSSession(const std::shared_ptr<WSSServer>& server);
32  WSSSession(const WSSSession&) = delete;
33  WSSSession(WSSSession&&) = delete;
34  virtual ~WSSSession() = default;
35 
36  WSSSession& operator=(const WSSSession&) = delete;
38 
39  // WebSocket connection methods
40  virtual bool Close() { return Close(0, nullptr, 0); }
41  virtual bool Close(int status) { return Close(status, nullptr, 0); }
42  virtual bool Close(int status, const void* buffer, size_t size) { SendCloseAsync(status, buffer, size); HTTPSSession::Disconnect(); return true; }
43  virtual bool Close(int status, std::string_view text) { SendCloseAsync(status, text); HTTPSSession::Disconnect(); return true; }
44 
45  // WebSocket send text methods
46  size_t SendText(const void* buffer, size_t size) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_TEXT, false, buffer, size); return HTTPSSession::Send(_ws_send_buffer.data(), _ws_send_buffer.size()); }
47  size_t SendText(std::string_view text) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_TEXT, false, text.data(), text.size()); return HTTPSSession::Send(_ws_send_buffer.data(), _ws_send_buffer.size()); }
48  size_t SendText(const void* buffer, size_t size, const CppCommon::Timespan& timeout) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_TEXT, false, buffer, size); return HTTPSSession::Send(_ws_send_buffer.data(), _ws_send_buffer.size(), timeout); }
49  size_t SendText(std::string_view text, const CppCommon::Timespan& timeout) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_TEXT, false, text.data(), text.size()); return HTTPSSession::Send(_ws_send_buffer.data(), _ws_send_buffer.size(), timeout); }
50  bool SendTextAsync(const void* buffer, size_t size) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_TEXT, false, buffer, size); return HTTPSSession::SendAsync(_ws_send_buffer.data(), _ws_send_buffer.size()); }
51  bool SendTextAsync(std::string_view text) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_TEXT, false, text.data(), text.size()); return HTTPSSession::SendAsync(_ws_send_buffer.data(), _ws_send_buffer.size()); }
52 
53  // WebSocket send binary methods
54  size_t SendBinary(const void* buffer, size_t size) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_BINARY, false, buffer, size); return HTTPSSession::Send(_ws_send_buffer.data(), _ws_send_buffer.size()); }
55  size_t SendBinary(std::string_view text) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_BINARY, false, text.data(), text.size()); return HTTPSSession::Send(_ws_send_buffer.data(), _ws_send_buffer.size()); }
56  size_t SendBinary(const void* buffer, size_t size, const CppCommon::Timespan& timeout) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_BINARY, false, buffer, size); return HTTPSSession::Send(_ws_send_buffer.data(), _ws_send_buffer.size(), timeout); }
57  size_t SendBinary(std::string_view text, const CppCommon::Timespan& timeout) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_BINARY, false, text.data(), text.size()); return HTTPSSession::Send(_ws_send_buffer.data(), _ws_send_buffer.size(), timeout); }
58  bool SendBinaryAsync(const void* buffer, size_t size) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_BINARY, false, buffer, size); return HTTPSSession::SendAsync(_ws_send_buffer.data(), _ws_send_buffer.size()); }
59  bool SendBinaryAsync(std::string_view text) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_BINARY, false, text.data(), text.size()); return HTTPSSession::SendAsync(_ws_send_buffer.data(), _ws_send_buffer.size()); }
60 
61  // WebSocket close methods
62  size_t SendClose(int status, const void* buffer, size_t size) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_CLOSE, false, buffer, size, status); return HTTPSSession::Send(_ws_send_buffer.data(), _ws_send_buffer.size()); }
63  size_t SendClose(int status, std::string_view text) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_CLOSE, false, text.data(), text.size(), status); return HTTPSSession::Send(_ws_send_buffer.data(), _ws_send_buffer.size()); }
64  size_t SendClose(int status, const void* buffer, size_t size, const CppCommon::Timespan& timeout) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_CLOSE, false, buffer, size, status); return HTTPSSession::Send(_ws_send_buffer.data(), _ws_send_buffer.size(), timeout); }
65  size_t SendClose(int status, std::string_view text, const CppCommon::Timespan& timeout) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_CLOSE, false, text.data(), text.size(), status); return HTTPSSession::Send(_ws_send_buffer.data(), _ws_send_buffer.size(), timeout); }
66  bool SendCloseAsync(int status, const void* buffer, size_t size) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_CLOSE, false, buffer, size, status); return HTTPSSession::SendAsync(_ws_send_buffer.data(), _ws_send_buffer.size()); }
67  bool SendCloseAsync(int status, std::string_view text) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_CLOSE, false, text.data(), text.size(), status); return HTTPSSession::SendAsync(_ws_send_buffer.data(), _ws_send_buffer.size()); }
68 
69  // WebSocket ping methods
70  size_t SendPing(const void* buffer, size_t size) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_PING, false, buffer, size); return HTTPSSession::Send(_ws_send_buffer.data(), _ws_send_buffer.size()); }
71  size_t SendPing(std::string_view text) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_PING, false, text.data(), text.size()); return HTTPSSession::Send(_ws_send_buffer.data(), _ws_send_buffer.size()); }
72  size_t SendPing(const void* buffer, size_t size, const CppCommon::Timespan& timeout) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_PING, false, buffer, size); return HTTPSSession::Send(_ws_send_buffer.data(), _ws_send_buffer.size(), timeout); }
73  size_t SendPing(std::string_view text, const CppCommon::Timespan& timeout) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_PING, false, text.data(), text.size()); return HTTPSSession::Send(_ws_send_buffer.data(), _ws_send_buffer.size(), timeout); }
74  bool SendPingAsync(const void* buffer, size_t size) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_PING, false, buffer, size); return HTTPSSession::SendAsync(_ws_send_buffer.data(), _ws_send_buffer.size()); }
75  bool SendPingAsync(std::string_view text) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_PING, false, text.data(), text.size()); return HTTPSSession::SendAsync(_ws_send_buffer.data(), _ws_send_buffer.size()); }
76 
77  // WebSocket pong methods
78  size_t SendPong(const void* buffer, size_t size) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_PONG, false, buffer, size); return HTTPSSession::Send(_ws_send_buffer.data(), _ws_send_buffer.size()); }
79  size_t SendPong(std::string_view text) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_PONG, false, text.data(), text.size()); return HTTPSSession::Send(_ws_send_buffer.data(), _ws_send_buffer.size()); }
80  size_t SendPong(const void* buffer, size_t size, const CppCommon::Timespan& timeout) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_PONG, false, buffer, size); return HTTPSSession::Send(_ws_send_buffer.data(), _ws_send_buffer.size(), timeout); }
81  size_t SendPong(std::string_view text, const CppCommon::Timespan& timeout) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_PONG, false, text.data(), text.size()); return HTTPSSession::Send(_ws_send_buffer.data(), _ws_send_buffer.size(), timeout); }
82  bool SendPongAsync(const void* buffer, size_t size) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_PONG, false, buffer, size); return HTTPSSession::SendAsync(_ws_send_buffer.data(), _ws_send_buffer.size()); }
83  bool SendPongAsync(std::string_view text) { std::scoped_lock locker(_ws_send_lock); PrepareSendFrame(WS_FIN | WS_PONG, false, text.data(), text.size()); return HTTPSSession::SendAsync(_ws_send_buffer.data(), _ws_send_buffer.size()); }
84 
85  // WebSocket receive methods
86  std::string ReceiveText();
87  std::string ReceiveText(const CppCommon::Timespan& timeout);
88  std::vector<uint8_t> ReceiveBinary();
89  std::vector<uint8_t> ReceiveBinary(const CppCommon::Timespan& timeout);
90 
91 protected:
92  void onDisconnected() override;
93  void onReceived(const void* buffer, size_t size) override;
94  void onReceivedRequestHeader(const HTTP::HTTPRequest& request) override;
95  void onReceivedRequest(const HTTP::HTTPRequest& request) override;
96  void onReceivedRequestError(const HTTP::HTTPRequest& request, const std::string& error) override;
97 
99  void onWSClose(const void* buffer, size_t size, int status = 1000) override { Close(); }
101  void onWSPing(const void* buffer, size_t size) override { SendPongAsync(buffer, size); }
103  void onWSError(const std::string& message) override { onError(asio::error::fault, "WebSocket error", message); }
104 
105 private:
106  // WebSocket send response
108 };
109 
110 } // namespace WS
111 } // namespace CppServer
112 
113 #endif // CPPSERVER_HTTP_WSS_SESSION_H
std::shared_ptr< SSLServer > & server() noexcept
Get the server.
Definition: ssl_session.h:48
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
Definition: ssl_session.h:238
size_t SendResponse()
Send the current HTTP response (synchronous)
Definition: https_session.h:52
HTTPResponse & response() noexcept
Get the HTTP response.
Definition: https_session.h:45
bool SendResponseAsync()
Send the current HTTP response (asynchronous)
WebSocket secure server.
Definition: wss_server.h:29
WebSocket secure session.
Definition: wss_session.h:27
size_t SendPing(std::string_view text, const CppCommon::Timespan &timeout)
Definition: wss_session.h:73
bool SendPongAsync(const void *buffer, size_t size)
Definition: wss_session.h:82
void onWSPing(const void *buffer, size_t size) override
Handle WebSocket ping notification.
Definition: wss_session.h:101
size_t SendPong(const void *buffer, size_t size)
Definition: wss_session.h:78
size_t SendText(const void *buffer, size_t size, const CppCommon::Timespan &timeout)
Definition: wss_session.h:48
size_t SendClose(int status, std::string_view text)
Definition: wss_session.h:63
bool SendPongAsync(std::string_view text)
Definition: wss_session.h:83
WSSSession & operator=(const WSSSession &)=delete
virtual bool Close(int status)
Definition: wss_session.h:41
size_t SendClose(int status, const void *buffer, size_t size, const CppCommon::Timespan &timeout)
Definition: wss_session.h:64
WSSSession(WSSSession &&)=delete
size_t SendBinary(std::string_view text)
Definition: wss_session.h:55
bool SendBinaryAsync(std::string_view text)
Definition: wss_session.h:59
bool SendTextAsync(std::string_view text)
Definition: wss_session.h:51
void onDisconnected() override
Handle session disconnected notification.
Definition: wss_session.cpp:20
bool SendBinaryAsync(const void *buffer, size_t size)
Definition: wss_session.h:58
size_t SendBinary(const void *buffer, size_t size)
Definition: wss_session.h:54
bool SendCloseAsync(int status, const void *buffer, size_t size)
Definition: wss_session.h:66
virtual bool Close(int status, std::string_view text)
Definition: wss_session.h:43
size_t SendPing(const void *buffer, size_t size)
Definition: wss_session.h:70
WSSSession(const WSSSession &)=delete
bool SendTextAsync(const void *buffer, size_t size)
Definition: wss_session.h:50
size_t SendPing(const void *buffer, size_t size, const CppCommon::Timespan &timeout)
Definition: wss_session.h:72
void onReceivedRequest(const HTTP::HTTPRequest &request) override
Handle HTTP request received notification.
Definition: wss_session.cpp:67
size_t SendClose(int status, std::string_view text, const CppCommon::Timespan &timeout)
Definition: wss_session.h:65
bool SendCloseAsync(int status, std::string_view text)
Definition: wss_session.h:67
WSSSession & operator=(WSSSession &&)=delete
size_t SendPong(std::string_view text, const CppCommon::Timespan &timeout)
Definition: wss_session.h:81
size_t SendClose(int status, const void *buffer, size_t size)
Definition: wss_session.h:62
void onReceived(const void *buffer, size_t size) override
Handle buffer received notification.
Definition: wss_session.cpp:40
size_t SendText(std::string_view text, const CppCommon::Timespan &timeout)
Definition: wss_session.h:49
std::vector< uint8_t > ReceiveBinary()
bool SendPingAsync(std::string_view text)
Definition: wss_session.h:75
virtual ~WSSSession()=default
virtual bool Close()
Definition: wss_session.h:40
bool SendPingAsync(const void *buffer, size_t size)
Definition: wss_session.h:74
size_t SendBinary(std::string_view text, const CppCommon::Timespan &timeout)
Definition: wss_session.h:57
void onReceivedRequestHeader(const HTTP::HTTPRequest &request) override
Handle HTTP request header received notification.
Definition: wss_session.cpp:53
void onWSClose(const void *buffer, size_t size, int status=1000) override
Handle WebSocket close notification.
Definition: wss_session.h:99
size_t SendBinary(const void *buffer, size_t size, const CppCommon::Timespan &timeout)
Definition: wss_session.h:56
size_t SendPing(std::string_view text)
Definition: wss_session.h:71
virtual bool Close(int status, const void *buffer, size_t size)
Definition: wss_session.h:42
size_t SendPong(const void *buffer, size_t size, const CppCommon::Timespan &timeout)
Definition: wss_session.h:80
void onReceivedRequestError(const HTTP::HTTPRequest &request, const std::string &error) override
Handle HTTP request error notification.
Definition: wss_session.cpp:81
WSSSession(const std::shared_ptr< WSSServer > &server)
Definition: wss_session.cpp:15
size_t SendPong(std::string_view text)
Definition: wss_session.h:79
void onWSError(const std::string &message) override
Handle WebSocket error notification.
Definition: wss_session.h:103
size_t SendText(const void *buffer, size_t size)
Definition: wss_session.h:46
size_t SendText(std::string_view text)
Definition: wss_session.h:47
WebSocket utility class.
Definition: ws.h:30
static const uint8_t WS_CLOSE
Close frame.
Definition: ws.h:39
void PrepareSendFrame(uint8_t opcode, bool mask, const void *buffer, size_t size, int status=0)
Prepare WebSocket send frame.
Definition: ws.cpp:212
static const uint8_t WS_TEXT
Text frame.
Definition: ws.h:35
static const uint8_t WS_BINARY
Binary frame.
Definition: ws.h:37
static const uint8_t WS_PING
Ping frame.
Definition: ws.h:41
static const uint8_t WS_FIN
Final frame.
Definition: ws.h:33
std::mutex _ws_send_lock
Send buffer lock.
Definition: ws.h:189
static const uint8_t WS_PONG
Pong frame.
Definition: ws.h:43
std::vector< uint8_t > _ws_send_buffer
Send buffer.
Definition: ws.h:191
HTTPS session definition.
C++ Server project definitions.
Definition: asio.h:56
WebSocket C++ Library definition.