ManaPlus
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Friends
Ea::Network Class Reference

#include <network.h>

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

Public Types

enum  {
  IDLE = 0 , CONNECTED , CONNECTING , DATA ,
  NET_ERROR
}
 

Public Member Functions

 Network ()
 
virtual ~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 ()
 

Protected Member Functions

void setError (const std::string &error)
 
uint16_t readWord (const int pos) const
 
bool realConnect ()
 
void receive ()
 

Protected Attributes

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
 

Friends

int networkThread (void *data)
 

Detailed Description

Definition at line 39 of file network.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
IDLE 
CONNECTED 
CONNECTING 
DATA 
NET_ERROR 

Definition at line 77 of file network.h.

78  {
79  IDLE = 0,
80  CONNECTED,
81  CONNECTING,
82  DATA,
83  NET_ERROR
84  };
@ NET_ERROR
Definition: network.h:83
@ CONNECTED
Definition: network.h:80
@ CONNECTING
Definition: network.h:81

Constructor & Destructor Documentation

◆ Network()

Ea::Network::Network ( )

Definition at line 64 of file network.cpp.

64  :
65  mSocket(nullptr),
66  mServer(),
67  mPackets(nullptr),
68  mInBuffer(new char[BUFFER_SIZE]),
69  mOutBuffer(new char[BUFFER_SIZE]),
70  mInSize(0),
71  mOutSize(0),
72  mToSkip(0),
73  mState(IDLE),
74  mError(),
75  mWorkerThread(nullptr),
76  mMutexIn(SDL_CreateMutex()),
77  mMutexOut(SDL_CreateMutex()),
78  mSleep(config.getIntValue("networksleep")),
79  mPauseDispatch(false)
80 {
81  TcpNet::init();
82 }
int getIntValue(const std::string &key) const
unsigned int mToSkip
Definition: network.h:108
SDL_Thread * mWorkerThread
Definition: network.h:113
SDL_mutex * mMutexIn
Definition: network.h:114
int mSleep
Definition: network.h:116
SDL_mutex * mMutexOut
Definition: network.h:115
ServerInfo mServer
Definition: network.h:99
bool mPauseDispatch
Definition: network.h:117
std::string mError
Definition: network.h:111
unsigned int mOutSize
Definition: network.h:106
int mState
Definition: network.h:110
PacketInfo * mPackets
Definition: network.h:101
char * mInBuffer
Definition: network.h:103
char * mOutBuffer
Definition: network.h:104
unsigned int mInSize
Definition: network.h:105
TcpNet::Socket mSocket
Definition: network.h:97
Configuration config
const unsigned int BUFFER_SIZE
Definition: network.cpp:49
void init()
Definition: sdltcpnet.cpp:78

References TcpNet::init().

◆ ~Network()

Ea::Network::~Network ( )
virtual

Reimplemented in TmwAthena::Network, and EAthena::Network.

Definition at line 84 of file network.cpp.

85 {
86  if (mState != IDLE && mState != NET_ERROR)
87  disconnect();
88 
89  SDL_DestroyMutex(mMutexIn);
90  mMutexIn = nullptr;
91  SDL_DestroyMutex(mMutexOut);
92  mMutexOut = nullptr;
93 
97 
98  TcpNet::quit();
99 }
void disconnect()
Definition: network.cpp:139
#define delete2Arr(var)
Definition: delete2.h:31
void quit()
Definition: sdltcpnet.cpp:83

References delete2Arr, disconnect(), IDLE, mInBuffer, mMutexIn, mMutexOut, mOutBuffer, mPackets, mState, NET_ERROR, and TcpNet::quit().

Member Function Documentation

◆ connect()

bool Ea::Network::connect ( const ServerInfo server)

Definition at line 101 of file network.cpp.

