channel.hpp 2.7 KB

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