2
0

websocket.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #if RTC_ENABLE_WEBSOCKET
  19. #include "websocket.hpp"
  20. #include "common.hpp"
  21. #include "impl/internals.hpp"
  22. #include "impl/websocket.hpp"
  23. namespace rtc {
  24. WebSocket::WebSocket() : WebSocket(Configuration()) {}
  25. WebSocket::WebSocket(Configuration config)
  26. : CheshireCat<impl::WebSocket>(std::move(config)),
  27. Channel(std::dynamic_pointer_cast<impl::Channel>(CheshireCat<impl::WebSocket>::impl())) {}
  28. WebSocket::WebSocket(impl_ptr<impl::WebSocket> impl)
  29. : CheshireCat<impl::WebSocket>(std::move(impl)),
  30. Channel(std::dynamic_pointer_cast<impl::Channel>(CheshireCat<impl::WebSocket>::impl())) {}
  31. WebSocket::~WebSocket() {
  32. try {
  33. impl()->remoteClose();
  34. impl()->resetCallbacks(); // not done by impl::WebSocket
  35. } catch (const std::exception &e) {
  36. PLOG_ERROR << e.what();
  37. }
  38. }
  39. WebSocket::State WebSocket::readyState() const { return impl()->state; }
  40. bool WebSocket::isOpen() const { return impl()->state.load() == State::Open; }
  41. bool WebSocket::isClosed() const { return impl()->state.load() == State::Closed; }
  42. size_t WebSocket::maxMessageSize() const { return DEFAULT_MAX_MESSAGE_SIZE; }
  43. void WebSocket::open(const string &url) {
  44. PLOG_VERBOSE << "Opening WebSocket to URL: " << url;
  45. impl()->open(url);
  46. }
  47. void WebSocket::close() { impl()->close(); }
  48. bool WebSocket::send(message_variant data) {
  49. return impl()->outgoing(make_message(std::move(data)));
  50. }
  51. bool WebSocket::send(const byte *data, size_t size) {
  52. return impl()->outgoing(make_message(data, data + size, Message::Binary));
  53. }
  54. optional<string> WebSocket::remoteAddress() const {
  55. auto tcpTransport = impl()->getTcpTransport();
  56. return tcpTransport ? make_optional(tcpTransport->remoteAddress()) : nullopt;
  57. }
  58. optional<string> WebSocket::path() const {
  59. auto state = impl()->state.load();
  60. auto handshake = impl()->getWsHandshake();
  61. return state != State::Connecting && handshake ? make_optional(handshake->path()) : nullopt;
  62. }
  63. } // namespace rtc
  64. #endif