102 {
103  if (mState != IDLE && mState != NET_ERROR)
104  {
105  logger->log1("Tried to connect an already connected socket!");
106  return false;
107  }
108 
109  if (server.hostname.empty())
110  {
111  // TRANSLATORS: error message
112  setError(_("Empty address given to Network::connect()!"));
113  return false;
114  }
115 
116  logger->log("Network::Connecting to %s:%i",
117  server.hostname.c_str(), server.port);
118 
119  mServer.hostname = server.hostname;
121  mServer.port = server.port;
122 
123  // Reset to sane values
124  mOutSize = 0;
125  mInSize = 0;
126  mToSkip = 0;
127 
128  mState = CONNECTING;
129  mWorkerThread = SDL::createThread(&networkThread, "network", this);
130  if (mWorkerThread == nullptr)
131  {
132  setError("Unable to create network worker thread");
133  return false;
134  }
135 
136  return true;
137 }
friend int networkThread(void *data)
Definition: network.cpp:52
void setError(const std::string &error)
Definition: network.cpp:343
void log(const char *const log_text,...)
Definition: logger.cpp:269
void log1(const char *const log_text)
Definition: logger.cpp:238
std::string hostname
Definition: serverinfo.h:45
std::string althostname
Definition: serverinfo.h:46
uint16_t port
Definition: serverinfo.h:58
#define _(s)
Definition: gettext.h:35
Logger * logger
Definition: logger.cpp:89
SDL_Thread * createThread(int(*fn)(void *), const char *const name, void *const data)
Definition: sdlhelper.cpp:118

References _, ServerInfo::althostname, CONNECTING, SDL::createThread(), ServerInfo::hostname, IDLE, Logger::log(), Logger::log1(), logger, mInSize, mOutSize, mServer, mState, mToSkip, mWorkerThread, NET_ERROR, networkThread, ServerInfo::port, and setError().

Referenced by EAthena::CharServerHandler::connect(), EAthena::GameHandler::connect(), EAthena::LoginHandler::connect(), TmwAthena::CharServerHandler::connect(), TmwAthena::GameHandler::connect(), and TmwAthena::LoginHandler::connect().

◆ disconnect()

void Ea::Network::disconnect ( )

◆ fixSendBuffer()

void Ea::Network::fixSendBuffer ( )

Definition at line 362 of file network.cpp.

363 {
364  if (mOutSize > BUFFER_LIMIT)
365  {
366  if (mState != CONNECTED)
367  mOutSize = 0;
368  else
369  flush();
370  }
371 }
void flush()
Definition: network.cpp:157
const unsigned int BUFFER_LIMIT
Definition: network.cpp:50

References Ea::BUFFER_LIMIT, CONNECTED, flush(), mOutSize, and mState.

Referenced by EAthena::MessageOut::MessageOut(), and TmwAthena::MessageOut::MessageOut().

◆ flush()

void Ea::Network::flush ( )

Definition at line 157 of file network.cpp.

158 {
159  if ((mOutSize == 0U) || mState != CONNECTED)
160  return;
161 
162  SDL_mutexP(mMutexOut);
163  const int ret = TcpNet::send(mSocket, mOutBuffer, mOutSize);
164 /*
165  if (logger)
166  {
167  logger->dlog(std::string("Send ").append(
168  toString(mOutSize)).append(" bytes"));
169  }
170 */
171  if (ret < CAST_S32(mOutSize))
172  {
173  SDL_mutexV(mMutexOut);
174  setError("Error in TcpNet::send(): " +
175  std::string(TcpNet::getError()));
176  }
177  mOutSize = 0;
178  SDL_mutexV(mMutexOut);
179 }
#define CAST_S32
Definition: cast.h:30
int send(const TcpNet::Socket sock, const void *const data, const int len)
Definition: sdltcpnet.cpp:93
const char * getError()
Definition: sdltcpnet.cpp:99

References CAST_S32, CONNECTED, TcpNet::getError(), mMutexOut, mOutBuffer, mOutSize, mSocket, mState, TcpNet::send(), and setError().

