Retro68/LaunchAPPL/Common/ReliableStream.h

66 lines
1.6 KiB
C
Raw Normal View History

#ifndef RELIABLESTREAM_H_
#define RELIABLESTREAM_H_
#include "Stream.h"
#include <vector>
#include <list>
2018-04-23 23:42:36 +00:00
class ReliableStream : public StreamWrapper
{
static const int maxInFlight = 3;
static const int packetSize = 1024;
void sendOnePacket();
void sendPackets();
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;
unsigned failedReceiveCount = 0;
unsigned failedSendCount = 0;
State state = State::waiting;
std::vector<uint8_t> incomingPacket;
int inputMatchMagic1, inputMatchMagic2;
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;
bool flushRequested = false;
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; }
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
unsigned getFailedReceiveCount() const { return failedReceiveCount; }
unsigned getFailedSendCount() const { return failedSendCount; }
};
#endif