SNode.C
Loading...
Searching...
No Matches
SocketContextUpgrade.h
Go to the documentation of this file.
1/*
2 * SNode.C - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef WEB_WEBSOCKET_SOCKETCONTEXT_H
21#define WEB_WEBSOCKET_SOCKETCONTEXT_H
22
23#include "web/http/SocketContextUpgrade.h" // IWYU pragma: export
24#include "web/websocket/SubProtocolContext.h"
25
26namespace core::socket {
27 class SocketConnection;
28} // namespace core::socket
29
30namespace web::http {
31 template <typename RequestT, typename ResponseT>
33} // namespace web::http
34
35namespace web::websocket {
36 template <typename SubProtocolT>
37 class SubProtocolFactory;
38}
39
40#ifndef DOXYGEN_SHOULD_SKIP_THIS
41
42#include "log/Logger.h"
43
44#include <cstdint>
45#include <endian.h>
46#include <iomanip>
47#include <string>
48
49#endif /* DOXYGEN_SHOULD_SKIP_THIS */
50
51#define CLOSE_SOCKET_TIMEOUT 10
52
53namespace web::websocket {
54
55 template <typename SubProtocolT, typename RequestT, typename ResponseT>
58 , public SubProtocolContext {
59 public:
63
64 private:
66
67 public:
68 using SubProtocol = SubProtocolT;
69 using Request = RequestT;
70 using Response = ResponseT;
71
72 private:
73 using Super::readFromPeer;
74 using Super::sendToPeer;
75 using Super::setTimeout;
76 using Super::shutdownWrite;
77
78 protected:
79 enum class Role { SERVER, CLIENT };
80
81 SocketContextUpgrade(core::socket::stream::SocketConnection* socketConnection,
82 web::http::SocketContextUpgradeFactory<Request, Response>* socketContextUpgradeFactory,
83 Role role);
84
85 public:
87
88 private:
89 void sendMessage(uint8_t opCode, const char* message, std::size_t messageLength) override;
90 void sendMessageStart(uint8_t opCode, const char* message, std::size_t messageLength) override;
91 void sendMessageFrame(const char* message, std::size_t messageLength) override;
92 void sendMessageEnd(const char* message, std::size_t messageLength) override;
93 void sendPing(const char* reason = nullptr, std::size_t reasonLength = 0) override;
94 void sendPong(const char* reason = nullptr, std::size_t reasonLength = 0) override;
95 void sendClose(uint16_t statusCode = 1000, const char* reason = nullptr, std::size_t reasonLength = 0) override;
96 void sendClose(const char* message, std::size_t messageLength) override;
97
99
100 /* WSReceiver */
101 void onMessageStart(int opCode) override;
102 void onMessageData(const char* chunk, uint64_t chunkLen) override;
103 void onMessageEnd() override;
104 void onMessageError(uint16_t errnum) override;
105
106 /* Callbacks (API) socketConnection -> WSProtocol */
107 void onConnected() override;
109
110 bool onSignal(int sig) override;
111
112 /* SocketContext */
113 std::size_t onReceivedFromPeer() override;
114
115 protected:
116 SubProtocol* subProtocol = nullptr;
117
118 private:
120
122 };
123
124} // namespace web::websocket
125
126#endif // WEB_WEBSOCKET_SOCKETCONTEXT_H
void sendClose(uint16_t statusCode=1000, const char *reason=nullptr, std::size_t reasonLength=0) override
void sendPing(const char *reason=nullptr, std::size_t reasonLength=0) override
~SocketContextUpgrade() override=default
void onMessageData(const char *chunk, uint64_t chunkLen) override
SocketContextUpgrade(const SocketContextUpgrade &)=delete
SocketContextUpgrade & operator=(const SocketContextUpgrade &)=delete
void sendMessage(uint8_t opCode, const char *message, std::size_t messageLength) override
void sendClose(const char *message, std::size_t messageLength) override
void sendMessageFrame(const char *message, std::size_t messageLength) override
void sendMessageEnd(const char *message, std::size_t messageLength) override
SocketContextUpgrade(core::socket::stream::SocketConnection *socketConnection, web::http::SocketContextUpgradeFactory< Request, Response > *socketContextUpgradeFactory, Role role)
void sendPong(const char *reason=nullptr, std::size_t reasonLength=0) override
void sendMessageStart(uint8_t opCode, const char *message, std::size_t messageLength) override
core::socket::stream::SocketConnection * getSocketConnection() override
void onMessageError(uint16_t errnum) override
web::websocket::SubProtocolFactory< SubProtocol > * subProtocolFactory
std::string loadSubProtocol(const std::string &subProtocolName)
SocketContextUpgrade(core::socket::stream::SocketConnection *socketConnection, web::http::SocketContextUpgradeFactory< web::http::client::Request, web::http::client::Response > *socketContextUpgradeFactory)