transport.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_IMPL_TRANSPORT_H
  19. #define RTC_IMPL_TRANSPORT_H
  20. #include "common.hpp"
  21. #include "internals.hpp"
  22. #include "message.hpp"
  23. #include <atomic>
  24. #include <functional>
  25. #include <memory>
  26. namespace rtc::impl {
  27. class Transport {
  28. public:
  29. enum class State { Disconnected, Connecting, Connected, Completed, Failed };
  30. using state_callback = std::function<void(State state)>;
  31. Transport(shared_ptr<Transport> lower = nullptr, state_callback callback = nullptr)
  32. : mLower(std::move(lower)), mStateChangeCallback(std::move(callback)) {}
  33. virtual ~Transport() { stop(); }
  34. virtual void start() { mStopped = false; }
  35. virtual bool stop() {
  36. if (mStopped.exchange(true))
  37. return false;
  38. // We don't want incoming() to be called by the lower layer anymore
  39. if (mLower) {
  40. PLOG_VERBOSE << "Unregistering incoming callback";
  41. mLower->onRecv(nullptr);
  42. }
  43. return true;
  44. }
  45. void registerIncoming() {
  46. if (mLower) {
  47. PLOG_VERBOSE << "Registering incoming callback";
  48. mLower->onRecv(std::bind(&Transport::incoming, this, std::placeholders::_1));
  49. }
  50. }
  51. void onRecv(message_callback callback) { mRecvCallback = std::move(callback); }
  52. void onStateChange(state_callback callback) { mStateChangeCallback = std::move(callback); }
  53. State state() const { return mState; }
  54. virtual bool send(message_ptr message) { return outgoing(message); }
  55. protected:
  56. void recv(message_ptr message) {
  57. try {
  58. mRecvCallback(message);
  59. } catch (const std::exception &e) {
  60. PLOG_WARNING << e.what();
  61. }
  62. }
  63. void changeState(State state) {
  64. try {
  65. if (mState.exchange(state) != state)
  66. mStateChangeCallback(state);
  67. } catch (const std::exception &e) {
  68. PLOG_WARNING << e.what();
  69. }
  70. }
  71. virtual void incoming(message_ptr message) { recv(message); }
  72. virtual bool outgoing(message_ptr message) {
  73. if (mLower)
  74. return mLower->send(message);
  75. else
  76. return false;
  77. }
  78. private:
  79. shared_ptr<Transport> mLower;
  80. synchronized_callback<State> mStateChangeCallback;
  81. synchronized_callback<message_ptr> mRecvCallback;
  82. std::atomic<State> mState = State::Disconnected;
  83. std::atomic<bool> mStopped = true;
  84. };
  85. } // namespace rtc::impl
  86. #endif