websocket.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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) { impl()->open(url); }
  44. void WebSocket::close() { impl()->close(); }
  45. void WebSocket::forceClose() { impl()->remoteClose(); }
  46. bool WebSocket::send(message_variant data) {
  47. return impl()->outgoing(make_message(std::move(data)));
  48. }
  49. bool WebSocket::send(const byte *data, size_t size) {
  50. return impl()->outgoing(make_message(data, data + size, Message::Binary));
  51. }
  52. optional<string> WebSocket::remoteAddress() const {
  53. auto tcpTransport = impl()->getTcpTransport();
  54. return tcpTransport ? make_optional(tcpTransport->remoteAddress()) : nullopt;
  55. }
  56. optional<string> WebSocket::path() const {
  57. auto state = impl()->state.load();
  58. auto handshake = impl()->getWsHandshake();
  59. return state != State::Connecting && handshake ? make_optional(handshake->path()) : nullopt;
  60. }
  61. } // namespace rtc
  62. #endif