1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-11 00:24:12 +02:00

Pimple Msg

This commit is contained in:
Uwe L. Korn
2013-06-16 21:13:59 +02:00
parent b64da4eb18
commit d3ce480480
18 changed files with 286 additions and 113 deletions

View File

@@ -309,6 +309,7 @@ list(APPEND libSources
filemetadata/MetadataEditor.cpp filemetadata/MetadataEditor.cpp
network/BufferIoDevice.cpp network/BufferIoDevice.cpp
network/Msg.cpp
network/MsgProcessor.cpp network/MsgProcessor.cpp
network/StreamConnection.cpp network/StreamConnection.cpp
network/DbSyncConnection.cpp network/DbSyncConnection.cpp

View File

@@ -28,6 +28,8 @@
#include "Source.h" #include "Source.h"
#include "TomahawkSqlQuery.h" #include "TomahawkSqlQuery.h"
#include <qjson/serializer.h>
#include <QSqlQuery> #include <QSqlQuery>
DatabaseCommand_SetDynamicPlaylistRevision::DatabaseCommand_SetDynamicPlaylistRevision( const Tomahawk::source_ptr& s, DatabaseCommand_SetDynamicPlaylistRevision::DatabaseCommand_SetDynamicPlaylistRevision( const Tomahawk::source_ptr& s,

View File

@@ -18,8 +18,6 @@
#include "DatabaseCommand_SetPlaylistRevision.h" #include "DatabaseCommand_SetPlaylistRevision.h"
#include <QSqlQuery>
#include "collection/Collection.h" #include "collection/Collection.h"
#include "network/Servent.h" #include "network/Servent.h"
#include "utils/Logger.h" #include "utils/Logger.h"
@@ -28,6 +26,11 @@
#include "Source.h" #include "Source.h"
#include "TomahawkSqlQuery.h" #include "TomahawkSqlQuery.h"
#include <qjson/parser.h>
#include <qjson/serializer.h>
#include <QSqlQuery>
using namespace Tomahawk; using namespace Tomahawk;

View File

@@ -21,12 +21,15 @@
#include "Connection_p.h" #include "Connection_p.h"
#include "network/Servent.h" #include "network/Servent.h"
#include "network/Msg.h"
#include "utils/Logger.h" #include "utils/Logger.h"
#include "AclRegistry.h" #include "AclRegistry.h"
#include "QTcpSocketExtra.h" #include "QTcpSocketExtra.h"
#include "Source.h" #include "Source.h"
#include <qjson/serializer.h>
#include <QTime> #include <QTime>
#include <QThread> #include <QThread>

View File

@@ -19,6 +19,7 @@
#include "ConnectionManager_p.h" #include "ConnectionManager_p.h"
#include "ControlConnection.h" #include "ControlConnection.h"
#include "network/Msg.h"
#include "QTcpSocketExtra.h" #include "QTcpSocketExtra.h"
#include "Servent.h" #include "Servent.h"

View File

@@ -20,17 +20,18 @@
#include "ControlConnection.h" #include "ControlConnection.h"
#include "StreamConnection.h"
#include "database/Database.h" #include "database/Database.h"
#include "database/DatabaseCommand_CollectionStats.h" #include "database/DatabaseCommand_CollectionStats.h"
#include "DbSyncConnection.h"
#include "SourceList.h"
#include "MsgProcessor.h"
#include "network/DbSyncConnection.h" #include "network/DbSyncConnection.h"
#include "network/Msg.h"
#include "network/MsgProcessor.h"
#include "network/Servent.h" #include "network/Servent.h"
#include "sip/PeerInfo.h" #include "sip/PeerInfo.h"
#include "utils/Logger.h" #include "utils/Logger.h"
#include "StreamConnection.h"
#include "SourceList.h"
#define TCP_TIMEOUT 600 #define TCP_TIMEOUT 600
using namespace Tomahawk; using namespace Tomahawk;

View File

@@ -35,11 +35,13 @@
#include "database/DatabaseCommand.h" #include "database/DatabaseCommand.h"
#include "database/DatabaseCommand_CollectionStats.h" #include "database/DatabaseCommand_CollectionStats.h"
#include "database/DatabaseCommand_LoadOps.h" #include "database/DatabaseCommand_LoadOps.h"
#include "utils/Logger.h"
#include "Msg.h"
#include "MsgProcessor.h" #include "MsgProcessor.h"
#include "RemoteCollection.h" #include "RemoteCollection.h"
#include "Source.h" #include "Source.h"
#include "SourceList.h" #include "SourceList.h"
#include "utils/Logger.h"
using namespace Tomahawk; using namespace Tomahawk;

View File

@@ -0,0 +1,140 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Msg_p.h"
#include <qjson/parser.h>
#include <QtEndian>
Msg::Msg( const QByteArray& ba, char f )
: d_ptr( new MsgPrivate( this, ba, f ) )
{
}
Msg::Msg( quint32 len, quint8 flags )
: d_ptr( new MsgPrivate( this, len, flags ) )
{
}
Msg::~Msg()
{
delete d_ptr;
}
msg_ptr
Msg::factory( const QByteArray& ba, char f )
{
return msg_ptr( new Msg( ba, f ) );
}
msg_ptr
Msg::begin( char* headerToParse )
{
quint32 lenBE = *( (quint32*) headerToParse );
quint8 flags = *( (quint8*) (headerToParse+4) );
return msg_ptr( new Msg( qFromBigEndian(lenBE), flags ) );
}
void
Msg::fill( const QByteArray& ba )
{
Q_D( Msg );
Q_ASSERT( d->incomplete );
Q_ASSERT( ba.length() == (qint32)d->length );
d->payload = ba;
d->incomplete = false;
}
bool
Msg::write( QIODevice * device )
{
Q_D( Msg );
quint32 size = qToBigEndian( d->length );
quint8 flags = d->flags;
if( device->write( (const char*) &size, sizeof(quint32) ) != sizeof(quint32) ) return false;
if( device->write( (const char*) &flags, sizeof(quint8) ) != sizeof(quint8) ) return false;
if( device->write( (const char*) d->payload.data(), d->length ) != d->length ) return false;
return true;
}
quint8
Msg::headerSize()
{
return sizeof(quint32) + sizeof(quint8);
}
quint32
Msg::length() const
{
Q_D( const Msg );
return d->length;
}
bool
Msg::is( Flag flag )
{
Q_D( Msg );
return d->flags & flag;
}
const QByteArray&
Msg::payload() const
{
Q_D( const Msg );
Q_ASSERT( d->incomplete == false );
return d->payload;
}
QVariant&
Msg::json()
{
Q_D( Msg );
Q_ASSERT( is(JSON) );
Q_ASSERT( !is(COMPRESSED) );
if( !d->json_parsed )
{
QJson::Parser p;
bool ok;
d->json = p.parse( d->payload, &ok );
d->json_parsed = true;
}
return d->json;
}
char
Msg::flags() const
{
Q_D( const Msg );
return d->flags;
}

View File

@@ -1,6 +1,7 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> === /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
* *
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org> * Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
* *
* Tomahawk is free software: you can redistribute it and/or modify * Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@@ -32,14 +33,11 @@
#include "Typedefs.h" #include "Typedefs.h"
#include <QByteArray>
#include <QSharedPointer> #include <QSharedPointer>
#include <QtEndian>
#include <QIODevice>
#include <qjson/parser.h> class MsgPrivate;
#include <qjson/serializer.h> class QByteArray;
#include <qjson/qobjecthelper.h> class QIODevice;
class Msg class Msg
{ {
@@ -58,101 +56,54 @@ public:
SETUP = 128 // used to handshake/auth the connection prior to handing over to Connection subclass SETUP = 128 // used to handshake/auth the connection prior to handing over to Connection subclass
}; };
virtual ~Msg() virtual ~Msg();
{
//qDebug() << Q_FUNC_INFO;
}
/// constructs new msg you wish to send /**
static msg_ptr factory( const QByteArray& ba, char f ) * constructs new msg you wish to send
{ */
return msg_ptr( new Msg( ba, f ) ); static msg_ptr factory( const QByteArray& ba, char f );
}
/// constructs an incomplete new msg that is missing the payload data /**
static msg_ptr begin( char* headerToParse ) * constructs an incomplete new msg that is missing the payload data
{ */
quint32 lenBE = *( (quint32*) headerToParse ); static msg_ptr begin( char* headerToParse );
quint8 flags = *( (quint8*) (headerToParse+4) );
return msg_ptr( new Msg( qFromBigEndian(lenBE), flags ) );
}
/// completes msg construction by providing payload data /**
void fill( const QByteArray& ba ) * completes msg construction by providing payload data
{ */
Q_ASSERT( m_incomplete ); void fill( const QByteArray& ba );
Q_ASSERT( ba.length() == (qint32)m_length );
m_payload = ba;
m_incomplete = false;
}
/// frames the msg and writes to the wire: /**
bool write( QIODevice * device ) * frames the msg and writes to the wire:
{ */
quint32 size = qToBigEndian( m_length ); bool write( QIODevice * device );
quint8 flags = m_flags;
if( device->write( (const char*) &size, sizeof(quint32) ) != sizeof(quint32) ) return false;
if( device->write( (const char*) &flags, sizeof(quint8) ) != sizeof(quint8) ) return false;
if( device->write( (const char*) m_payload.data(), m_length ) != m_length ) return false;
return true;
}
// len(4) + flags(1) // len(4) + flags(1)
static quint8 headerSize() { return sizeof(quint32) + sizeof(quint8); } static quint8 headerSize();
quint32 length() const { return m_length; } quint32 length() const;
bool is( Flag flag ) { return m_flags & flag; } bool is( Flag flag );
const QByteArray& payload() const const QByteArray& payload() const;
{
Q_ASSERT( m_incomplete == false );
return m_payload;
}
QVariant& json() QVariant& json();
{
Q_ASSERT( is(JSON) );
Q_ASSERT( !is(COMPRESSED) );
if( !m_json_parsed ) char flags() const;
{
QJson::Parser p;
bool ok;
m_json = p.parse( m_payload, &ok );
m_json_parsed = true;
}
return m_json;
}
char flags() const { return m_flags; }
private: private:
/// used when constructing Msg you wish to send /**
Msg( const QByteArray& ba, char f ) * Used when constructing Msg you wish to send
: m_payload( ba ), */
m_length( ba.length() ), Msg( const QByteArray& ba, char f );
m_flags( f ),
m_incomplete( false ),
m_json_parsed( false)
{
}
/// used when constructung Msg off the wire: /**
Msg( quint32 len, quint8 flags ) * used when constructung Msg off the wire:
: m_length( len ), */
m_flags( flags ), Msg( quint32 len, quint8 flags );
m_incomplete( true ),
m_json_parsed( false)
{
}
QByteArray m_payload; Q_DECLARE_PRIVATE( Msg )
quint32 m_length; MsgPrivate* d_ptr;
char m_flags;
bool m_incomplete;
QVariant m_json;
bool m_json_parsed;
}; };
#endif // MSG_H #endif // MSG_H

View File

@@ -18,6 +18,7 @@
#include "MsgProcessor.h" #include "MsgProcessor.h"
#include "network/Msg_p.h"
#include "network/Servent.h" #include "network/Servent.h"
#include "utils/Logger.h" #include "utils/Logger.h"
@@ -114,21 +115,21 @@ MsgProcessor::process( msg_ptr msg, quint32 mode, quint32 threshold )
if( (mode & UNCOMPRESS_ALL) && msg->is( Msg::COMPRESSED ) ) if( (mode & UNCOMPRESS_ALL) && msg->is( Msg::COMPRESSED ) )
{ {
// qDebug() << "MsgProcessor::UNCOMPRESSING"; // qDebug() << "MsgProcessor::UNCOMPRESSING";
msg->m_payload = qUncompress( msg->payload() ); msg->d_func()->payload = qUncompress( msg->payload() );
msg->m_length = msg->m_payload.length(); msg->d_func()->length = msg->d_func()->payload.length();
msg->m_flags ^= Msg::COMPRESSED; msg->d_func()->flags ^= Msg::COMPRESSED;
} }
// parse json payload into qvariant if needed // parse json payload into qvariant if needed
if( (mode & PARSE_JSON) && if( (mode & PARSE_JSON) &&
msg->is( Msg::JSON ) && msg->is( Msg::JSON ) &&
msg->m_json_parsed == false ) msg->d_func()->json_parsed == false )
{ {
// qDebug() << "MsgProcessor::PARSING JSON"; // qDebug() << "MsgProcessor::PARSING JSON";
bool ok; bool ok;
QJson::Parser parser; QJson::Parser parser;
msg->m_json = parser.parse( msg->payload(), &ok ); msg->d_func()->json = parser.parse( msg->payload(), &ok );
msg->m_json_parsed = true; msg->d_func()->json_parsed = true;
} }
// compress if needed // compress if needed
@@ -137,9 +138,9 @@ MsgProcessor::process( msg_ptr msg, quint32 mode, quint32 threshold )
&& msg->length() > threshold ) && msg->length() > threshold )
{ {
// qDebug() << "MsgProcessor::COMPRESSING"; // qDebug() << "MsgProcessor::COMPRESSING";
msg->m_payload = qCompress( msg->payload(), 9 ); msg->d_func()->payload = qCompress( msg->payload(), 9 );
msg->m_length = msg->m_payload.length(); msg->d_func()->length = msg->d_func()->payload.length();
msg->m_flags |= Msg::COMPRESSED; msg->d_func()->flags |= Msg::COMPRESSED;
} }
return msg; return msg;
} }

