CppServer 1.0.4.0
C++ Server Library
Loading...
Searching...
No Matches
ws_session.h
Go to the documentation of this file.
1
9#ifndef CPPSERVER_HTTP_WS_SESSION_H
10#define CPPSERVER_HTTP_WS_SESSION_H
11
13#include "server/ws/ws.h"
14
15namespace CppServer {
16namespace WS {
17
18class WSServer;
19
21
26class WSSession : public HTTP::HTTPSession, protected WebSocket
27{
28 friend class WSServer;
29
30public:
31 explicit WSSession(const std::shared_ptr<WSServer>& server);
32 WSSession(const WSSession&) = delete;
33 WSSession(WSSession&&) = delete;
34 virtual ~WSSession() = default;
35
36 WSSession& operator=(const WSSession&) = 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); HTTPSession::Disconnect(); return true; }
43 virtual bool Close(int status, std::string_view text) { SendCloseAsync(status, text); HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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 HTTPSession::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
91protected:
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
105private:
106 // WebSocket send response
108};
109
110} // namespace WS
111} // namespace CppServer
112
113#endif // CPPSERVER_HTTP_WS_SESSION_H
std::shared_ptr< TCPServer > & server() noexcept
Get the server.
Definition tcp_session.h:48
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
bool SendResponseAsync()
Send the current HTTP response (asynchronous)
HTTPResponse & response() noexcept
Get the HTTP response.
size_t SendResponse()
Send the current HTTP response (synchronous)
WebSocket server.
Definition ws_server.h:29
WebSocket session.
Definition ws_session.h:27
size_t SendPong(const void *buffer, size_t size)
Definition ws_session.h:78
size_t SendPing(std::string_view text)
Definition ws_session.h:71
size_t SendPing(const void *buffer, size_t size, const CppCommon::Timespan &timeout)
Definition ws_session.h:72
void onWSError(const std::string &message) override
Handle WebSocket error notification.
Definition ws_session.h:103
void onReceived(const void *buffer, size_t size) override
Handle buffer received notification.
std::vector< uint8_t > ReceiveBinary()
size_t SendClose(int status, const void *buffer, size_t size)
Definition ws_session.h:62
size_t SendBinary(const void *buffer, size_t size, const CppCommon::Timespan &timeout)
Definition ws_session.h:56
size_t SendPong(const void *buffer, size_t size, const CppCommon::Timespan &timeout)
Definition ws_session.h:80
void onReceivedRequest(const HTTP::HTTPRequest &request) override
Handle HTTP request received notification.
WSSession(const WSSession &)=delete
bool SendCloseAsync(int status, const void *buffer, size_t size)
Definition ws_session.h:66
size_t SendClose(int status, std::string_view text, const CppCommon::Timespan &timeout)
Definition ws_session.h:65
size_t SendPing(std::string_view text, const CppCommon::Timespan &timeout)
Definition ws_session.h:73
size_t SendText(std::string_view text, const CppCommon::Timespan &timeout)
Definition ws_session.h:49
virtual ~WSSession()=default
virtual bool Close(int status)
Definition ws_session.h:41
void onReceivedRequestHeader(const HTTP::HTTPRequest &request) override
Handle HTTP request header received notification.
virtual bool Close(int status, std::string_view text)
Definition ws_session.h:43
size_t SendText(std::string_view text)
Definition ws_session.h:47
bool SendTextAsync(const void *buffer, size_t size)
Definition ws_session.h:50
size_t SendBinary(std::string_view text, const CppCommon::Timespan &timeout)
Definition ws_session.h:57
virtual bool Close(int status, const void *buffer, size_t size)
Definition ws_session.h:42
void onWSPing(const void *buffer, size_t size) override
Handle WebSocket ping notification.
Definition ws_session.h:101
bool SendPongAsync(std::string_view text)
Definition ws_session.h:83
size_t SendText(const void *buffer, size_t size, const CppCommon::Timespan &timeout)
Definition ws_session.h:48
void onWSClose(const void *buffer, size_t size, int status=1000) override
Handle WebSocket close notification.
Definition ws_session.h:99
size_t SendBinary(const void *buffer, size_t size)
Definition ws_session.h:54
bool SendBinaryAsync(std::string_view text)
Definition ws_session.h:59
WSSession & operator=(const WSSession &)=delete
WSSession & operator=(WSSession &&)=delete
bool SendBinaryAsync(const void *buffer, size_t size)
Definition ws_session.h:58
size_t SendPong(std::string_view text)
Definition ws_session.h:79
size_t SendPong(std::string_view text, const CppCommon::Timespan &timeout)
Definition ws_session.h:81
void onReceivedRequestError(const HTTP::HTTPRequest &request, const std::string &error) override
Handle HTTP request error notification.
size_t SendClose(int status, const void *buffer, size_t size, const CppCommon::Timespan &timeout)
Definition ws_session.h:64
virtual bool Close()
Definition ws_session.h:40
bool SendPongAsync(const void *buffer, size_t size)
Definition ws_session.h:82
bool SendTextAsync(std::string_view text)
Definition ws_session.h:51
WSSession(WSSession &&)=delete
size_t SendText(const void *buffer, size_t size)
Definition ws_session.h:46
bool SendPingAsync(std::string_view text)
Definition ws_session.h:75
size_t SendBinary(std::string_view text)
Definition ws_session.h:55
size_t SendPing(const void *buffer, size_t size)
Definition ws_session.h:70
bool SendCloseAsync(int status, std::string_view text)
Definition ws_session.h:67
void onDisconnected() override
Handle session disconnected notification.
size_t SendClose(int status, std::string_view text)
Definition ws_session.h:63
bool SendPingAsync(const void *buffer, size_t size)
Definition ws_session.h:74
std::string ReceiveText()
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
HTTP session definition.
C++ Server project definitions.
Definition asio.h:56
WebSocket C++ Library definition.