websocket.hpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Copyright (c) 2020-2021 Paul-Louis Ageneau
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #ifndef RTC_IMPL_WEBSOCKET_H
  9. #define RTC_IMPL_WEBSOCKET_H
  10. #if RTC_ENABLE_WEBSOCKET
  11. #include "channel.hpp"
  12. #include "common.hpp"
  13. #include "httpproxytransport.hpp"
  14. #include "init.hpp"
  15. #include "message.hpp"
  16. #include "queue.hpp"
  17. #include "tcptransport.hpp"
  18. #include "tlstransport.hpp"
  19. #include "wstransport.hpp"
  20. #include "rtc/websocket.hpp"
  21. #include <atomic>
  22. #include <thread>
  23. namespace rtc::impl {
  24. struct WebSocket final : public Channel, public std::enable_shared_from_this<WebSocket> {
  25. using State = rtc::WebSocket::State;
  26. using Configuration = rtc::WebSocket::Configuration;
  27. WebSocket(optional<Configuration> optConfig = nullopt, certificate_ptr certificate = nullptr);
  28. ~WebSocket();
  29. void open(const string &url);
  30. void close();
  31. void remoteClose();
  32. bool outgoing(message_ptr message);
  33. void incoming(message_ptr message);
  34. optional<message_variant> receive() override;
  35. optional<message_variant> peek() override;
  36. size_t availableAmount() const override;
  37. bool isOpen() const;
  38. bool isClosed() const;
  39. size_t maxMessageSize() const;
  40. bool changeState(State state);
  41. shared_ptr<TcpTransport> setTcpTransport(shared_ptr<TcpTransport> transport);
  42. shared_ptr<HttpProxyTransport> initProxyTransport();
  43. shared_ptr<TlsTransport> initTlsTransport();
  44. shared_ptr<WsTransport> initWsTransport();
  45. shared_ptr<TcpTransport> getTcpTransport() const;
  46. shared_ptr<TlsTransport> getTlsTransport() const;
  47. shared_ptr<WsTransport> getWsTransport() const;
  48. shared_ptr<WsHandshake> getWsHandshake() const;
  49. void closeTransports();
  50. const Configuration config;
  51. std::atomic<State> state = State::Closed;
  52. private:
  53. static certificate_ptr loadCertificate(const Configuration& config);
  54. void scheduleConnectionTimeout();
  55. const init_token mInitToken = Init::Instance().token();
  56. certificate_ptr mCertificate;
  57. bool mIsSecure;
  58. optional<string> mHostname; // for TLS SNI and Proxy
  59. optional<string> mService; // for Proxy
  60. shared_ptr<TcpTransport> mTcpTransport;
  61. shared_ptr<HttpProxyTransport> mProxyTransport;
  62. shared_ptr<TlsTransport> mTlsTransport;
  63. shared_ptr<WsTransport> mWsTransport;
  64. shared_ptr<WsHandshake> mWsHandshake;
  65. Queue<message_ptr> mRecvQueue;
  66. };
  67. } // namespace rtc::impl
  68. #endif
  69. #endif // RTC_IMPL_WEBSOCKET_H