Retro68/LaunchAPPL/Common/UnreliableStream.h

55 lines
1.1 KiB
C
Raw Normal View History

#ifndef UNRELIABLESTREAM_H_
#define UNRELIABLESTREAM_H_
#include "Stream.h"
// A stream filter to simulate bit errors
2018-04-23 23:42:36 +00:00
class UnreliableStream : public StreamWrapper
{
int nextError = 0;
public:
2018-04-23 23:42:36 +00:00
UnreliableStream(Stream* stream)
: StreamWrapper(stream)
{
setupNextError();
}
virtual void write(const void* p, size_t n) override
{
std::vector<uint8_t> tmp(n);
memcpy(tmp.data(), p, n);
maybeFlipBit(tmp.data(), n);
2018-04-23 23:42:36 +00:00
underlying().write(tmp.data(),n);
}
virtual void flushWrite() override
{
2018-04-23 23:42:36 +00:00
underlying().flushWrite();
}
private:
virtual size_t onReceive(const uint8_t* p, size_t n) override
{
std::vector<uint8_t> tmp(n);
memcpy(tmp.data(), p, n);
maybeFlipBit(tmp.data(), n);
notifyReceive(tmp.data(), n);
return n;
}
void setupNextError()
{
nextError += rand() % (40 * 8 * 1000 + 3);
}
void maybeFlipBit(uint8_t* p, size_t n)
{
while(nextError < n * 8)
{
p[n / 8] ^= 0x80 >> (n%8);
setupNextError();
}
nextError -= n * 8;
}
};
#endif