channel.hpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 <functional>
  22. #include <mutex>
  23. #include <variant>
  24. namespace rtc {
  25. class Channel {
  26. public:
  27. virtual void close(void) = 0;
  28. virtual void send(const std::variant<binary, string> &data) = 0;
  29. virtual std::optional<std::variant<binary, string>> receive() = 0;
  30. virtual bool isOpen(void) const = 0;
  31. virtual bool isClosed(void) const = 0;
  32. void onOpen(std::function<void()> callback);
  33. void onClosed(std::function<void()> callback);
  34. void onError(std::function<void(const string &error)> callback);
  35. void onMessage(std::function<void(const std::variant<binary, string> &data)> callback);
  36. void onMessage(std::function<void(const binary &data)> binaryCallback,
  37. std::function<void(const string &data)> stringCallback);
  38. void onAvailable(std::function<void()> callback);
  39. void onSent(std::function<void()> callback);
  40. protected:
  41. virtual void triggerOpen(void);
  42. virtual void triggerClosed(void);
  43. virtual void triggerError(const string &error);
  44. virtual void triggerAvailable(size_t available);
  45. virtual void triggerSent();
  46. private:
  47. synchronized_callback<> mOpenCallback;
  48. synchronized_callback<> mClosedCallback;
  49. synchronized_callback<const string &> mErrorCallback;
  50. synchronized_callback<const std::variant<binary, string> &> mMessageCallback;
  51. synchronized_callback<> mAvailableCallback;
  52. synchronized_callback<> mSentCallback;
  53. };
  54. } // namespace rtc
  55. #endif // RTC_CHANNEL_H