| 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 |
/***************************************************************************** |
| 12 |
Written by |
| 13 |
Haivision Systems Inc. |
| 14 |
*****************************************************************************/ |
| 15 |
|
| 16 |
#ifndef INC_SRT_GROUP_BACKUP_H |
| 17 |
#define INC_SRT_GROUP_BACKUP_H |
| 18 |
|
| 19 |
#include "srt.h" |
| 20 |
#include "common.h" |
| 21 |
#include "group_common.h" |
| 22 |
|
| 23 |
#include <list> |
| 24 |
|
| 25 |
namespace srt |
| 26 |
{ |
| 27 |
namespace groups |
| 28 |
{ |
| 29 |
enum BackupMemberState |
| 30 |
{ |
| 31 |
BKUPST_UNKNOWN = -1, |
| 32 |
|
| 33 |
BKUPST_PENDING = 0, |
| 34 |
BKUPST_STANDBY = 1, |
| 35 |
BKUPST_BROKEN = 2, |
| 36 |
|
| 37 |
BKUPST_ACTIVE_UNSTABLE = 3, |
| 38 |
BKUPST_ACTIVE_UNSTABLE_WARY = 4, |
| 39 |
BKUPST_ACTIVE_FRESH = 5, |
| 40 |
BKUPST_ACTIVE_STABLE = 6, |
| 41 |
|
| 42 |
BKUPST_E_SIZE = 7 |
| 43 |
}; |
| 44 |
|
| 45 |
const char* stateToStr(BackupMemberState state); |
| 46 |
|
| 47 |
inline bool isStateActive(BackupMemberState state) |
| 48 |
{ |
| 49 |
if (state == BKUPST_ACTIVE_FRESH |
| 50 |
|| state == BKUPST_ACTIVE_STABLE |
| 51 |
|| state == BKUPST_ACTIVE_UNSTABLE |
| 52 |
|| state == BKUPST_ACTIVE_UNSTABLE_WARY) |
| 53 |
{ |
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
struct BackupMemberStateEntry |
| 61 |
{ |
| 62 |
BackupMemberStateEntry(SocketData* psock, BackupMemberState st) |
| 63 |
: pSocketData(psock) |
| 64 |
, socketID(psock->id) |
| 65 |
, state(st) |
| 66 |
{} |
| 67 |
|
| 68 |
SocketData* pSocketData; // accessing pSocketDataIt requires m_GroupLock |
| 69 |
SRTSOCKET socketID; // therefore socketID is saved separately (needed to close broken sockets) |
| 70 |
BackupMemberState state; |
| 71 |
}; |
| 72 |
|
| 73 |
/// @brief A context needed for main/backup sending function. |
| 74 |
/// @todo Using gli_t here does not allow to safely store the context outside of the sendBackup calls. |
| 75 |
class SendBackupCtx |
| 76 |
{ |
| 77 |
public: |
| 78 |
SendBackupCtx() |
| 79 |
: m_stateCounter() // default init with zeros |
| 80 |
, m_activeMaxWeight() |
| 81 |
, m_standbyMaxWeight() |
| 82 |
// XXX Setting AF_INET6 is a temporary solution for using rate estimator |
| 83 |
// that counts a rate based on the current link's IP version. The results |
| 84 |
// for links using IPv4 could be slightly falsified due to that (16 bytes |
| 85 |
// more per a packet), but this makes the estimation results the same for |
| 86 |
// the same data sent over the group, regardless of the IP version used |
| 87 |
// for the currently active link (which in reality results in different |
| 88 |
// load for the same stream, if links use different IP version). |
| 89 |
, m_rateEstimate(AF_INET6) |
| 90 |
{ |
| 91 |
} |
| 92 |
|
| 93 |
/// @brief Adds or updates a record of the member socket state. |
| 94 |
/// @param pSocketDataIt Iterator to a socket |
| 95 |
/// @param st State of the member socket |
| 96 |
/// @todo Implement updating member state |
| 97 |
void recordMemberState(SocketData* pSocketDataIt, BackupMemberState st); |
| 98 |
|
| 99 |
/// @brief Updates a record of the member socket state. |
| 100 |
/// @param pSocketDataIt Iterator to a socket |
| 101 |
/// @param st State of the member socket |
| 102 |
/// @todo To be replaced by recordMemberState |
| 103 |
/// @todo Update max weights? |
| 104 |
void updateMemberState(const SocketData* pSocketDataIt, BackupMemberState st); |
| 105 |
|
| 106 |
/// @brief sorts members in order |
| 107 |
/// Higher weight comes first, same weight: stable first, then fresh active. |
| 108 |
void sortByWeightAndState(); |
| 109 |
|
| 110 |
BackupMemberState getMemberState(const SocketData* pSocketDataIt) const; |
| 111 |
|
| 112 |
unsigned countMembersByState(BackupMemberState st) const; |
| 113 |
|
| 114 |
const std::vector<BackupMemberStateEntry>& memberStates() const { return m_memberStates; } |
| 115 |
|
| 116 |
uint16_t maxStandbyWeight() const { return m_standbyMaxWeight; } |
| 117 |
uint16_t maxActiveWeight() const { return m_activeMaxWeight; } |
| 118 |
|
| 119 |
std::string printMembers() const; |
| 120 |
|
| 121 |
void setRateEstimate(const CRateEstimator& rate) { m_rateEstimate = rate; } |
| 122 |
|
| 123 |
const CRateEstimator& getRateEstimate() const { return m_rateEstimate; } |
| 124 |
|
| 125 |
private: |
| 126 |
std::vector<BackupMemberStateEntry> m_memberStates; // TODO: consider std::map here? |
| 127 |
unsigned m_stateCounter[BKUPST_E_SIZE]; |
| 128 |
uint16_t m_activeMaxWeight; |
| 129 |
uint16_t m_standbyMaxWeight; |
| 130 |
CRateEstimator m_rateEstimate; // The rate estimator state of the active link to copy to a backup on activation. |
| 131 |
}; |
| 132 |
|
| 133 |
} // namespace groups |
| 134 |
} // namespace srt |
| 135 |
|
| 136 |
#endif // INC_SRT_GROUP_BACKUP_H |