1515namespace {
1616const int FLATBUFFER_PRIORITY_MIN = 100 ;
1717const int FLATBUFFER_PRIORITY_MAX = 199 ;
18- const int FLATBUFFER_MAX_MSG_LENGTH = 10'000'000 ;
18+
1919} // End of constants
2020
2121FlatBufferClient::FlatBufferClient (QTcpSocket* socket, int timeout, QObject *parent)
@@ -50,33 +50,33 @@ void FlatBufferClient::readyRead()
5050{
5151 if (_socket == nullptr ) { return ; }
5252
53- while (_socket->bytesAvailable () > 0 )
54- {
55- _timeoutTimer->start ();
56- _receiveBuffer += _socket->readAll ();
57- processNextMessage ();
58- }
53+ _timeoutTimer->start ();
54+ _receiveBuffer += _socket->readAll ();
55+
56+ processNextMessage ();
5957}
6058
61- bool FlatBufferClient::processNextMessageInline ()
59+ bool FlatBufferClient::processNextMessage ()
6260{
6361 if (_processingMessage) { return false ; } // Avoid re-entrancy
6462
6563 // Wait for at least 4 bytes to read the message size
66- if (_receiveBuffer.size () < 4 ) {
64+ if (_receiveBuffer.size () < 4 )
65+ {
6766 return false ;
6867 }
6968
7069 _processingMessage = true ;
7170
7271 // Directly read message size (no memcpy)
7372 const uint8_t * raw = reinterpret_cast <const uint8_t *>(_receiveBuffer.constData ());
74- uint32_t messageSize = (raw[0 ] << 24 ) | (raw[1 ] << 16 ) | (raw[2 ] << 8 ) | raw[3 ];
73+ uint32_t const messageSize = (raw[0 ] << 24 ) | (raw[1 ] << 16 ) | (raw[2 ] << 8 ) | raw[3 ];
7574
76- // Validate message size
77- if (messageSize == 0 || messageSize > FLATBUFFER_MAX_MSG_LENGTH )
75+ // // Validate message size
76+ if (messageSize == 0 )
7877 {
79- Warning (_log, " Invalid message size: %d - dropping received data" , messageSize);
78+ Warning (_log, " Invalid message size: %u - dropping received data" , messageSize);
79+ _receiveBuffer.clear ();
8080 _processingMessage = false ;
8181 return true ;
8282 }
@@ -88,6 +88,9 @@ bool FlatBufferClient::processNextMessageInline()
8888 return false ;
8989 }
9090
91+ // Remove the processed message from the buffer (header + body)
92+ _receiveBuffer.remove (0 , messageSize + 4 );
93+
9194 // Extract the message and remove it from the buffer (no copying)
9295 const uint8_t * msgData = reinterpret_cast <const uint8_t *>(_receiveBuffer.constData () + 4 );
9396 flatbuffers::Verifier verifier (msgData, messageSize);
@@ -97,34 +100,22 @@ bool FlatBufferClient::processNextMessageInline()
97100 sendErrorReply (" Invalid FlatBuffer message received" );
98101 _processingMessage = false ;
99102
100- // Clear the buffer in case of an invalid message
101- _receiveBuffer.clear ();
103+ QMetaObject::invokeMethod (this , &FlatBufferClient::processNextMessage, Qt::QueuedConnection);
102104 return true ;
103105 }
104106
105107 // Invoke message handling
106- QMetaObject::invokeMethod (this , [this , msgData, messageSize ]() {
108+ QMetaObject::invokeMethod (this , [this , msgData]() {
107109 handleMessage (hyperionnet::GetRequest (msgData));
108110 _processingMessage = false ;
109111
110- // Remove the processed message from the buffer (header + body)
111- _receiveBuffer.remove (0 , messageSize + 4 ); // Clear the processed message + header
112-
113112 // Continue processing the next message
114- processNextMessage ( );
113+ QMetaObject::invokeMethod ( this , &FlatBufferClient::processNextMessage, Qt::QueuedConnection );
115114 });
116115
117116 return true ;
118117}
119118
120- void FlatBufferClient::processNextMessage ()
121- {
122- // Run the message processing inline until the buffer is empty or we can't process further
123- while (processNextMessageInline ()) {
124- // Keep processing as long as we can
125- }
126- }
127-
128119void FlatBufferClient::noDataReceived ()
129120{
130121 Error (_log," No data received for %dms - drop connection with client \" %s\" " ,_timeout, QSTRING_CSTR (QString (" %1@%2" ).arg (_origin, _clientAddress)));
0 commit comments