1
0
mirror of https://github.com/danog/libtgvoip.git synced 2024-12-03 10:07:45 +01:00
libtgvoip/controller/protocol/PacketManager.h

73 lines
1.7 KiB
C
Raw Normal View History

2020-01-27 19:53:32 +01:00
#pragma once
2020-01-27 17:18:33 +01:00
#include <atomic>
2020-01-27 19:53:32 +01:00
#include <list>
#include "PacketStructs.h"
2020-01-27 17:18:33 +01:00
namespace tgvoip
{
2020-01-28 13:18:38 +01:00
#define SEQ_MAX 0xFFFFFFFF
inline bool seqgt(uint32_t s1, uint32_t s2)
{
return ((s1 > s2) && (s1 - s2 <= SEQ_MAX / 2)) || ((s1 < s2) && (s2 - s1 > SEQ_MAX / 2));
}
inline bool seqgte(uint32_t s1, uint32_t s2)
{
return s1 == s2 || seqgt(s1, s2);
}
2020-01-27 19:53:32 +01:00
// Local and remote packet history management
2020-01-28 13:18:38 +01:00
struct PacketManager
2020-01-27 19:53:32 +01:00
{
2020-01-28 13:18:38 +01:00
PacketManager();
2020-01-27 19:53:32 +01:00
/* Local seqno ack */
// Ack specified local seq + up to 32 seqs ago, specified by mask
void ackLocal(uint32_t ackId, uint32_t mask);
// Check if local seq was acked
bool wasLocalAcked(uint32_t seq);
// Get next local seqno
inline uint32_t nextLocalSeq()
2020-01-27 17:18:33 +01:00
{
2020-01-27 19:53:32 +01:00
return seq++;
}
2020-01-28 13:18:38 +01:00
// Get current local seqno
inline uint32_t getLocalSeq()
{
return seq;
}
private:
2020-01-27 19:53:32 +01:00
// Stream-specific local seqno
std::atomic<uint32_t> seq = ATOMIC_VAR_INIT(1);
2020-01-28 13:18:38 +01:00
public:
2020-01-27 19:53:32 +01:00
// Recent ougoing packets
std::vector<RecentOutgoingPacket> recentOutgoingPackets;
// Seqno of last sent local packet
uint32_t lastSentSeq = 0;
// Status list of acked local seqnos, starting from the seq explicitly present in the packet + up to 32 seqs ago
std::array<uint32_t, 33> peerAcks{0};
/* Remote seqno ack */
// Ack specified remote seq, returns false if too old or dupe
bool ackRemoteSeq(uint32_t ackId);
// Get ack mask for remote packets
uint32_t getRemoteAckMask();
// Seqno of last received remote packet
uint32_t lastRemoteSeq = 0;
private: // Slowly encapsulate all the things
// Recent incoming remote packets
std::list<uint32_t> recentIncomingSeqs;
};
} // namespace tgvoip