channel.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * Copyright (c) 2019-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. #include "channel.hpp"
  19. #include "impl/channel.hpp"
  20. #include "impl/internals.hpp"
  21. namespace rtc {
  22. Channel::~Channel() { impl()->resetCallbacks(); }
  23. Channel::Channel(impl_ptr<impl::Channel> impl) : CheshireCat<impl::Channel>(std::move(impl)) {}
  24. size_t Channel::maxMessageSize() const { return DEFAULT_MAX_MESSAGE_SIZE; }
  25. size_t Channel::bufferedAmount() const { return impl()->bufferedAmount; }
  26. void Channel::onOpen(std::function<void()> callback) { impl()->openCallback = callback; }
  27. void Channel::onClosed(std::function<void()> callback) { impl()->closedCallback = callback; }
  28. void Channel::onError(std::function<void(string error)> callback) {
  29. impl()->errorCallback = callback;
  30. }
  31. void Channel::onMessage(std::function<void(message_variant data)> callback) {
  32. impl()->messageCallback = callback;
  33. impl()->flushPendingMessages();
  34. }
  35. void Channel::onMessage(std::function<void(binary data)> binaryCallback,
  36. std::function<void(string data)> stringCallback) {
  37. onMessage([binaryCallback, stringCallback](variant<binary, string> data) {
  38. std::visit(overloaded{binaryCallback, stringCallback}, std::move(data));
  39. });
  40. }
  41. void Channel::onBufferedAmountLow(std::function<void()> callback) {
  42. impl()->bufferedAmountLowCallback = callback;
  43. }
  44. void Channel::setBufferedAmountLowThreshold(size_t amount) {
  45. impl()->bufferedAmountLowThreshold = amount;
  46. }
  47. optional<message_variant> Channel::receive() { return impl()->receive(); }
  48. optional<message_variant> Channel::peek() { return impl()->peek(); }
  49. size_t Channel::availableAmount() const { return impl()->availableAmount(); }
  50. void Channel::onAvailable(std::function<void()> callback) { impl()->availableCallback = callback; }
  51. } // namespace rtc