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
22#include "net/tmwa/messageout.h"
23
24#include "net/tmwa/network.h"
25
26#include "log.h"
27
28#include <SDL_endian.h>
29
30#include <cstring>
31
32namespace TmwAthena {
33
35{
36#ifdef DEBUG
37 Log::info("Sending %s (0x%x)", Network::mInstance->messageName(id), id);
38#endif
39 writeInt16(id);
40}
41
42char *MessageOut::expand(size_t bytes)
43{
45 char *data = net.mOutBuffer + net.mOutSize;
46 net.mOutSize += bytes;
47 return data;
48}
49
51{
52 *expand(1) = value;
53}
54
55void MessageOut::writeInt16(uint16_t value)
56{
57 value = SDL_SwapLE16(value);
58 memcpy(expand(sizeof(uint16_t)), &value, sizeof(uint16_t));
59}
60
62{
63 value = SDL_SwapLE32(value);
64 memcpy(expand(sizeof(uint32_t)), &value, sizeof(uint32_t));
65}
66
67void MessageOut::writeString(const std::string &string, int length)
68{
69 int stringLength = string.length();
70 if (length < 0)
71 {
72 // Write the length at the start if not fixed
73 writeInt16(stringLength);
74 length = stringLength;
75 }
76 else if (length < stringLength)
77 {
78 // Make sure the length of the string is no longer than specified
79 stringLength = length;
80 }
81
82 char *data = expand(length);
83
84 // Write the actual string
85 memcpy(data, string.data(), stringLength);
86
87 // Pad remaining space with zeros
88 if (length > stringLength)
89 {
90 memset(data + stringLength, '\0', length - stringLength);
91 }
92}
93
94void MessageOut::writeCoordinates(uint16_t x, uint16_t y, uint8_t direction)
95{
96 char *data = expand(3);
97
98 uint16_t temp = x;
99 temp <<= 6;
100 data[0] = temp >> 8;
101 data[1] = temp;
102
103 temp = y;
104 temp <<= 4;
105 data[1] |= temp >> 8;
106 data[2] = temp;
107
108 // Translate direction to eAthena format
109 switch (direction)
110 {
111 case 1:
112 direction = 0;
113 break;
114 case 3:
115 direction = 1;
116 break;
117 case 2:
118 direction = 2;
119 break;
120 case 6:
121 direction = 3;
122 break;
123 case 4:
124 direction = 4;
125 break;
126 case 12:
127 direction = 5;
128 break;
129 case 8:
130 direction = 6;
131 break;
132 case 9:
133 direction = 7;
134 break;
135 default:
136 // OOPSIE! Impossible or unknown
137 direction = 15;
138 }
139 data[2] |= direction;
140}
141
142} // namespace TmwAthena
void writeInt32(uint32_t value)
Writes an unsigned 32-bit integer to the message.
MessageOut(uint16_t id)
static char * expand(size_t size)
Expand the packet data to be able to hold more data.
void writeInt8(uint8_t value)
Writes an unsigned 8-bit integer to the message.
void writeInt16(uint16_t value)
Writes an unsigned 16-bit integer to the message.
void writeString(const std::string &string, int length=-1)
Writes a string.
void writeCoordinates(uint16_t x, uint16_t y, uint8_t direction)
Encodes coordinates and direction in 3 bytes.
unsigned int mOutSize
Definition network.h:112
static Network * mInstance
Definition network.h:125
void info(const char *log_text,...) LOG_PRINTF_ATTR
Warning: buffers and other variables are shared, so there can be only one connection active at a time...
unsigned char uint8_t
Definition sha256.cpp:81
unsigned int uint32_t
Definition sha256.cpp:82