48constexpr uint16_t
VAR = 1;
236 auto *network =
static_cast<Network*
>(data);
238 if (!network->realConnect())
256 for (
const auto &packetInfo : packet_infos)
258 assert(packetInfo.length != 0);
282 Log::info(
"Tried to connect an already connected socket!");
289 setError(
_(
"Empty address given to Network::connect()!"));
308 setError(
"Unable to create network worker thread");
351 messageHandler->setNetwork(
nullptr);
360 return packetInfoIt->second->name;
379 Log::critical(
strprintf(
"Unknown packet 0x%x received.", msgId));
383 auto packetInfo = packetInfoIt->second;
386 uint16_t len = packetInfo->length;
397 Log::critical(
strprintf(
"Variable length packet 0x%x has invalid length %d.",
414 Log::info(
"Handling %s (0x%x) of length %d", packetInfo->name, msgId, len);
417 iter->second->handleMessage(message);
421 Log::info(
"Unhandled %s (0x%x) of length %d", packetInfo->name, msgId, len);
439 setError(
"Error in SDLNet_TCP_Send(): " +
440 std::string(SDLNet_GetError()));
481 mSocket = SDLNet_TCP_Open(&ipAddress);
484 Log::info(
"Error in SDLNet_TCP_Open(): %s", SDLNet_GetError());
489 Log::info(
"Network::Started session with %s:%i",
499 SDLNet_SocketSet set;
501 if (!(set = SDLNet_AllocSocketSet(1)))
503 setError(
"Error in SDLNet_AllocSocketSet(): " +
504 std::string(SDLNet_GetError()));
508 if (SDLNet_TCP_AddSocket(set,
mSocket) == -1)
510 setError(
"Error in SDLNet_AddSocket(): " +
511 std::string(SDLNet_GetError()));
518 int numReady = SDLNet_CheckSockets(set, ((Uint32)500));
542 setError(
_(
"Connection to server terminated. ") +
543 std::string(SDLNet_GetError()));
569 std::stringstream errorStream;
570 errorStream <<
"Error in SDLNet_TCP_Recv(), " << numReady
571 <<
" sockets are ready: " << SDLNet_GetError();
577 if (SDLNet_TCP_DelSocket(set,
mSocket) == -1)
579 Log::info(
"Error in SDLNet_DelSocket(): %s", SDLNet_GetError());
582 SDLNet_FreeSocketSet(set);
587 Log::info(
"Network error: %s", error.c_str());
595 memcpy(&value,
mInBuffer + pos,
sizeof(uint16_t));
596 return SDL_SwapLE16(value);
A convenience class for locking a mutex.
const uint16_t * handledMessages
void setNetwork(Network *network)
Used for parsing an incoming message from eAthena.
void setError(const std::string &error)
bool connect(const ServerInfo &server)
SDL_Thread * mWorkerThread
void unregisterHandler(MessageHandler *handler)
std::unordered_map< uint16_t, const PacketInfo * > mPacketInfo
void registerHandler(MessageHandler *handler)
friend int networkThread(void *data)
uint16_t readWord(int pos)
static Network * mInstance
std::map< uint16_t, MessageHandler * > mMessageHandlers
const char * messageName(uint16_t id) const
void info(const char *log_text,...) LOG_PRINTF_ATTR
void error(const char *log_text,...) LOG_PRINTF_ATTR
Warning: buffers and other variables are shared, so there can be only one connection active at a time...
const unsigned int BUFFER_SIZE
@ SMSG_PLAYER_STAT_UPDATE_1
@ SMSG_PARTY_UPDATE_COORDS
@ SMSG_PLAYER_STAT_UPDATE_4
@ SMSG_PLAYER_ATTACK_RANGE
@ SMSG_BEING_CHANGE_DIRECTION
@ SMSG_CHAR_PASSWORD_RESPONSE
@ SMSG_PLAYER_INVENTORY_ADD
@ CMSG_TRADE_ADD_COMPLETE
@ SMSG_CHAR_SWITCH_RESPONSE
@ SMSG_PLAYER_INVENTORY_USE
@ SMSG_PLAYER_STORAGE_CLOSE
@ SMSG_CONNECTION_PROBLEM
@ SMSG_PARTY_INVITE_RESPONSE
@ SMSG_PLAYER_ARROW_MESSAGE
@ SMSG_PLAYER_STAT_UPDATE_5
@ SMSG_BEING_NAME_RESPONSE
@ SMSG_PLAYER_STORAGE_REMOVE
@ CMSG_TRADE_ITEM_ADD_REQUEST
@ SMSG_CHAR_DELETE_SUCCEEDED
@ CMSG_STAT_UPDATE_REQUEST
@ SMSG_PLAYER_INVENTORY_REMOVE
@ CMSG_PLAYER_INVENTORY_USE
@ SMSG_CHAR_CREATE_SUCCEEDED
@ SMSG_PLAYER_MOVE_TO_ATTACK
@ SMSG_CHAR_DELETE_FAILED
@ SMSG_PLAYER_STORAGE_ADD
@ CMSG_SKILL_LEVELUP_REQUEST
@ SMSG_NPC_BUY_SELL_CHOICE
@ SMSG_PLAYER_STORAGE_EQUIP
@ SMSG_BEING_CHANGE_LOOKS2
@ SMSG_PLAYER_STAT_UPDATE_6
@ SMSG_CHAR_CREATE_FAILED
@ CMSG_PLAYER_CHANGE_DEST
@ SMSG_MAP_SET_TILES_TYPE
@ CMSG_CHAR_SERVER_CONNECT
@ SMSG_PLAYER_GUILD_PARTY_INFO
@ SMSG_TRADE_ITEM_ADD_RESPONSE
@ CMSG_PLAYER_INVENTORY_DROP
@ SMSG_PLAYER_STATUS_CHANGE
@ SMSG_BEING_STATUS_CHANGE
@ SMSG_PLAYER_ARROW_EQUIP
@ CMSG_MAP_SERVER_CONNECT
@ CMSG_CHAR_PASSWORD_CHANGE
@ SMSG_PLAYER_CLIENT_COMMAND
@ CMSG_TRADE_CANCEL_REQUEST
@ CMSG_SERVER_VERSION_REQUEST
@ SMSG_PLAYER_STORAGE_STATUS
@ SMSG_PLAYER_STORAGE_ITEMS
@ CMSG_PLAYER_STOP_ATTACK
@ CMSG_NPC_BUY_SELL_REQUEST
@ SMSG_SERVER_VERSION_RESPONSE
@ SMSG_PLAYER_STAT_UPDATE_2
@ SMSG_PLAYER_STAT_UPDATE_3
int networkThread(void *data)
std::string strprintf(char const *format,...)
A safe version of sprintf that returns a std::string of the result.
const char * ipToString(int address)
Converts the given IP address to a string.