/** * Copyright (c) 2020-2021 Paul-Louis Ageneau * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ #ifndef RTC_IMPL_WEBSOCKET_H #define RTC_IMPL_WEBSOCKET_H #if RTC_ENABLE_WEBSOCKET #include "channel.hpp" #include "common.hpp" #include "httpproxytransport.hpp" #include "init.hpp" #include "message.hpp" #include "queue.hpp" #include "tcptransport.hpp" #include "tlstransport.hpp" #include "wstransport.hpp" #include "rtc/websocket.hpp" #include #include namespace rtc::impl { struct WebSocket final : public Channel, public std::enable_shared_from_this { using State = rtc::WebSocket::State; using Configuration = rtc::WebSocket::Configuration; WebSocket(optional optConfig = nullopt, certificate_ptr certificate = nullptr); ~WebSocket(); void open(const string &url); void close(); void remoteClose(); bool outgoing(message_ptr message); void incoming(message_ptr message); optional receive() override; optional peek() override; size_t availableAmount() const override; bool isOpen() const; bool isClosed() const; size_t maxMessageSize() const; bool changeState(State state); shared_ptr setTcpTransport(shared_ptr transport); shared_ptr initProxyTransport(); shared_ptr initTlsTransport(); shared_ptr initWsTransport(); shared_ptr getTcpTransport() const; shared_ptr getTlsTransport() const; shared_ptr getWsTransport() const; shared_ptr getWsHandshake() const; void closeTransports(); const Configuration config; std::atomic state = State::Closed; private: static certificate_ptr loadCertificate(const Configuration& config); void scheduleConnectionTimeout(); const init_token mInitToken = Init::Instance().token(); certificate_ptr mCertificate; bool mIsSecure; optional mHostname; // for TLS SNI and Proxy optional mService; // for Proxy shared_ptr mTcpTransport; shared_ptr mProxyTransport; shared_ptr mTlsTransport; shared_ptr mWsTransport; shared_ptr mWsHandshake; Queue mRecvQueue; }; } // namespace rtc::impl #endif #endif // RTC_IMPL_WEBSOCKET_H