websocket.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #if RTC_ENABLE_WEBSOCKET
  9. #include "websocket.hpp"
  10. #include "common.hpp"
  11. #include "impl/internals.hpp"
  12. #include "impl/websocket.hpp"
  13. namespace rtc {
  14. WebSocket::WebSocket() : WebSocket(Configuration()) {}
  15. WebSocket::WebSocket(Configuration config)
  16. : CheshireCat<impl::WebSocket>(std::move(config)),
  17. Channel(std::dynamic_pointer_cast<impl::Channel>(CheshireCat<impl::WebSocket>::impl())) {}
  18. WebSocket::WebSocket(impl_ptr<impl::WebSocket> impl)
  19. : CheshireCat<impl::WebSocket>(std::move(impl)),
  20. Channel(std::dynamic_pointer_cast<impl::Channel>(CheshireCat<impl::WebSocket>::impl())) {}
  21. WebSocket::~WebSocket() {
  22. try {
  23. impl()->remoteClose();
  24. impl()->resetCallbacks(); // not done by impl::WebSocket
  25. } catch (const std::exception &e) {
  26. PLOG_ERROR << e.what();
  27. }
  28. }
  29. WebSocket::State WebSocket::readyState() const { return impl()->state; }
  30. bool WebSocket::isOpen() const { return impl()->state.load() == State::Open; }
  31. bool WebSocket::isClosed() const { return impl()->state.load() == State::Closed; }
  32. size_t WebSocket::maxMessageSize() const { return impl()->maxMessageSize(); }
  33. void WebSocket::open(const string &url) { impl()->open(url); }
  34. void WebSocket::close() { impl()->close(); }
  35. void WebSocket::forceClose() { impl()->remoteClose(); }
  36. bool WebSocket::send(message_variant data) {
  37. return impl()->outgoing(make_message(std::move(data)));
  38. }
  39. bool WebSocket::send(const byte *data, size_t size) {
  40. return impl()->outgoing(make_message(data, data + size, Message::Binary));
  41. }
  42. optional<string> WebSocket::remoteAddress() const {
  43. auto tcpTransport = impl()->getTcpTransport();
  44. return tcpTransport ? make_optional(tcpTransport->remoteAddress()) : nullopt;
  45. }
  46. optional<string> WebSocket::path() const {
  47. auto state = impl()->state.load();
  48. auto handshake = impl()->getWsHandshake();
  49. return state != State::Connecting && handshake ? make_optional(handshake->path()) : nullopt;
  50. }
  51. std::ostream &operator<<(std::ostream &out, WebSocket::State state) {
  52. using State = WebSocket::State;
  53. const char *str;
  54. switch (state) {
  55. case State::Connecting:
  56. str = "connecting";
  57. break;
  58. case State::Open:
  59. str = "open";
  60. break;
  61. case State::Closing:
  62. str = "closing";
  63. break;
  64. case State::Closed:
  65. str = "closed";
  66. break;
  67. default:
  68. str = "unknown";
  69. break;
  70. }
  71. return out << str;
  72. }
  73. } // namespace rtc
  74. #endif