@@ -53,67 +53,31 @@ void FlatBufferClient::readyRead()
5353 _timeoutTimer->start ();
5454 _receiveBuffer += _socket->readAll ();
5555
56- processNextMessage ();
57- }
58-
59- bool FlatBufferClient::processNextMessage ()
60- {
61- if (_processingMessage) { return false ; } // Avoid re-entrancy
62-
63- // Wait for at least 4 bytes to read the message size
64- if (_receiveBuffer.size () < 4 )
65- {
66- return false ;
67- }
68-
69- _processingMessage = true ;
70-
71- // Directly read message size (no memcpy)
72- const uint8_t * raw = reinterpret_cast <const uint8_t *>(_receiveBuffer.constData ());
73- uint32_t const messageSize = (raw[0 ] << 24 ) | (raw[1 ] << 16 ) | (raw[2 ] << 8 ) | raw[3 ];
74-
75- // // Validate message size
76- if (messageSize == 0 )
56+ // check if we can read a header
57+ while (_receiveBuffer.size () >= 4 )
7758 {
78- Warning (_log, " Invalid message size: %u - dropping received data" , messageSize);
79- _receiveBuffer.clear ();
80- _processingMessage = false ;
81- return true ;
82- }
59+ // Directly read message size
60+ const uint8_t * raw = reinterpret_cast <const uint8_t *>(_receiveBuffer.constData ());
61+ uint32_t const messageSize = (raw[0 ] << 24 ) | (raw[1 ] << 16 ) | (raw[2 ] << 8 ) | raw[3 ];
8362
84- // Wait for full message
85- if (_receiveBuffer.size () < static_cast <int >(messageSize + 4 ))
86- {
87- _processingMessage = false ;
88- return false ;
89- }
63+ // check if we can read a complete message
64+ if ((uint32_t ) _receiveBuffer.size () < messageSize + 4 ) { return ; }
9065
91- // Remove the processed message from the buffer (header + body)
92- _receiveBuffer.remove (0 , messageSize + 4 );
66+ // extract message without header and remove header + msg from buffer :: QByteArray::remove() does not return the removed data
67+ const uint8_t * msgData = reinterpret_cast <const uint8_t *>(_receiveBuffer.constData () + 4 );
68+ _receiveBuffer.remove (0 , messageSize + 4 );
9369
94- // Extract the message and remove it from the buffer (no copying)
95- const uint8_t * msgData = reinterpret_cast <const uint8_t *>(_receiveBuffer.constData () + 4 );
96- flatbuffers::Verifier verifier (msgData, messageSize);
70+ flatbuffers::Verifier verifier (msgData, messageSize);
9771
98- if (!hyperionnet::VerifyRequestBuffer (verifier)) {
99- Error (_log, " Invalid FlatBuffer message received" );
100- sendErrorReply (" Invalid FlatBuffer message received" );
101- _processingMessage = false ;
72+ if (!hyperionnet::VerifyRequestBuffer (verifier)) {
73+ Error (_log, " Invalid FlatBuffer message received" );
74+ sendErrorReply (" Invalid FlatBuffer message received" );
75+ continue ;
76+ }
10277
103- QMetaObject::invokeMethod ( this , &FlatBufferClient::processNextMessage, Qt::QueuedConnection );
104- return true ;
78+ const auto *message = hyperionnet::GetRequest (msgData );
79+ handleMessage (message) ;
10580 }
106-
107- // Invoke message handling
108- QMetaObject::invokeMethod (this , [this , msgData]() {
109- handleMessage (hyperionnet::GetRequest (msgData));
110- _processingMessage = false ;
111-
112- // Continue processing the next message
113- QMetaObject::invokeMethod (this , &FlatBufferClient::processNextMessage, Qt::QueuedConnection);
114- });
115-
116- return true ;
11781}
11882
11983void FlatBufferClient::noDataReceived ()
@@ -213,8 +177,8 @@ void FlatBufferClient::handleImageCommand(const hyperionnet::Image *image)
213177 const auto * img = static_cast <const hyperionnet::RawImage*>(image->data_as_RawImage ());
214178
215179 // Read image properties directly from FlatBuffer
216- const int width = img->width ();
217- const int height = img->height ();
180+ int32_t const width = img->width ();
181+ int32_t const height = img->height ();
218182 const auto * data = img->data ();
219183
220184 if (width <= 0 || height <= 0 || data == nullptr || data->size () == 0 )
@@ -224,8 +188,8 @@ void FlatBufferClient::handleImageCommand(const hyperionnet::Image *image)
224188 }
225189
226190 // Check consistency of image data size
227- const int dataSize = data->size ();
228- const int bytesPerPixel = dataSize / (width * height);
191+ auto dataSize = data->size ();
192+ int const bytesPerPixel = dataSize / (width * height);
229193 if (bytesPerPixel != 3 && bytesPerPixel != 4 )
230194 {
231195 sendErrorReply (" Size of image data does not match with the width and height" );
@@ -244,9 +208,9 @@ void FlatBufferClient::handleImageCommand(const hyperionnet::Image *image)
244208 {
245209 const auto * img = static_cast <const hyperionnet::NV12Image*>(image->data_as_NV12Image ());
246210
247- const int width = img->width ();
248- const int height = img->height ();
249- const auto * data_y = img->data_y ();
211+ int32_t const width = img->width ();
212+ int32_t const height = img->height ();
213+ const auto * const data_y = img->data_y ();
250214 const auto * data_uv = img->data_uv ();
251215
252216 if (width <= 0 || height <= 0 || data_y == nullptr || data_uv == nullptr ||
@@ -257,10 +221,10 @@ void FlatBufferClient::handleImageCommand(const hyperionnet::Image *image)
257221 }
258222
259223 // Combine Y and UV into one contiguous buffer (reuse class member buffer)
260- const size_t y_size = data_y->size ();
261- const size_t uv_size = data_uv->size ();
224+ size_t const y_size = data_y->size ();
225+ size_t const uv_size = data_uv->size ();
262226
263- size_t required_size = y_size + uv_size;
227+ size_t const required_size = y_size + uv_size;
264228 if (_combinedNv12Buffer.capacity () < required_size)
265229 {
266230 _combinedNv12Buffer.reserve (required_size);
@@ -269,7 +233,7 @@ void FlatBufferClient::handleImageCommand(const hyperionnet::Image *image)
269233 std::memcpy (_combinedNv12Buffer.data () + y_size, data_uv->data (), uv_size);
270234
271235 // Determine stride for Y
272- const int stride_y = img->stride_y () > 0 ? img->stride_y () : width;
236+ int32_t const stride_y = img->stride_y () > 0 ? img->stride_y () : width;
273237
274238 // Resize only when needed
275239 if (_imageOutputBuffer.width () != width || _imageOutputBuffer.height () != height)
@@ -326,7 +290,7 @@ void FlatBufferClient::sendMessage(const uint8_t* data, size_t size)
326290
327291 // write message
328292 _socket->write (reinterpret_cast <const char *>(header), sizeof (header));
329- _socket->write (reinterpret_cast <const char *>(data), size);
293+ _socket->write (reinterpret_cast <const char *>(data), static_cast <qint64>( size) );
330294 _socket->flush ();
331295}
332296
@@ -349,13 +313,13 @@ void FlatBufferClient::sendErrorReply(const QString& error)
349313}
350314
351315inline void FlatBufferClient::processRawImage (const uint8_t * buffer,
352- int width,
353- int height,
316+ int32_t width,
317+ int32_t height,
354318 int bytesPerPixel,
355- ImageResampler& resampler,
319+ const ImageResampler& resampler,
356320 Image<ColorRgb>& outputImage)
357321{
358- int const lineLength = width * bytesPerPixel;
322+ const size_t lineLength = static_cast < size_t >( width) * bytesPerPixel;
359323 PixelFormat const pixelFormat = (bytesPerPixel == 4 ) ? PixelFormat::RGB32 : PixelFormat::RGB24;
360324
361325 resampler.processImage (
@@ -369,13 +333,13 @@ inline void FlatBufferClient::processRawImage(const uint8_t* buffer,
369333}
370334
371335inline void FlatBufferClient::processNV12Image (const uint8_t * nv12_data,
372- int width,
373- int height,
374- int stride_y,
375- ImageResampler& resampler,
336+ int32_t width,
337+ int32_t height,
338+ int32_t stride_y,
339+ const ImageResampler& resampler,
376340 Image<ColorRgb>& outputImage)
377341{
378- PixelFormat pixelFormat = PixelFormat::NV12;
342+ PixelFormat const pixelFormat = PixelFormat::NV12;
379343
380344 resampler.processImage (
381345 nv12_data, // Combined NV12 buffer
0 commit comments