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