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-22 09:23:08 +00:00
|
|
|
class ReliableStream : public Stream, public StreamListener
|
|
|
|
{
|
|
|
|
Stream& stream;
|
|
|
|
|
|
|
|
static const int packetSize = 256;
|
|
|
|
|
2018-04-22 20:04:10 +00:00
|
|
|
unsigned receivedInputPacket = 0;
|
|
|
|
unsigned sentOutputPacket = 0;
|
|
|
|
unsigned ackedOutputPacket = 0;
|
2018-04-22 09:23:08 +00:00
|
|
|
|
|
|
|
void sendPacket();
|
|
|
|
void nack();
|
|
|
|
void ack();
|
|
|
|
|
|
|
|
void gotNack(uint8_t id);
|
|
|
|
void gotAck(uint8_t id);
|
|
|
|
|
|
|
|
void processIncoming();
|
|
|
|
|
|
|
|
enum class State
|
|
|
|
{
|
|
|
|
waiting,
|
|
|
|
skipping,
|
|
|
|
receiving
|
|
|
|
};
|
|
|
|
|
|
|
|
State state = State::waiting;
|
|
|
|
std::vector<uint8_t> incomingPacket;
|
|
|
|
|
2018-04-22 20:04:10 +00:00
|
|
|
std::list<std::vector<uint8_t>> packetsToSend;
|
|
|
|
std::list<std::vector<uint8_t>> sentPackets;
|
|
|
|
|
2018-04-22 09:23:08 +00:00
|
|
|
public:
|
|
|
|
ReliableStream(Stream& stream);
|
|
|
|
virtual ~ReliableStream();
|
|
|
|
|
|
|
|
virtual void write(const void* p, size_t n) override;
|
|
|
|
virtual void flushWrite() override;
|
|
|
|
|
|
|
|
virtual size_t onReceive(const uint8_t* p, size_t n) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|