2
0

group_backup.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * SRT - Secure, Reliable, Transport
  3. * Copyright (c) 2021 Haivision Systems Inc.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. *
  9. */
  10. /*****************************************************************************
  11. Written by
  12. Haivision Systems Inc.
  13. *****************************************************************************/
  14. #ifndef INC_SRT_GROUP_BACKUP_H
  15. #define INC_SRT_GROUP_BACKUP_H
  16. #include "srt.h"
  17. #include "common.h"
  18. #include "group_common.h"
  19. #include <list>
  20. namespace srt
  21. {
  22. namespace groups
  23. {
  24. enum BackupMemberState
  25. {
  26. BKUPST_UNKNOWN = -1,
  27. BKUPST_PENDING = 0,
  28. BKUPST_STANDBY = 1,
  29. BKUPST_BROKEN = 2,
  30. BKUPST_ACTIVE_UNSTABLE = 3,
  31. BKUPST_ACTIVE_UNSTABLE_WARY = 4,
  32. BKUPST_ACTIVE_FRESH = 5,
  33. BKUPST_ACTIVE_STABLE = 6,
  34. BKUPST_E_SIZE = 7
  35. };
  36. const char* stateToStr(BackupMemberState state);
  37. inline bool isStateActive(BackupMemberState state)
  38. {
  39. if (state == BKUPST_ACTIVE_FRESH
  40. || state == BKUPST_ACTIVE_STABLE
  41. || state == BKUPST_ACTIVE_UNSTABLE
  42. || state == BKUPST_ACTIVE_UNSTABLE_WARY)
  43. {
  44. return true;
  45. }
  46. return false;
  47. }
  48. struct BackupMemberStateEntry
  49. {
  50. BackupMemberStateEntry(SocketData* psock, BackupMemberState st)
  51. : pSocketData(psock)
  52. , socketID(psock->id)
  53. , state(st)
  54. {}
  55. SocketData* pSocketData; // accessing pSocketDataIt requires m_GroupLock
  56. SRTSOCKET socketID; // therefore socketID is saved separately (needed to close broken sockets)
  57. BackupMemberState state;
  58. };
  59. /// @brief A context needed for main/backup sending function.
  60. /// @todo Using gli_t here does not allow to safely store the context outside of the sendBackup calls.
  61. class SendBackupCtx
  62. {
  63. public:
  64. SendBackupCtx()
  65. : m_stateCounter() // default init with zeros
  66. , m_activeMaxWeight()
  67. , m_standbyMaxWeight()
  68. {
  69. }
  70. /// @brief Adds or updates a record of the member socket state.
  71. /// @param pSocketDataIt Iterator to a socket
  72. /// @param st State of the memmber socket
  73. /// @todo Implement updating member state
  74. void recordMemberState(SocketData* pSocketDataIt, BackupMemberState st);
  75. /// @brief Updates a record of the member socket state.
  76. /// @param pSocketDataIt Iterator to a socket
  77. /// @param st State of the memmber socket
  78. /// @todo To be replaced by recordMemberState
  79. /// @todo Update max weights?
  80. void updateMemberState(const SocketData* pSocketDataIt, BackupMemberState st);
  81. /// @brief sorts members in order
  82. /// Higher weight comes first, same weight: stable first, then fresh active.
  83. void sortByWeightAndState();
  84. BackupMemberState getMemberState(const SocketData* pSocketDataIt) const;
  85. unsigned countMembersByState(BackupMemberState st) const;
  86. const std::vector<BackupMemberStateEntry>& memberStates() const { return m_memberStates; }
  87. uint16_t maxStandbyWeight() const { return m_standbyMaxWeight; }
  88. uint16_t maxActiveWeight() const { return m_activeMaxWeight; }
  89. std::string printMembers() const;
  90. void setRateEstimate(const CRateEstimator& rate) { m_rateEstimate = rate; }
  91. const CRateEstimator& getRateEstimate() const { return m_rateEstimate; }
  92. private:
  93. std::vector<BackupMemberStateEntry> m_memberStates; // TODO: consider std::map here?
  94. unsigned m_stateCounter[BKUPST_E_SIZE];
  95. uint16_t m_activeMaxWeight;
  96. uint16_t m_standbyMaxWeight;
  97. CRateEstimator m_rateEstimate; // The rate estimator state of the active link to copy to a backup on activation.
  98. };
  99. } // namespace groups
  100. } // namespace srt
  101. #endif // INC_SRT_GROUP_BACKUP_H