ManaPlus
Public Member Functions | Static Public Attributes | Static Protected Member Functions | Friends
EAthena::Network Class Reference

#include <network.h>

Inheritance diagram for EAthena::Network:
Ea::Network

Public Member Functions

 Network ()
 
 ~Network ()
 
void clearHandlers ()
 
bool messageReady ()
 
void dispatchMessages ()
 
void registerHandlers ()
 
void registerFakeHandlers ()
 
- Public Member Functions inherited from Ea::Network
 Network ()
 
bool connect (const ServerInfo &server)
 
void disconnect ()
 
ServerInfo getServer () const
 
int getState () const
 
const std::string & getError () const
 
bool isConnected () const
 
int getInSize () const
 
void skip (const int len)
 
void flush ()
 
void fixSendBuffer ()
 
void pauseDispatch ()
 

Static Public Attributes

static NetworkmInstance = 0
 

Static Protected Member Functions

static Networkinstance ()
 

Friends

class MessageOut
 

Additional Inherited Members

- Public Types inherited from Ea::Network
enum  {
  IDLE = 0 , CONNECTED , CONNECTING , DATA ,
  NET_ERROR
}
 
- Protected Member Functions inherited from Ea::Network
void setError (const std::string &error)
 
uint16_t readWord (const int pos) const
 
bool realConnect ()
 
void receive ()
 
- Protected Attributes inherited from Ea::Network
TcpNet::Socket mSocket
 
ServerInfo mServer
 
PacketInfomPackets
 
char * mInBuffer
 
char * mOutBuffer
 
unsigned int mInSize
 
unsigned int mOutSize
 
unsigned int mToSkip
 
int mState
 
std::string mError
 
SDL_Thread * mWorkerThread
 
SDL_mutex * mMutexIn
 
SDL_mutex * mMutexOut
 
int mSleep
 
bool mPauseDispatch
 

Detailed Description

Definition at line 38 of file network.h.

Constructor & Destructor Documentation

◆ Network()

EAthena::Network::Network ( )

Definition at line 112 of file network.cpp.

112  :
113  Ea::Network()
114 {
115  mInstance = this;
117 }
static Network * mInstance
Definition: network.h:57
PacketInfo * mPackets
Definition: network.h:101
static const unsigned int messagesSize
Definition: network.cpp:109

References EAthena::messagesSize, mInstance, and Ea::Network::mPackets.

◆ ~Network()

EAthena::Network::~Network ( )
virtual

Reimplemented from Ea::Network.

Definition at line 119 of file network.cpp.

120 {
121  clearHandlers();
122  mInstance = nullptr;
123 }
void clearHandlers()
Definition: network.cpp:183

References clearHandlers(), and mInstance.

Member Function Documentation

◆ clearHandlers()

void EAthena::Network::clearHandlers ( )

Definition at line 183 of file network.cpp.

184 {
185  for (size_t f = 0; f < packet_lengths_size; f ++)
186  {
187  mPackets[f].name = "";
188  mPackets[f].len = 0;
189  mPackets[f].func = nullptr;
190  mPackets[f].version = 0;
191  }
192 }
static const unsigned int packet_lengths_size
Definition: network.cpp:108
PacketFuncPtr func
Definition: packetinfo.h:42
const char * name
Definition: packetinfo.h:41
int version
Definition: packetinfo.h:44

References PacketInfo::func, PacketInfo::len, Ea::Network::mPackets, PacketInfo::name, EAthena::packet_lengths_size, and PacketInfo::version.

Referenced by EAthena::GeneralHandler::clearHandlers(), EAthena::updateProtocol(), and ~Network().

◆ dispatchMessages()

void EAthena::Network::dispatchMessages ( )

Definition at line 194 of file network.cpp.

195 {
196  mPauseDispatch = false;
197  while (messageReady())
198  {
199  SDL_mutexP(mMutexIn);
200  const unsigned int msgId = readWord(0);
201  int len = -1;
202  if (msgId < packet_lengths_size)
203  len = mPackets[msgId].len;
204  if (len == -1)
205  len = readWord(2);
206 
207  MessageIn msg(mInBuffer, len);
208  unsigned int ver = mPackets[msgId].version;
209  if (ver == 0)
210  ver = packetVersion;
211  msg.postInit(mPackets[msgId].name, ver);
212  SDL_mutexV(mMutexIn);
213 
214  if (len == 0)
215  {
216  // need copy data for safty
217  std::string str = strprintf(
218  "Wrong packet %u 0x%x received. Exiting.",
219  msgId, msgId);
220  logger->safeError(str);
221  }
222 
223  if (msgId < messagesSize)
224  {
225  const PacketFuncPtr func = mPackets[msgId].func;
226  if (func != nullptr)
227  func(msg);
228  else
229  logger->log("Unhandled packet: %u 0x%x", msgId, msgId);
230  }
231 
232  skip(len);
233  if (mPauseDispatch)
234  break;
235  }
236 }
bool messageReady()
Definition: network.cpp:238
SDL_mutex * mMutexIn
Definition: network.h:114
uint16_t readWord(const int pos) const
Definition: network.cpp:350
bool mPauseDispatch
Definition: network.h:117
char * mInBuffer
Definition: network.h:103
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
int packetVersion
Definition: client.cpp:125
Logger * logger
Definition: logger.cpp:89
bool msg(InputEvent &event)
Definition: chat.cpp:39
void(* PacketFuncPtr)(Net::MessageIn &msg)
std::string strprintf(const char *const format,...)

