Skip to content

Commit

Permalink
Fix uncaught exception in FrameReader
Browse files Browse the repository at this point in the history
  • Loading branch information
Fanda Vacek committed Oct 22, 2024
1 parent ee0753f commit a822a05
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
19 changes: 13 additions & 6 deletions libshviotqt/src/rpc/localsocket.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <shv/iotqt/rpc/localsocket.h>

#include "shv/chainpack/utils.h"
#include <shv/iotqt/rpc/serialportsocket.h>

#include <QLocalSocket>
Expand Down Expand Up @@ -142,13 +143,19 @@ void LocalSocket::ignoreSslErrors()
void LocalSocket::onDataReadyRead()
{
auto ba = m_socket->readAll();
std::string_view escaped_data(ba.constData(), ba.size());
for (auto rqid : m_frameReader->addData(escaped_data)) {
emit responseMetaReceived(rqid);
try {
std::string_view escaped_data(ba.constData(), ba.size());
for (auto rqid : m_frameReader->addData(escaped_data)) {
emit responseMetaReceived(rqid);
}
emit dataChunkReceived();
if (!m_frameReader->isEmpty()) {
emit readyRead();
}
}
emit dataChunkReceived();
if (!m_frameReader->isEmpty()) {
emit readyRead();
catch (const std::exception &e) {
shvWarning() << "Corrupted meta data received:\n" << shv::chainpack::utils::hexDump(std::string_view(ba.constData(), std::min(ba.size(), static_cast<qsizetype>(64))));
emit error(QAbstractSocket::SocketError::UnknownSocketError);
}
}

Expand Down
18 changes: 12 additions & 6 deletions libshviotqt/src/rpc/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,19 @@ void TcpSocket::ignoreSslErrors()
void TcpSocket::onDataReadyRead()
{
auto ba = m_socket->readAll();
std::string_view data(ba.constData(), ba.size());
for (auto rqid : m_frameReader->addData(data)) {
emit responseMetaReceived(rqid);
try {
std::string_view data(ba.constData(), ba.size());
for (auto rqid : m_frameReader->addData(data)) {
emit responseMetaReceived(rqid);
}
emit dataChunkReceived();
if (!m_frameReader->isEmpty()) {
emit readyRead();
}
}
emit dataChunkReceived();
if (!m_frameReader->isEmpty()) {
emit readyRead();
catch (const std::exception &e) {
shvWarning() << "Corrupted meta data received:\n" << shv::chainpack::utils::hexDump(std::string_view(ba.constData(), std::min(ba.size(), static_cast<qsizetype>(64))));
emit error(QAbstractSocket::SocketError::UnknownSocketError);
}
}

Expand Down
37 changes: 24 additions & 13 deletions libshviotqt/src/rpc/websocket.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <shv/iotqt/rpc/websocket.h>

#include <shv/chainpack/utils.h>
#include <shv/coreqt/log.h>

#include <QWebSocket>
Expand Down Expand Up @@ -82,26 +83,36 @@ void WebSocket::flushWriteBuffer()

void WebSocket::onTextMessageReceived(const QString &message)
{
shvDebug() << "text message received:" << message;
auto ba = message.toUtf8();
for (auto rqid : m_frameReader->addData(std::string_view(ba.constData(), ba.size()))) {
emit responseMetaReceived(rqid);
try {
shvDebug() << "text message received:" << message;
auto ba = message.toUtf8();
for (auto rqid : m_frameReader->addData(std::string_view(ba.constData(), ba.size()))) {
emit responseMetaReceived(rqid);
}
emit dataChunkReceived();
if (!m_frameReader->isEmpty()) {
emit readyRead();
}
}
emit dataChunkReceived();
if (!m_frameReader->isEmpty()) {
emit readyRead();
catch (const std::exception &e) {
shvWarning() << "Corrupted meta data received:\n" << shv::chainpack::utils::hexDump(std::string_view(message.constData(), std::min(message.size(), static_cast<qsizetype>(64))));
}
}

void WebSocket::onBinaryMessageReceived(const QByteArray &message)
{
shvDebug() << "binary message received:" << message;
for (auto rqid : m_frameReader->addData(std::string_view(message.constData(), message.size()))) {
emit responseMetaReceived(rqid);
try {
shvDebug() << "binary message received:" << message;
for (auto rqid : m_frameReader->addData(std::string_view(message.constData(), message.size()))) {
emit responseMetaReceived(rqid);
}
emit dataChunkReceived();
if (!m_frameReader->isEmpty()) {
emit readyRead();
}
}
emit dataChunkReceived();
if (!m_frameReader->isEmpty()) {
emit readyRead();
catch (const std::exception &e) {
shvWarning() << "Corrupted meta data received:\n" << shv::chainpack::utils::hexDump(std::string_view(message.constData(), std::min(message.size(), static_cast<qsizetype>(64))));
}
}

Expand Down

0 comments on commit a822a05

Please sign in to comment.