2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
20#ifndef WEB_WEBSOCKET_SUBPROTOCOLSELECTOR_H
21#define WEB_WEBSOCKET_SUBPROTOCOLSELECTOR_H
23#ifndef DOXYGEN_SHOULD_SKIP_THIS
25#include "core/DynamicLoader.h"
32namespace web::websocket {
34 template <
typename SubProtocolFactoryT>
35 class SubProtocolFactorySelector {
37 SubProtocolFactorySelector(
const SubProtocolFactorySelector&) =
delete;
38 SubProtocolFactorySelector& operator=(
const SubProtocolFactorySelector&) =
delete;
40 using SubProtocolFactory = SubProtocolFactoryT;
42 enum class Role { SERVER, CLIENT };
45 SubProtocolFactorySelector() =
default;
46 virtual ~SubProtocolFactorySelector() =
default;
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);
54 SubProtocolFactory* select(
const std::string& subProtocolName);
55 SubProtocolFactory* select(
const std::string& subProtocolName, Role role);
57 template <
typename SubProtocolFactory>
58 void unload(SubProtocolFactory* subProtocolFactory) {
59 std::string name = subProtocolFactory->getName();
61 if (subProtocolFactories.contains(name)) {
62 subProtocolFactories.erase(name);
64 void* handle = subProtocolFactory->getHandle();
65 delete subProtocolFactory;
67 if (handle !=
nullptr) {
68 core::DynamicLoader::dlCloseDelayed(handle);
76 template <
typename SubProtocolFactory>
77 void link(
const std::string& subProtocolName, SubProtocolFactory* (*subProtocolFactory)()) {
79 linkedSubProtocolFactories[subProtocolName] =
80 reinterpret_cast<SubProtocolFactorySelector::SubProtocolFactory* (*) ()>(subProtocolFactory);
84 std::map<std::string, SubProtocolFactory*> subProtocolFactories;
85 std::map<std::string, SubProtocolFactory* (*) ()> linkedSubProtocolFactories;
87 bool onlyLinked =
false;
void sendClose(uint16_t statusCode=1000, const char *reason=nullptr, std::size_t reasonLength=0) override
bool onSignal(int sig) override
void onDisconnected() 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
std::string pongCloseData
SocketContextUpgrade & operator=(const SocketContextUpgrade &)=delete
void sendMessage(uint8_t opCode, const char *message, std::size_t messageLength) override
SubProtocol * subProtocol
void onMessageEnd() 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()=delete
std::size_t onReceivedFromPeer() 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
void onMessageStart(int opCode) override
void onConnected() 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
~SubProtocolContext() override
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
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
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
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)
~SocketContextUpgrade() 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)
static void allowDlOpen()
SubProtocolFactory * load(const std::string &subProtocolName) override
SubProtocolFactorySelector(const SubProtocolFactorySelector &)=delete
SubProtocolFactorySelector()=default
SubProtocolFactorySelector & operator=(const SubProtocolFactorySelector &)=delete
static void link(const std::string &subProtocolName, SubProtocolFactory *(*getSubProtocolFactory)())
static SubProtocolFactorySelector * instance()
~SubProtocolFactorySelector() override
#define CLOSE_SOCKET_TIMEOUT