2018-04-22 09:23:08 +00:00
|
|
|
#include "ReliableStream.h"
|
|
|
|
#include <iterator>
|
|
|
|
#include "CRC32.h"
|
|
|
|
|
|
|
|
using std::begin;
|
|
|
|
using std::end;
|
|
|
|
|
|
|
|
const uint8_t magic1[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
|
|
|
|
const uint8_t magic2[4] = { 0xFA, 0xCE, 0xCA, 0xFE };
|
|
|
|
|
|
|
|
const uint32_t magic1_32 = 0xDEADBEEF;
|
|
|
|
const uint32_t magic2_32 = 0xFACECAFE;
|
|
|
|
|
|
|
|
enum thing : uint8_t
|
|
|
|
{
|
|
|
|
kDataPacket = 0,
|
|
|
|
kEndOfPacket,
|
|
|
|
kEscapedMagic1,
|
|
|
|
kEscapedMagic2,
|
|
|
|
kAck,
|
2018-04-23 23:42:36 +00:00
|
|
|
kNack,
|
|
|
|
kReset1,
|
|
|
|
kReset2
|
2018-04-22 09:23:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
inline uint32_t readLong(uint8_t* p)
|
|
|
|
{
|
|
|
|
uint32_t x;
|
|
|
|
x = *p++;
|
|
|
|
x = (x << 8) | *p++;
|
|
|
|
x = (x << 8) | *p++;
|
|
|
|
x = (x << 8) | *p++;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2018-04-23 23:42:36 +00:00
|
|
|
ReliableStream::ReliableStream(Stream* stream)
|
|
|
|
: StreamWrapper(stream)
|
2018-04-22 09:23:08 +00:00
|
|
|
{
|
|
|
|
incomingPacket.reserve(packetSize + 4);
|
|
|
|
}
|
2018-04-23 23:42:36 +00:00
|
|
|
|
|
|
|
void ReliableStream::reset(int sendReset)
|
2018-04-22 09:23:08 +00:00
|
|
|
{
|
2018-04-23 23:42:36 +00:00
|
|
|
printf("reset %d\n", sendReset);
|
|
|
|
receivedInputPacket = 0;
|
|
|
|
sentOutputPacket = 0;
|
|
|
|
ackedOutputPacket = 0;
|
2018-04-22 09:23:08 +00:00
|
|
|
|
2018-04-23 23:42:36 +00:00
|
|
|
incomingPacket.clear();
|
|
|
|
state = State::waiting;
|
|
|
|
sentPackets.clear();
|
|
|
|
packetsToSend.clear();
|
2018-04-22 09:23:08 +00:00
|
|
|
|
2018-04-23 23:42:36 +00:00
|
|
|
resetResponse = false;
|
|
|
|
|
|
|
|
if(sendReset)
|
|
|
|
{
|
|
|
|
uint8_t resetKind = sendReset == 1 ? kReset1 : kReset2;
|
|
|
|
uint8_t packet[] = {
|
|
|
|
magic1[0], magic1[1], magic1[2], magic1[3],
|
|
|
|
resetKind, (uint8_t)~resetKind
|
|
|
|
};
|
|
|
|
underlying().write(packet, 6);
|
|
|
|
}
|
|
|
|
}
|
2018-04-22 09:23:08 +00:00
|
|
|
|
|
|
|
void ReliableStream::ack()
|
|
|
|
{
|
|
|
|
uint8_t packet[] = {
|
|
|
|
magic1[0], magic1[1], magic1[2], magic1[3],
|
|
|
|
kAck, (uint8_t)~kAck, (uint8_t)receivedInputPacket, (uint8_t)~receivedInputPacket
|
|
|
|
};
|
2018-04-23 23:42:36 +00:00
|
|
|
underlying().write(packet, 8);
|
2018-04-22 23:11:56 +00:00
|
|
|
//printf("ack sent\n");
|
2018-04-22 09:23:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReliableStream::nack()
|
|
|
|
{
|
|
|
|
uint8_t packet[] = {
|
|
|
|
magic1[0], magic1[1], magic1[2], magic1[3],
|
|
|
|
kNack, (uint8_t)~kNack, (uint8_t)receivedInputPacket, (uint8_t)~receivedInputPacket
|
|
|
|
};
|
2018-04-23 23:42:36 +00:00
|
|
|
underlying().write(packet, 8);
|
2018-04-22 23:11:56 +00:00
|
|
|
//printf("nack sent\n");
|
2018-04-22 09:23:08 +00:00
|
|
|
}
|
|
|
|
|
2018-04-22 12:07:52 +00:00
|
|
|
void ReliableStream::gotAck(uint8_t id)
|
2018-04-22 09:23:08 +00:00
|
|
|
{
|
2018-04-22 20:04:10 +00:00
|
|
|
unsigned nAcked = (id - ackedOutputPacket) & 0xFF;
|
2018-04-23 23:42:36 +00:00
|
|
|
printf("got ack %d -> %u packets of %u acked\n", (int)id, nAcked, (unsigned)sentPackets.size());
|
2018-04-22 22:32:43 +00:00
|
|
|
if(nAcked <= sentPackets.size())
|
|
|
|
{
|
|
|
|
ackedOutputPacket += nAcked;
|
|
|
|
for(int i = 0; i < nAcked; i++)
|
|
|
|
sentPackets.pop_front();
|
|
|
|
|
|
|
|
sendPackets();
|
|
|
|
}
|
2018-04-22 09:23:08 +00:00
|
|
|
}
|
|
|
|
|
2018-04-22 12:07:52 +00:00
|
|
|
void ReliableStream::gotNack(uint8_t id)
|
2018-04-22 09:23:08 +00:00
|
|
|
{
|
2018-04-22 20:04:10 +00:00
|
|
|
printf("got nack %d\n", (int)id);
|
|
|
|
|
|
|
|
unsigned nAcked = (id - ackedOutputPacket) & 0xFF;
|
2018-04-22 22:32:43 +00:00
|
|
|
if(nAcked <= sentPackets.size())
|
|
|
|
{
|
|
|
|
ackedOutputPacket += nAcked;
|
|
|
|
for(int i = 0; i < nAcked; i++)
|
|
|
|
sentPackets.pop_front();
|
2018-04-22 20:04:10 +00:00
|
|
|
|
2018-04-22 22:32:43 +00:00
|
|
|
sentOutputPacket = ackedOutputPacket;
|
2018-04-22 20:04:10 +00:00
|
|
|
|
2018-04-22 22:32:43 +00:00
|
|
|
packetsToSend.splice(packetsToSend.begin(), sentPackets);
|
|
|
|
|
|
|
|
sendPackets();
|
|
|
|
}
|
2018-04-22 09:23:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReliableStream::processIncoming()
|
|
|
|
{
|
2018-04-23 23:42:36 +00:00
|
|
|
printf("Received packet %d - %d bytes\n", receivedInputPacket + 1, (int)incomingPacket.size());
|
2018-04-22 09:23:08 +00:00
|
|
|
if(incomingPacket.size() < 4)
|
|
|
|
{
|
|
|
|
nack();
|
|
|
|
incomingPacket.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint32_t expectedCRC = crc32(0, incomingPacket.begin(), incomingPacket.end() - 4);
|
|
|
|
uint32_t receivedCRC = readLong(&incomingPacket[incomingPacket.size()-4]);
|
|
|
|
if(receivedCRC != expectedCRC)
|
|
|
|
{
|
|
|
|
printf("CRC mismatch %x != %x\n", receivedCRC, expectedCRC);
|
|
|
|
|
|
|
|
nack();
|
|
|
|
incomingPacket.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
receivedInputPacket++;
|
|
|
|
printf("Verified packet %d - %d bytes\n", receivedInputPacket, (int)incomingPacket.size());
|
|
|
|
ack();
|
|
|
|
notifyReceive(incomingPacket.data(), incomingPacket.size() - 4);
|
|
|
|
incomingPacket.clear();
|
|
|
|
}
|
|
|
|
|
2018-04-22 22:32:43 +00:00
|
|
|
void ReliableStream::sendOnePacket()
|
2018-04-22 09:23:08 +00:00
|
|
|
{
|
2018-04-22 20:04:10 +00:00
|
|
|
if(packetsToSend.empty())
|
|
|
|
return;
|
2018-04-22 09:23:08 +00:00
|
|
|
|
2018-04-22 20:04:10 +00:00
|
|
|
const uint8_t* p = packetsToSend.front().data();
|
|
|
|
size_t n = packetsToSend.front().size();
|
|
|
|
|
2018-04-22 09:23:08 +00:00
|
|
|
++sentOutputPacket;
|
2018-04-22 20:04:10 +00:00
|
|
|
sentPackets.splice(sentPackets.end(), packetsToSend, packetsToSend.begin());
|
|
|
|
|
|
|
|
std::vector<uint8_t> packet;
|
|
|
|
packet.reserve(n + 32);
|
|
|
|
packet = {
|
2018-04-22 09:23:08 +00:00
|
|
|
magic1[0], magic1[1], magic1[2], magic1[3],
|
|
|
|
kDataPacket, (uint8_t)~kDataPacket, (uint8_t)sentOutputPacket, (uint8_t)~sentOutputPacket
|
|
|
|
};
|
2018-04-22 20:04:10 +00:00
|
|
|
|
2018-04-22 09:23:08 +00:00
|
|
|
int match = 0, match2 = 0;
|
|
|
|
int i;
|
|
|
|
int consumed = 0;
|
|
|
|
for(i = 0; i < n; i++)
|
|
|
|
{
|
2018-04-22 22:32:43 +00:00
|
|
|
if(p[i] == magic1[match])
|
|
|
|
match++;
|
|
|
|
else if(p[i] == magic1[0])
|
|
|
|
match = 1;
|
2018-04-22 09:23:08 +00:00
|
|
|
else
|
|
|
|
match = 0;
|
|
|
|
|
2018-04-22 22:32:43 +00:00
|
|
|
if(match == 4)
|
|
|
|
{
|
|
|
|
packet.push_back(magic2[0]);
|
|
|
|
packet.push_back(magic2[1]);
|
|
|
|
packet.push_back(magic2[2]);
|
|
|
|
packet.push_back(magic2[3]);
|
|
|
|
packet.push_back(kEscapedMagic1);
|
|
|
|
match = 0;
|
|
|
|
consumed = i+1;
|
|
|
|
}
|
|
|
|
|
2018-04-22 09:23:08 +00:00
|
|
|
if(match == 0)
|
|
|
|
while(consumed <= i)
|
|
|
|
packet.push_back(p[consumed++]);
|
|
|
|
|
|
|
|
|
2018-04-22 22:32:43 +00:00
|
|
|
if(p[i] == magic2[match2])
|
|
|
|
match2++;
|
|
|
|
else if(p[i] == magic2[0])
|
|
|
|
match2 = 1;
|
2018-04-22 09:23:08 +00:00
|
|
|
else
|
2018-04-22 22:32:43 +00:00
|
|
|
match2 = 0;
|
|
|
|
|
|
|
|
if(match2 == 4)
|
2018-04-22 09:23:08 +00:00
|
|
|
{
|
|
|
|
match2 = 0;
|
2018-04-22 22:32:43 +00:00
|
|
|
packet.push_back(kEscapedMagic2);
|
2018-04-22 09:23:08 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-22 20:04:10 +00:00
|
|
|
while(consumed < i)
|
|
|
|
packet.push_back(p[consumed++]);
|
2018-04-22 09:23:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
packet.push_back(magic2[0]);
|
|
|
|
packet.push_back(magic2[1]);
|
|
|
|
packet.push_back(magic2[2]);
|
|
|
|
packet.push_back(magic2[3]);
|
|
|
|
packet.push_back(kEndOfPacket);
|
|
|
|
|
2018-04-22 20:04:10 +00:00
|
|
|
printf("sent packet: %d, total %d bytes\n", sentOutputPacket, (int)packet.size());
|
2018-04-23 23:42:36 +00:00
|
|
|
printf("sendOnePacket: %d - %d packets, next = %d\n", (int)packetsToSend.size(), (int)sentPackets.size(), sentOutputPacket + 1);
|
|
|
|
underlying().write(packet.data(), packet.size());
|
2018-04-22 09:23:08 +00:00
|
|
|
}
|
|
|
|
|
2018-04-22 22:32:43 +00:00
|
|
|
void ReliableStream::sendPackets()
|
|
|
|
{
|
2018-04-23 23:42:36 +00:00
|
|
|
printf("sendPackets: %d - %d packets, next = %d\n", (int)packetsToSend.size(), (int)sentPackets.size(), sentOutputPacket + 1);
|
2018-04-22 22:32:43 +00:00
|
|
|
while(!packetsToSend.empty() && sentPackets.size() < maxInFlight)
|
|
|
|
sendOnePacket();
|
|
|
|
}
|
2018-04-22 20:04:10 +00:00
|
|
|
|
2018-04-22 09:23:08 +00:00
|
|
|
void ReliableStream::flushWrite()
|
|
|
|
{
|
2018-04-22 22:32:43 +00:00
|
|
|
sendPackets();
|
2018-04-22 20:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReliableStream::write(const void* p, size_t n)
|
|
|
|
{
|
|
|
|
while(n)
|
|
|
|
{
|
|
|
|
size_t n1 = n > packetSize ? packetSize : n;
|
|
|
|
|
|
|
|
packetsToSend.emplace_back();
|
|
|
|
auto& packet = packetsToSend.back();
|
|
|
|
packet.reserve(n1 + 4);
|
|
|
|
packet.insert(packet.end(), (const uint8_t*)p, ((const uint8_t*)p)+n1);
|
|
|
|
uint32_t crc = crc32(0, packet.begin(), packet.end());
|
|
|
|
printf("outgoing crc: %x (bytes: %d without crc and header)\n", (unsigned) crc, (int)packet.size());
|
|
|
|
packet.push_back(crc >> 24);
|
|
|
|
packet.push_back(crc >> 16);
|
|
|
|
packet.push_back(crc >> 8);
|
|
|
|
packet.push_back(crc);
|
2018-04-22 09:23:08 +00:00
|
|
|
|
2018-04-22 20:04:10 +00:00
|
|
|
p = ((const uint8_t*)p) + n1;
|
|
|
|
n -= n1;
|
|
|
|
}
|
|
|
|
flushWrite();
|
2018-04-22 09:23:08 +00:00
|
|
|
}
|
2018-04-23 23:42:36 +00:00
|
|
|
//#include <MacTypes.h>
|
2018-04-22 09:23:08 +00:00
|
|
|
|
|
|
|
size_t ReliableStream::onReceive(const uint8_t* p, size_t n)
|
|
|
|
{
|
2018-04-22 23:11:56 +00:00
|
|
|
//printf("data available (%d) - state %d\n", (int)n, (int)state);
|
2018-04-22 09:23:08 +00:00
|
|
|
switch(state)
|
|
|
|
{
|
|
|
|
case State::waiting:
|
|
|
|
if( (n > 0 && p[0] != magic1[0])
|
|
|
|
|| (n > 1 && p[1] != magic1[1])
|
|
|
|
|| (n > 2 && p[2] != magic1[2])
|
|
|
|
|| (n > 3 && p[3] != magic1[3]) )
|
|
|
|
{
|
|
|
|
state = State::skipping;
|
2018-04-22 20:04:10 +00:00
|
|
|
printf("no magic\n");
|
2018-04-22 09:23:08 +00:00
|
|
|
nack();
|
|
|
|
gotNack(ackedOutputPacket);
|
2018-04-23 23:42:36 +00:00
|
|
|
|
|
|
|
if(p[0] != magic1[0])
|
|
|
|
return 0;
|
|
|
|
else if(p[1] != magic1[1])
|
|
|
|
return 1;
|
|
|
|
else if(p[2] != magic1[2])
|
|
|
|
return 2;
|
|
|
|
else
|
|
|
|
return 3;
|
2018-04-22 09:23:08 +00:00
|
|
|
}
|
|
|
|
if(n < 6)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if(p[5] != (uint8_t)~p[4])
|
|
|
|
{
|
|
|
|
state = State::skipping;
|
|
|
|
nack();
|
|
|
|
gotNack(ackedOutputPacket);
|
|
|
|
return 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(p[4])
|
|
|
|
{
|
|
|
|
case kAck:
|
|
|
|
if(n < 8)
|
|
|
|
return 0;
|
|
|
|
if(p[6] != (uint8_t)~p[7])
|
|
|
|
{
|
|
|
|
state = State::skipping;
|
|
|
|
nack();
|
|
|
|
gotNack(ackedOutputPacket);
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
gotAck(p[6]);
|
|
|
|
return 8;
|
|
|
|
case kNack:
|
|
|
|
if(n < 8)
|
|
|
|
return 0;
|
|
|
|
if(p[6] != (uint8_t)~p[7])
|
|
|
|
{
|
|
|
|
state = State::skipping;
|
|
|
|
nack();
|
|
|
|
gotNack(ackedOutputPacket);
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
gotNack(p[6]);
|
|
|
|
return 8;
|
|
|
|
case kDataPacket:
|
|
|
|
if(n < 8)
|
|
|
|
return 0;
|
|
|
|
if(p[6] != (uint8_t)~p[7])
|
|
|
|
{
|
|
|
|
state = State::skipping;
|
|
|
|
nack();
|
|
|
|
gotNack(ackedOutputPacket);
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
if(p[6] != ((receivedInputPacket + 1) & 0xFF))
|
|
|
|
{
|
2018-04-22 20:04:10 +00:00
|
|
|
printf("bad serial: %d %d\n", p[6], receivedInputPacket+1);
|
|
|
|
//nack();
|
2018-04-22 09:23:08 +00:00
|
|
|
state = State::skipping;
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
state = State::receiving;
|
2018-04-22 23:11:56 +00:00
|
|
|
inputMatchMagic1 = inputMatchMagic2 = 0;
|
2018-04-22 09:23:08 +00:00
|
|
|
return 8;
|
2018-04-23 23:42:36 +00:00
|
|
|
|
|
|
|
case kReset1:
|
|
|
|
reset(2);
|
|
|
|
notifyReset();
|
|
|
|
return 6;
|
|
|
|
|
|
|
|
case kReset2:
|
|
|
|
reset(0);
|
|
|
|
resetResponse = true;
|
|
|
|
notifyReset();
|
|
|
|
return 6;
|
|
|
|
|
2018-04-22 09:23:08 +00:00
|
|
|
default:
|
|
|
|
state = State::skipping;
|
|
|
|
nack();
|
|
|
|
gotNack(ackedOutputPacket);
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case State::skipping:
|
|
|
|
{
|
|
|
|
int match = 0;
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
if(p[i] == magic1[match++])
|
|
|
|
{
|
|
|
|
if(match == 4)
|
|
|
|
{
|
|
|
|
state = State::waiting;
|
|
|
|
return i-3;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
match = 0;
|
|
|
|
}
|
|
|
|
return i - match;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case State::receiving:
|
|
|
|
{
|
|
|
|
int i;
|
2018-04-22 22:32:43 +00:00
|
|
|
|
2018-04-22 09:23:08 +00:00
|
|
|
for(i = 0; i < n; i++)
|
|
|
|
{
|
2018-04-22 22:32:43 +00:00
|
|
|
incomingPacket.push_back(p[i]);
|
2018-04-22 09:23:08 +00:00
|
|
|
|
2018-04-22 23:11:56 +00:00
|
|
|
if(inputMatchMagic2 == 4)
|
2018-04-22 09:23:08 +00:00
|
|
|
{
|
2018-04-22 23:11:56 +00:00
|
|
|
inputMatchMagic2 = 0;
|
2018-04-22 22:32:43 +00:00
|
|
|
incomingPacket.pop_back();
|
2018-04-22 09:23:08 +00:00
|
|
|
switch(p[i])
|
|
|
|
{
|
|
|
|
case kEscapedMagic1:
|
2018-04-22 22:32:43 +00:00
|
|
|
std::copy(begin(magic1), end(magic1), incomingPacket.end()-4);
|
2018-04-22 09:23:08 +00:00
|
|
|
break;
|
|
|
|
case kEscapedMagic2:
|
|
|
|
break;
|
|
|
|
case kEndOfPacket:
|
2018-04-22 22:32:43 +00:00
|
|
|
incomingPacket.erase(incomingPacket.end()-4, incomingPacket.end());
|
2018-04-22 09:23:08 +00:00
|
|
|
processIncoming();
|
|
|
|
state = State::waiting;
|
|
|
|
return i + 1;
|
|
|
|
default:
|
|
|
|
state = State::waiting;
|
|
|
|
nack();
|
|
|
|
return i + 1;
|
|
|
|
}
|
|
|
|
}
|
2018-04-22 22:32:43 +00:00
|
|
|
|
2018-04-22 23:11:56 +00:00
|
|
|
if(p[i] != magic1[inputMatchMagic1])
|
|
|
|
inputMatchMagic1 = 0;
|
|
|
|
if(p[i] == magic1[inputMatchMagic1])
|
|
|
|
inputMatchMagic1++;
|
|
|
|
if(p[i] != magic2[inputMatchMagic2])
|
|
|
|
inputMatchMagic2 = 0;
|
|
|
|
if(p[i] == magic2[inputMatchMagic2])
|
|
|
|
inputMatchMagic2++;
|
2018-04-22 22:32:43 +00:00
|
|
|
|
2018-04-22 23:11:56 +00:00
|
|
|
if(inputMatchMagic1 == 4)
|
2018-04-22 22:32:43 +00:00
|
|
|
{
|
|
|
|
state = State::waiting;
|
|
|
|
nack();
|
|
|
|
return i-3;
|
2018-04-22 09:23:08 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-22 23:11:56 +00:00
|
|
|
return n;
|
2018-04-22 09:23:08 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|