2018-04-22 09:23:08 +00:00
|
|
|
#ifndef RELIABLESTREAM_H_
|
|
|
|
#define RELIABLESTREAM_H_
|
|
|
|
|
|
|
|
#include "Stream.h"
|
|
|
|
|
2018-04-22 20:04:10 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <list>
|
|
|
|
|
2018-04-23 23:42:36 +00:00
|
|
|
class ReliableStream : public StreamWrapper
|
2018-04-22 09:23:08 +00:00
|
|
|
{
|
2018-04-26 22:25:48 +00:00
|
|
|
static const int maxInFlight = 3;
|
2018-04-22 22:32:43 +00:00
|
|
|
static const int packetSize = 1024;
|
2018-04-22 09:23:08 +00:00
|
|
|
|
2018-04-22 22:32:43 +00:00
|
|
|
void sendOnePacket();
|
|
|
|
void sendPackets();
|
2018-04-22 09:23:08 +00: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-23 23:42:36 +00:00
|
|
|
unsigned receivedInputPacket = 0;
|
|
|
|
unsigned sentOutputPacket = 0;
|
|
|
|
unsigned ackedOutputPacket = 0;
|
|
|
|
|
2018-05-07 20:39:24 +00:00
|
|
|
unsigned failedReceiveCount = 0;
|
|
|
|
unsigned failedSendCount = 0;
|
|
|
|
|
2018-04-22 09:23:08 +00:00
|
|
|
State state = State::waiting;
|
|
|
|
std::vector<uint8_t> incomingPacket;
|
2018-04-22 23:11:56 +00:00
|
|
|
int inputMatchMagic1, inputMatchMagic2;
|
2018-04-22 09:23:08 +00:00
|
|
|
|
2018-04-22 20:04:10 +00:00
|
|
|
std::list<std::vector<uint8_t>> packetsToSend;
|
|
|
|
std::list<std::vector<uint8_t>> sentPackets;
|
|
|
|
|
2018-04-23 23:42:36 +00:00
|
|
|
bool resetResponse = false;
|
2018-05-05 16:40:04 +00:00
|
|
|
bool flushRequested = false;
|
2018-04-22 09:23:08 +00:00
|
|
|
|
2018-12-25 21:20:00 +00:00
|
|
|
virtual size_t onReceive(const uint8_t* p, size_t n) override;
|
2018-04-23 23:42:36 +00:00
|
|
|
public:
|
|
|
|
explicit ReliableStream(Stream* stream);
|
|
|
|
void reset(int sendReset);
|
|
|
|
bool resetResponseArrived() { return resetResponse; }
|
|
|
|
|
2018-04-22 09:23:08 +00:00
|
|
|
virtual void write(const void* p, size_t n) override;
|
|
|
|
virtual void flushWrite() override;
|
|
|
|
|
2018-04-23 23:42:36 +00:00
|
|
|
|
2018-05-08 00:15:05 +00: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-23 23:42:36 +00:00
|
|
|
|
2018-05-07 20:39:24 +00:00
|
|
|
unsigned getFailedReceiveCount() const { return failedReceiveCount; }
|
|
|
|
unsigned getFailedSendCount() const { return failedSendCount; }
|
2018-04-22 09:23:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|