Referenced by fixSendBuffer(), EAthena::GeneralHandler::flushNetwork(), TmwAthena::GeneralHandler::flushNetwork(), EAthena::GeneralHandler::flushSend(), and TmwAthena::GeneralHandler::flushSend().

◆ getError()

const std::string& Ea::Network::getError ( ) const
inline

Definition at line 58 of file network.h.

59  { return mError; }

References mError.

Referenced by EAthena::GeneralHandler::flushNetwork(), and TmwAthena::GeneralHandler::flushNetwork().

◆ getInSize()

int Ea::Network::getInSize ( ) const
inline

Definition at line 64 of file network.h.

65  { return mInSize; }

References mInSize.

◆ getServer()

ServerInfo Ea::Network::getServer ( ) const
inline

Definition at line 52 of file network.h.

53  { return mServer; }

References mServer.

◆ getState()

int Ea::Network::getState ( ) const
inline

Definition at line 55 of file network.h.

56  { return mState; }

References mState.

◆ isConnected()

bool Ea::Network::isConnected ( ) const
inline

◆ pauseDispatch()

void Ea::Network::pauseDispatch ( )
inline

Definition at line 73 of file network.h.

74  { mPauseDispatch = true; }

References mPauseDispatch.

Referenced by EAthena::GameRecv::processMapLogin(), and TmwAthena::GameRecv::processMapLogin().

◆ readWord()

uint16_t Ea::Network::readWord ( const int  pos) const
protected

Definition at line 350 of file network.cpp.

351 {
352 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
353  return SDL_Swap16(*reinterpret_cast<uint16_t*>(
354  mInBuffer + CAST_SIZE(pos)));
355 #else // SDL_BYTEORDER == SDL_BIG_ENDIAN
356 
357  return (*reinterpret_cast<uint16_t*>(
358  mInBuffer + CAST_SIZE(pos)));
359 #endif // SDL_BYTEORDER == SDL_BIG_ENDIAN
360 }
#define CAST_SIZE
Definition: cast.h:34

References CAST_SIZE, and mInBuffer.

Referenced by EAthena::Network::dispatchMessages(), TmwAthena::Network::dispatchMessages(), EAthena::Network::messageReady(), and TmwAthena::Network::messageReady().

◆ realConnect()

bool Ea::Network::realConnect ( )
protected

Definition at line 205 of file network.cpp.

206 {
207  IPaddress ipAddress;
208 
209  if (TcpNet::resolveHost(&ipAddress, mServer.hostname.c_str(),
210  mServer.port) == -1)
211  {
212  if (mServer.althostname.empty() || TcpNet::resolveHost(&ipAddress,
213  mServer.althostname.c_str(), mServer.port) == -1)
214  {
215  const std::string errorMessage = std::string(
216  // TRANSLATORS: error message
217  _("Unable to resolve host \"")).append(
218  mServer.hostname).append("\".");
220  logger->log_r("TcpNet::ResolveHost: %s", errorMessage.c_str());
221  return false;
222  }
223  logger->log_r("using alt host name: %s", mServer.althostname.c_str());
224  }
225 
226  mState = CONNECTING;
227 
228  mSocket = TcpNet::open(&ipAddress);
229  if (mSocket == nullptr)
230  {
231  logger->log_r("Error in TcpNet::open(): %s", TcpNet::getError());
233  return false;
234  }
235 
236  mLastHost = ipAddress.host;
237  logger->log_r("Network::Started session with %s:%i",
238  ipToString(ipAddress.host), ipAddress.port);
239 
240  mState = CONNECTED;
241  return true;
242 }
void log_r(const char *const log_text,...)
Definition: logger.cpp:365
std::string errorMessage
Definition: client.cpp:116
unsigned int mLastHost
Definition: client.cpp:136
int resolveHost(IPaddress *const address, const char *const host, const Uint16 port)
Definition: sdltcpnet.cpp:104
TcpNet::Socket open(IPaddress *const ip)
Definition: sdltcpnet.cpp:110
const char * ipToString(const uint32_t address)
Definition: stringutils.cpp:86