View File

@@ -30,7 +30,8 @@
#ifndef MSGPROCESSOR_H #ifndef MSGPROCESSOR_H
#define MSGPROCESSOR_H #define MSGPROCESSOR_H
#include "Msg.h" #include "Typedefs.h"
#include "Msg.h" // Needed because we have msg_ptr in a slot
#include <QObject> #include <QObject>

View File

@@ -0,0 +1,61 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MSG_P_H
#define MSG_P_H
#include "Msg.h"
class MsgPrivate
{
friend class MsgProcessor;
public:
MsgPrivate( Msg* q, const QByteArray& ba, char f )
: q_ptr ( q )
, payload( ba )
, length( ba.length() )
, flags( f )
, incomplete( false )
, json_parsed( false )
{
}
MsgPrivate( Msg* q, quint32 len, quint8 flags )
: q_ptr( q)
, length( len )
, flags( flags )
, incomplete( true )
, json_parsed( false)
{
}
Msg* q_ptr;
Q_DECLARE_PUBLIC ( Msg )
private:
QByteArray payload;
quint32 length;
char flags;
bool incomplete;
QVariant json;
bool json_parsed;
};
#endif // MSG_P_H

View File

@@ -23,6 +23,10 @@
#include "utils/Logger.h" #include "utils/Logger.h"
#if QT_VERSION < QT_VERSION_CHECK( 5, 0, 0 )
#include "Msg.h"
#endif
void void
QTcpSocketExtra::connectToHost( const QHostAddress& host, quint16 port, OpenMode openMode ) QTcpSocketExtra::connectToHost( const QHostAddress& host, quint16 port, OpenMode openMode )
{ {

View File

@@ -30,8 +30,8 @@
#include <QTcpSocket> #include <QTcpSocket>
#include <QTimer> #include <QTimer>
#include "Msg.h"
#include "DllMacro.h" #include "DllMacro.h"
#include "Typedefs.h"
class Connection; class Connection;

View File

@@ -24,6 +24,7 @@
#include "accounts/AccountManager.h" #include "accounts/AccountManager.h"
#include "database/Database.h" #include "database/Database.h"
#include "database/DatabaseImpl.h" #include "database/DatabaseImpl.h"
#include "network/Msg.h"
#include "network/ConnectionManager.h" #include "network/ConnectionManager.h"
#include "network/DbSyncConnection.h" #include "network/DbSyncConnection.h"
#include "sip/SipInfo.h" #include "sip/SipInfo.h"

View File

@@ -30,10 +30,8 @@
#include <QSharedPointer> #include <QSharedPointer>
#include <QTcpServer> #include <QTcpServer>
#include "Typedefs.h"
#include "Msg.h"
#include "DllMacro.h" #include "DllMacro.h"
#include "Typedefs.h"
class Connection; class Connection;
class Connector; class Connector;

View File

@@ -20,16 +20,18 @@
#include "StreamConnection.h" #include "StreamConnection.h"
#include "Result.h"
#include "BufferIoDevice.h"
#include "network/ControlConnection.h"
#include "network/Servent.h"
#include "database/DatabaseCommand_LoadFiles.h" #include "database/DatabaseCommand_LoadFiles.h"
#include "database/Database.h" #include "database/Database.h"
#include "MsgProcessor.h" #include "network/ControlConnection.h"
#include "SourceList.h" #include "network/Servent.h"
#include "utils/Logger.h" #include "utils/Logger.h"
#include "BufferIoDevice.h"
#include "Msg.h"
#include "MsgProcessor.h"
#include "Result.h"
#include "SourceList.h"
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <boost/function.hpp> #include <boost/function.hpp>

View File

@@ -55,6 +55,7 @@
#include "DropJob.h" #include "DropJob.h"
#include "EchonestCatalogSynchronizer.h" #include "EchonestCatalogSynchronizer.h"
#include "database/DatabaseImpl.h" #include "database/DatabaseImpl.h"
#include "network/Msg.h"
#include "audio/AudioEngine.h" #include "audio/AudioEngine.h"
#include "utils/XspfLoader.h" #include "utils/XspfLoader.h"