1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-26 17:46:14 +01:00

make sending loop ringbuffer to ESP32 Wifi more simple

This commit is contained in:
Harald Barth 2022-08-09 15:25:29 +02:00
parent 76c5608181
commit ecda69ba32

View File

@ -270,45 +270,29 @@ void WifiESP::loop() {
// something to write out? // something to write out?
clientId=outboundRing->read(); clientId=outboundRing->read();
if (clientId >= 0) { if (clientId >= 0) {
if ((unsigned int)clientId > clients.size()) { // We have data to send in outboundRing
// something is wrong with the ringbuffer position // and we have a valid clientId.
// or client has disconnected // First read it out to buffer
outboundRing->info(); // and then look if it can be sent because
if ((unsigned int)clientId < 8) { // we can not leave it in the ring for ever
// try to recover by reading out to nowhere int count=outboundRing->count();
int count=outboundRing->count(); {
for(int i=0;i<count;i++) { char buffer[count+1]; // one extra for '\0'
int c = outboundRing->read(); for(int i=0;i<count;i++) {
if (c < 0) { int c = outboundRing->read();
DIAG(F("Ringread fail in discarding data for client %d at pos %d"),clientId, i); if (c >= 0) // Panic check, should never be false
break; buffer[i] = (char)c;
} else {
DIAG(F("Ringread fail at %d"),i);
break;
} }
outboundRing->info();
} else {
DIAG(F("No clientId where expected: Ring beyond rescue"));
} }
} else { // buffer filled, end with '\0' so we can use it as C string
// We have data to send in outboundRing buffer[count]='\0';
// and we have a valid clientId. if((unsigned int)clientId <= clients.size() && clients[clientId].ok()) {
// First read it out to buffer clients[clientId].wifi.write(buffer,count);
// and then look if it can be sent because } else {
// we can not leave it in the ring for ever DIAG(F("Unsent(%d): %s"), clientId, buffer);
int count=outboundRing->count();
{
char buffer[count+1];
for(int i=0;i<count;i++) {
int c = outboundRing->read();
if (c >= 0)
buffer[i] = (char)c;
else {
DIAG(F("Ringread fail at %d"),i);
break;
}
}
buffer[count]=0;
if(clients[clientId].ok())
clients[clientId].wifi.write(buffer,count);
} }
} }
} }