channel.hpp 2.8 KB

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