GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/messagein.h Lines: 0 4 0.0 %
Date: 2021-03-17 Branches: 0 4 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#ifndef NET_MESSAGEIN_H
25
#define NET_MESSAGEIN_H
26
27
#include "enums/simpletypes/beingid.h"
28
29
#include <string>
30
31
#include "localconsts.h"
32
33
namespace Net
34
{
35
36
/**
37
 * Used for parsing an incoming message.
38
 *
39
 * \ingroup Network
40
 */
41
class MessageIn notfinal
42
{
43
    public:
44
        A_DELETE_COPY(MessageIn)
45
46
        virtual ~MessageIn();
47
48
        uint16_t readId() const;
49
50
        /**
51
         * Returns the message ID.
52
         */
53
        int getId() const noexcept2 A_WARN_UNUSED
54
        { return mId; }
55
56
        /**
57
         * Returns the message length.
58
         */
59
        unsigned int getLength() const noexcept2 A_WARN_UNUSED
60
        { return mLength; }
61
62
        /**
63
         * Returns the length of unread data.
64
         */
65
        unsigned int getUnreadLength() const noexcept2 A_WARN_UNUSED
66
        { return mLength > mPos ? mLength - mPos : 0; }
67
68
        /**< Reads a byte. */
69
        unsigned char readUInt8(const char *const str);
70
71
        /**< Reads a byte. */
72
        signed char readInt8(const char *const str);
73
74
        /**< Reads a short. */
75
        int16_t readInt16(const char *const str);
76
77
        uint16_t readUInt16(const char *const str);
78
79
        /**< Reads a long. */
80
        int32_t readInt32(const char *const str);
81
82
        uint32_t readUInt32(const char *const str);
83
84
        int readItemId(const char *const str);
85
86
        int64_t readInt64(const char *const str);
87
88
        BeingId readBeingId(const char *const str);
89
90
        float readFloat(const char *const str);
91
92
        /**
93
         * Reads a special 3 byte block used by eAthena, containing x and y
94
         * coordinates and direction.
95
         */
96
        void readCoordinates(uint16_t &restrict x,
97
                             uint16_t &restrict y,
98
                             uint8_t &restrict direction,
99
                             const char *const str);
100
101
        /**
102
         * Reads a special 5 byte block used by eAthena, containing a source
103
         * and destination coordinate pair.
104
         */
105
        void readCoordinatePair(uint16_t &restrict srcX,
106
                                uint16_t &restrict srcY,
107
                                uint16_t &restrict dstX,
108
                                uint16_t &restrict dstY,
109
                                const char *const str);
110
111
        /**
112
         * Skips a given number of bytes.
113
         */
114
        void skip(const unsigned int length,
115
                  const char *const str);
116
117
        void skipToEnd(const char *const str);
118
119
        /**
120
         * Reads a string. If a length is not given (-1), it is assumed
121
         * that the length of the string is stored in a short at the
122
         * start of the string.
123
         */
124
        std::string readString(int length,
125
                               const char *const dstr);
126
127
        std::string readRawString(int length,
128
                                  const char *const dstr);
129
130
        unsigned char *readBytes(int length,
131
                                 const char *const dstr);
132
133
        static uint8_t fromServerDirection(const uint8_t serverDir)
134
                                           A_WARN_UNUSED;
135
136
        int getVersion() const noexcept2 A_WARN_UNUSED
137
        { return mVersion; }
138
139
        int getVersionMain() const noexcept2 A_WARN_UNUSED;
140
141
        int getVersionRe() const noexcept2 A_WARN_UNUSED;
142
143
        int getVersionZero() const noexcept2 A_WARN_UNUSED;
144
145
    protected:
146
        /**
147
         * Constructor.
148
         */
149
        MessageIn(const char *const data, const unsigned int length);
150
151
        const char *mData;     /**< The message data. */
152
        unsigned int mLength;  /**< The length of the data. */
153
154
        /**
155
         * Actual position in the packet. From 0 to packet->length.
156
         * A value bigger than packet->length means EOP was reached when
157
         * reading it.
158
         */
159
        unsigned int mPos;
160
        int mVersion;
161
        uint16_t mId;          /**< The message ID. */
162
        bool mIgnore;
163
};
164
165
}  // namespace Net
166
167
#endif  // NET_MESSAGEIN_H