websocket.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. WebSocket();
  43. ~WebSocket();
  44. State readyState() const;
  45. void open(const string &url);
  46. void close() override;
  47. bool send(const std::variant<binary, string> &data) override;
  48. bool isOpen() const override;
  49. bool isClosed() const override;
  50. size_t maxMessageSize() const override;
  51. // Extended API
  52. std::optional<std::variant<binary, string>> receive() override;
  53. size_t availableAmount() const override; // total size available to receive
  54. private:
  55. bool changeState(State state);
  56. void remoteClose();
  57. bool outgoing(message_ptr message);
  58. void incoming(message_ptr message);
  59. std::shared_ptr<TcpTransport> initTcpTransport();
  60. std::shared_ptr<TlsTransport> initTlsTransport();
  61. std::shared_ptr<WsTransport> initWsTransport();
  62. void closeTransports();
  63. init_token mInitToken = Init::Token();
  64. std::shared_ptr<TcpTransport> mTcpTransport;
  65. std::shared_ptr<TlsTransport> mTlsTransport;
  66. std::shared_ptr<WsTransport> mWsTransport;
  67. std::recursive_mutex mInitMutex;
  68. string mScheme, mHost, mHostname, mService, mPath;
  69. std::atomic<State> mState = State::Closed;
  70. Queue<message_ptr> mRecvQueue;
  71. };
  72. } // namespace rtc
  73. #endif
  74. #endif // RTC_WEBSOCKET_H