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