Mana
Loading...
Searching...
No Matches
messageout.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 <enet/enet.h>
25
26#include <cstring>
27
28namespace ManaServ {
29
31 mData(nullptr),
32 mPos(0),
33 mDataSize(0),
34 mDebugMode(false)
35{
36 bool debug = true;
37 if (debug)
39
40 writeInt16(id);
41 mDebugMode = debug;
42}
43
45{
46 free(mData);
47}
48
49void MessageOut::expand(size_t bytes)
50{
51 mData = (char*)realloc(mData, mPos + bytes);
52 mDataSize = mPos + bytes;
53}
54
56{
57 if (mDebugMode)
59
60 expand(1);
61 mData[mPos] = value;
62 mPos += 1;
63}
64
65void MessageOut::writeInt16(uint16_t value)
66{
67 if (mDebugMode)
69
70 expand(2);
71 uint16_t t = ENET_HOST_TO_NET_16(value);
72 memcpy(mData + mPos, &t, 2);
73 mPos += 2;
74}
75
77{
78 if (mDebugMode)
80
81 expand(4);
82 uint32_t t = ENET_HOST_TO_NET_32(value);
83 memcpy(mData + mPos, &t, 4);
84 mPos += 4;
85}
86
87void MessageOut::writeString(const std::string &string, int length)
88{
89 if (mDebugMode)
90 {
92 writeInt16(length);
93 }
94
95 int stringLength = string.length();
96 if (length < 0)
97 {
98 // Write the length at the start if not fixed
99 writeInt16(stringLength);
100 length = stringLength;
101 }
102 else if (length < stringLength)
103 {
104 // Make sure the length of the string is no longer than specified
105 stringLength = length;
106 }
107 expand(length);
108
109 // Write the actual string
110 memcpy(mData + mPos, string.data(), stringLength);
111
112 if (length > stringLength)
113 {
114 // Pad remaining space with zeros
115 memset(mData + mPos + stringLength, '\0', length - stringLength);
116 }
117 mPos += length;
118}
119
121{
122 expand(1);
123 mData[mPos] = type;
124 mPos += 1;
125}
126
127} // namespace ManaServ
void writeInt16(uint16_t value)
Writes an unsigned 16-bit integer to the message.
bool mDebugMode
Include debugging information.
Definition messageout.h:89
void writeInt32(uint32_t value)
Writes an unsigned 32-bit integer to the message.
void expand(size_t size)
Expand the packet data to be able to hold more data.
unsigned int mPos
Position in the data.
Definition messageout.h:87
MessageOut(uint16_t id)
void writeValueType(ManaServ::ValueType type)
unsigned int mDataSize
Size of data.
Definition messageout.h:88
void writeInt8(uint8_t value)
Writes an unsigned 8-bit integer to the message.
void writeString(const std::string &string, int length=-1)
Writes a string.
char * mData
Data building up.
Definition messageout.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