2
0

websocket.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * Copyright (c) 2020-2021 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_IMPL_WEBSOCKET_H
  19. #define RTC_IMPL_WEBSOCKET_H
  20. #if RTC_ENABLE_WEBSOCKET
  21. #include "channel.hpp"
  22. #include "common.hpp"
  23. #include "init.hpp"
  24. #include "message.hpp"
  25. #include "queue.hpp"
  26. #include "tcptransport.hpp"
  27. #include "tlstransport.hpp"
  28. #include "wstransport.hpp"
  29. #include "rtc/websocket.hpp"
  30. #include <atomic>
  31. #include <thread>
  32. namespace rtc::impl {
  33. struct WebSocket final : public Channel, public std::enable_shared_from_this<WebSocket> {
  34. using State = rtc::WebSocket::State;
  35. using Configuration = rtc::WebSocket::Configuration;
  36. WebSocket(optional<Configuration> optConfig = nullopt, certificate_ptr certificate = nullptr);
  37. ~WebSocket();
  38. void open(const string &url);
  39. void close();
  40. bool outgoing(message_ptr message);
  41. void incoming(message_ptr message);
  42. optional<message_variant> receive() override;
  43. optional<message_variant> peek() override;
  44. size_t availableAmount() const override;
  45. bool isOpen() const;
  46. bool isClosed() const;
  47. size_t maxMessageSize() const;
  48. bool changeState(State state);
  49. void remoteClose();
  50. shared_ptr<TcpTransport> setTcpTransport(shared_ptr<TcpTransport> transport);
  51. shared_ptr<TlsTransport> initTlsTransport();
  52. shared_ptr<WsTransport> initWsTransport();
  53. shared_ptr<TcpTransport> getTcpTransport() const;
  54. shared_ptr<TlsTransport> getTlsTransport() const;
  55. shared_ptr<WsTransport> getWsTransport() const;
  56. shared_ptr<WsHandshake> getWsHandshake() const;
  57. void closeTransports();
  58. const Configuration config;
  59. std::atomic<State> state = State::Closed;
  60. private:
  61. const init_token mInitToken = Init::Instance().token();
  62. const certificate_ptr mCertificate;
  63. bool mIsSecure;
  64. optional<string> mHostname; // for TLS SNI
  65. shared_ptr<TcpTransport> mTcpTransport;
  66. shared_ptr<TlsTransport> mTlsTransport;
  67. shared_ptr<WsTransport> mWsTransport;
  68. shared_ptr<WsHandshake> mWsHandshake;
  69. Queue<message_ptr> mRecvQueue;
  70. };
  71. } // namespace rtc::impl
  72. #endif
  73. #endif // RTC_IMPL_WEBSOCKET_H