2018-04-22 11:23:08 +02:00
|
|
|
#ifndef RELIABLESTREAM_H_
|
|
|
|
#define RELIABLESTREAM_H_
|
|
|
|
|
|
|
|
#include "Stream.h"
|
|
|
|
|
2018-04-22 22:04:10 +02:00
|
|
|
#include <vector>
|
|
|
|
#include <list>
|
|
|
|
|
2018-04-24 01:42:36 +02:00
|
|
|
class ReliableStream : public StreamWrapper
|
2018-04-22 11:23:08 +02:00
|
|
|
{
|
2018-04-27 00:25:48 +02:00
|
|
|
static const int maxInFlight = 3;
|
2018-04-23 00:32:43 +02:00
|
|
|
static const int packetSize = 1024;
|
2018-04-22 11:23:08 +02:00
|
|
|
|
2018-04-23 00:32:43 +02:00
|
|
|
void sendOnePacket();
|
|
|
|
void sendPackets();
|
2018-04-22 11:23:08 +02:00
|
|
|
void nack();
|
|
|
|
void ack();
|
|
|
|
|
|
|
|
void gotNack(uint8_t id);
|
|
|
|
void gotAck(uint8_t id);
|
|
|
|
|
|
|
|
void processIncoming();
|
|
|
|
|
|
|
|
enum class State
|
|
|
|
{
|
|
|
|
waiting,
|
|
|
|
skipping,
|
|
|
|
receiving
|
|
|
|
};
|
|
|
|
|
2018-04-24 01:42:36 +02:00
|
|
|
unsigned receivedInputPacket = 0;
|
|
|
|
unsigned sentOutputPacket = 0;
|
|
|
|
unsigned ackedOutputPacket = 0;
|
|
|
|
|
2018-05-07 22:39:24 +02:00
|
|
|
unsigned failedReceiveCount = 0;
|
|
|
|
unsigned failedSendCount = 0;
|
|
|
|
|
2018-04-22 11:23:08 +02:00
|
|
|
State state = State::waiting;
|
|
|
|
std::vector<uint8_t> incomingPacket;
|
2018-04-23 01:11:56 +02:00
|
|
|
int inputMatchMagic1, inputMatchMagic2;
|
2018-04-22 11:23:08 +02:00
|
|
|
|
2018-04-22 22:04:10 +02:00
|
|
|
std::list<std::vector<uint8_t>> packetsToSend;
|
|
|
|
std::list<std::vector<uint8_t>> sentPackets;
|
|
|
|
|
2018-04-24 01:42:36 +02:00
|
|
|
bool resetResponse = false;
|
2018-05-05 18:40:04 +02:00
|
|
|
bool flushRequested = false;
|
2018-04-22 11:23:08 +02:00
|
|
|
|
2018-12-25 22:20:00 +01:00
|
|
|
virtual size_t onReceive(const uint8_t* p, size_t n) override;
|
2018-04-24 01:42:36 +02:00
|
|
|
public:
|
|
|
|
explicit ReliableStream(Stream* stream);
|
|
|
|
void reset(int sendReset);
|
|
|
|
bool resetResponseArrived() { return resetResponse; }
|
|
|
|
|
2018-04-22 11:23:08 +02:00
|
|
|
virtual void write(const void* p, size_t n) override;
|
|
|
|
virtual void flushWrite() override;
|
|
|
|
|
2018-04-24 01:42:36 +02:00
|
|
|
|
2018-05-08 02:15:05 +02:00
|
|
|
virtual bool readyToWrite() const override { return packetsToSend.empty() && underlying().readyToWrite(); }
|
|
|
|
virtual bool allDataArrived() const override { return packetsToSend.empty() && sentPackets.empty() && underlying().readyToWrite(); }
|
2018-04-24 01:42:36 +02:00
|
|
|
|
2018-05-07 22:39:24 +02:00
|
|
|
unsigned getFailedReceiveCount() const { return failedReceiveCount; }
|
|
|
|
unsigned getFailedSendCount() const { return failedSendCount; }
|
2018-04-22 11:23:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|