SNode.C
Loading...
Searching...
No Matches
SubProtocolFactorySelector.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_SUBPROTOCOLSELECTOR_H
21#define WEB_WEBSOCKET_SUBPROTOCOLSELECTOR_H
22
23#ifndef DOXYGEN_SHOULD_SKIP_THIS
24
25#include "core/DynamicLoader.h"
26
27#include <map>
28#include <string>
29
30#endif /* DOXYGEN_SHOULD_SKIP_THIS */
31
32namespace web::websocket {
33
34 template <typename SubProtocolFactoryT>
35 class SubProtocolFactorySelector {
36 public:
37 SubProtocolFactorySelector(const SubProtocolFactorySelector&) = delete;
38 SubProtocolFactorySelector& operator=(const SubProtocolFactorySelector&) = delete;
39
40 using SubProtocolFactory = SubProtocolFactoryT;
41
42 enum class Role { SERVER, CLIENT };
43
44 protected:
45 SubProtocolFactorySelector() = default;
46 virtual ~SubProtocolFactorySelector() = default;
47
48 virtual SubProtocolFactory* load(const std::string& subProtocolName) = 0;
49 static SubProtocolFactory* load(const std::string& subProtocolName,
50 const std::string& subProtocolLibraryFile,
51 const std::string& subProtocolFactoryFunctionName);
52
53 public:
54 SubProtocolFactory* select(const std::string& subProtocolName);
55 SubProtocolFactory* select(const std::string& subProtocolName, Role role);
56
57 template <typename SubProtocolFactory>
58 void unload(SubProtocolFactory* subProtocolFactory) {
59 std::string name = subProtocolFactory->getName();
60
61 if (subProtocolFactories.contains(name)) {
62 subProtocolFactories.erase(name);
63
64 void* handle = subProtocolFactory->getHandle();
65 delete subProtocolFactory;
66
67 if (handle != nullptr) {
68 core::DynamicLoader::dlCloseDelayed(handle);
69 }
70 }
71 }
72
73 protected:
74 void allowDlOpen();
75
76 template <typename SubProtocolFactory>
77 void link(const std::string& subProtocolName, SubProtocolFactory* (*subProtocolFactory)()) {
78 onlyLinked = true;
79 linkedSubProtocolFactories[subProtocolName] =
80 reinterpret_cast<SubProtocolFactorySelector::SubProtocolFactory* (*) ()>(subProtocolFactory);
81 }
82
83 private:
84 std::map<std::string, SubProtocolFactory*> subProtocolFactories;
85 std::map<std::string, SubProtocolFactory* (*) ()> linkedSubProtocolFactories;
86
87 bool onlyLinked = false;
88 };
89
90} // namespace web::websocket
91
92#endif // WEB_WEBSOCKET_SUBPROTOCOLSELECTOR_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
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
SubProtocolContext & operator=(const SubProtocolContext &)=delete
virtual void sendPong(const char *reason=nullptr, std::size_t reasonLength=0)=0
virtual void sendClose(const char *message, std::size_t messageLength)=0
SubProtocolContext(core::socket::stream::SocketConnection *socketConnection, bool role)
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
SubProtocolContext(const SubProtocolContext &)=delete
void sendMessageEnd(const char *message, std::size_t messageLength)
void sendFrameData(const char *frame, uint64_t frameLength) const
Transmitter & operator=(const Transmitter &)=delete
std::random_device randomDevice
Definition Transmitter.h:68
void sendFrameData(uint8_t data) const
void sendFrameData(uint16_t data) const
void sendMessage(uint8_t opCode, const char *message, std::size_t messageLength)
void sendFrameData(uint32_t data) const
Transmitter(core::socket::stream::SocketConnection *socketConnection, bool masking)
core::socket::stream::SocketConnection * socketConnection
Definition Transmitter.h:71
void send(bool end, uint8_t opCode, const char *message, std::size_t messageLength)
void sendFrame(bool fin, uint8_t opCode, const char *payload, uint64_t payloadLength)
void sendFrameData(uint64_t data) const
std::uniform_int_distribution< uint32_t > distribution
Definition Transmitter.h:69
Transmitter(const Transmitter &)=delete
void sendMessageStart(uint8_t opCode, const char *message, std::size_t messageLength)
void sendMessageFrame(const char *message, std::size_t messageLength)
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)
SubProtocolFactory * load(const std::string &subProtocolName) override
SubProtocolFactorySelector(const SubProtocolFactorySelector &)=delete
SubProtocolFactorySelector & operator=(const SubProtocolFactorySelector &)=delete
static void link(const std::string &subProtocolName, SubProtocolFactory *(*getSubProtocolFactory)())
#define CLOSE_SOCKET_TIMEOUT