websocket.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * Copyright (c) 2020 Paul-Louis Ageneau
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef RTC_WEBSOCKET_H
  19. #define RTC_WEBSOCKET_H
  20. #if RTC_ENABLE_WEBSOCKET
  21. #include "channel.hpp"
  22. #include "include.hpp"
  23. #include "init.hpp"
  24. #include "message.hpp"
  25. #include "queue.hpp"
  26. #include <atomic>
  27. #include <optional>
  28. #include <thread>
  29. #include <variant>
  30. namespace rtc {
  31. class TcpTransport;
  32. class TlsTransport;
  33. class WsTransport;
  34. class WebSocket final : public Channel, public std::enable_shared_from_this<WebSocket> {
  35. public:
  36. enum class State : int {
  37. Connecting = 0,
  38. Open = 1,
  39. Closing = 2,
  40. Closed = 3,
  41. };
  42. struct Configuration {
  43. bool disableTlsVerification = false; // if true, don't verify the TLS certificate
  44. };
  45. WebSocket(std::optional<Configuration> config = nullopt);
  46. ~WebSocket();
  47. State readyState() const;
  48. void open(const string &url);
  49. void close() override;
  50. bool send(const message_variant data) override;
  51. bool isOpen() const override;
  52. bool isClosed() const override;
  53. size_t maxMessageSize() const override;
  54. // Extended API
  55. std::optional<message_variant> receive() override;
  56. std::optional<message_variant> peek() override;
  57. size_t availableAmount() const override; // total size available to receive
  58. private:
  59. bool changeState(State state);
  60. void remoteClose();
  61. bool outgoing(message_ptr message);
  62. void incoming(message_ptr message);
  63. std::shared_ptr<TcpTransport> initTcpTransport();
  64. std::shared_ptr<TlsTransport> initTlsTransport();
  65. std::shared_ptr<WsTransport> initWsTransport();
  66. void closeTransports();
  67. init_token mInitToken = Init::Token();
  68. std::shared_ptr<TcpTransport> mTcpTransport;
  69. std::shared_ptr<TlsTransport> mTlsTransport;
  70. std::shared_ptr<WsTransport> mWsTransport;
  71. std::recursive_mutex mInitMutex;
  72. const Configuration mConfig;
  73. string mScheme, mHost, mHostname, mService, mPath;
  74. std::atomic<State> mState = State::Closed;
  75. Queue<message_ptr> mRecvQueue;
  76. };
  77. } // namespace rtc
  78. #endif
  79. #endif // RTC_WEBSOCKET_H