GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/network.cpp Lines: 0 84 0.0 %
Date: 2019-06-27 Branches: 0 68 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-2019  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/eathena/network.h"
24
25
#include "net/packetinfo.h"
26
27
#include "net/ea/adminrecv.h"
28
#include "net/ea/beingrecv.h"
29
#include "net/ea/buysellrecv.h"
30
#include "net/ea/charserverrecv.h"
31
#include "net/ea/chatrecv.h"
32
#include "net/ea/gamerecv.h"
33
#include "net/ea/inventoryrecv.h"
34
#include "net/ea/itemrecv.h"
35
#include "net/ea/loginrecv.h"
36
#include "net/ea/maprecv.h"
37
#include "net/ea/npcrecv.h"
38
#include "net/ea/partyrecv.h"
39
#include "net/ea/playerrecv.h"
40
#include "net/ea/skillrecv.h"
41
#include "net/ea/traderecv.h"
42
43
#include "net/eathena/achievementrecv.h"
44
#include "net/eathena/adminrecv.h"
45
#include "net/eathena/auctionrecv.h"
46
#include "net/eathena/attendancerecv.h"
47
#include "net/eathena/bankrecv.h"
48
#include "net/eathena/barterrecv.h"
49
#include "net/eathena/battlegroundrecv.h"
50
#include "net/eathena/beingrecv.h"
51
#include "net/eathena/buyingstorerecv.h"
52
#include "net/eathena/buysellrecv.h"
53
#include "net/eathena/cashshoprecv.h"
54
#include "net/eathena/charserverrecv.h"
55
#include "net/eathena/chatrecv.h"
56
#include "net/eathena/clanrecv.h"
57
#include "net/eathena/elementalrecv.h"
58
#include "net/eathena/familyrecv.h"
59
#include "net/eathena/friendsrecv.h"
60
#include "net/eathena/gamerecv.h"
61
#include "net/eathena/generalrecv.h"
62
#include "net/eathena/guildrecv.h"
63
#include "net/eathena/homunculusrecv.h"
64
#include "net/eathena/inventoryrecv.h"
65
#include "net/eathena/itemrecv.h"
66
#include "net/eathena/loginrecv.h"
67
#include "net/eathena/mail2recv.h"
68
#include "net/eathena/mailrecv.h"
69
#include "net/eathena/maprecv.h"
70
#include "net/eathena/marketrecv.h"
71
#include "net/eathena/mercenaryrecv.h"
72
#include "net/eathena/npcrecv.h"
73
#include "net/eathena/partyrecv.h"
74
#include "net/eathena/petrecv.h"
75
#include "net/eathena/playerrecv.h"
76
#include "net/eathena/questrecv.h"
77
#include "net/eathena/refinerecv.h"
78
#include "net/eathena/rouletterecv.h"
79
#include "net/eathena/searchstorerecv.h"
80
#include "net/eathena/skillrecv.h"
81
#include "net/eathena/traderecv.h"
82
#include "net/eathena/vendingrecv.h"
83
84
#include "net/eathena/messagein.h"
85
86
#include "resources/db/networkdb.h"
87
88
#include "utils/cast.h"
89
#include "utils/checkutils.h"
90
#include "utils/foreach.h"
91
92
#include "debug.h"
93
94
extern int packetVersion;
95
extern int packetVersionMain;
96
extern int packetVersionRe;
97
extern int packetVersionZero;
98
extern int serverVersion;
99
extern int evolPacketOffset;
100
extern bool packets_zero;
101
extern bool packets_re;
102
extern bool packets_main;
103
104
namespace EAthena
105
{
106
107
static const unsigned int packet_lengths_size = 0xFFFFU;
108
static const unsigned int messagesSize = 0xFFFFU;
109
Network *Network::mInstance = nullptr;
110
111
Network::Network() :
112
    Ea::Network()
113
{
114
    mInstance = this;
115
    mPackets = new PacketInfo[messagesSize];
116
}
117
118
Network::~Network()
119
{
120
    clearHandlers();
121
    mInstance = nullptr;
122
}
123
124
void Network::registerHandlers()
125
{
126
#include "net/eathena/recvpackets.inc"
127
    RECVPACKETS_VOID
128
}
129
130
void Network::registerFakeHandlers()
131
{
132
    const NetworkInPacketInfos &packets = NetworkDb::getFakePackets();
133
    FOR_EACH (NetworkInPacketInfosIter, it, packets)
134
    {
135
        const size_t id = (*it).first;
136
        if (id >= packet_lengths_size)
137
        {
138
            reportAlways("Wrong fake packet id %d", CAST_S32(id))
139
            continue;
140
        }
141
        if (mPackets[id].len != 0 ||
142
            mPackets[id].func != nullptr ||
143
            mPackets[id].version != 0)
144
        {
145
            continue;
146
        }
147
        const int32_t len = (*it).second;
148
        logger->log("Add fake packet: %d, %d",
149
            CAST_S32(id),
150
            len);
151
        mPackets[id].name = "fake";
152
        mPackets[id].len = len;
153
        mPackets[id].func = nullptr;
154
        mPackets[id].version = 0;
155
    }
156
157
    const NetworkRemovePacketInfos &removePackets =
158
        NetworkDb::getRemovePackets();
159
    FOR_EACH (NetworkRemovePacketInfosIter, it, removePackets)
160
    {
161
        const size_t id = *it;
162
        if (id >= packet_lengths_size)
163
        {
164
            reportAlways("Wrong remove packet id %d", CAST_S32(id))
165
            continue;
166
        }
167
        if (mPackets[id].len == 0 &&
168
            mPackets[id].func == nullptr &&
169
            mPackets[id].version == 0)
170
        {
171
            continue;
172
        }
173
        logger->log("Remove packet: %d",
174
            CAST_S32(id));
175
        mPackets[id].name = "";
176
        mPackets[id].len = 0;
177
        mPackets[id].func = nullptr;
178
        mPackets[id].version = 0;
179
    }
180
}
181
182
void Network::clearHandlers()
183
{
184
    for (size_t f = 0; f < packet_lengths_size; f ++)
185
    {
186
        mPackets[f].name = "";
187
        mPackets[f].len = 0;
188
        mPackets[f].func = nullptr;
189
        mPackets[f].version = 0;
190
    }
191
}
192
193
void Network::dispatchMessages()
194
{
195
    mPauseDispatch = false;
196
    while (messageReady())
197
    {
198
        SDL_mutexP(mMutexIn);
199
        const unsigned int msgId = readWord(0);
200
        int len = -1;
201
        if (msgId < packet_lengths_size)
202
            len = mPackets[msgId].len;
203
        if (len == -1)
204
            len = readWord(2);
205
206
        MessageIn msg(mInBuffer, len);
207
        unsigned int ver = mPackets[msgId].version;
208
        if (ver == 0)
209
            ver = packetVersion;
210
        msg.postInit(mPackets[msgId].name, ver);
211
        SDL_mutexV(mMutexIn);
212
213
        if (len == 0)
214
        {
215
            // need copy data for safty
216
            std::string str = strprintf(
217
                "Wrong packet %u 0x%x received. Exiting.",
218
                msgId, msgId);
219
            logger->safeError(str);
220
        }
221
222
        if (msgId < messagesSize)
223
        {
224
            const PacketFuncPtr func = mPackets[msgId].func;
225
            if (func != nullptr)
226
                func(msg);
227
            else
228
                logger->log("Unhandled packet: %u 0x%x", msgId, msgId);
229
        }
230
231
        skip(len);
232
        if (mPauseDispatch)
233
            break;
234
    }
235
}
236
237
bool Network::messageReady()
238
{
239
    int len = -1;
240
241
    SDL_mutexP(mMutexIn);
242
    if (mInSize >= 2)
243
    {
244
        const int msgId = readWord(0);
245
        if (msgId >= 0 &&
246
            CAST_U32(msgId) < packet_lengths_size)
247
        {
248
            len = mPackets[msgId].len;
249
        }
250
251
        if (len == -1 && mInSize > 4)
252
            len = readWord(2);
253
    }
254
255
    const bool ret = (mInSize >= CAST_U32(len));
256
    SDL_mutexV(mMutexIn);
257
258
    return ret;
259
}
260
261
Network *Network::instance()
262
{
263
    return mInstance;
264
}
265
266
}  // namespace EAthena