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