GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/messageout.cpp Lines: 0 47 0.0 %
Date: 2017-11-29 Branches: 0 44 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-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "net/messageout.h"
24
25
#include "logger.h"
26
27
#include "net/net.h"
28
#include "net/packetcounters.h"
29
30
#include "utils/stringutils.h"
31
32
#include "debug.h"
33
34
namespace Net
35
{
36
37
MessageOut::MessageOut(const int16_t id) :
38
    mData(nullptr),
39
    mDataSize(0),
40
    mPos(0),
41
    mId(id),
42
    mIgnore(false)
43
{
44
    PacketCounters::incOutPackets();
45
    IGNOREDEBUGLOG;
46
    DEBUGLOG2("Send packet", 0, "MessageOut");
47
}
48
49
void MessageOut::writeInt8(const int8_t value, const char *const str)
50
{
51
    expand(1);
52
    mData[mPos] = value;
53
    DEBUGLOG2("writeInt8:  " + toStringPrint(CAST_U32(
54
        CAST_U8(value))),
55
        mPos, str);
56
    mPos += 1;
57
    PacketCounters::incOutBytes(1);
58
}
59
60
void MessageOut::writeString(const std::string &string,
61
                             int length,
62
                             const char *const str)
63
{
64
    int stringLength = CAST_S32(string.length());
65
    if (length < 0)
66
    {
67
        // Write the length at the start if not fixed
68
        writeInt16(CAST_S16(stringLength), "len");
69
        length = stringLength;
70
    }
71
    else if (length < stringLength)
72
    {
73
        // Make sure the length of the string is no longer than specified
74
        stringLength = length;
75
    }
76
    expand(length);
77
78
    // Write the actual string
79
    memcpy(mData + CAST_SIZE(mPos), string.c_str(), stringLength);
80
81
    // Pad remaining space with zeros
82
    if (length > stringLength)
83
    {
84
        memset(mData + CAST_SIZE(mPos + stringLength),
85
            '\0',
86
            length - stringLength);
87
    }
88
89
    DEBUGLOG2("writeString: " + string, mPos, str);
90
    mPos += length;
91
    PacketCounters::incOutBytes(length);
92
}
93
94
void MessageOut::writeStringNoLog(const std::string &string,
95
                                  int length,
96
                                  const char *const str)
97
{
98
    int stringLength = CAST_S32(string.length());
99
    if (length < 0)
100
    {
101
        // Write the length at the start if not fixed
102
        writeInt16(CAST_S16(stringLength), "len");
103
        length = stringLength;
104
    }
105
    else if (length < stringLength)
106
    {
107
        // Make sure the length of the string is no longer than specified
108
        stringLength = length;
109
    }
110
    expand(length);
111
112
    // Write the actual string
113
    memcpy(mData + CAST_SIZE(mPos), string.c_str(), stringLength);
114
115
    // Pad remaining space with zeros
116
    if (length > stringLength)
117
    {
118
        memset(mData + CAST_SIZE(mPos + stringLength),
119
            '\0',
120
            length - stringLength);
121
    }
122
123
    DEBUGLOG2("writeString: ***", mPos, str);
124
    mPos += length;
125
    PacketCounters::incOutBytes(length);
126
}
127
128
const char *MessageOut::getData() const
129
{
130
    return mData;
131
}
132
133
unsigned int MessageOut::getDataSize() const
134
{
135
    return mDataSize;
136
}
137
138
unsigned char MessageOut::toServerDirection(unsigned char direction)
139
{
140
    // Translate direction to eAthena format
141
    switch (direction)
142
    {
143
        case 1:  // DOWN
144
            direction = 0;
145
            break;
146
        case 3:  // DOWN | LEFT
147
            direction = 1;
148
            break;
149
        case 2:  // LEFT
150
            direction = 2;
151
            break;
152
        case 6:  // LEFT | UP
153
            direction = 3;
154
            break;
155
        case 4:  // UP
156
            direction = 4;
157
            break;
158
        case 12:  // UP | RIGHT
159
            direction = 5;
160
            break;
161
        case 8:  // RIGHT
162
            direction = 6;
163
            break;
164
        case 9:  // RIGHT + DOWN
165
            direction = 7;
166
            break;
167
        default:
168
            // OOPSIE! Impossible or unknown
169
            direction = CAST_U8(-1);
170
            break;
171
    }
172
    return direction;
173
}
174
175
}  // namespace Net