channel.hpp 2.2 KB

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