channel.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #ifndef RTC_CHANNEL_H
  19. #define RTC_CHANNEL_H
  20. #include "include.hpp"
  21. #include "message.hpp"
  22. #include <atomic>
  23. #include <functional>
  24. #include <variant>
  25. namespace rtc {
  26. class Channel {
  27. public:
  28. Channel() = default;
  29. virtual ~Channel() = default;
  30. virtual void close() = 0;
  31. virtual bool send(message_variant data) = 0; // returns false if buffered
  32. virtual bool isOpen() const = 0;
  33. virtual bool isClosed() const = 0;
  34. virtual size_t maxMessageSize() const; // max message size in a call to send
  35. virtual size_t bufferedAmount() const; // total size buffered to send
  36. void onOpen(std::function<void()> callback);
  37. void onClosed(std::function<void()> callback);
  38. void onError(std::function<void(string error)> callback);
  39. void onMessage(std::function<void(message_variant data)> callback);
  40. void onMessage(std::function<void(binary data)> binaryCallback,
  41. std::function<void(string data)> stringCallback);
  42. void onBufferedAmountLow(std::function<void()> callback);
  43. void setBufferedAmountLowThreshold(size_t amount);
  44. // Extended API
  45. virtual std::optional<message_variant> receive() = 0; // only if onMessage unset
  46. virtual std::optional<message_variant> peek() = 0; // only if onMessage unset
  47. virtual size_t availableAmount() const; // total size available to receive
  48. void onAvailable(std::function<void()> callback);
  49. protected:
  50. virtual void triggerOpen();
  51. virtual void triggerClosed();
  52. virtual void triggerError(string error);
  53. virtual void triggerAvailable(size_t count);
  54. virtual void triggerBufferedAmount(size_t amount);
  55. void resetCallbacks();
  56. private:
  57. synchronized_callback<> mOpenCallback;
  58. synchronized_callback<> mClosedCallback;
  59. synchronized_callback<string> mErrorCallback;
  60. synchronized_callback<message_variant> mMessageCallback;
  61. synchronized_callback<> mAvailableCallback;
  62. synchronized_callback<> mBufferedAmountLowCallback;
  63. std::atomic<size_t> mBufferedAmount = 0;
  64. std::atomic<size_t> mBufferedAmountLowThreshold = 0;
  65. };
  66. } // namespace rtc
  67. #endif // RTC_CHANNEL_H