diff --git a/CommandDistributor.cpp b/CommandDistributor.cpp index 94be666..2068dc6 100644 --- a/CommandDistributor.cpp +++ b/CommandDistributor.cpp @@ -47,7 +47,7 @@ #ifdef CD_HANDLE_RING // wifi or ethernet ring streams with multiple client types RingStream * CommandDistributor::ring=0; - byte CommandDistributor::ringClient=NO_CLIENT; +// byte CommandDistributor::ringClient=NO_CLIENT; CommandDistributor::clientType CommandDistributor::clients[8]={ NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE}; @@ -57,7 +57,7 @@ void CommandDistributor::parse(byte clientId,byte * buffer, RingStream * stream if (Diag::WIFI && Diag::CMD) DIAG(F("Parse C=%d T=%d B=%s"),clientId, clients[clientId], buffer); ring=stream; - ringClient=stream->peekTargetMark(); + // ringClient=stream->peekTargetMark(); // First check if the client is not known // yet and in that case determinine type @@ -78,7 +78,7 @@ void CommandDistributor::parse(byte clientId,byte * buffer, RingStream * stream else if (clients[clientId] == WITHROTTLE_TYPE) WiThrottle::getThrottle(clientId)->parse(ring, buffer); - ringClient=NO_CLIENT; + // ringClient=NO_CLIENT; } void CommandDistributor::forget(byte clientId) { @@ -90,6 +90,8 @@ void CommandDistributor::forget(byte clientId) { // This will not be called on a uno void CommandDistributor::broadcastToClients(clientType type) { + byte rememberClient; + /* Boadcast to Serials */ if (type==COMMAND_TYPE) SerialManager::broadcast(broadcastBufferWriter->getString()); @@ -97,8 +99,9 @@ void CommandDistributor::broadcastToClients(clientType type) { // If we are broadcasting from a wifi/eth process we need to complete its output // before merging broadcasts in the ring, then reinstate it in case // the process continues to output to its client. - if (ringClient!=NO_CLIENT) { - DIAG(F("CD precommit client %d"), ringClient); + if (ring) { + if ((rememberClient = ring->peekTargetMark()) != NO_CLIENT) { + DIAG(F("CD precommit client %d"), rememberClient); ring->commit(); } /* loop through ring clients */ @@ -111,11 +114,11 @@ void CommandDistributor::broadcastToClients(clientType type) { ring->commit(); } } - if (ringClient!=NO_CLIENT) { - DIAG(F("CD postmark client %d"), ringClient); - ring->mark(ringClient); + if (ring->peekTargetMark()!=NO_CLIENT) { + DIAG(F("CD postmark client %d"), rememberClient); + ring->mark(rememberClient); + } } - #endif } diff --git a/CommandDistributor.h b/CommandDistributor.h index 7da3c8f..c5b6ad7 100644 --- a/CommandDistributor.h +++ b/CommandDistributor.h @@ -47,8 +47,8 @@ private: static StringBuffer * broadcastBufferWriter; #ifdef CD_HANDLE_RING static RingStream * ring; - static const byte NO_CLIENT=255; - static byte ringClient; + static const byte NO_CLIENT=255; //XXX remove later + // static byte ringClient; static clientType clients[8]; #endif }; diff --git a/RingStream.cpp b/RingStream.cpp index 4c7176e..fcf6ed7 100644 --- a/RingStream.cpp +++ b/RingStream.cpp @@ -143,7 +143,8 @@ int RingStream::freeSpace() { // mark start of message with client id (0...9) void RingStream::mark(uint8_t b) { - DIAG(F("RS mark client %d at %d"), b, _pos_write); + DIAG(F("RS mark client %d at %d core %d"), b, _pos_write, xPortGetCoreID()); + _ringClient = b; _mark=_pos_write; write(b); // client id write((uint8_t)0); // count MSB placemarker @@ -154,7 +155,7 @@ void RingStream::mark(uint8_t b) { // peekTargetMark is used by the parser stash routines to know which client // to send a callback response to some time later. uint8_t RingStream::peekTargetMark() { - return _buffer[_mark]; + return _ringClient; } void RingStream::info() { @@ -171,9 +172,10 @@ bool RingStream::commit() { return false; // commit failed } if (_count==0) { - DIAG(F("RS commit count=0 rewind back to %d"), _mark); + DIAG(F("RS commit count=0 rewind back to %d core %d"), _mark, xPortGetCoreID()); // ignore empty response _pos_write=_mark; + _ringClient = NO_CLIENT; //XXX make else clause later return true; // true=commit ok } // Go back to the _mark and inject the count 1 byte later @@ -184,10 +186,11 @@ bool RingStream::commit() { if (_mark==_len) _mark=0; _buffer[_mark]=lowByte(_count); { char s[_count+2]; - strncpy(s, (const char*)&(_buffer[_mark+1]), _count-2); - s[_count-1]=0; - DIAG(F("RS commit count=%d %s"), _count, s); + strncpy(s, (const char*)&(_buffer[_mark+1]), _count); + s[_count]=0; + DIAG(F("RS commit count=%d core %d \"%s\""), _count, xPortGetCoreID(), s); } + _ringClient = NO_CLIENT; return true; // commit worked } void RingStream::flush() { @@ -195,6 +198,6 @@ void RingStream::flush() { _pos_read=0; _buffer[0]=0; _flashInsert=NULL; // prepared for first read - + _ringClient = NO_CLIENT; } diff --git a/RingStream.h b/RingStream.h index cfc82eb..a3aad49 100644 --- a/RingStream.h +++ b/RingStream.h @@ -61,6 +61,8 @@ class RingStream : public Print { int _count; byte * _buffer; char * _flashInsert; + static const byte NO_CLIENT=255; // must be same as in CommandDistributor + byte _ringClient = NO_CLIENT; }; #endif diff --git a/WifiESP32.cpp b/WifiESP32.cpp index a91372f..164374f 100644 --- a/WifiESP32.cpp +++ b/WifiESP32.cpp @@ -95,6 +95,7 @@ public: static std::vector clients; // a list to hold all clients static WiFiServer *server = NULL; static RingStream *outboundRing = new RingStream(2048); +//static RingStream *eventRing = new RingStream(2048); static bool APmode = false; void wifiLoop(void *){ @@ -260,7 +261,8 @@ void WifiESP::loop() { cmd[len]=0; outboundRing->mark(clientId); CommandDistributor::parse(clientId,cmd,outboundRing); - outboundRing->commit(); + if (outboundRing->peekTargetMark()!=255) //XXX fix 255 later + outboundRing->commit(); } } } // all clients