/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. Msg is a wire msg used by p2p connections.
  21. Msgs have a 5-byte header:
  22. - 4 bytes length, big endian
  23. - 1 byte flags
  24. Flags indicate if the payload is compressed/json/etc.
  25. Use static factory method to create, pass around shared pointers: msp_ptr
  26. */
  27. #ifndef MSG_H
  28. #define MSG_H
  29. #include "Typedefs.h"
  30. #include <QSharedPointer>
  31. class MsgPrivate;
  32. class QByteArray;
  33. class QIODevice;
  34. class Msg
  35. {
  36. friend class MsgProcessor;
  37. public:
  38. enum Flag
  39. {
  40. RAW = 1,
  41. JSON = 2,
  42. FRAGMENT = 4,
  43. COMPRESSED = 8,
  44. DBOP = 16,
  45. PING = 32,
  46. RESERVED_1 = 64,
  47. SETUP = 128 // used to handshake/auth the connection prior to handing over to Connection subclass
  48. };
  49. virtual ~Msg();
  50. /**
  51. * constructs new msg you wish to send
  52. */
  53. static msg_ptr factory( const QByteArray& ba, char f );
  54. /**
  55. * constructs an incomplete new msg that is missing the payload data
  56. */
  57. static msg_ptr begin( char* headerToParse );
  58. /**
  59. * completes msg construction by providing payload data
  60. */
  61. void fill( const QByteArray& ba );
  62. /**
  63. * frames the msg and writes to the wire:
  64. */
  65. bool write( QIODevice * device );
  66. // len(4) + flags(1)
  67. static quint8 headerSize();
  68. quint32 length() const;
  69. bool is( Flag flag );
  70. const QByteArray& payload() const;
  71. QVariant& json();
  72. char flags() const;
  73. private:
  74. /**
  75. * Used when constructing Msg you wish to send
  76. */
  77. Msg( const QByteArray& ba, char f );
  78. /**
  79. * used when constructung Msg off the wire:
  80. */
  81. Msg( quint32 len, quint8 flags );
  82. Q_DECLARE_PRIVATE( Msg )
  83. MsgPrivate* d_ptr;
  84. };
  85. #endif // MSG_H