channel.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 <variant>
  23. namespace rtc {
  24. class Channel {
  25. public:
  26. virtual void close(void) = 0;
  27. virtual void send(const std::variant<binary, string> &data) = 0;
  28. virtual bool isOpen(void) const = 0;
  29. virtual bool isClosed(void) const = 0;
  30. void onOpen(std::function<void()> callback);
  31. void onClosed(std::function<void()> callback);
  32. void onError(std::function<void(const string &error)> callback);
  33. void onMessage(std::function<void(const std::variant<binary, string> &data)> callback);
  34. void onMessage(std::function<void(const binary &data)> binaryCallback,
  35. std::function<void(const string &data)> stringCallback);
  36. protected:
  37. virtual void triggerOpen(void);
  38. virtual void triggerClosed(void);
  39. virtual void triggerError(const string &error);
  40. virtual void triggerMessage(const std::variant<binary, string> &data);
  41. private:
  42. std::function<void()> mOpenCallback;
  43. std::function<void()> mClosedCallback;
  44. std::function<void(const string &)> mErrorCallback;
  45. std::function<void(const std::variant<binary, string> &)> mMessageCallback;
  46. };
  47. } // namespace rtc
  48. #endif // RTC_CHANNEL_H