ManaPlus
messageout.cpp
Go to the documentation of this file.
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-2018 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 {
46 }
47 
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);
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;
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;
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;
130 }
131 
132 } // namespace TmwAthena
#define CAST_U32
Definition: cast.h:30
#define CAST_U8
Definition: cast.h:26
std::string toStringPrint(const unsigned int val)
static unsigned char toServerDirection(unsigned char direction) A_CONST
Definition: messageout.cpp:138
void writeInt32(const int32_t value, const char *const str)
Definition: messageout.cpp:77
#define DEBUGLOG2(str, pos, comment)
Definition: logger.h:41
static void incOutBytes(const int cnt)
void writeBeingId(const BeingId value, const char *const str)
Definition: messageout.cpp:94
int BeingId
Definition: beingid.h:29
void fixSendBuffer()
Definition: network.cpp:361
unsigned int mPos
Definition: messageout.h:116
void writeCoordinates(const uint16_t x, const uint16_t y, unsigned char direction, const char *const str)
Definition: messageout.cpp:103
char * mOutBuffer
Definition: network.h:103
#define toInt(val, name)
Definition: intdefines.h:46
#define CAST_U16
Definition: cast.h:28
#define CAST_S32
Definition: cast.h:29
std::string strprintf(const char *const format,...)
Definition: stringutils.cpp:99
uint32_t data
#define HIBYTE(w)
Definition: messageout.cpp:100
void expand(const size_t size) const
Definition: messageout.cpp:53
#define LOBYTE(w)
Definition: messageout.cpp:99
#define CAST_SIZE
Definition: cast.h:33
unsigned int mOutSize
Definition: network.h:105
void writeInt16(const int16_t value, const char *const str)
Definition: messageout.cpp:59
MessageOut(const int16_t id)
Definition: messageout.cpp:40