websocket.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_WEBSOCKET_H
  9. #define RTC_WEBSOCKET_H
  10. #if RTC_ENABLE_WEBSOCKET
  11. #include "channel.hpp"
  12. #include "common.hpp"
  13. #include "configuration.hpp" // for ProxyServer
  14. namespace rtc {
  15. namespace impl {
  16. struct WebSocket;
  17. }
  18. class RTC_CPP_EXPORT WebSocket final : private CheshireCat<impl::WebSocket>, public Channel {
  19. public:
  20. enum class State : int {
  21. Connecting = 0,
  22. Open = 1,
  23. Closing = 2,
  24. Closed = 3,
  25. };
  26. struct Configuration {
  27. bool disableTlsVerification = false; // if true, don't verify the TLS certificate
  28. optional<ProxyServer> proxyServer; // only non-authenticated http supported for now
  29. std::vector<string> protocols;
  30. optional<std::chrono::milliseconds> connectionTimeout; // zero to disable
  31. optional<std::chrono::milliseconds> pingInterval; // zero to disable
  32. optional<int> maxOutstandingPings;
  33. optional<string> caCertificatePemFile;
  34. optional<string> certificatePemFile;
  35. optional<string> keyPemFile;
  36. optional<string> keyPemPass;
  37. };
  38. WebSocket();
  39. WebSocket(Configuration config);
  40. WebSocket(impl_ptr<impl::WebSocket> impl);
  41. ~WebSocket() override;
  42. State readyState() const;
  43. bool isOpen() const override;
  44. bool isClosed() const override;
  45. size_t maxMessageSize() const override;
  46. void open(const string &url);
  47. void close() override;
  48. void forceClose();
  49. bool send(const message_variant data) override;
  50. bool send(const byte *data, size_t size) override;
  51. optional<string> remoteAddress() const;
  52. optional<string> path() const;
  53. private:
  54. using CheshireCat<impl::WebSocket>::impl;
  55. };
  56. } // namespace rtc
  57. #endif
  58. #endif // RTC_WEBSOCKET_H