datachannel.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * Copyright (c) 2019-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. #include "datachannel.hpp"
  9. #include "common.hpp"
  10. #include "peerconnection.hpp"
  11. #include "impl/datachannel.hpp"
  12. #include "impl/internals.hpp"
  13. #include "impl/peerconnection.hpp"
  14. #ifdef _WIN32
  15. #include <winsock2.h>
  16. #else
  17. #include <arpa/inet.h>
  18. #endif
  19. namespace rtc {
  20. DataChannel::DataChannel(impl_ptr<impl::DataChannel> impl)
  21. : CheshireCat<impl::DataChannel>(impl),
  22. Channel(std::dynamic_pointer_cast<impl::Channel>(impl)) {}
  23. DataChannel::~DataChannel() {
  24. try {
  25. close();
  26. } catch (const std::exception &e) {
  27. PLOG_ERROR << e.what();
  28. }
  29. }
  30. void DataChannel::close() { return impl()->close(); }
  31. optional<uint16_t> DataChannel::stream() const { return impl()->stream(); }
  32. optional<uint16_t> DataChannel::id() const { return impl()->stream(); }
  33. string DataChannel::label() const { return impl()->label(); }
  34. string DataChannel::protocol() const { return impl()->protocol(); }
  35. Reliability DataChannel::reliability() const { return impl()->reliability(); }
  36. bool DataChannel::isOpen(void) const { return impl()->isOpen(); }
  37. bool DataChannel::isClosed(void) const { return impl()->isClosed(); }
  38. size_t DataChannel::maxMessageSize() const { return impl()->maxMessageSize(); }
  39. bool DataChannel::send(message_variant data) {
  40. return impl()->outgoing(make_message(std::move(data)));
  41. }
  42. bool DataChannel::send(const byte *data, size_t size) {
  43. return impl()->outgoing(std::make_shared<Message>(data, data + size, Message::Binary));
  44. }
  45. } // namespace rtc