IceServer.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. ISC License
  3. Copyright © 2015, Iñaki Baz Castillo <ibc@aliax.net>
  4. Permission to use, copy, modify, and/or distribute this software for any
  5. purpose with or without fee is hereby granted, provided that the above
  6. copyright notice and this permission notice appear in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #ifndef MS_RTC_ICE_SERVER_HPP
  16. #define MS_RTC_ICE_SERVER_HPP
  17. #include "StunPacket.hpp"
  18. #include "Network/Session.h"
  19. #include "logger.h"
  20. #include "Utils.hpp"
  21. #include <list>
  22. #include <string>
  23. #include <functional>
  24. #include <memory>
  25. namespace RTC
  26. {
  27. using TransportTuple = toolkit::Session;
  28. class IceServer
  29. {
  30. public:
  31. enum class IceState
  32. {
  33. NEW = 1,
  34. CONNECTED,
  35. COMPLETED,
  36. DISCONNECTED
  37. };
  38. public:
  39. class Listener
  40. {
  41. public:
  42. virtual ~Listener() = default;
  43. public:
  44. /**
  45. * These callbacks are guaranteed to be called before ProcessStunPacket()
  46. * returns, so the given pointers are still usable.
  47. */
  48. virtual void OnIceServerSendStunPacket(
  49. const RTC::IceServer* iceServer, const RTC::StunPacket* packet, RTC::TransportTuple* tuple) = 0;
  50. virtual void OnIceServerSelectedTuple(
  51. const RTC::IceServer* iceServer, RTC::TransportTuple* tuple) = 0;
  52. virtual void OnIceServerConnected(const RTC::IceServer* iceServer) = 0;
  53. virtual void OnIceServerCompleted(const RTC::IceServer* iceServer) = 0;
  54. virtual void OnIceServerDisconnected(const RTC::IceServer* iceServer) = 0;
  55. };
  56. public:
  57. IceServer(Listener* listener, const std::string& usernameFragment, const std::string& password);
  58. public:
  59. void ProcessStunPacket(RTC::StunPacket* packet, RTC::TransportTuple* tuple);
  60. const std::string& GetUsernameFragment() const
  61. {
  62. return this->usernameFragment;
  63. }
  64. const std::string& GetPassword() const
  65. {
  66. return this->password;
  67. }
  68. IceState GetState() const
  69. {
  70. return this->state;
  71. }
  72. RTC::TransportTuple* GetSelectedTuple(bool try_last_tuple = false) const
  73. {
  74. return try_last_tuple ? this->lastSelectedTuple.lock().get() : this->selectedTuple;
  75. }
  76. void SetUsernameFragment(const std::string& usernameFragment)
  77. {
  78. this->oldUsernameFragment = this->usernameFragment;
  79. this->usernameFragment = usernameFragment;
  80. }
  81. void SetPassword(const std::string& password)
  82. {
  83. this->oldPassword = this->password;
  84. this->password = password;
  85. }
  86. bool IsValidTuple(const RTC::TransportTuple* tuple) const;
  87. void RemoveTuple(RTC::TransportTuple* tuple);
  88. // This should be just called in 'connected' or completed' state
  89. // and the given tuple must be an already valid tuple.
  90. void ForceSelectedTuple(const RTC::TransportTuple* tuple);
  91. const std::list<RTC::TransportTuple *>& GetTuples() const { return tuples; }
  92. private:
  93. void HandleTuple(RTC::TransportTuple* tuple, bool hasUseCandidate);
  94. /**
  95. * Store the given tuple and return its stored address.
  96. */
  97. RTC::TransportTuple* AddTuple(RTC::TransportTuple* tuple);
  98. /**
  99. * If the given tuple exists return its stored address, nullptr otherwise.
  100. */
  101. RTC::TransportTuple* HasTuple(const RTC::TransportTuple* tuple) const;
  102. /**
  103. * Set the given tuple as the selected tuple.
  104. * NOTE: The given tuple MUST be already stored within the list.
  105. */
  106. void SetSelectedTuple(RTC::TransportTuple* storedTuple);
  107. private:
  108. // Passed by argument.
  109. Listener* listener{ nullptr };
  110. // Others.
  111. std::string usernameFragment;
  112. std::string password;
  113. std::string oldUsernameFragment;
  114. std::string oldPassword;
  115. IceState state{ IceState::NEW };
  116. std::list<RTC::TransportTuple *> tuples;
  117. RTC::TransportTuple *selectedTuple { nullptr };
  118. std::weak_ptr<RTC::TransportTuple> lastSelectedTuple;
  119. //最大不超过mtu
  120. static constexpr size_t StunSerializeBufferSize{ 1600 };
  121. uint8_t StunSerializeBuffer[StunSerializeBufferSize];
  122. };
  123. } // namespace RTC
  124. #endif