SNode.C
Loading...
Searching...
No Matches
SubProtocolFactory.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_SUBPROTOCOLPLUGININTERFACE_H
21#define WEB_WEBSOCKET_SUBPROTOCOLPLUGININTERFACE_H
22
23namespace web::websocket {
24 template <typename SubProtocolT, typename RequestT, typename ResponseT>
27} // namespace web::websocket
28
29#ifndef DOXYGEN_SHOULD_SKIP_THIS
30
31#include <string> // IWYU pragma: export
32
33#endif /* DOXYGEN_SHOULD_SKIP_THIS */
34
35namespace web::websocket {
36
37 template <typename SubProtocolT>
38 class SubProtocolFactory {
39 public:
40 using SubProtocol = SubProtocolT;
41
42 SubProtocolFactory() = delete;
43
44 SubProtocolFactory(const std::string& name)
45 : subProtocolName(name) {
46 }
47
48 SubProtocolFactory(SubProtocolFactory&) = delete;
49 SubProtocolFactory(SubProtocolFactory&&) = delete;
50
51 SubProtocolFactory& operator=(SubProtocolFactory&) = delete;
52 SubProtocolFactory& operator=(SubProtocolFactory&&) = delete;
53
54 virtual ~SubProtocolFactory() = default;
55
56 SubProtocol* createSubProtocol(SubProtocolContext* subProtocolContext) {
57 SubProtocol* subProtocol = create(subProtocolContext);
58
59 if (subProtocol != nullptr) {
60 refCount++;
61 }
62
63 return subProtocol;
64 }
65
66 private:
67 virtual SubProtocol* create(SubProtocolContext* subProtocolContext) = 0;
68
69 public:
70 virtual std::size_t deleteSubProtocol(SubProtocol* subProtocol) {
71 if (subProtocol != nullptr) {
72 delete subProtocol;
73
74 refCount--;
75 }
76
77 return refCount;
78 }
79
80 const std::string& getName() {
81 return subProtocolName;
82 }
83
84 void setHandle(void* handle) {
85 this->handle = handle;
86 }
87
88 void* getHandle() {
89 return handle;
90 }
91
92 private:
93 std::size_t refCount = 0;
94
95 std::string subProtocolName;
96
97 void* handle = nullptr;
98 };
99
100} // namespace web::websocket
101
102#endif // WEB_WEBSOCKET_SUBPROTOCOLPLUGININTERFACE_H
Echo * create(web::websocket::SubProtocolContext *subProtocolContext) override
friend class web::websocket::SocketContextUpgrade
Definition SubProtocol.h:57
apps::websocket::subprotocol::echo::client::EchoFactory * echoClientSubProtocolFactory()
#define NAME