GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/tmwa/network.cpp Lines: 0 51 0.0 %
Date: 2017-11-29 Branches: 0 42 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/tmwa/network.h"
24
25
#include "logger.h"
26
27
#include "net/packetinfo.h"
28
29
#include "net/ea/adminrecv.h"
30
#include "net/ea/beingrecv.h"
31
#include "net/ea/buysellrecv.h"
32
#include "net/ea/charserverrecv.h"
33
#include "net/ea/chatrecv.h"
34
#include "net/ea/gamerecv.h"
35
#include "net/ea/inventoryrecv.h"
36
#include "net/ea/itemrecv.h"
37
#include "net/ea/loginrecv.h"
38
#include "net/ea/maprecv.h"
39
#include "net/ea/npcrecv.h"
40
#include "net/ea/partyrecv.h"
41
#include "net/ea/playerrecv.h"
42
#include "net/ea/skillrecv.h"
43
#include "net/ea/traderecv.h"
44
45
#include "net/tmwa/beingrecv.h"
46
#include "net/tmwa/buysellrecv.h"
47
#include "net/tmwa/charserverrecv.h"
48
#include "net/tmwa/chatrecv.h"
49
#include "net/tmwa/gamerecv.h"
50
#include "net/tmwa/generalrecv.h"
51
#include "net/tmwa/inventoryrecv.h"
52
#include "net/tmwa/itemrecv.h"
53
#include "net/tmwa/loginrecv.h"
54
#include "net/tmwa/partyrecv.h"
55
#include "net/tmwa/playerrecv.h"
56
#include "net/tmwa/questrecv.h"
57
#include "net/tmwa/skillrecv.h"
58
#include "net/tmwa/traderecv.h"
59
60
#include "net/tmwa/messagein.h"
61
62
#include "utils/cast.h"
63
64
#include "debug.h"
65
66
namespace TmwAthena
67
{
68
69
static const unsigned int packet_lengths_size = 0xFFFFU;
70
static const unsigned int messagesSize = 0xFFFFU;
71
Network *Network::mInstance = nullptr;
72
73
Network::Network() :
74
    Ea::Network()
75
{
76
    mInstance = this;
77
    mPackets = new PacketInfo[messagesSize];
78
}
79
80
Network::~Network()
81
{
82
    clearHandlers();
83
    mInstance = nullptr;
84
}
85
86
void Network::registerHandlers()
87
{
88
#include "net/tmwa/recvpackets.inc"
89
    RECVPACKETS_VOID
90
}
91
92
void Network::clearHandlers()
93
{
94
    for (size_t f = 0; f < packet_lengths_size; f ++)
95
    {
96
        mPackets[f].name = "";
97
        mPackets[f].len = 0;
98
        mPackets[f].func = nullptr;
99
        mPackets[f].version = 0;
100
    }
101
}
102
103
void Network::dispatchMessages()
104
{
105
    BLOCK_START("Network::dispatchMessages 1")
106
    mPauseDispatch = false;
107
    while (messageReady())
108
    {
109
        SDL_mutexP(mMutexIn);
110
        BLOCK_START("Network::dispatchMessages 2")
111
        const unsigned int msgId = readWord(0);
112
        int len = -1;
113
        if (msgId < packet_lengths_size)
114
            len = mPackets[msgId].len;
115
116
        if (len == -1)
117
            len = readWord(2);
118
119
        MessageIn msg(mInBuffer, len);
120
        msg.postInit(mPackets[msgId].name);
121
        SDL_mutexV(mMutexIn);
122
        BLOCK_END("Network::dispatchMessages 2")
123
        BLOCK_START("Network::dispatchMessages 3")
124
125
        if (len == 0)
126
        {
127
            // need copy data for safty
128
            std::string str = strprintf(
129
                "Wrong packet %u 0x%x received. Exiting.",
130
                msgId, msgId);
131
            logger->safeError(str);
132
        }
133
134
        if (msgId < messagesSize)
135
        {
136
            const PacketFuncPtr func = mPackets[msgId].func;
137
            if (func != nullptr)
138
                func(msg);
139
            else
140
                logger->log("Unhandled packet: %u 0x%x", msgId, msgId);
141
        }
142
143
        skip(len);
144
        if (mPauseDispatch)
145
        {
146
            BLOCK_END("Network::dispatchMessages 3")
147
            break;
148
        }
149
        BLOCK_END("Network::dispatchMessages 3")
150
    }
151
    BLOCK_END("Network::dispatchMessages 1")
152
}
153
154
bool Network::messageReady()
155
{
156
    int len = -1;
157
158
    SDL_mutexP(mMutexIn);
159
    if (mInSize >= 2)
160
    {
161
        const int msgId = readWord(0);
162
        if (msgId >= 0 && CAST_U32(msgId)
163
            < packet_lengths_size)
164
        {
165
            len = mPackets[msgId].len;
166
        }
167
168
        if (len == -1 && mInSize > 4)
169
            len = readWord(2);
170
    }
171
172
    const bool ret = (mInSize >= CAST_U32(len));
173
    SDL_mutexV(mMutexIn);
174
175
    return ret;
176
}
177
178
Network *Network::instance()
179
{
180
    return mInstance;
181
}
182
183
}  // namespace TmwAthena