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

156 lines
3.5 KiB
C
Raw Normal View History

2020-01-28 13:18:38 +01:00
#pragma once
#include "../PrivateDefines.h"
#include "../../tools/Buffers.h"
2020-03-17 21:28:03 +01:00
#include "protocol/Interface.h"
#include "protocol/Extra.h"
2020-01-28 13:18:38 +01:00
//#include "../net/PacketSender.h"
namespace tgvoip
{
class PacketSender;
2020-03-17 20:11:27 +01:00
2020-03-18 21:43:44 +01:00
struct Packet : public Serializable, SingleChoice<Packet>
2020-03-17 20:11:27 +01:00
{
public:
2020-03-18 21:43:44 +01:00
bool parse(const BufferInputStream &in, const VersionInfo &ver) override;
void serialize(BufferOutputStream &out, const VersionInfo &ver) const override;
private:
bool parseLegacy(const BufferInputStream &in, const VersionInfo &ver);
bool parseLegacyLegacy(const BufferInputStream &in, unsigned char &type, uint32_t &ackId, uint32_t &pseq, uint32_t &acks, unsigned char &pflags, size_t &packetInnerLen, int peerVersion);
2020-03-17 20:11:27 +01:00
void serializeLegacy(BufferOutputStream &out, const VersionInfo &ver) const;
void serializeLegacyLegacy(BufferOutputStream &out, uint32_t pseq, uint32_t acks, unsigned char type, uint32_t length) const;
2020-03-17 20:11:27 +01:00
public:
enum Flags : uint8_t
{
Len16 = 1,
2020-03-18 21:43:44 +01:00
RecvTS = 2,
ExtraEC = 4,
2020-03-18 21:43:44 +01:00
ExtraSignaling = 8
2020-03-17 20:11:27 +01:00
};
enum EFlags : uint8_t
{
Fragmented = 1,
Keyframe = 2
};
enum StreamId : uint8_t
{
Signaling = 0,
Audio = 1,
Video = 2,
Extended = 3
};
bool legacy = false;
uint32_t legacySeq = 0;
uint32_t seq = 0;
uint32_t ackSeq = 0;
uint32_t ackMask = 0;
2020-03-16 16:07:13 +01:00
uint8_t streamId = 0;
2020-03-17 20:11:27 +01:00
uint8_t eFlags = 0;
uint8_t fragmentIndex = 0;
uint8_t fragmentCount = 1;
2020-03-17 20:11:27 +01:00
uint32_t recvTS = 0;
2020-03-16 16:07:13 +01:00
Buffer data;
2020-03-17 20:11:27 +01:00
Mask<Wrapped<Bytes>> extraEC;
2020-03-18 21:43:44 +01:00
Array<Wrapped<Extra>> extraSignaling;
// Ugly backwards compatibility hack
std::vector<Packet> otherPackets;
public:
operator bool() {
return data || extraEC || extraSignaling || seq;
}
2020-03-17 20:11:27 +01:00
};
// Legacy stuff
2020-01-28 13:18:38 +01:00
struct RecentOutgoingPacket
{
2020-01-29 15:52:43 +01:00
// For simple NACK reliable resending
int64_t endpoint;
Buffer data;
2020-01-28 13:18:38 +01:00
uint32_t seq;
uint16_t id; // for group calls only
double sendTime;
double ackTime;
double rttTime;
uint8_t type;
uint32_t size;
PacketSender *sender;
bool lost;
};
struct UnacknowledgedExtraData
{
unsigned char type;
Buffer data;
uint32_t firstContainingSeq;
};
struct ReliableOutgoingPacket
{
Buffer data;
unsigned char type;
HistoricBuffer<uint32_t, 16> seqs;
double firstSentTime;
double lastSentTime;
double retryInterval;
double timeout;
uint8_t tries;
};
struct PendingOutgoingPacket
{
PendingOutgoingPacket(uint32_t seq_, uint8_t type_, size_t len_, Buffer &&data_, int64_t endpoint_)
: seq(seq_),
type(type_),
len(len_),
data(std::move(data_)),
endpoint(endpoint_)
{
}
PendingOutgoingPacket(PendingOutgoingPacket &&other)
: seq(other.seq),
type(other.type),
len(other.len),
data(std::move(other.data)),
endpoint(other.endpoint)
{
}
PendingOutgoingPacket &operator=(PendingOutgoingPacket &&other)
{
if (this != &other)
{
seq = other.seq;
type = other.type;
len = other.len;
data = std::move(other.data);
endpoint = other.endpoint;
}
return *this;
}
TGVOIP_DISALLOW_COPY_AND_ASSIGN(PendingOutgoingPacket);
uint32_t seq;
uint8_t type;
size_t len;
Buffer data;
int64_t endpoint;
};
/*
struct DebugLoggedPacket
{
int32_t seq;
double timestamp;
int32_t length;
};
*/
} // namespace tgvoip