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