GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/network.cpp Lines: 0 84 0.0 %
Date: 2019-03-19 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/rouletterecv.h"
78
#include "net/eathena/searchstorerecv.h"
79
#include "net/eathena/skillrecv.h"
80
#include "net/eathena/traderecv.h"
81
#include "net/eathena/vendingrecv.h"
82
83
#include "net/eathena/messagein.h"
84
85
#include "resources/db/networkdb.h"
86
87
#include "utils/cast.h"
88
#include "utils/checkutils.h"
89
#include "utils/foreach.h"
90
91
#include "debug.h"
92
93
extern int packetVersion;
94
extern int packetVersionMain;
95
extern int packetVersionRe;
96
extern int packetVersionZero;
97
extern int serverVersion;
98
extern int evolPacketOffset;
99
extern bool packets_zero;
100
extern bool packets_re;
101
extern bool packets_main;
102
103
namespace EAthena
104
{
105
106
static const unsigned int packet_lengths_size = 0xFFFFU;
107
static const unsigned int messagesSize = 0xFFFFU;
108
Network *Network::mInstance = nullptr;
109
110
Network::Network() :
111
    Ea::Network()
112
{
113
    mInstance = this;
114
    mPackets = new PacketInfo[messagesSize];
115
}
116
117
Network::~Network()
118
{
119
    clearHandlers();
120
    mInstance = nullptr;
121
}
122
123
void Network::registerHandlers()
124
{
125
#include "net/eathena/recvpackets.inc"
126
    RECVPACKETS_VOID
127
}
128
129
void Network::registerFakeHandlers()
130
{
131
    const NetworkInPacketInfos &packets = NetworkDb::getFakePackets();
132
    FOR_EACH (NetworkInPacketInfosIter, it, packets)
133
    {
134
        const size_t id = (*it).first;
135
        if (id >= packet_lengths_size)
136
        {
137
            reportAlways("Wrong fake packet id %d", CAST_S32(id))
138
            continue;
139
        }
140
        if (mPackets[id].len != 0 ||
141
            mPackets[id].func != nullptr ||
142
            mPackets[id].version != 0)
143
        {
144
            continue;
145
        }
146
        const int32_t len = (*it).second;
147
        logger->log("Add fake packet: %d, %d",
148
            CAST_S32(id),
149
            len);
150
        mPackets[id].name = "fake";
151
        mPackets[id].len = len;
152
        mPackets[id].func = nullptr;
153
        mPackets[id].version = 0;
154
    }
155
156
    const NetworkRemovePacketInfos &removePackets =
157
        NetworkDb::getRemovePackets();
158
    FOR_EACH (NetworkRemovePacketInfosIter, it, removePackets)
159
    {
160
        const size_t id = *it;
161
        if (id >= packet_lengths_size)
162
        {
163
            reportAlways("Wrong remove packet id %d", CAST_S32(id))
164
            continue;
165
        }
166
        if (mPackets[id].len == 0 &&
167
            mPackets[id].func == nullptr &&
168
            mPackets[id].version == 0)
169
        {
170
            continue;
171
        }
172
        logger->log("Remove packet: %d",
173
            CAST_S32(id));
174
        mPackets[id].name = "";
175
        mPackets[id].len = 0;
176
        mPackets[id].func = nullptr;
177
        mPackets[id].version = 0;
178
    }
179
}
180
181
void Network::clearHandlers()
182
{
183
    for (size_t f = 0; f < packet_lengths_size; f ++)
184
    {
185
        mPackets[f].name = "";
186
        mPackets[f].len = 0;
187
        mPackets[f].func = nullptr;
188
        mPackets[f].version = 0;
189
    }
190
}
191
192
void Network::dispatchMessages()
193
{
194
    mPauseDispatch = false;
195
    while (messageReady())
196
    {
197
        SDL_mutexP(mMutexIn);
198
        const unsigned int msgId = readWord(0);
199
        int len = -1;
200
        if (msgId < packet_lengths_size)
201
            len = mPackets[msgId].len;
202
        if (len == -1)
203
            len = readWord(2);
204
205
        MessageIn msg(mInBuffer, len);
206
        unsigned int ver = mPackets[msgId].version;
207
        if (ver == 0)
208
            ver = packetVersion;
209
        msg.postInit(mPackets[msgId].name, ver);
210
        SDL_mutexV(mMutexIn);
211
212
        if (len == 0)
213
        {
214
            // need copy data for safty
215
            std::string str = strprintf(
216
                "Wrong packet %u 0x%x received. Exiting.",
217
                msgId, msgId);
218
            logger->safeError(str);
219
        }
220
221
        if (msgId < messagesSize)
222
        {
223
            const PacketFuncPtr func = mPackets[msgId].func;
224
            if (func != nullptr)
225
                func(msg);
226
            else
227
                logger->log("Unhandled packet: %u 0x%x", msgId, msgId);
228
        }
229
230
        skip(len);
231
        if (mPauseDispatch)
232
            break;
233
    }
234
}
235
236
bool Network::messageReady()
237
{
238
    int len = -1;
239
240
    SDL_mutexP(mMutexIn);
241
    if (mInSize >= 2)
242
    {
243
        const int msgId = readWord(0);
244
        if (msgId >= 0 &&
245
            CAST_U32(msgId) < packet_lengths_size)
246
        {
247
            len = mPackets[msgId].len;
248
        }
249
250
        if (len == -1 && mInSize > 4)
251
            len = readWord(2);
252
    }
253
254
    const bool ret = (mInSize >= CAST_U32(len));
255
    SDL_mutexV(mMutexIn);
256
257
    return ret;
258
}
259
260
Network *Network::instance()
261
{
262
    return mInstance;
263
}
264
265
}  // namespace EAthena