GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/tmwa/messageout.cpp Lines: 0 40 0.0 %
Date: 2017-11-29 Branches: 0 26 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/tmwa/messageout.h"
24
25
#include "net/packetcounters.h"
26
27
#include "net/tmwa/network.h"
28
29
#include "logger.h"
30
31
#include "debug.h"
32
33
#ifndef SDL_BIG_ENDIAN
34
#error missing SDL_endian.h
35
#endif  // SDL_BYTEORDER
36
37
namespace TmwAthena
38
{
39
40
MessageOut::MessageOut(const int16_t id) :
41
    Net::MessageOut(id),
42
    mNetwork(TmwAthena::Network::instance())
43
{
44
    mNetwork->fixSendBuffer();
45
    mData = mNetwork->mOutBuffer + CAST_SIZE(mNetwork->mOutSize);
46
}
47
48
MessageOut::~MessageOut()
49
{
50
    DEBUGLOG2("writeEnd: ", mPos, "position after end of packet");
51
}
52
53
void MessageOut::expand(const size_t bytes) const
54
{
55
    mNetwork->mOutSize += CAST_U32(bytes);
56
    PacketCounters::incOutBytes(CAST_S32(bytes));
57
}
58
59
void MessageOut::writeInt16(const int16_t value, const char *const str)
60
{
61
    DEBUGLOG2("writeInt16: " + toStringPrint(CAST_U32(
62
        CAST_U16(value))),
63
        mPos, str);
64
    expand(2);
65
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
66
    int16_t swap = SDL_Swap16(value);
67
    memcpy(mData + CAST_SIZE(mPos), &swap, sizeof(int16_t));
68
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
69
70
    memcpy(mData + CAST_SIZE(mPos), &value, sizeof(int16_t));
71
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
72
73
    mPos += 2;
74
    PacketCounters::incOutBytes(2);
75
}
76
77
void MessageOut::writeInt32(const int32_t value, const char *const str)
78
{
79
    DEBUGLOG2("writeInt32: " + toStringPrint(CAST_U32(value)),
80
        mPos, str);
81
    expand(4);
82
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
83
    int32_t swap = SDL_Swap32(value);
84
    memcpy(mData + CAST_SIZE(mPos), &swap, sizeof(int32_t));
85
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
86
87
    memcpy(mData + CAST_SIZE(mPos), &value, sizeof(int32_t));
88
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
89
90
    mPos += 4;
91
    PacketCounters::incOutBytes(4);
92
}
93
94
void MessageOut::writeBeingId(const BeingId value, const char *const str)
95
{
96
    writeInt32(toInt(value, int32_t), str);
97
}
98
99
#define LOBYTE(w)  (CAST_U8(w))
100
#define HIBYTE(w)  (CAST_U8(( \
101
CAST_U16(w)) >> 8U))
102
103
void MessageOut::writeCoordinates(const uint16_t x,
104
                                  const uint16_t y,
105
                                  unsigned char direction,
106
                                  const char *const str)
107
{
108
    DEBUGLOG2(strprintf("writeCoordinates: %u,%u %u",
109
        CAST_U32(x), static_cast<unsigned>(y),
110
        CAST_U32(direction)), mPos, str);
111
    unsigned char *const data = reinterpret_cast<unsigned char*>(mData)
112
        + CAST_SIZE(mPos);
113
    mNetwork->mOutSize += 3;
114
    mPos += 3;
115
116
    uint16_t temp = x;
117
    temp <<= 6;
118
    data[0] = 0;
119
    data[1] = 1;
120
    data[2] = 2;
121
    data[0] = HIBYTE(temp);
122
    data[1] = CAST_U8(temp);
123
    temp = y;
124
    temp <<= 4;
125
    data[1] |= HIBYTE(temp);
126
    data[2] = LOBYTE(temp);
127
    direction = toServerDirection(direction);
128
    data[2] |= direction;
129
    PacketCounters::incOutBytes(3);
130
}
131
132
}  // namespace TmwAthena