Skip to content

Commit

Permalink
qt6: Add qt6 support to mythwebsockets.
Browse files Browse the repository at this point in the history
Add code to use the qt6 QStringDecoder object instead of the obsoleted
qt5 QTextCodec object when compiling MythTV against the Qt6 libs.
  • Loading branch information
linuxdude42 committed May 14, 2023
1 parent 17247e9 commit 1bcfda7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
37 changes: 35 additions & 2 deletions mythtv/libs/libmythbase/http/mythwebsocket.cpp
Expand Up @@ -62,7 +62,11 @@ MythWebSocket::MythWebSocket(bool Server, QTcpSocket *Socket, MythSocketProtocol

MythWebSocket::~MythWebSocket()
{
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
delete m_utf8CodecState;
#else
delete m_toUtf16;
#endif
delete m_timer;
}

Expand Down Expand Up @@ -298,16 +302,28 @@ void MythWebSocket::Read()
if (m_preferRawText)
{
m_dataFragments.emplace_back(payload);
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
(void)m_utf8Codec->toUnicode(payload->data(), payload->size(), m_utf8CodecState);
#endif
}
else
{
if (!m_string)
m_string = MythSharedString::CreateString();
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
(*m_string).append(m_utf8Codec->toUnicode(payload->data(), payload->size(), m_utf8CodecState));
#else
(*m_string).append(m_toUtf16->decode(*payload));
#endif
}

if (m_utf8CodecState->invalidChars)
if (
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
m_utf8CodecState->invalidChars
#else
m_toUtf16->hasError()
#endif
)
{
LOG(VB_HTTP, LOG_ERR, LOC + "Invalid UTF-8");
SendClose(WSCloseBadData, "Invalid UTF-8");
Expand Down Expand Up @@ -358,14 +374,25 @@ void MythWebSocket::Read()
// Final UTF-8 validation
if ((WSOpTextFrame == messageopcode))
{
if (m_utf8CodecState->remainingChars)
if (
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
m_utf8CodecState->remainingChars
#else
m_toUtf16->hasError()
#endif
)
{
LOG(VB_HTTP, LOG_ERR, LOC + "Invalid UTF-8");
SendClose(WSCloseBadData, "Invalid UTF-8");
valid = false;
}
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
delete m_utf8CodecState;
m_utf8CodecState = new QTextCodec::ConverterState;
#else
delete m_toUtf16;
m_toUtf16 = new QStringDecoder;
#endif
}

// Echo back to the Autobahn server
Expand Down Expand Up @@ -516,10 +543,16 @@ void MythWebSocket::CloseReceived(const DataPayload& Payload)
if ((*Payload).size() > 2)
{
auto utf8 = (*Payload).mid(2);
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
QTextCodec::ConverterState state;
(void)m_utf8Codec->toUnicode(utf8.constData(), utf8.size(), &state);
if (state.invalidChars || state.remainingChars)
close = WSCloseProtocolError;
#else
(void)m_toUtf16->decode(utf8);
if(m_toUtf16->hasError())
close = WSCloseProtocolError;
#endif
}

if (WSCloseNormal == close)
Expand Down
9 changes: 9 additions & 0 deletions mythtv/libs/libmythbase/http/mythwebsocket.h
Expand Up @@ -4,7 +4,12 @@
// Qt
#include <QTimer>
#include <QObject>

#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
#include <QTextCodec>
#else
#include <QStringDecoder>
#endif
#include <QElapsedTimer>

// MythTV
Expand Down Expand Up @@ -71,8 +76,12 @@ class MythWebSocket : public QObject
int64_t m_messageSize { 0 };
DataPayloads m_dataFragments;
StringPayload m_string { nullptr };
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
QTextCodec* m_utf8Codec { QTextCodec::codecForName("UTF-8") };
QTextCodec::ConverterState* m_utf8CodecState { new QTextCodec::ConverterState };
#else
QStringDecoder *m_toUtf16 { new QStringDecoder };
#endif

QByteArray m_lastPingPayload;
WSQueue m_writeQueue;
Expand Down

0 comments on commit 1bcfda7

Please sign in to comment.