SNode.C
Loading...
Searching...
No Matches
SubProtocol.hpp
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/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#include "core/socket/stream/SocketConnection.h"
43#include "web/websocket/SubProtocol.h"
44#include "web/websocket/SubProtocolContext.h" // IWYU pragma: export
45
46#ifndef DOXYGEN_SHOULD_SKIP_THIS
47
48#include "log/Logger.h"
49
50#include <cstddef>
51#include <cstdint>
52#include <string>
53
54#endif /* DOXYGEN_SHOULD_SKIP_THIS */
55
56namespace web::websocket {
57
58 template <typename SocketContextUpgrade>
59 SubProtocol<SocketContextUpgrade>::SubProtocol(SubProtocolContext* subProtocolContext,
60 const std::string& name,
61 int pingInterval,
62 int maxFlyingPings)
63 : name(name)
64 , subProtocolContext(subProtocolContext) {
65 if (pingInterval > 0) {
66 pingTimer = core::timer::Timer::intervalTimer(
67 [this, maxFlyingPings](const std::function<void()>& stop) {
68 if (flyingPings < maxFlyingPings) {
71 } else {
72 LOG(WARNING) << this->subProtocolContext->getSocketConnection()->getConnectionName() << " Subprotocol '"
73 << this->name << "': MaxFlyingPings exceeded - closing";
74
76 stop();
77 }
78 },
79 pingInterval);
80 }
82 getSocketConnection()->setTimeout(0);
83 }
84
85 template <typename SocketContextUpgrade>
86 SubProtocol<SocketContextUpgrade>::~SubProtocol() {
88 }
90 template <typename SocketContextUpgrade>
91 void SubProtocol<SocketContextUpgrade>::sendMessage(const char* message, std::size_t messageLength) const {
93 }
95 template <typename SocketContextUpgrade>
96 void SubProtocol<SocketContextUpgrade>::sendMessage(const std::string& message) const {
97 subProtocolContext->sendMessage(1, message.data(), message.length());
98 }
100 template <typename SocketContextUpgrade>
101 void SubProtocol<SocketContextUpgrade>::sendMessageStart(const char* message, std::size_t messageLength) const {
102 subProtocolContext->sendMessageStart(2, message, messageLength);
103 }
104
105 template <typename SocketContextUpgrade>
106 void SubProtocol<SocketContextUpgrade>::sendMessageStart(const std::string& message) const {
107 subProtocolContext->sendMessageStart(1, message.data(), message.length());
108 }
109
110 template <typename SocketContextUpgrade>
111 void SubProtocol<SocketContextUpgrade>::sendMessageFrame(const char* message, std::size_t messageLength) const {
112 subProtocolContext->sendMessageFrame(message, messageLength);
115 template <typename SocketContextUpgrade>
116 void SubProtocol<SocketContextUpgrade>::sendMessageFrame(const std::string& message) const {
117 sendMessageFrame(message.data(), message.length());
118 }
120 template <typename SocketContextUpgrade>
121 void SubProtocol<SocketContextUpgrade>::sendMessageEnd(const char* message, std::size_t messageLength) const {
122 subProtocolContext->sendMessageEnd(message, messageLength);
123 }
124
125 template <typename SocketContextUpgrade>
126 void SubProtocol<SocketContextUpgrade>::sendMessageEnd(const std::string& message) const {
127 sendMessageEnd(message.data(), message.length());
128 }
129
130 template <typename SocketContextUpgrade>
131 void SubProtocol<SocketContextUpgrade>::sendPing(const char* reason, std::size_t reasonLength) const {
132 LOG(DEBUG) << subProtocolContext->getSocketConnection()->getConnectionName() << " Subprotocol '" << name << "': Ping sent";
133
134 subProtocolContext->sendPing(reason, reasonLength);
135 }
136
137 template <typename SocketContextUpgrade>
138 void SubProtocol<SocketContextUpgrade>::sendClose(uint16_t statusCode, const char* reason, std::size_t reasonLength) {
139 subProtocolContext->sendClose(statusCode, reason, reasonLength);
140 }
141
142 template <typename SocketContextUpgrade>
143 void SubProtocol<SocketContextUpgrade>::attach() {
144 LOG(DEBUG) << subProtocolContext->getSocketConnection()->getConnectionName() << " Subprotocol '" << name << "': start";
145
147 }
148
149 template <typename SocketContextUpgrade>
150 void SubProtocol<SocketContextUpgrade>::detach() {
152
153 LOG(DEBUG) << subProtocolContext->getSocketConnection()->getConnectionName() << " Subprotocol '" << name << "': stopped";
154
155 LOG(DEBUG) << " Total Payload sent: " << getPayloadTotalSent();
156 LOG(DEBUG) << " Total Payload processed: " << getPayloadTotalRead();
157 }
158
159 template <typename SocketContextUpgrade>
160 void SubProtocol<SocketContextUpgrade>::onPongReceived() {
161 LOG(DEBUG) << subProtocolContext->getSocketConnection()->getConnectionName() << " Subprotocol '" << name << "': Pong received";
162
163 flyingPings = 0;
164 }
165
166 template <typename SocketContextUpgrade>
167 const std::string& SubProtocol<SocketContextUpgrade>::getName() {
168 return name;
169 }
170
171 template <typename SocketContextUpgrade>
172 core::socket::stream::SocketConnection* SubProtocol<SocketContextUpgrade>::getSocketConnection() const {
174 }
175
176 template <typename SocketContextUpgrade>
177 std::size_t SubProtocol<SocketContextUpgrade>::getPayloadTotalSent() const {
179 }
180
181 template <typename SocketContextUpgrade>
182 std::size_t SubProtocol<SocketContextUpgrade>::getPayloadTotalRead() const {
184 }
185
186 template <typename SocketContextUpgrade>
187 std::string SubProtocol<SocketContextUpgrade>::getOnlineSince() const {
189 }
190
191 template <typename SocketContextUpgrade>
192 std::string SubProtocol<SocketContextUpgrade>::getOnlineDuration() const {
194 }
195
196} // namespace web::websocket
void cancel()
Definition Timer.cpp:84
const std::string & getConnectionName() const
virtual void sendMessage(uint8_t opCode, const char *message, std::size_t messageLength)=0
virtual void sendPing(const char *reason=nullptr, std::size_t reasonLength=0)=0
virtual std::size_t getPayloadTotalSent() const =0
virtual void sendMessageFrame(const char *message, std::size_t messageLength)=0
virtual std::string getOnlineSince() const =0
virtual std::size_t getPayloadTotalRead() const =0
virtual void sendMessageEnd(const char *message, std::size_t messageLength)=0
virtual void sendClose(uint16_t statusCode=1000, const char *reason=nullptr, std::size_t reasonLength=0)=0
virtual void sendMessageStart(uint8_t opCode, const char *message, std::size_t messageLength)=0
virtual core::socket::stream::SocketConnection * getSocketConnection() const =0
virtual std::string getOnlineDuration() const =0
void sendMessage(const char *message, std::size_t messageLength) const
void sendMessageStart(const std::string &message) const
std::size_t getPayloadTotalSent() const
void sendMessageEnd(const char *message, std::size_t messageLength) const
core::socket::stream::SocketConnection * getSocketConnection() const
SubProtocol(SubProtocolContext *subProtocolContext, const std::string &name, int pingInterval=60, int maxFlyingPings=3)
void sendPing(const char *reason=nullptr, std::size_t reasonLength=0) const
std::string getOnlineSince() const
virtual void onDisconnected()=0
std::size_t getPayloadTotalRead() const
void sendMessage(const std::string &message) const
SubProtocolContext * subProtocolContext
void sendMessageFrame(const std::string &message) const
const std::string & getName()
void sendMessageStart(const char *message, std::size_t messageLength) const
void sendClose(uint16_t statusCode=1000, const char *reason=nullptr, std::size_t reasonLength=0)
void sendMessageFrame(const char *message, std::size_t messageLength) const
virtual void onConnected()=0
void sendMessageEnd(const std::string &message) const
std::string getOnlineDuration() const
core::timer::Timer pingTimer
Definition Timer.h:59