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-2019 The ManaPlus Developers |
6 |
|
|
* Copyright (C) 2019-2021 Andrei Karas |
7 |
|
|
* |
8 |
|
|
* This file is part of The ManaPlus Client. |
9 |
|
|
* |
10 |
|
|
* This program is free software; you can redistribute it and/or modify |
11 |
|
|
* it under the terms of the GNU General Public License as published by |
12 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
13 |
|
|
* any later version. |
14 |
|
|
* |
15 |
|
|
* This program is distributed in the hope that it will be useful, |
16 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
|
|
* GNU General Public License for more details. |
19 |
|
|
* |
20 |
|
|
* You should have received a copy of the GNU General Public License |
21 |
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
22 |
|
|
*/ |
23 |
|
|
|
24 |
|
|
#include "net/tmwa/network.h" |
25 |
|
|
|
26 |
|
|
#include "logger.h" |
27 |
|
|
|
28 |
|
|
#include "net/packetinfo.h" |
29 |
|
|
|
30 |
|
|
#include "net/ea/adminrecv.h" |
31 |
|
|
#include "net/ea/beingrecv.h" |
32 |
|
|
#include "net/ea/buysellrecv.h" |
33 |
|
|
#include "net/ea/charserverrecv.h" |
34 |
|
|
#include "net/ea/chatrecv.h" |
35 |
|
|
#include "net/ea/gamerecv.h" |
36 |
|
|
#include "net/ea/inventoryrecv.h" |
37 |
|
|
#include "net/ea/itemrecv.h" |
38 |
|
|
#include "net/ea/loginrecv.h" |
39 |
|
|
#include "net/ea/maprecv.h" |
40 |
|
|
#include "net/ea/npcrecv.h" |
41 |
|
|
#include "net/ea/partyrecv.h" |
42 |
|
|
#include "net/ea/playerrecv.h" |
43 |
|
|
#include "net/ea/skillrecv.h" |
44 |
|
|
#include "net/ea/traderecv.h" |
45 |
|
|
|
46 |
|
|
#include "net/tmwa/beingrecv.h" |
47 |
|
|
#include "net/tmwa/buysellrecv.h" |
48 |
|
|
#include "net/tmwa/charserverrecv.h" |
49 |
|
|
#include "net/tmwa/chatrecv.h" |
50 |
|
|
#include "net/tmwa/gamerecv.h" |
51 |
|
|
#include "net/tmwa/generalrecv.h" |
52 |
|
|
#include "net/tmwa/inventoryrecv.h" |
53 |
|
|
#include "net/tmwa/itemrecv.h" |
54 |
|
|
#include "net/tmwa/loginrecv.h" |
55 |
|
|
#include "net/tmwa/partyrecv.h" |
56 |
|
|
#include "net/tmwa/playerrecv.h" |
57 |
|
|
#include "net/tmwa/questrecv.h" |
58 |
|
|
#include "net/tmwa/skillrecv.h" |
59 |
|
|
#include "net/tmwa/traderecv.h" |
60 |
|
|
|
61 |
|
|
#include "net/tmwa/messagein.h" |
62 |
|
|
|
63 |
|
|
#include "utils/cast.h" |
64 |
|
|
|
65 |
|
|
#include "debug.h" |
66 |
|
|
|
67 |
|
|
namespace TmwAthena |
68 |
|
|
{ |
69 |
|
|
|
70 |
|
|
static const unsigned int packet_lengths_size = 0xFFFFU; |
71 |
|
|
static const unsigned int messagesSize = 0xFFFFU; |
72 |
|
|
Network *Network::mInstance = nullptr; |
73 |
|
|
|
74 |
|
|
Network::Network() : |
75 |
|
|
Ea::Network() |
76 |
|
|
{ |
77 |
|
|
mInstance = this; |
78 |
|
|
mPackets = new PacketInfo[messagesSize]; |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
Network::~Network() |
82 |
|
|
{ |
83 |
|
|
clearHandlers(); |
84 |
|
|
mInstance = nullptr; |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
void Network::registerHandlers() |
88 |
|
|
{ |
89 |
|
|
#include "net/tmwa/recvpackets.inc" |
90 |
|
|
RECVPACKETS_VOID |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
void Network::clearHandlers() |
94 |
|
|
{ |
95 |
|
|
for (size_t f = 0; f < packet_lengths_size; f ++) |
96 |
|
|
{ |
97 |
|
|
mPackets[f].name = ""; |
98 |
|
|
mPackets[f].len = 0; |
99 |
|
|
mPackets[f].func = nullptr; |
100 |
|
|
mPackets[f].version = 0; |
101 |
|
|
} |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
void Network::dispatchMessages() |
105 |
|
|
{ |
106 |
|
|
BLOCK_START("Network::dispatchMessages 1") |
107 |
|
|
mPauseDispatch = false; |
108 |
|
|
while (messageReady()) |
109 |
|
|
{ |
110 |
|
|
SDL_mutexP(mMutexIn); |
111 |
|
|
BLOCK_START("Network::dispatchMessages 2") |
112 |
|
|
const unsigned int msgId = readWord(0); |
113 |
|
|
int len = -1; |
114 |
|
|
if (msgId < packet_lengths_size) |
115 |
|
|
len = mPackets[msgId].len; |
116 |
|
|
|
117 |
|
|
if (len == -1) |
118 |
|
|
len = readWord(2); |
119 |
|
|
|
120 |
|
|
MessageIn msg(mInBuffer, len); |
121 |
|
|
msg.postInit(mPackets[msgId].name); |
122 |
|
|
SDL_mutexV(mMutexIn); |
123 |
|
|
BLOCK_END("Network::dispatchMessages 2") |
124 |
|
|
BLOCK_START("Network::dispatchMessages 3") |
125 |
|
|
|
126 |
|
|
if (len == 0) |
127 |
|
|
{ |
128 |
|
|
// need copy data for safty |
129 |
|
|
std::string str = strprintf( |
130 |
|
|
"Wrong packet %u 0x%x received. Exiting.", |
131 |
|
|
msgId, msgId); |
132 |
|
|
logger->safeError(str); |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
if (msgId < messagesSize) |
136 |
|
|
{ |
137 |
|
|
const PacketFuncPtr func = mPackets[msgId].func; |
138 |
|
|
if (func != nullptr) |
139 |
|
|
func(msg); |
140 |
|
|
else |
141 |
|
|
logger->log("Unhandled packet: %u 0x%x", msgId, msgId); |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
skip(len); |
145 |
|
|
if (mPauseDispatch) |
146 |
|
|
{ |
147 |
|
|
BLOCK_END("Network::dispatchMessages 3") |
148 |
|
|
break; |
149 |
|
|
} |
150 |
|
|
BLOCK_END("Network::dispatchMessages 3") |
151 |
|
|
} |
152 |
|
|
BLOCK_END("Network::dispatchMessages 1") |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
bool Network::messageReady() |
156 |
|
|
{ |
157 |
|
|
int len = -1; |
158 |
|
|
|
159 |
|
|
SDL_mutexP(mMutexIn); |
160 |
|
|
if (mInSize >= 2) |
161 |
|
|
{ |
162 |
|
|
const int msgId = readWord(0); |
163 |
|
|
if (msgId >= 0 && CAST_U32(msgId) |
164 |
|
|
< packet_lengths_size) |
165 |
|
|
{ |
166 |
|
|
len = mPackets[msgId].len; |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
if (len == -1 && mInSize > 4) |
170 |
|
|
len = readWord(2); |
171 |
|
|
} |
172 |
|
|
|
173 |
|
|
const bool ret = (mInSize >= CAST_U32(len)); |
174 |
|
|
SDL_mutexV(mMutexIn); |
175 |
|
|
|
176 |
|
|
return ret; |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
Network *Network::instance() |
180 |
|
|
{ |
181 |
|
|
return mInstance; |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
} // namespace TmwAthena |