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

#include <network.h>

Inheritance diagram for TmwAthena::Network:
Ea::Network

Public Member Functions

 Network ()
 
 ~Network ()
 
void clearHandlers ()
 
bool messageReady ()
 
void dispatchMessages ()
 
void registerHandlers ()
 
- 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 37 of file network.h.

Constructor & Destructor Documentation

◆ Network()

TmwAthena::Network::Network ( )

Definition at line 74 of file network.cpp.

74  :
75  Ea::Network()
76 {
77  mInstance = this;
79 }
PacketInfo * mPackets
Definition: network.h:101
static Network * mInstance
Definition: network.h:54
static const unsigned int messagesSize
Definition: network.cpp:71

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

◆ ~Network()

TmwAthena::Network::~Network ( )
virtual

Reimplemented from Ea::Network.

Definition at line 81 of file network.cpp.

82 {
83  clearHandlers();
84  mInstance = nullptr;
85 }
void clearHandlers()
Definition: network.cpp:93

References clearHandlers(), and mInstance.

Member Function Documentation

◆ clearHandlers()

void TmwAthena::Network::clearHandlers ( )

Definition at line 93 of file network.cpp.

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 }
static const unsigned int packet_lengths_size
Definition: network.cpp:70
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, TmwAthena::packet_lengths_size, and PacketInfo::version.

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

◆ dispatchMessages()

void TmwAthena::Network::dispatchMessages ( )

Definition at line 104 of file network.cpp.

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);
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  }
151  }
153 }
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
void dispatchMessages()
Definition: network.cpp:104
if(!vert) return
Logger * logger
Definition: logger.cpp:89
bool msg(InputEvent &event)
Definition: chat.cpp:39
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,...)

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

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

◆ instance()

Network * TmwAthena::Network::instance ( )
staticprotected

Definition at line 179 of file network.cpp.

180 {
181  return mInstance;
182 }

References mInstance.

◆ messageReady()

bool TmwAthena::Network::messageReady ( )

Definition at line 155 of file network.cpp.

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 }
#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, TmwAthena::packet_lengths_size, and Ea::Network::readWord().

Referenced by dispatchMessages().

◆ registerHandlers()

void TmwAthena::Network::registerHandlers ( )

Definition at line 87 of file network.cpp.

88 {
89 #include "net/tmwa/recvpackets.inc"
90  RECVPACKETS_VOID
91 }

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

Friends And Related Function Documentation

◆ MessageOut

friend class MessageOut
friend

Definition at line 57 of file network.h.

Field Documentation

◆ mInstance

Network * TmwAthena::Network::mInstance = 0
static

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