References _, ServerInfo::althostname, CONNECTED, CONNECTING, errorMessage, TcpNet::getError(), ServerInfo::hostname, ipToString(), Logger::log_r(), logger, mLastHost, mServer, mSocket, mState, TcpNet::open(), ServerInfo::port, TcpNet::resolveHost(), and setError().

◆ receive()

void Ea::Network::receive ( )
protected

Definition at line 244 of file network.cpp.

245 {
246  TcpNet::SocketSet set;
247 
248  if ((set = TcpNet::allocSocketSet(1)) == nullptr)
249  {
250  setError("Error in TcpNet::allocSocketSet(): " +
251  std::string(TcpNet::getError()));
252  return;
253  }
254 
255  if (TcpNet::addSocket(set, mSocket) == -1)
256  {
257  setError("Error in TcpNet::addSocket(): " +
258  std::string(TcpNet::getError()));
259  }
260 
261  while (mState == CONNECTED)
262  {
263  const int numReady = TcpNet::checkSockets(
264  set, (CAST_U32(500)));
265  switch (numReady)
266  {
267  case -1:
268  logger->log_r("Error: TcpNet::checkSockets");
269  break;
270  // FALLTHROUGH
271  case 0:
272  break;
273 
274  case 1:
275  {
276  // Receive data from the socket
277  SDL_mutexP(mMutexIn);
278  if (mInSize > BUFFER_LIMIT)
279  {
280  SDL_mutexV(mMutexIn);
281  SDL_Delay(100);
282  continue;
283  }
284 
285  const int ret = TcpNet::recv(mSocket,
287  BUFFER_SIZE - mInSize);
288 
289  if (ret == 0)
290  {
291  // We got disconnected
292  mState = IDLE;
293  logger->log_r("Disconnected.");
294  }
295  else if (ret < 0)
296  {
297  // TRANSLATORS: error message
298  setError(_("Connection to server terminated. ") +
299  std::string(TcpNet::getError()));
300  }
301  else
302  {
303 // DEBUGLOG("Receive " + toString(ret) + " bytes");
304  mInSize += ret;
305  if (mToSkip != 0U)
306  {
307  if (mInSize >= mToSkip)
308  {
309  mInSize -= mToSkip;
310  memmove(mInBuffer,
312  mInSize);
313  mToSkip = 0;
314  }
315  else
316  {
317  mToSkip -= mInSize;
318  mInSize = 0;
319  }
320  }
321  }
322  SDL_mutexV(mMutexIn);
323  break;
324  }
325 
326  default:
327  // more than one socket is ready..
328  // this should not happen since we only listen once socket.
329  std::stringstream errorStream;
330  errorStream << "Error in TcpNet::recv(), " << numReady
331  << " sockets are ready: " << TcpNet::getError();
332  setError(errorStream.str());
333  break;
334  }
335  }
336 
337  if (TcpNet::delSocket(set, mSocket) == -1)
338  logger->log_r("Error in TcpNet::delSocket(): %s", TcpNet::getError());
339 
341 }
#define CAST_U32
Definition: cast.h:31
int delSocket(const TcpNet::SocketSet set, const TcpNet::Socket sock)
Definition: sdltcpnet.cpp:185
::SDLNet_SocketSet SocketSet
Definition: sdltcpnet.h:37
void freeSocketSet(const TcpNet::SocketSet set)
Definition: sdltcpnet.cpp:193
int addSocket(const TcpNet::SocketSet set, const TcpNet::Socket sock)
Definition: sdltcpnet.cpp:156
SocketSet allocSocketSet(const int maxsockets)
Definition: sdltcpnet.cpp:151
int checkSockets(const TcpNet::SocketSet set, const Uint32 timeout)
Definition: sdltcpnet.cpp:175
int recv(const TcpNet::Socket sock, void *const data, const int maxlen)
Definition: sdltcpnet.cpp:180