References PacketInfo::func, PacketInfo::len, Logger::log(), logger, messageReady(), EAthena::messagesSize, Ea::Network::mInBuffer, Ea::Network::mMutexIn, Ea::Network::mPackets, Ea::Network::mPauseDispatch, Actions::msg(), EAthena::packet_lengths_size, packetVersion, Ea::Network::readWord(), Logger::safeError(), Ea::Network::skip(), strprintf(), and PacketInfo::version.

Referenced by EAthena::GeneralHandler::flushNetwork().

◆ instance()

Network * EAthena::Network::instance ( )
staticprotected

Definition at line 262 of file network.cpp.

263 {
264  return mInstance;
265 }

References mInstance.

◆ messageReady()

bool EAthena::Network::messageReady ( )

Definition at line 238 of file network.cpp.

239 {
240  int len = -1;
241 
242  SDL_mutexP(mMutexIn);
243  if (mInSize >= 2)
244  {
245  const int msgId = readWord(0);
246  if (msgId >= 0 &&
247  CAST_U32(msgId) < packet_lengths_size)
248  {
249  len = mPackets[msgId].len;
250  }
251 
252  if (len == -1 && mInSize > 4)
253  len = readWord(2);
254  }
255 
256  const bool ret = (mInSize >= CAST_U32(len));
257  SDL_mutexV(mMutexIn);
258 
259  return ret;
260 }
#define CAST_U32
Definition: cast.h:31
unsigned int mInSize
Definition: network.h:105

References CAST_U32, PacketInfo::len, Ea::Network::mInSize, Ea::Network::mMutexIn, Ea::Network::mPackets, EAthena::packet_lengths_size, and Ea::Network::readWord().

Referenced by dispatchMessages().

◆ registerFakeHandlers()

void EAthena::Network::registerFakeHandlers ( )

Definition at line 131 of file network.cpp.

132 {
134  FOR_EACH (NetworkInPacketInfosIter, it, packets)
135  {
136  const size_t id = (*it).first;
137  if (id >= packet_lengths_size)
138  {
139  reportAlways("Wrong fake packet id %d", CAST_S32(id))
140  continue;
141  }
142  if (mPackets[id].len != 0 ||
143  mPackets[id].func != nullptr ||
144  mPackets[id].version != 0)
145  {
146  continue;
147  }
148  const int32_t len = (*it).second;
149  logger->log("Add fake packet: %d, %d",
150  CAST_S32(id),
151  len);
152  mPackets[id].name = "fake";
153  mPackets[id].len = len;
154  mPackets[id].func = nullptr;
155  mPackets[id].version = 0;
156  }
157 
158  const NetworkRemovePacketInfos &removePackets =
160  FOR_EACH (NetworkRemovePacketInfosIter, it, removePackets)
161  {
162  const size_t id = *it;
163  if (id >= packet_lengths_size)
164  {
165  reportAlways("Wrong remove packet id %d", CAST_S32(id))
166  continue;
167  }
168  if (mPackets[id].len == 0 &&
169  mPackets[id].func == nullptr &&
170  mPackets[id].version == 0)
171  {
172  continue;
173  }
174  logger->log("Remove packet: %d",
175  CAST_S32(id));
176  mPackets[id].name = "";
177  mPackets[id].len = 0;
178  mPackets[id].func = nullptr;
179  mPackets[id].version = 0;
180  }
181 }
#define CAST_S32
Definition: cast.h:30
#define reportAlways(...)
Definition: checkutils.h:253
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
const NetworkRemovePacketInfos & getRemovePackets()
Definition: networkdb.cpp:86
const NetworkInPacketInfos & getFakePackets()
Definition: networkdb.cpp:81
NetworkInPacketInfos::const_iterator NetworkInPacketInfosIter
Definition: networkdb.h:33
std::map< int32_t, int32_t > NetworkInPacketInfos
Definition: networkdb.h:32
std::vector< int > NetworkRemovePacketInfos
Definition: networkdb.h:34
NetworkRemovePacketInfos::const_iterator NetworkRemovePacketInfosIter
Definition: networkdb.h:35

References CAST_S32, FOR_EACH, PacketInfo::func, NetworkDb::getFakePackets(), NetworkDb::getRemovePackets(), PacketInfo::len, Logger::log(), logger, Ea::Network::mPackets, PacketInfo::name, EAthena::packet_lengths_size, reportAlways, and PacketInfo::version.

Referenced by EAthena::updateProtocol().

◆ registerHandlers()

void EAthena::Network::registerHandlers ( )

Definition at line 125 of file network.cpp.

126 {
127 #include "net/eathena/recvpackets.inc"
128  RECVPACKETS_VOID
129 }

Referenced by EAthena::GeneralHandler::load(), and EAthena::updateProtocol().

Friends And Related Function Documentation

◆ MessageOut

friend class MessageOut
friend

Definition at line 60 of file network.h.

Field Documentation

◆ mInstance

Network * EAthena::Network::mInstance = 0
static

The documentation for this class was generated from the following files: