websocket.hpp 1.9 KB

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