PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 5ms RepoModel.GetById 35ms app.codeStats 1ms

/src/libtomahawk/network/Msg.h

http://github.com/tomahawk-player/tomahawk
C Header | 109 lines | 40 code | 22 blank | 47 comment | 0 complexity | dd719195c06117e66265f53984a9fb23 MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4 *   Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
  5 *
  6 *   Tomahawk is free software: you can redistribute it and/or modify
  7 *   it under the terms of the GNU General Public License as published by
  8 *   the Free Software Foundation, either version 3 of the License, or
  9 *   (at your option) any later version.
 10 *
 11 *   Tomahawk is distributed in the hope that it will be useful,
 12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14 *   GNU General Public License for more details.
 15 *
 16 *   You should have received a copy of the GNU General Public License
 17 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 18 */
 19
 20/*
 21    Msg is a wire msg used by p2p connections.
 22    Msgs have a 5-byte header:
 23    - 4 bytes length, big endian
 24    - 1 byte flags
 25
 26    Flags indicate if the payload is compressed/json/etc.
 27
 28    Use static factory method to create, pass around shared pointers: msp_ptr
 29*/
 30
 31#ifndef MSG_H
 32#define MSG_H
 33
 34#include "Typedefs.h"
 35
 36#include <QSharedPointer>
 37
 38class MsgPrivate;
 39class QByteArray;
 40class QIODevice;
 41
 42class Msg
 43{
 44    friend class MsgProcessor;
 45
 46public:
 47    enum Flag
 48    {
 49        RAW = 1,
 50        JSON = 2,
 51        FRAGMENT = 4,
 52        COMPRESSED = 8,
 53        DBOP = 16,
 54        PING = 32,
 55        RESERVED_1 = 64,
 56        SETUP = 128 // used to handshake/auth the connection prior to handing over to Connection subclass
 57    };
 58
 59    virtual ~Msg();
 60
 61    /**
 62     * constructs new msg you wish to send
 63     */
 64    static msg_ptr factory( const QByteArray& ba, char f );
 65
 66    /**
 67     * constructs an incomplete new msg that is missing the payload data
 68     */
 69    static msg_ptr begin( char* headerToParse );
 70
 71    /**
 72     * completes msg construction by providing payload data
 73     */
 74    void fill( const QByteArray& ba );
 75
 76    /**
 77     * frames the msg and writes to the wire:
 78     */
 79    bool write( QIODevice * device );
 80
 81    // len(4) + flags(1)
 82    static quint8 headerSize();
 83
 84    quint32 length() const;
 85
 86    bool is( Flag flag );
 87
 88    const QByteArray& payload() const;
 89
 90    QVariant& json();
 91
 92    char flags() const;
 93
 94private:
 95    /**
 96     * Used when constructing Msg you wish to send
 97     */
 98    Msg( const QByteArray& ba, char f );
 99
100    /**
101     * used when constructung Msg off the wire:
102     */
103    Msg( quint32 len, quint8 flags );
104
105    Q_DECLARE_PRIVATE( Msg )
106    MsgPrivate* d_ptr;
107};
108
109#endif // MSG_H