File indexing completed on 2024-05-12 05:39:52

0001 /*************************************************************************
0002  *     Copyright (C) 2011 by Joseph Boudou                               *
0003  *     https://rolisteam.org/                                         *
0004  *                                                                       *
0005  *   rolisteam is free software; you can redistribute it and/or modify   *
0006  *   it under the terms of the GNU General Public License as published   *
0007  *   by the Free Software Foundation; either version 2 of the License,   *
0008  *   or (at your option) any later version.                              *
0009  *                                                                       *
0010  *   This program is distributed in the hope that it will be useful,     *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of      *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       *
0013  *   GNU General Public License for more details.                        *
0014  *                                                                       *
0015  *   You should have received a copy of the GNU General Public License   *
0016  *   along with this program; if not, write to the                       *
0017  *   Free Software Foundation, Inc.,                                     *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           *
0019  *************************************************************************/
0020 
0021 #ifndef DATA_READER_H
0022 #define DATA_READER_H
0023 
0024 #include "network_global.h"
0025 #include "networkmessage.h"
0026 #include <QDateTime>
0027 #include <QString>
0028 #ifdef QT_GUI_LIB
0029 #include <QPixmap>
0030 #endif
0031 /**
0032  * @brief The NetworkMessageReader class
0033  */
0034 class NETWORK_EXPORT NetworkMessageReader : public NetworkMessage
0035 {
0036 public:
0037     NetworkMessageReader();
0038     NetworkMessageReader(const NetworkMessageHeader& header, const char* buffer);
0039     NetworkMessageReader(const NetworkMessageReader& other);
0040     virtual ~NetworkMessageReader() override;
0041 
0042     bool isValid();
0043 
0044     NetMsg::Category category() const override;
0045     NetMsg::Action action() const override;
0046 
0047     void reset();
0048 
0049     void setData(const QByteArray& bytes);
0050 
0051     size_t left() const;
0052     size_t currentPos() const;
0053 
0054     quint8 uint8();
0055     quint16 uint16();
0056     quint32 uint32();
0057     quint64 uint64();
0058 
0059     QString string8();
0060     QString string16();
0061     QString string32();
0062 
0063     QByteArray byteArray32();
0064 
0065     QString string(quint64 size);
0066     unsigned int rgb();
0067     qreal real();
0068     qint8 int8();
0069     qint16 int16();
0070     qint32 int32();
0071     qint64 int64();
0072 
0073 #ifdef QT_GUI_LIB
0074     QPixmap pixmap();
0075 #endif
0076 
0077     QDateTime dateTime();
0078 
0079     NetworkMessageHeader* header() const;
0080     void setHeader(NetworkMessageHeader* header);
0081 
0082     void resetToData();
0083     void resetToPos(const char* pos);
0084     const char* pos() const;
0085     /////////////////////////////////
0086     ///
0087     /// \brief readRecipient
0088     ///
0089     /////////////////////////////////
0090     void readRecipient();
0091 
0092     NetworkMessage::RecipientMode getRecipientMode() const override;
0093 
0094     virtual QStringList getRecipientList() const override;
0095 
0096     void setInternalData(const QByteArray& bytes);
0097 
0098 protected:
0099     NetworkMessageHeader* buffer() const override;
0100     bool isSizeReadable(size_t size);
0101 
0102 private:
0103     NetworkMessageHeader* m_header= nullptr;
0104     bool m_outMemory= false;
0105     char* m_buffer= nullptr;
0106     const char* m_pos= nullptr;
0107     const char* m_end= nullptr;
0108     RecipientMode m_mode;
0109     QStringList m_recipientList;
0110 };
0111 
0112 #endif