channel.hpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. void resetCallbacks();
  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