1
0
mirror of https://github.com/danog/libtgvoip.git synced 2024-12-11 16:49:52 +01:00
libtgvoip/controller/net/PacketSender.h

117 lines
2.6 KiB
C
Raw Normal View History

2020-01-22 12:43:51 +01:00
//
// Created by Grishka on 19/03/2019.
//
#ifndef LIBTGVOIP_PACKETSENDER_H
#define LIBTGVOIP_PACKETSENDER_H
2020-01-28 16:14:43 +01:00
#include "../../VoIPController.h"
2020-01-27 19:53:32 +01:00
#include "../protocol/PacketStructs.h"
2020-01-28 23:45:47 +01:00
#include "../protocol/PacketManager.h"
2020-01-22 12:43:51 +01:00
#include <functional>
#include <stdint.h>
namespace tgvoip
{
class PacketSender
{
public:
2020-01-29 15:52:43 +01:00
PacketSender(VoIPController *controller, const std::shared_ptr<VoIPController::Stream> &stream) : controller(controller), stream(stream), packetManager(stream->id - 1){};
2020-01-27 19:53:32 +01:00
virtual ~PacketSender() = default;
2020-01-22 12:43:51 +01:00
virtual void PacketAcknowledged(uint32_t seq, double sendTime, double ackTime, uint8_t type, uint32_t size) = 0;
virtual void PacketLost(uint32_t seq, uint8_t type, uint32_t size) = 0;
2020-01-28 23:45:47 +01:00
inline PacketManager &getPacketManager()
{
return packetManager;
}
2020-01-29 19:12:12 +01:00
2020-01-22 12:43:51 +01:00
protected:
2020-01-28 16:14:43 +01:00
inline void SendExtra(Buffer &data, unsigned char type)
{
controller->SendExtra(data, type);
}
2020-01-22 12:43:51 +01:00
2020-01-28 16:14:43 +01:00
inline void IncrementUnsentStreamPackets()
{
controller->unsentStreamPackets++;
}
2020-01-22 12:43:51 +01:00
2020-01-28 16:14:43 +01:00
inline uint32_t SendPacket(PendingOutgoingPacket pkt)
{
2020-01-28 23:45:47 +01:00
uint32_t seq = controller->peerVersion < PROTOCOL_RELIABLE ? controller->packetManager.nextLocalSeq() : packetManager.nextLocalSeq();
2020-01-28 16:14:43 +01:00
pkt.seq = seq;
controller->SendOrEnqueuePacket(std::move(pkt), true, this);
return seq;
}
2020-01-22 12:43:51 +01:00
2020-01-28 23:45:47 +01:00
inline void SendPacketReliably(unsigned char type, unsigned char *data, size_t len, double retryInterval, double timeout, uint8_t tries = 0xFF)
2020-01-28 16:14:43 +01:00
{
controller->SendPacketReliably(type, data, len, retryInterval, timeout, tries);
}
2020-01-22 12:43:51 +01:00
2020-01-28 16:14:43 +01:00
inline double GetConnectionInitTime()
{
return controller->connectionInitTime;
}
2020-01-22 12:43:51 +01:00
2020-01-28 16:14:43 +01:00
inline const HistoricBuffer<double, 32> &RTTHistory() const
{
return controller->rttHistory;
}
2020-01-22 12:43:51 +01:00
2020-01-28 16:14:43 +01:00
inline MessageThread &GetMessageThread()
{
return controller->messageThread;
}
2020-01-22 12:43:51 +01:00
2020-01-28 16:14:43 +01:00
inline const VoIPController::ProtocolInfo &GetProtocolInfo() const
{
return controller->protocolInfo;
}
2020-01-22 12:43:51 +01:00
2020-01-28 16:14:43 +01:00
inline void SendStreamFlags(VoIPController::Stream &stm)
{
controller->SendStreamFlags(stm);
}
inline const VoIPController::Config &GetConfig() const
{
return controller->config;
}
inline const bool IsStopping() const
{
return controller->stopping;
}
inline const bool ReceivedInitAck() const
{
return controller->receivedInitAck;
}
2020-01-29 19:12:12 +01:00
inline auto &GetConctl()
{
return controller->conctl;
}
2020-01-28 16:14:43 +01:00
inline const int32_t PeerVersion() const
{
return controller->peerVersion;
}
inline const double LastRtt() const
{
return controller->rttHistory[0];
}
2020-01-22 12:43:51 +01:00
VoIPController *controller;
2020-01-28 23:45:47 +01:00
std::shared_ptr<VoIPController::Stream> stream;
PacketManager packetManager;
2020-01-29 15:52:43 +01:00
std::vector<PendingOutgoingPacket> reliableQueue;
2020-01-22 12:43:51 +01:00
};
} // namespace tgvoip
#endif //LIBTGVOIP_PACKETSENDER_H