channel.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Copyright (c) 2019 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. namespace rtc {
  20. size_t Channel::maxMessageSize() const { return DEFAULT_MAX_MESSAGE_SIZE; }
  21. size_t Channel::bufferedAmount() const { return mBufferedAmount; }
  22. size_t Channel::availableAmount() const { return 0; }
  23. void Channel::onOpen(std::function<void()> callback) {
  24. mOpenCallback = callback;
  25. }
  26. void Channel::onClosed(std::function<void()> callback) {
  27. mClosedCallback = callback;
  28. }
  29. void Channel::onError(std::function<void(string error)> callback) { mErrorCallback = callback; }
  30. void Channel::onMessage(std::function<void(message_variant data)> callback) {
  31. mMessageCallback = callback;
  32. // Pass pending messages
  33. while (auto message = receive())
  34. mMessageCallback(*message);
  35. }
  36. void Channel::onMessage(std::function<void(binary data)> binaryCallback,
  37. std::function<void(string data)> stringCallback) {
  38. onMessage([binaryCallback, stringCallback](std::variant<binary, string> data) {
  39. std::visit(overloaded{binaryCallback, stringCallback}, std::move(data));
  40. });
  41. }
  42. void Channel::onBufferedAmountLow(std::function<void()> callback) {
  43. mBufferedAmountLowCallback = callback;
  44. }
  45. void Channel::setBufferedAmountLowThreshold(size_t amount) { mBufferedAmountLowThreshold = amount; }
  46. void Channel::onAvailable(std::function<void()> callback) {
  47. mAvailableCallback = callback;
  48. }
  49. void Channel::triggerOpen() { mOpenCallback(); }
  50. void Channel::triggerClosed() { mClosedCallback(); }
  51. void Channel::triggerError(string error) { mErrorCallback(error); }
  52. void Channel::triggerAvailable(size_t count) {
  53. if (count == 1)
  54. mAvailableCallback();
  55. while (mMessageCallback && count--) {
  56. auto message = receive();
  57. if (!message)
  58. break;
  59. mMessageCallback(*message);
  60. }
  61. }
  62. void Channel::triggerBufferedAmount(size_t amount) {
  63. size_t previous = mBufferedAmount.exchange(amount);
  64. size_t threshold = mBufferedAmountLowThreshold.load();
  65. if (previous > threshold && amount <= threshold)
  66. mBufferedAmountLowCallback();
  67. }
  68. void Channel::resetCallbacks() {
  69. mOpenCallback = nullptr;
  70. mClosedCallback = nullptr;
  71. mErrorCallback = nullptr;
  72. mMessageCallback = nullptr;
  73. mAvailableCallback = nullptr;
  74. mBufferedAmountLowCallback = nullptr;
  75. }
  76. } // namespace rtc