#ifndef HUB_UWS_H #define HUB_UWS_H #include "Group.h" #include "Node.h" #include #include #include #include namespace uWS { struct WIN32_EXPORT Hub : protected uS::Node, public Group, public Group { protected: struct ConnectionData { std::string path; void *user; Group *group; }; static z_stream *allocateDefaultCompressor(z_stream *zStream); z_stream inflationStream = {}, deflationStream = {}; char *deflate(char *data, size_t &length, z_stream *slidingDeflateWindow); char *inflate(char *data, size_t &length, size_t maxPayload); char *zlibBuffer; std::string dynamicZlibBuffer; static const int LARGE_BUFFER_SIZE = 300 * 1024; static void onServerAccept(uS::Socket *s); static void onClientConnection(uS::Socket *s, bool error); public: template Group *createGroup(int extensionOptions = 0, unsigned int maxPayload = 16777216) { return new Group(extensionOptions, maxPayload, this, nodeData); } template Group &getDefaultGroup() { return static_cast &>(*this); } bool listen(int port, uS::TLS::Context sslContext = nullptr, int options = 0, Group *eh = nullptr); bool listen(const char *host, int port, uS::TLS::Context sslContext = nullptr, int options = 0, Group *eh = nullptr); void connect(std::string uri, void *user = nullptr, std::map extraHeaders = {}, int timeoutMs = 5000, Group *eh = nullptr); void upgrade(uv_os_sock_t fd, const char *secKey, SSL *ssl, const char *extensions, size_t extensionsLength, const char *subprotocol, size_t subprotocolLength, Group *serverGroup = nullptr); Hub(int extensionOptions = 0, bool useDefaultLoop = false, unsigned int maxPayload = 16777216) : uS::Node(LARGE_BUFFER_SIZE, WebSocketProtocol>::CONSUME_PRE_PADDING, WebSocketProtocol>::CONSUME_POST_PADDING, useDefaultLoop), Group(extensionOptions, maxPayload, this, nodeData), Group(0, maxPayload, this, nodeData) { inflateInit2(&inflationStream, -15); zlibBuffer = new char[LARGE_BUFFER_SIZE]; allocateDefaultCompressor(&deflationStream); #ifdef UWS_THREADSAFE getLoop()->preCbData = nodeData; getLoop()->preCb = [](void *nodeData) { static_cast(nodeData)->asyncMutex->lock(); }; getLoop()->postCbData = nodeData; getLoop()->postCb = [](void *nodeData) { static_cast(nodeData)->asyncMutex->unlock(); }; #endif } ~Hub() { inflateEnd(&inflationStream); deflateEnd(&deflationStream); delete [] zlibBuffer; } using uS::Node::run; using uS::Node::poll; using uS::Node::getLoop; using Group::onConnection; using Group::onConnection; using Group::onTransfer; using Group::onTransfer; using Group::onMessage; using Group::onMessage; using Group::onDisconnection; using Group::onDisconnection; using Group::onPing; using Group::onPing; using Group::onPong; using Group::onPong; using Group::onError; using Group::onError; using Group::onHttpRequest; using Group::onHttpData; using Group::onHttpConnection; using Group::onHttpDisconnection; using Group::onHttpUpgrade; using Group::onCancelledHttpRequest; friend struct WebSocket; friend struct WebSocket; }; } #endif // HUB_UWS_H