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