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) << getSocketConnection()->getConnectionName() << " WebSocket: MaxFlyingPings exceeded - closing";
73
75 stop();
76 }
77 },
78 pingInterval);
79 }
81 getSocketConnection()->setTimeout(0);
82 }
83
84 template <typename SocketContextUpgrade>
85 SubProtocol<SocketContextUpgrade>::~SubProtocol() {
87 }
89 template <typename SocketContextUpgrade>
90 void SubProtocol<SocketContextUpgrade>::sendMessage(const char* message, std::size_t messageLength) const {
92 }
94 template <typename SocketContextUpgrade>
95 void SubProtocol<SocketContextUpgrade>::sendMessage(const std::string& message) const {
96 subProtocolContext->sendMessage(1, message.data(), message.length());
97 }
98
99 template <typename SocketContextUpgrade>
100 void SubProtocol<SocketContextUpgrade>::sendMessageStart(const char* message, std::size_t messageLength) const {
101 subProtocolContext->sendMessageStart(2, message, messageLength);
102 }
103
104 template <typename SocketContextUpgrade>
105 void SubProtocol<SocketContextUpgrade>::sendMessageStart(const std::string& message) const {
106 subProtocolContext->sendMessageStart(1, message.data(), message.length());
107 }
109 template <typename SocketContextUpgrade>
110 void SubProtocol<SocketContextUpgrade>::sendMessageFrame(const char* message, std::size_t messageLength) const {
113
114 template <typename SocketContextUpgrade>
115 void SubProtocol<SocketContextUpgrade>::sendMessageFrame(const std::string& message) const {
116 sendMessageFrame(message.data(), message.length());
117 }
118
119 template <typename SocketContextUpgrade>
120 void SubProtocol<SocketContextUpgrade>::sendMessageEnd(const char* message, std::size_t messageLength) const {
121 subProtocolContext->sendMessageEnd(message, messageLength);
122 }
123
124 template <typename SocketContextUpgrade>
125 void SubProtocol<SocketContextUpgrade>::sendMessageEnd(const std::string& message) const {
126 sendMessageEnd(message.data(), message.length());
127 }
128
129 template <typename SocketContextUpgrade>
130 void SubProtocol<SocketContextUpgrade>::sendPing(const char* reason, std::size_t reasonLength) const {
131 LOG(DEBUG) << getSocketConnection()->getConnectionName() << " WebSocket: Ping sent";
132
133 subProtocolContext->sendPing(reason, reasonLength);
134 }
135
136 template <typename SocketContextUpgrade>
137 void SubProtocol<SocketContextUpgrade>::sendClose(uint16_t statusCode, const char* reason, std::size_t reasonLength) {
138 subProtocolContext->sendClose(statusCode, reason, reasonLength);
139 }
140
141 template <typename SocketContextUpgrade>
142 void SubProtocol<SocketContextUpgrade>::onPongReceived() {
143 LOG(DEBUG) << subProtocolContext->getSocketConnection()->getConnectionName() << " WebSocket: Pong received";
144
145 flyingPings = 0;
146 }
147
148 template <typename SocketContextUpgrade>
149 const std::string& SubProtocol<SocketContextUpgrade>::getName() {
150 return name;
151 }
152
153 template <typename SocketContextUpgrade>
154 core::socket::stream::SocketConnection* SubProtocol<SocketContextUpgrade>::getSocketConnection() const {
156 }
157
158} // 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 void sendMessageFrame(const char *message, std::size_t messageLength)=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()=0
void sendMessage(const char *message, std::size_t messageLength) const
void sendMessageStart(const std::string &message) 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
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
void sendMessageEnd(const std::string &message) const
core::timer::Timer pingTimer
Definition Timer.h:59