1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-23 21:01:25 +01:00

move ringClient into RingStream

This commit is contained in:
Harald Barth 2022-08-16 09:38:40 +02:00
parent ff28dbd561
commit 162e1f9d3e
5 changed files with 29 additions and 19 deletions

View File

@ -47,7 +47,7 @@
#ifdef CD_HANDLE_RING #ifdef CD_HANDLE_RING
// wifi or ethernet ring streams with multiple client types // wifi or ethernet ring streams with multiple client types
RingStream * CommandDistributor::ring=0; RingStream * CommandDistributor::ring=0;
byte CommandDistributor::ringClient=NO_CLIENT; // byte CommandDistributor::ringClient=NO_CLIENT;
CommandDistributor::clientType CommandDistributor::clients[8]={ CommandDistributor::clientType CommandDistributor::clients[8]={
NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE}; 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) if (Diag::WIFI && Diag::CMD)
DIAG(F("Parse C=%d T=%d B=%s"),clientId, clients[clientId], buffer); DIAG(F("Parse C=%d T=%d B=%s"),clientId, clients[clientId], buffer);
ring=stream; ring=stream;
ringClient=stream->peekTargetMark(); // ringClient=stream->peekTargetMark();
// First check if the client is not known // First check if the client is not known
// yet and in that case determinine type // 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) else if (clients[clientId] == WITHROTTLE_TYPE)
WiThrottle::getThrottle(clientId)->parse(ring, buffer); WiThrottle::getThrottle(clientId)->parse(ring, buffer);
ringClient=NO_CLIENT; // ringClient=NO_CLIENT;
} }
void CommandDistributor::forget(byte clientId) { void CommandDistributor::forget(byte clientId) {
@ -90,6 +90,8 @@ void CommandDistributor::forget(byte clientId) {
// This will not be called on a uno // This will not be called on a uno
void CommandDistributor::broadcastToClients(clientType type) { void CommandDistributor::broadcastToClients(clientType type) {
byte rememberClient;
/* Boadcast to Serials */ /* Boadcast to Serials */
if (type==COMMAND_TYPE) SerialManager::broadcast(broadcastBufferWriter->getString()); 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 // 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 // before merging broadcasts in the ring, then reinstate it in case
// the process continues to output to its client. // the process continues to output to its client.
if (ringClient!=NO_CLIENT) { if (ring) {
DIAG(F("CD precommit client %d"), ringClient); if ((rememberClient = ring->peekTargetMark()) != NO_CLIENT) {
DIAG(F("CD precommit client %d"), rememberClient);
ring->commit(); ring->commit();
} }
/* loop through ring clients */ /* loop through ring clients */
@ -111,11 +114,11 @@ void CommandDistributor::broadcastToClients(clientType type) {
ring->commit(); ring->commit();
} }
} }
if (ringClient!=NO_CLIENT) { if (ring->peekTargetMark()!=NO_CLIENT) {
DIAG(F("CD postmark client %d"), ringClient); DIAG(F("CD postmark client %d"), rememberClient);
ring->mark(ringClient); ring->mark(rememberClient);
}
} }
#endif #endif
} }

View File

@ -47,8 +47,8 @@ private:
static StringBuffer * broadcastBufferWriter; static StringBuffer * broadcastBufferWriter;
#ifdef CD_HANDLE_RING #ifdef CD_HANDLE_RING
static RingStream * ring; static RingStream * ring;
static const byte NO_CLIENT=255; static const byte NO_CLIENT=255; //XXX remove later
static byte ringClient; // static byte ringClient;
static clientType clients[8]; static clientType clients[8];
#endif #endif
}; };

View File

@ -143,7 +143,8 @@ int RingStream::freeSpace() {
// mark start of message with client id (0...9) // mark start of message with client id (0...9)
void RingStream::mark(uint8_t b) { 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; _mark=_pos_write;
write(b); // client id write(b); // client id
write((uint8_t)0); // count MSB placemarker 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 // peekTargetMark is used by the parser stash routines to know which client
// to send a callback response to some time later. // to send a callback response to some time later.
uint8_t RingStream::peekTargetMark() { uint8_t RingStream::peekTargetMark() {
return _buffer[_mark]; return _ringClient;
} }
void RingStream::info() { void RingStream::info() {
@ -171,9 +172,10 @@ bool RingStream::commit() {
return false; // commit failed return false; // commit failed
} }
if (_count==0) { 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 // ignore empty response
_pos_write=_mark; _pos_write=_mark;
_ringClient = NO_CLIENT; //XXX make else clause later
return true; // true=commit ok return true; // true=commit ok
} }
// Go back to the _mark and inject the count 1 byte later // Go back to the _mark and inject the count 1 byte later
@ -184,10 +186,11 @@ bool RingStream::commit() {
if (_mark==_len) _mark=0; if (_mark==_len) _mark=0;
_buffer[_mark]=lowByte(_count); _buffer[_mark]=lowByte(_count);
{ char s[_count+2]; { char s[_count+2];
strncpy(s, (const char*)&(_buffer[_mark+1]), _count-2); strncpy(s, (const char*)&(_buffer[_mark+1]), _count);
s[_count-1]=0; s[_count]=0;
DIAG(F("RS commit count=%d %s"), _count, s); DIAG(F("RS commit count=%d core %d \"%s\""), _count, xPortGetCoreID(), s);
} }
_ringClient = NO_CLIENT;
return true; // commit worked return true; // commit worked
} }
void RingStream::flush() { void RingStream::flush() {
@ -195,6 +198,6 @@ void RingStream::flush() {
_pos_read=0; _pos_read=0;
_buffer[0]=0; _buffer[0]=0;
_flashInsert=NULL; // prepared for first read _flashInsert=NULL; // prepared for first read
_ringClient = NO_CLIENT;
} }

View File

@ -61,6 +61,8 @@ class RingStream : public Print {
int _count; int _count;
byte * _buffer; byte * _buffer;
char * _flashInsert; char * _flashInsert;
static const byte NO_CLIENT=255; // must be same as in CommandDistributor
byte _ringClient = NO_CLIENT;
}; };
#endif #endif

View File

@ -95,6 +95,7 @@ public:
static std::vector<NetworkClient> clients; // a list to hold all clients static std::vector<NetworkClient> clients; // a list to hold all clients
static WiFiServer *server = NULL; static WiFiServer *server = NULL;
static RingStream *outboundRing = new RingStream(2048); static RingStream *outboundRing = new RingStream(2048);
//static RingStream *eventRing = new RingStream(2048);
static bool APmode = false; static bool APmode = false;
void wifiLoop(void *){ void wifiLoop(void *){
@ -260,7 +261,8 @@ void WifiESP::loop() {
cmd[len]=0; cmd[len]=0;
outboundRing->mark(clientId); outboundRing->mark(clientId);
CommandDistributor::parse(clientId,cmd,outboundRing); CommandDistributor::parse(clientId,cmd,outboundRing);
outboundRing->commit(); if (outboundRing->peekTargetMark()!=255) //XXX fix 255 later
outboundRing->commit();
} }
} }
} // all clients } // all clients