websocket.hpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. optional<size_t> maxMessageSize;
  38. };
  39. WebSocket();
  40. WebSocket(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. using CheshireCat<impl::WebSocket>::impl;
  56. };
  57. } // namespace rtc
  58. #endif
  59. #endif // RTC_WEBSOCKET_H