ManaPlus
network.cpp
Go to the documentation of this file.
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  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "net/tmwa/network.h"
25 
26 #include "logger.h"
27 
28 #include "net/packetinfo.h"
29 
30 #include "net/ea/adminrecv.h"
31 #include "net/ea/beingrecv.h"
32 #include "net/ea/buysellrecv.h"
33 #include "net/ea/charserverrecv.h"
34 #include "net/ea/chatrecv.h"
35 #include "net/ea/gamerecv.h"
36 #include "net/ea/inventoryrecv.h"
37 #include "net/ea/itemrecv.h"
38 #include "net/ea/loginrecv.h"
39 #include "net/ea/maprecv.h"
40 #include "net/ea/npcrecv.h"
41 #include "net/ea/partyrecv.h"
42 #include "net/ea/playerrecv.h"
43 #include "net/ea/skillrecv.h"
44 #include "net/ea/traderecv.h"
45 
46 #include "net/tmwa/beingrecv.h"
47 #include "net/tmwa/buysellrecv.h"
49 #include "net/tmwa/chatrecv.h"
50 #include "net/tmwa/gamerecv.h"
51 #include "net/tmwa/generalrecv.h"
52 #include "net/tmwa/inventoryrecv.h"
53 #include "net/tmwa/itemrecv.h"
54 #include "net/tmwa/loginrecv.h"
55 #include "net/tmwa/partyrecv.h"
56 #include "net/tmwa/playerrecv.h"
57 #include "net/tmwa/questrecv.h"
58 #include "net/tmwa/skillrecv.h"
59 #include "net/tmwa/traderecv.h"
60 
61 #include "net/tmwa/messagein.h"
62 
63 #include "utils/cast.h"
64 
65 #include "debug.h"
66 
67 namespace TmwAthena
68 {
69 
70 static const unsigned int packet_lengths_size = 0xFFFFU;
71 static const unsigned int messagesSize = 0xFFFFU;
72 Network *Network::mInstance = nullptr;
73 
75  Ea::Network()
76 {
77  mInstance = this;
79 }
80 
82 {
83  clearHandlers();
84  mInstance = nullptr;
85 }
86 
88 {
89 #include "net/tmwa/recvpackets.inc"
90  RECVPACKETS_VOID
91 }
92 
94 {
95  for (size_t f = 0; f < packet_lengths_size; f ++)
96  {
97  mPackets[f].name = "";
98  mPackets[f].len = 0;
99  mPackets[f].func = nullptr;
100  mPackets[f].version = 0;
101  }
102 }
103 
105 {
106  BLOCK_START("Network::dispatchMessages 1")
107  mPauseDispatch = false;
108  while (messageReady())
109  {
110  SDL_mutexP(mMutexIn);
111  BLOCK_START("Network::dispatchMessages 2")
112  const unsigned int msgId = readWord(0);
113  int len = -1;
114  if (msgId < packet_lengths_size)
115  len = mPackets[msgId].len;
116 
117  if (len == -1)
118  len = readWord(2);
119 
120  MessageIn msg(mInBuffer, len);
121  msg.postInit(mPackets[msgId].name);
122  SDL_mutexV(mMutexIn);
123  BLOCK_END("Network::dispatchMessages 2")
124  BLOCK_START("Network::dispatchMessages 3")
125 
126  if (len == 0)
127  {
128  // need copy data for safty
129  std::string str = strprintf(
130  "Wrong packet %u 0x%x received. Exiting.",
131  msgId, msgId);
132  logger->safeError(str);
133  }
134 
135  if (msgId < messagesSize)
136  {
137  const PacketFuncPtr func = mPackets[msgId].func;
138  if (func != nullptr)
139  func(msg);
140  else
141  logger->log("Unhandled packet: %u 0x%x", msgId, msgId);
142  }
143 
144  skip(len);
145  if (mPauseDispatch)
146  {
147  BLOCK_END("Network::dispatchMessages 3")
148  break;
149  }
150  BLOCK_END("Network::dispatchMessages 3")
151  }
152  BLOCK_END("Network::dispatchMessages 1")
153 }
154 
156 {
157  int len = -1;
158 
159  SDL_mutexP(mMutexIn);
160  if (mInSize >= 2)
161  {
162  const int msgId = readWord(0);
163  if (msgId >= 0 && CAST_U32(msgId)
165  {
166  len = mPackets[msgId].len;
167  }
168 
169  if (len == -1 && mInSize > 4)
170  len = readWord(2);
171  }
172 
173  const bool ret = (mInSize >= CAST_U32(len));
174  SDL_mutexV(mMutexIn);
175 
176  return ret;
177 }
178 
180 {
181  return mInstance;
182 }
183 
184 } // namespace TmwAthena
#define CAST_U32
Definition: cast.h:31
SDL_mutex * mMutexIn
Definition: network.h:114
uint16_t readWord(const int pos) const
Definition: network.cpp:350
bool mPauseDispatch
Definition: network.h:117
PacketInfo * mPackets
Definition: network.h:101
char * mInBuffer
Definition: network.h:103
unsigned int mInSize
Definition: network.h:105
void skip(const int len)
Definition: network.cpp:181
void log(const char *const log_text,...)
Definition: logger.cpp:269
void safeError(const std::string &error_text) __attribute__((noreturn))
Definition: logger.cpp:435
void registerHandlers()
Definition: network.cpp:87
void dispatchMessages()
Definition: network.cpp:104
static Network * mInstance
Definition: network.h:54
static Network * instance()
Definition: network.cpp:179
void clearHandlers()
Definition: network.cpp:93
Logger * logger
Definition: logger.cpp:89
bool msg(InputEvent &event)
Definition: chat.cpp:39
static const unsigned int packet_lengths_size
Definition: network.cpp:70
static const unsigned int messagesSize
Definition: network.cpp:71
void(* PacketFuncPtr)(Net::MessageIn &msg)
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79
std::string strprintf(const char *const format,...)
PacketFuncPtr func
Definition: packetinfo.h:42
const char * name
Definition: packetinfo.h:41
int version
Definition: packetinfo.h:44