channel.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #ifndef RTC_CHANNEL_H
  9. #define RTC_CHANNEL_H
  10. #include "common.hpp"
  11. #include <atomic>
  12. #include <functional>
  13. namespace rtc {
  14. namespace impl {
  15. struct Channel;
  16. }
  17. class RTC_CPP_EXPORT Channel : private CheshireCat<impl::Channel> {
  18. public:
  19. virtual ~Channel();
  20. virtual void close() = 0;
  21. virtual bool send(message_variant data) = 0; // returns false if buffered
  22. virtual bool send(const byte *data, size_t size) = 0;
  23. virtual bool isOpen() const = 0;
  24. virtual bool isClosed() const = 0;
  25. virtual size_t maxMessageSize() const; // max message size in a call to send
  26. virtual size_t bufferedAmount() const; // total size buffered to send
  27. void onOpen(std::function<void()> callback);
  28. void onClosed(std::function<void()> callback);
  29. void onError(std::function<void(string error)> callback);
  30. void onMessage(std::function<void(message_variant data)> callback);
  31. void onMessage(std::function<void(binary data)> binaryCallback,
  32. std::function<void(string data)> stringCallback);
  33. void onBufferedAmountLow(std::function<void()> callback);
  34. void setBufferedAmountLowThreshold(size_t amount);
  35. void resetCallbacks();
  36. // Extended API
  37. optional<message_variant> receive(); // only if onMessage unset
  38. optional<message_variant> peek(); // only if onMessage unset
  39. size_t availableAmount() const; // total size available to receive
  40. void onAvailable(std::function<void()> callback);
  41. protected:
  42. Channel(impl_ptr<impl::Channel> impl);
  43. };
  44. } // namespace rtc
  45. #endif // RTC_CHANNEL_H