References _, TcpNet::addSocket(), TcpNet::allocSocketSet(), Ea::BUFFER_LIMIT, Ea::BUFFER_SIZE, CAST_SIZE, CAST_U32, TcpNet::checkSockets(), CONNECTED, TcpNet::delSocket(), TcpNet::freeSocketSet(), TcpNet::getError(), IDLE, Logger::log_r(), logger, mInBuffer, mInSize, mMutexIn, mSocket, mState, mToSkip, TcpNet::recv(), and setError().

◆ setError()

void Ea::Network::setError ( const std::string &  error)
protected

Definition at line 343 of file network.cpp.

344 {
345  logger->log_r("Network error: %s", error.c_str());
346  mError = error;
347  mState = NET_ERROR;
348 }
bool error(InputEvent &event) __attribute__((noreturn))
Definition: actions.cpp:82

References Actions::error(), Logger::log_r(), logger, mError, mState, and NET_ERROR.

Referenced by connect(), flush(), realConnect(), and receive().

◆ skip()

void Ea::Network::skip ( const int  len)

Definition at line 181 of file network.cpp.

182 {
183  SDL_mutexP(mMutexIn);
184  mToSkip += len;
185  if (mInSize == 0U)
186  {
187  SDL_mutexV(mMutexIn);
188  return;
189  }
190 
191  if (mInSize >= mToSkip)
192  {
193  mInSize -= mToSkip;
195  mToSkip = 0;
196  }
197  else
198  {
199  mToSkip -= mInSize;
200  mInSize = 0;
201  }
202  SDL_mutexV(mMutexIn);
203 }

References CAST_SIZE, mInBuffer, mInSize, mMutexIn, and mToSkip.

Referenced by EAthena::CharServerHandler::connect(), TmwAthena::CharServerHandler::connect(), TmwAthena::GameHandler::connect(), EAthena::Network::dispatchMessages(), and TmwAthena::Network::dispatchMessages().

Friends And Related Function Documentation

◆ networkThread

int networkThread ( void *  data)
friend

Definition at line 52 of file network.cpp.

53 {
54  Network *const network = static_cast<Network *>(data);
55 
56  if ((network == nullptr) || !network->realConnect())
57  return -1;
58 
59  network->receive();
60 
61  return 0;
62 }
uint32_t data

Referenced by connect().

Field Documentation

◆ mError

std::string Ea::Network::mError
protected

Definition at line 111 of file network.h.

Referenced by getError(), and setError().

◆ mInBuffer

char* Ea::Network::mInBuffer
protected

◆ mInSize

unsigned int Ea::Network::mInSize
protected

◆ mMutexIn

SDL_mutex* Ea::Network::mMutexIn
protected

◆ mMutexOut

SDL_mutex* Ea::Network::mMutexOut
protected

Definition at line 115 of file network.h.

Referenced by flush(), and ~Network().

◆ mOutBuffer

char* Ea::Network::mOutBuffer
protected

◆ mOutSize

unsigned int Ea::Network::mOutSize
protected

◆ mPackets

PacketInfo* Ea::Network::mPackets
protected

◆ mPauseDispatch

bool Ea::Network::mPauseDispatch
protected

◆ mServer

ServerInfo Ea::Network::mServer
protected

Definition at line 99 of file network.h.

Referenced by connect(), getServer(), and realConnect().

◆ mSleep

int Ea::Network::mSleep
protected

Definition at line 116 of file network.h.

Referenced by disconnect().

◆ mSocket

TcpNet::Socket Ea::Network::mSocket
protected

Definition at line 97 of file network.h.

Referenced by disconnect(), flush(), realConnect(), and receive().

◆ mState

int Ea::Network::mState
protected

◆ mToSkip

unsigned int Ea::Network::mToSkip
protected

Definition at line 108 of file network.h.

Referenced by connect(), receive(), and skip().

◆ mWorkerThread

SDL_Thread* Ea::Network::mWorkerThread
protected

Definition at line 113 of file network.h.

Referenced by connect(), and disconnect().


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