Mana
Loading...
Searching...
No Matches
messagein.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
23
24#include <cstring>
25#include <enet/enet.h>
26
27namespace ManaServ {
28
29MessageIn::MessageIn(const char *data, unsigned int length):
30 mData(data),
31 mLength(length),
32 mDebugMode(false),
33 mPos(0)
34{
35 // Read the message ID
36 mId = readInt16();
37
38 // Read and clear the debug flag
41}
42
44{
45 uint8_t value = 0;
46
48 return value;
49
50 if (mPos < mLength)
51 {
52 value = mData[mPos];
53 }
54 mPos++;
55 return value;
56}
57
59{
60 uint16_t value = 0;
61
63 return value;
64
65 if (mPos + 2 <= mLength)
66 {
67 uint16_t t;
68 memcpy(&t, mData + mPos, 2);
69 value = (unsigned short) ENET_NET_TO_HOST_16(t);
70 }
71 mPos += 2;
72 return value;
73}
74
76{
77 uint32_t value = 0;
78
80 return value;
81
82 if (mPos + 4 <= mLength)
83 {
84 uint32_t t;
85 memcpy(&t, mData + mPos, 4);
86 value = ENET_NET_TO_HOST_32(t);
87 }
88 mPos += 4;
89 return value;
90}
91
92std::string MessageIn::readString(int length)
93{
95 return std::string();
96
97 if (mDebugMode)
98 {
99 int fixedLength = (int16_t) readInt16();
100 if (fixedLength != length)
101 {
102 // String does not have the expected length
103 mPos = mLength + 1;
104 return std::string();
105 }
106 }
107
108 // Get string length
109 if (length < 0)
110 {
111 length = readInt16();
112 }
113
114 // Make sure the string isn't erroneous
115 if (length < 0 || mPos + length > mLength)
116 {
117 mPos = mLength + 1;
118 return std::string();
119 }
120
121 // Read the string
122 const char *stringBeg = mData + mPos;
123 const char *stringEnd = (const char *)memchr(stringBeg, '\0', length);
124 std::string readString(stringBeg,
125 stringEnd ? stringEnd - stringBeg : length);
126 mPos += length;
127
128 return readString;
129}
130
132{
133 if (!mDebugMode) // Verification not possible
134 return true;
135
136 if (mPos >= mLength)
137 return false;
138
139 uint8_t t = mData[mPos];
140 ++mPos;
141
142 return t == type;
143}
144
145} // ManaServ
bool readValueType(ManaServ::ValueType type)
uint16_t readInt16()
Reads an unsigned 16-bit integer from the message.
Definition messagein.cpp:58
unsigned int mLength
The length of the data.
Definition messagein.h:85
const char * mData
The message data.
Definition messagein.h:84
unsigned int mPos
Actual position in the packet.
Definition messagein.h:93
std::string readString(int length=-1)
Reads a string.
Definition messagein.cpp:92
MessageIn(const char *data, unsigned int length)
Definition messagein.cpp:29
bool mDebugMode
Includes debugging information.
Definition messagein.h:87
uint32_t readInt32()
Reads an unsigned 32-bit integer from the message.
Definition messagein.cpp:75
uint8_t readInt8()
Reads an unsigned 8-bit integer from the message.
Definition messagein.cpp:43
unsigned short mId
The message ID.
Definition messagein.h:86
ValueType
The type of a value in a message.
unsigned char uint8_t
Definition sha256.cpp:81
unsigned int uint32_t
Definition sha256.cpp:82