diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt index 7a3f2ce3a..7c85bb9c2 100644 --- a/src/libtomahawk/CMakeLists.txt +++ b/src/libtomahawk/CMakeLists.txt @@ -309,6 +309,7 @@ list(APPEND libSources filemetadata/MetadataEditor.cpp network/BufferIoDevice.cpp + network/Msg.cpp network/MsgProcessor.cpp network/StreamConnection.cpp network/DbSyncConnection.cpp diff --git a/src/libtomahawk/database/DatabaseCommand_SetDynamicPlaylistRevision.cpp b/src/libtomahawk/database/DatabaseCommand_SetDynamicPlaylistRevision.cpp index 7e42cd497..e79ba3908 100644 --- a/src/libtomahawk/database/DatabaseCommand_SetDynamicPlaylistRevision.cpp +++ b/src/libtomahawk/database/DatabaseCommand_SetDynamicPlaylistRevision.cpp @@ -28,6 +28,8 @@ #include "Source.h" #include "TomahawkSqlQuery.h" +#include + #include DatabaseCommand_SetDynamicPlaylistRevision::DatabaseCommand_SetDynamicPlaylistRevision( const Tomahawk::source_ptr& s, diff --git a/src/libtomahawk/database/DatabaseCommand_SetPlaylistRevision.cpp b/src/libtomahawk/database/DatabaseCommand_SetPlaylistRevision.cpp index 94fc9983f..7dbb52f0d 100644 --- a/src/libtomahawk/database/DatabaseCommand_SetPlaylistRevision.cpp +++ b/src/libtomahawk/database/DatabaseCommand_SetPlaylistRevision.cpp @@ -18,8 +18,6 @@ #include "DatabaseCommand_SetPlaylistRevision.h" -#include - #include "collection/Collection.h" #include "network/Servent.h" #include "utils/Logger.h" @@ -28,6 +26,11 @@ #include "Source.h" #include "TomahawkSqlQuery.h" +#include +#include + +#include + using namespace Tomahawk; diff --git a/src/libtomahawk/network/Connection.cpp b/src/libtomahawk/network/Connection.cpp index 5a74ea648..03adfb45e 100644 --- a/src/libtomahawk/network/Connection.cpp +++ b/src/libtomahawk/network/Connection.cpp @@ -21,12 +21,15 @@ #include "Connection_p.h" #include "network/Servent.h" +#include "network/Msg.h" #include "utils/Logger.h" #include "AclRegistry.h" #include "QTcpSocketExtra.h" #include "Source.h" +#include + #include #include diff --git a/src/libtomahawk/network/ConnectionManager.cpp b/src/libtomahawk/network/ConnectionManager.cpp index 790d8f5d6..c48c3bcfc 100644 --- a/src/libtomahawk/network/ConnectionManager.cpp +++ b/src/libtomahawk/network/ConnectionManager.cpp @@ -19,6 +19,7 @@ #include "ConnectionManager_p.h" #include "ControlConnection.h" +#include "network/Msg.h" #include "QTcpSocketExtra.h" #include "Servent.h" diff --git a/src/libtomahawk/network/ControlConnection.cpp b/src/libtomahawk/network/ControlConnection.cpp index 19bd38a60..3817fc84b 100644 --- a/src/libtomahawk/network/ControlConnection.cpp +++ b/src/libtomahawk/network/ControlConnection.cpp @@ -20,17 +20,18 @@ #include "ControlConnection.h" -#include "StreamConnection.h" #include "database/Database.h" #include "database/DatabaseCommand_CollectionStats.h" -#include "DbSyncConnection.h" -#include "SourceList.h" -#include "MsgProcessor.h" #include "network/DbSyncConnection.h" +#include "network/Msg.h" +#include "network/MsgProcessor.h" #include "network/Servent.h" #include "sip/PeerInfo.h" #include "utils/Logger.h" +#include "StreamConnection.h" +#include "SourceList.h" + #define TCP_TIMEOUT 600 using namespace Tomahawk; diff --git a/src/libtomahawk/network/DbSyncConnection.cpp b/src/libtomahawk/network/DbSyncConnection.cpp index 86f30c213..b2b49369b 100644 --- a/src/libtomahawk/network/DbSyncConnection.cpp +++ b/src/libtomahawk/network/DbSyncConnection.cpp @@ -35,11 +35,13 @@ #include "database/DatabaseCommand.h" #include "database/DatabaseCommand_CollectionStats.h" #include "database/DatabaseCommand_LoadOps.h" +#include "utils/Logger.h" + +#include "Msg.h" #include "MsgProcessor.h" #include "RemoteCollection.h" #include "Source.h" #include "SourceList.h" -#include "utils/Logger.h" using namespace Tomahawk; diff --git a/src/libtomahawk/network/Msg.cpp b/src/libtomahawk/network/Msg.cpp new file mode 100644 index 000000000..bdc9bd0c6 --- /dev/null +++ b/src/libtomahawk/network/Msg.cpp @@ -0,0 +1,140 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Christian Muehlhaeuser + * Copyright 2013, Uwe L. Korn + * + * 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 . + */ + +#include "Msg_p.h" + +#include + +#include + +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; +} diff --git a/src/libtomahawk/network/Msg.h b/src/libtomahawk/network/Msg.h index 77aff75b2..9d1d59591 100644 --- a/src/libtomahawk/network/Msg.h +++ b/src/libtomahawk/network/Msg.h @@ -1,6 +1,7 @@ /* === This file is part of Tomahawk Player - === * * Copyright 2010-2011, Christian Muehlhaeuser + * Copyright 2013, Uwe L. Korn * * Tomahawk is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -32,14 +33,11 @@ #include "Typedefs.h" -#include #include -#include -#include -#include -#include -#include +class MsgPrivate; +class QByteArray; +class QIODevice; class Msg { @@ -58,101 +56,54 @@ public: SETUP = 128 // used to handshake/auth the connection prior to handing over to Connection subclass }; - virtual ~Msg() - { - //qDebug() << Q_FUNC_INFO; - } + virtual ~Msg(); - /// constructs new msg you wish to send - static msg_ptr factory( const QByteArray& ba, char f ) - { - return msg_ptr( new Msg( ba, f ) ); - } + /** + * constructs new msg you wish to send + */ + 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 ) - { - quint32 lenBE = *( (quint32*) headerToParse ); - quint8 flags = *( (quint8*) (headerToParse+4) ); - return msg_ptr( new Msg( qFromBigEndian(lenBE), flags ) ); - } + /** + * constructs an incomplete new msg that is missing the payload data + */ + static msg_ptr begin( char* headerToParse ); - /// completes msg construction by providing payload data - void fill( const QByteArray& ba ) - { - Q_ASSERT( m_incomplete ); - Q_ASSERT( ba.length() == (qint32)m_length ); - m_payload = ba; - m_incomplete = false; - } + /** + * completes msg construction by providing payload data + */ + void fill( const QByteArray& ba ); - /// frames the msg and writes to the wire: - bool write( QIODevice * device ) - { - quint32 size = qToBigEndian( m_length ); - 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; - } + /** + * frames the msg and writes to the wire: + */ + bool write( QIODevice * device ); // 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 - { - Q_ASSERT( m_incomplete == false ); - return m_payload; - } + const QByteArray& payload() const; - QVariant& json() - { - Q_ASSERT( is(JSON) ); - Q_ASSERT( !is(COMPRESSED) ); + QVariant& json(); - if( !m_json_parsed ) - { - 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; } + char flags() const; private: - /// used when constructing Msg you wish to send - Msg( const QByteArray& ba, char f ) - : m_payload( ba ), - m_length( ba.length() ), - m_flags( f ), - m_incomplete( false ), - m_json_parsed( false) - { - } + /** + * Used when constructing Msg you wish to send + */ + Msg( const QByteArray& ba, char f ); - /// used when constructung Msg off the wire: - Msg( quint32 len, quint8 flags ) - : m_length( len ), - m_flags( flags ), - m_incomplete( true ), - m_json_parsed( false) - { - } + /** + * used when constructung Msg off the wire: + */ + Msg( quint32 len, quint8 flags ); - QByteArray m_payload; - quint32 m_length; - char m_flags; - bool m_incomplete; - QVariant m_json; - bool m_json_parsed; + Q_DECLARE_PRIVATE( Msg ) + MsgPrivate* d_ptr; }; #endif // MSG_H diff --git a/src/libtomahawk/network/MsgProcessor.cpp b/src/libtomahawk/network/MsgProcessor.cpp index 4e6a883b8..22357abc3 100644 --- a/src/libtomahawk/network/MsgProcessor.cpp +++ b/src/libtomahawk/network/MsgProcessor.cpp @@ -18,6 +18,7 @@ #include "MsgProcessor.h" +#include "network/Msg_p.h" #include "network/Servent.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 ) ) { // qDebug() << "MsgProcessor::UNCOMPRESSING"; - msg->m_payload = qUncompress( msg->payload() ); - msg->m_length = msg->m_payload.length(); - msg->m_flags ^= Msg::COMPRESSED; + msg->d_func()->payload = qUncompress( msg->payload() ); + msg->d_func()->length = msg->d_func()->payload.length(); + msg->d_func()->flags ^= Msg::COMPRESSED; } // parse json payload into qvariant if needed if( (mode & PARSE_JSON) && msg->is( Msg::JSON ) && - msg->m_json_parsed == false ) + msg->d_func()->json_parsed == false ) { // qDebug() << "MsgProcessor::PARSING JSON"; bool ok; QJson::Parser parser; - msg->m_json = parser.parse( msg->payload(), &ok ); - msg->m_json_parsed = true; + msg->d_func()->json = parser.parse( msg->payload(), &ok ); + msg->d_func()->json_parsed = true; } // compress if needed @@ -137,9 +138,9 @@ MsgProcessor::process( msg_ptr msg, quint32 mode, quint32 threshold ) && msg->length() > threshold ) { // qDebug() << "MsgProcessor::COMPRESSING"; - msg->m_payload = qCompress( msg->payload(), 9 ); - msg->m_length = msg->m_payload.length(); - msg->m_flags |= Msg::COMPRESSED; + msg->d_func()->payload = qCompress( msg->payload(), 9 ); + msg->d_func()->length = msg->d_func()->payload.length(); + msg->d_func()->flags |= Msg::COMPRESSED; } return msg; } diff --git a/src/libtomahawk/network/MsgProcessor.h b/src/libtomahawk/network/MsgProcessor.h index ae0892dc9..9539f519b 100644 --- a/src/libtomahawk/network/MsgProcessor.h +++ b/src/libtomahawk/network/MsgProcessor.h @@ -30,7 +30,8 @@ #ifndef MSGPROCESSOR_H #define MSGPROCESSOR_H -#include "Msg.h" +#include "Typedefs.h" +#include "Msg.h" // Needed because we have msg_ptr in a slot #include diff --git a/src/libtomahawk/network/Msg_p.h b/src/libtomahawk/network/Msg_p.h new file mode 100644 index 000000000..73b6ce60f --- /dev/null +++ b/src/libtomahawk/network/Msg_p.h @@ -0,0 +1,61 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Christian Muehlhaeuser + * Copyright 2013, Uwe L. Korn + * + * 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 . + */ + +#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 diff --git a/src/libtomahawk/network/QTcpSocketExtra.cpp b/src/libtomahawk/network/QTcpSocketExtra.cpp index 4c1a51e5f..6b8906db0 100644 --- a/src/libtomahawk/network/QTcpSocketExtra.cpp +++ b/src/libtomahawk/network/QTcpSocketExtra.cpp @@ -23,6 +23,10 @@ #include "utils/Logger.h" +#if QT_VERSION < QT_VERSION_CHECK( 5, 0, 0 ) + #include "Msg.h" +#endif + void QTcpSocketExtra::connectToHost( const QHostAddress& host, quint16 port, OpenMode openMode ) { diff --git a/src/libtomahawk/network/QTcpSocketExtra.h b/src/libtomahawk/network/QTcpSocketExtra.h index 0ca56da1d..1d2cbad67 100644 --- a/src/libtomahawk/network/QTcpSocketExtra.h +++ b/src/libtomahawk/network/QTcpSocketExtra.h @@ -30,8 +30,8 @@ #include #include -#include "Msg.h" #include "DllMacro.h" +#include "Typedefs.h" class Connection; diff --git a/src/libtomahawk/network/Servent.cpp b/src/libtomahawk/network/Servent.cpp index 3d6d13b7c..315035955 100644 --- a/src/libtomahawk/network/Servent.cpp +++ b/src/libtomahawk/network/Servent.cpp @@ -24,6 +24,7 @@ #include "accounts/AccountManager.h" #include "database/Database.h" #include "database/DatabaseImpl.h" +#include "network/Msg.h" #include "network/ConnectionManager.h" #include "network/DbSyncConnection.h" #include "sip/SipInfo.h" diff --git a/src/libtomahawk/network/Servent.h b/src/libtomahawk/network/Servent.h index 895dbba5d..379eedc5c 100644 --- a/src/libtomahawk/network/Servent.h +++ b/src/libtomahawk/network/Servent.h @@ -30,10 +30,8 @@ #include #include -#include "Typedefs.h" -#include "Msg.h" - #include "DllMacro.h" +#include "Typedefs.h" class Connection; class Connector; diff --git a/src/libtomahawk/network/StreamConnection.cpp b/src/libtomahawk/network/StreamConnection.cpp index c204ce347..b6fcec806 100644 --- a/src/libtomahawk/network/StreamConnection.cpp +++ b/src/libtomahawk/network/StreamConnection.cpp @@ -20,16 +20,18 @@ #include "StreamConnection.h" -#include "Result.h" -#include "BufferIoDevice.h" -#include "network/ControlConnection.h" -#include "network/Servent.h" #include "database/DatabaseCommand_LoadFiles.h" #include "database/Database.h" -#include "MsgProcessor.h" -#include "SourceList.h" +#include "network/ControlConnection.h" +#include "network/Servent.h" #include "utils/Logger.h" +#include "BufferIoDevice.h" +#include "Msg.h" +#include "MsgProcessor.h" +#include "Result.h" +#include "SourceList.h" + #include #include diff --git a/src/tomahawk/TomahawkApp.cpp b/src/tomahawk/TomahawkApp.cpp index 4e6727a35..3aaa75e14 100644 --- a/src/tomahawk/TomahawkApp.cpp +++ b/src/tomahawk/TomahawkApp.cpp @@ -55,6 +55,7 @@ #include "DropJob.h" #include "EchonestCatalogSynchronizer.h" #include "database/DatabaseImpl.h" +#include "network/Msg.h" #include "audio/AudioEngine.h" #include "utils/XspfLoader.h"