diff options
author | megamage <none@none> | 2008-11-06 16:10:28 -0600 |
---|---|---|
committer | megamage <none@none> | 2008-11-06 16:10:28 -0600 |
commit | 37d4724c9790b32b902d295b1de7b64f1e932292 (patch) | |
tree | 2318e67254429d1026cae5d28dd596078edbfc3e | |
parent | 0aada07187e373ef9763bfb2ae6e6f706f56dfd4 (diff) |
[svn] Merge from Mangos:
3c7ac5bd3e20c33a22ac57c5c3bac23a0798dc9e 2008-10-23 19:06:27
Some endianess related fixes and cleanups. By VladimirMangos.
--HG--
branch : trunk
-rw-r--r-- | src/framework/Utilities/ByteConverter.h | 12 | ||||
-rw-r--r-- | src/game/World.cpp | 181 | ||||
-rw-r--r-- | src/game/WorldSession.cpp | 57 | ||||
-rw-r--r-- | src/game/WorldSocket.cpp | 1184 | ||||
-rw-r--r-- | src/game/WorldSocket.h | 262 |
5 files changed, 827 insertions, 869 deletions
diff --git a/src/framework/Utilities/ByteConverter.h b/src/framework/Utilities/ByteConverter.h index 49fe8a75ef4..5549a9d8100 100644 --- a/src/framework/Utilities/ByteConverter.h +++ b/src/framework/Utilities/ByteConverter.h @@ -10,12 +10,12 @@ * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef TRINITY_BYTECONVERTER_H @@ -48,12 +48,18 @@ namespace ByteConverter #if TRINITY_ENDIAN == TRINITY_BIGENDIAN template<typename T> inline void EndianConvert(T& val) { ByteConverter::apply<T>(&val); } +template<typename T> inline void EndianConvertReverse(T&) { } #else template<typename T> inline void EndianConvert(T&) { } +template<typename T> inline void EndianConvertReverse(T& val) { ByteConverter::apply<T>(&val); } #endif -template<typename T> inline void EndianConvert(T*) { } +template<typename T> void EndianConvert(T*); // will generate link error +template<typename T> void EndianConvertReverse(T*); // will generate link error + inline void EndianConvert(uint8&) { } inline void EndianConvert( int8&) { } +inline void EndianConvertReverse(uint8&) { } +inline void EndianConvertReverse( int8&) { } #endif diff --git a/src/game/World.cpp b/src/game/World.cpp index d02aa8a1f6f..6e1de2d8567 100644 --- a/src/game/World.cpp +++ b/src/game/World.cpp @@ -128,7 +128,7 @@ World::~World() VMAP::VMapFactory::clear(); if(m_resultQueue) delete m_resultQueue; - + //TODO free addSessQueue } @@ -182,67 +182,67 @@ bool World::RemoveSession(uint32 id) void World::AddSession(WorldSession* s) { - addSessQueue.add(s); + addSessQueue.add(s); } void World::AddSession_ (WorldSession* s) { - ASSERT (s); + ASSERT (s); - //NOTE - Still there is race condition in WorldSession* being used in the Sockets + //NOTE - Still there is race condition in WorldSession* being used in the Sockets - ///- kick already loaded player with same account (if any) and remove session - ///- if player is in loading and want to load again, return - if (!RemoveSession (s->GetAccountId ())) + ///- kick already loaded player with same account (if any) and remove session + ///- if player is in loading and want to load again, return + if (!RemoveSession (s->GetAccountId ())) { - s->KickPlayer (); - m_kicked_sessions.insert (s); - return; + s->KickPlayer (); + m_kicked_sessions.insert (s); + return; } - WorldSession* old = m_sessions[s->GetAccountId ()]; - m_sessions[s->GetAccountId ()] = s; + WorldSession* old = m_sessions[s->GetAccountId ()]; + m_sessions[s->GetAccountId ()] = s; - // if session already exist, prepare to it deleting at next world update - // NOTE - KickPlayer() should be called on "old" in RemoveSession() - if (old) - m_kicked_sessions.insert (old); + // if session already exist, prepare to it deleting at next world update + // NOTE - KickPlayer() should be called on "old" in RemoveSession() + if (old) + m_kicked_sessions.insert (old); - uint32 Sessions = GetActiveAndQueuedSessionCount (); - uint32 pLimit = GetPlayerAmountLimit (); - uint32 QueueSize = GetQueueSize (); //number of players in the queue - bool inQueue = false; - //so we don't count the user trying to - //login as a session and queue the socket that we are using - --Sessions; + uint32 Sessions = GetActiveAndQueuedSessionCount (); + uint32 pLimit = GetPlayerAmountLimit (); + uint32 QueueSize = GetQueueSize (); //number of players in the queue + bool inQueue = false; + //so we don't count the user trying to + //login as a session and queue the socket that we are using + --Sessions; - if (pLimit > 0 && Sessions >= pLimit && s->GetSecurity () == SEC_PLAYER ) + if (pLimit > 0 && Sessions >= pLimit && s->GetSecurity () == SEC_PLAYER ) { - AddQueuedPlayer (s); - UpdateMaxSessionCounters (); - sLog.outDetail ("PlayerQueue: Account id %u is in Queue Position (%u).", s->GetAccountId (), ++QueueSize); - return; + AddQueuedPlayer (s); + UpdateMaxSessionCounters (); + sLog.outDetail ("PlayerQueue: Account id %u is in Queue Position (%u).", s->GetAccountId (), ++QueueSize); + return; } - - WorldPacket packet(SMSG_AUTH_RESPONSE, 1 + 4 + 1 + 4 + 1); - packet << uint8 (AUTH_OK); - packet << uint32 (0); // unknown random value... - packet << uint8 (0); - packet << uint32 (0); - packet << uint8 (s->Expansion () ? 1 : 0); // 0 - normal, 1 - TBC, must be set in database manually for each account - s->SendPacket (&packet); - UpdateMaxSessionCounters (); + WorldPacket packet(SMSG_AUTH_RESPONSE, 1 + 4 + 1 + 4 + 1); + packet << uint8 (AUTH_OK); + packet << uint32 (0); // unknown random value... + packet << uint8 (0); + packet << uint32 (0); + packet << uint8 (s->Expansion()); // 0 - normal, 1 - TBC, must be set in database manually for each account + s->SendPacket (&packet); - // Updates the population - if (pLimit > 0) + UpdateMaxSessionCounters (); + + // Updates the population + if (pLimit > 0) { - float popu = GetActiveSessionCount (); //updated number of users on the server - popu /= pLimit; - popu *= 2; - loginDatabase.PExecute ("UPDATE realmlist SET population = '%f' WHERE id = '%d'", popu, realmID); - sLog.outDetail ("Server Population (%f).", popu); + float popu = GetActiveSessionCount (); //updated number of users on the server + popu /= pLimit; + popu *= 2; + loginDatabase.PExecute ("UPDATE realmlist SET population = '%f' WHERE id = '%d'", popu, realmID); + sLog.outDetail ("Server Population (%f).", popu); } } @@ -260,7 +260,7 @@ int32 World::GetQueuePos(WorldSession* sess) void World::AddQueuedPlayer(WorldSession* sess) { m_QueuedPlayer.push_back (sess); - + // The 1st SMSG_AUTH_RESPONSE needs to contain other info too. WorldPacket packet (SMSG_AUTH_RESPONSE, 1 + 4 + 1 + 4 + 1); packet << uint8 (AUTH_WAIT_QUEUE); @@ -270,7 +270,7 @@ void World::AddQueuedPlayer(WorldSession* sess) packet << uint8 (sess->Expansion () ? 1 : 0); // 0 - normal, 1 - TBC, must be set in database manually for each account packet << uint32(GetQueuePos (sess)); sess->SendPacket (&packet); - + //sess->SendAuthWaitQue (GetQueuePos (sess)); } @@ -2282,32 +2282,32 @@ BanReturn World::BanAccount(BanMode mode, std::string nameOrIP, std::string dura QueryResult *resultAccounts = NULL; //used for kicking ///- Update the database with ban information - switch(mode) + switch(mode) { case BAN_IP: - //No SQL injection as strings are escaped - resultAccounts = loginDatabase.PQuery("SELECT id FROM account WHERE last_ip = '%s'",nameOrIP.c_str()); - loginDatabase.PExecute("INSERT INTO ip_banned VALUES ('%s',UNIX_TIMESTAMP(),UNIX_TIMESTAMP()+%u,'%s','%s')",nameOrIP.c_str(),duration_secs,safe_author.c_str(),reason.c_str()); - break; - case BAN_ACCOUNT: - //No SQL injection as string is escaped - resultAccounts = loginDatabase.PQuery("SELECT id FROM account WHERE username = '%s'",nameOrIP.c_str()); - break; - case BAN_CHARACTER: - //No SQL injection as string is escaped - resultAccounts = CharacterDatabase.PQuery("SELECT account FROM characters WHERE name = '%s'",nameOrIP.c_str()); - break; - default: - return BAN_SYNTAX_ERROR; + //No SQL injection as strings are escaped + resultAccounts = loginDatabase.PQuery("SELECT id FROM account WHERE last_ip = '%s'",nameOrIP.c_str()); + loginDatabase.PExecute("INSERT INTO ip_banned VALUES ('%s',UNIX_TIMESTAMP(),UNIX_TIMESTAMP()+%u,'%s','%s')",nameOrIP.c_str(),duration_secs,safe_author.c_str(),reason.c_str()); + break; + case BAN_ACCOUNT: + //No SQL injection as string is escaped + resultAccounts = loginDatabase.PQuery("SELECT id FROM account WHERE username = '%s'",nameOrIP.c_str()); + break; + case BAN_CHARACTER: + //No SQL injection as string is escaped + resultAccounts = CharacterDatabase.PQuery("SELECT account FROM characters WHERE name = '%s'",nameOrIP.c_str()); + break; + default: + return BAN_SYNTAX_ERROR; + } + + if(!resultAccounts) + { + if(mode==BAN_IP) + return BAN_SUCCESS; // ip correctly banned but nobody affected (yet) + else + return BAN_NOTFOUND; // Nobody to ban } - - if(!resultAccounts) - { - if(mode==BAN_IP) - return BAN_SUCCESS; - else - return BAN_NOTFOUND; // Nobody to ban - } ///- Disconnect all affected players (for IP it can be several) do @@ -2316,11 +2316,11 @@ BanReturn World::BanAccount(BanMode mode, std::string nameOrIP, std::string dura uint32 account = fieldsAccount->GetUInt32(); if(mode!=BAN_IP) - { + { //No SQL injection as strings are escaped loginDatabase.PExecute("INSERT INTO account_banned VALUES ('%u', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()+%u, '%s', '%s', '1')", account,duration_secs,safe_author.c_str(),reason.c_str()); - } + } if (WorldSession* sess = FindSession(account)) if(std::string(sess->GetPlayerName()) != author) @@ -2342,15 +2342,15 @@ bool World::RemoveBanAccount(BanMode mode, std::string nameOrIP) } else { - uint32 account=0; + uint32 account = 0; if (mode == BAN_ACCOUNT) account = accmgr.GetId (nameOrIP); else if (mode == BAN_CHARACTER) account = objmgr.GetPlayerAccountIdByPlayerName (nameOrIP); - if(!account) + if (!account) return false; - + //NO SQL injection as account is uint32 loginDatabase.PExecute("UPDATE account_banned SET active = '0' WHERE id = '%u'",account); } @@ -2433,7 +2433,7 @@ void World::ShutdownMsg(bool show, Player* player) uint32 msgid = (m_ShutdownMask & SHUTDOWN_MASK_RESTART) ? SERVER_MSG_RESTART_TIME : SERVER_MSG_SHUTDOWN_TIME; SendServerMessage(msgid,str.c_str(),player); - outstring_log("Server will %s in %s", (m_ShutdownMask & SHUTDOWN_MASK_RESTART ? "restart" : "shutdown"), str.c_str()); + DEBUG_LOG("Server is %s in %s",(m_ShutdownMask & SHUTDOWN_MASK_RESTART ? "restart" : "shuttingdown"),str.c_str()); } } @@ -2473,10 +2473,10 @@ void World::UpdateSessions( time_t diff ) WorldSession* sess = addSessQueue.next (); AddSession_ (sess); } - + ///- Delete kicked sessions at add new session for (std::set<WorldSession*>::iterator itr = m_kicked_sessions.begin(); itr != m_kicked_sessions.end(); ++itr) - { + { RemoveQueuedPlayer (*itr); delete *itr; } @@ -2504,17 +2504,18 @@ void World::UpdateSessions( time_t diff ) void World::ProcessCliCommands() { if (cliCmdQueue.empty()) - return; + return; CliCommandHolder::Print* zprint; + while (!cliCmdQueue.empty()) { sLog.outDebug("CLI command under processing..."); CliCommandHolder *command = cliCmdQueue.next(); - zprint = command->m_print; + zprint = command->m_print; - CliHandler(zprint).ParseCommands(command->m_command); + CliHandler(zprint).ParseCommands(command->m_command); delete command; } @@ -2622,14 +2623,14 @@ void World::UpdateMaxSessionCounters() void World::LoadDBVersion() { - QueryResult* result = WorldDatabase.Query("SELECT version FROM db_version LIMIT 1"); - if(result) - { - Field* fields = result->Fetch(); - - m_DBVersion = fields[0].GetString(); - delete result; - } - else - m_DBVersion = "unknown world database"; + QueryResult* result = WorldDatabase.Query("SELECT version FROM db_version LIMIT 1"); + if(result) + { + Field* fields = result->Fetch(); + + m_DBVersion = fields[0].GetString(); + delete result; + } + else + m_DBVersion = "unknown world database"; } diff --git a/src/game/WorldSession.cpp b/src/game/WorldSession.cpp index d8f5c8877cb..8ac04561733 100644 --- a/src/game/WorldSession.cpp +++ b/src/game/WorldSession.cpp @@ -10,12 +10,12 @@ * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /** \file @@ -50,35 +50,34 @@ _player(NULL), m_Socket(sock),_security(sec), _accountId(id), m_expansion(expans m_sessionDbcLocale(sWorld.GetAvailableDbcLocale(locale)), m_sessionDbLocaleIndex(objmgr.GetIndexForLocale(locale)), _logoutTime(0), m_playerLoading(false), m_playerLogout(false), m_playerRecentlyLogout(false), m_latency(0) { - if (sock) - { - m_Address = sock->GetRemoteAddress (); - sock->AddReference (); - } + if (sock) + { + m_Address = sock->GetRemoteAddress (); + sock->AddReference (); + } } /// WorldSession destructor WorldSession::~WorldSession() { ///- unload player if not unloaded - if(_player) - LogoutPlayer(true); + if (_player) + LogoutPlayer (true); /// - If have unclosed socket, close it - if (m_Socket) + if (m_Socket) { - m_Socket->CloseSocket (); - m_Socket->RemoveReference (); - m_Socket = NULL; + m_Socket->CloseSocket (); + m_Socket->RemoveReference (); + m_Socket = NULL; } ///- empty incoming packet queue while(!_recvQueue.empty()) { - WorldPacket *packet = _recvQueue.next(); + WorldPacket *packet = _recvQueue.next (); delete packet; } - } void WorldSession::SizeError(WorldPacket const& packet, uint32 size) const @@ -133,10 +132,10 @@ void WorldSession::SendPacket(WorldPacket const* packet) sendLastPacketBytes = packet->wpos(); // wpos is real written size } - #endif // !TRINITY_DEBUG + #endif // !MANGOS_DEBUG - if (m_Socket->SendPacket (*packet) == -1) - m_Socket->CloseSocket (); + if (m_Socket->SendPacket (*packet) == -1) + m_Socket->CloseSocket (); } /// Add an incoming packet to the queue @@ -157,12 +156,12 @@ void WorldSession::logUnexpectedOpcode(WorldPacket* packet, const char *reason) /// Update the WorldSession (triggered by World update) bool WorldSession::Update(uint32 /*diff*/) { - if (m_Socket && m_Socket->IsClosed ()) - { + if (m_Socket && m_Socket->IsClosed ()) + { m_Socket->RemoveReference (); m_Socket = NULL; - } - + } + WorldPacket *packet; ///- Retrieve packets from the receive queue and call the appropriate handlers @@ -247,7 +246,7 @@ void WorldSession::LogoutPlayer(bool Save) { if (uint64 lguid = GetPlayer()->GetLootGUID()) DoLootRelease(lguid); - + ///- If the player just died before logging out, make him appear as a ghost //FIXME: logout must be delayed in case lost connection with client in time of combat if (_player->GetDeathTimer()) @@ -284,7 +283,7 @@ void WorldSession::LogoutPlayer(bool Save) // give honor to all attackers from set like group case for(std::set<Player*>::const_iterator itr = aset.begin(); itr != aset.end(); ++itr) - (*itr)->RewardHonor(_player, aset.size(), -1, true); + (*itr)->RewardHonor(_player,aset.size()); // give bg rewards and update counters like kill by first from attackers // this can't be called for all attackers. @@ -402,7 +401,7 @@ void WorldSession::LogoutPlayer(bool Save) ///- Since each account can only have one online character at any given time, ensure all characters for active account are marked as offline //No SQL injection as AccountId is uint32 CharacterDatabase.PExecute("UPDATE characters SET online = 0 WHERE account = '%u'", - GetAccountId()); + GetAccountId()); sLog.outDebug( "SESSION: Sent SMSG_LOGOUT_COMPLETE Message" ); } @@ -414,8 +413,8 @@ void WorldSession::LogoutPlayer(bool Save) /// Kick a player out of the World void WorldSession::KickPlayer() { - if (m_Socket) - m_Socket->CloseSocket (); + if (m_Socket) + m_Socket->CloseSocket (); } /// Cancel channeling handler @@ -521,7 +520,3 @@ void WorldSession::SendAuthWaitQue(uint32 position) SendPacket(&packet); } } - - - - diff --git a/src/game/WorldSocket.cpp b/src/game/WorldSocket.cpp index bf9dd0aefb4..620c2116c30 100644 --- a/src/game/WorldSocket.cpp +++ b/src/game/WorldSocket.cpp @@ -1,22 +1,22 @@ /* - * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> - * - * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ +* Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> +* +* Copyright (C) 2008 Trinity <http://www.trinitycore.org/> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ #include <ace/Message_Block.h> #include <ace/OS_NS_string.h> @@ -54,14 +54,14 @@ struct ServerPktHeader { - ACE_UINT16 size; - ACE_UINT16 cmd; + uint16 size; + uint16 cmd; }; struct ClientPktHeader { - ACE_UINT16 size; - ACE_UINT32 cmd; + uint16 size; + uint32 cmd; }; #if defined( __GNUC__ ) @@ -83,642 +83,607 @@ m_Seed (static_cast<uint32> (rand32 ())), m_OverSpeedPings (0), m_LastPingTime (ACE_Time_Value::zero) { - this->reference_counting_policy ().value (ACE_Event_Handler::Reference_Counting_Policy::ENABLED); + reference_counting_policy ().value (ACE_Event_Handler::Reference_Counting_Policy::ENABLED); } WorldSocket::~WorldSocket (void) { - if (m_RecvWPct) - delete m_RecvWPct; + if (m_RecvWPct) + delete m_RecvWPct; - if (m_OutBuffer) - m_OutBuffer->release (); + if (m_OutBuffer) + m_OutBuffer->release (); - this->closing_ = true; + closing_ = true; - this->peer ().close (); + peer ().close (); - WorldPacket* pct; - while (m_PacketQueue.dequeue_head (pct) == 0) - delete pct; + WorldPacket* pct; + while (m_PacketQueue.dequeue_head (pct) == 0) + delete pct; } -bool -WorldSocket::IsClosed (void) const +bool WorldSocket::IsClosed (void) const { - return this->closing_; + return closing_; } -void -WorldSocket::CloseSocket (void) +void WorldSocket::CloseSocket (void) { - { - ACE_GUARD (LockType, Guard, m_OutBufferLock); - - if (this->closing_) - return; + { + ACE_GUARD (LockType, Guard, m_OutBufferLock); - this->closing_ = true; + if (closing_) + return; - this->peer ().close_writer (); - } + closing_ = true; - { - ACE_GUARD (LockType, Guard, m_SessionLock); + peer ().close_writer (); + } - m_Session = NULL; - } + { + ACE_GUARD (LockType, Guard, m_SessionLock); + m_Session = NULL; + } } -const std::string& -WorldSocket::GetRemoteAddress (void) const +const std::string& WorldSocket::GetRemoteAddress (void) const { - return m_Address; + return m_Address; } -int -WorldSocket::SendPacket (const WorldPacket& pct) +int WorldSocket::SendPacket (const WorldPacket& pct) { - ACE_GUARD_RETURN (LockType, Guard, m_OutBufferLock, -1); + ACE_GUARD_RETURN (LockType, Guard, m_OutBufferLock, -1); - if (this->closing_) - return -1; + if (closing_) + return -1; - // Dump outgoing packet. - if (sWorldLog.LogWorld ()) + // Dump outgoing packet. + if (sWorldLog.LogWorld ()) { - sWorldLog.Log ("SERVER:\nSOCKET: %u\nLENGTH: %u\nOPCODE: %s (0x%.4X)\nDATA:\n", + sWorldLog.Log ("SERVER:\nSOCKET: %u\nLENGTH: %u\nOPCODE: %s (0x%.4X)\nDATA:\n", (uint32) get_handle (), pct.size (), LookupOpcodeName (pct.GetOpcode ()), pct.GetOpcode ()); - uint32 p = 0; - while (p < pct.size ()) + uint32 p = 0; + while (p < pct.size ()) { - for (uint32 j = 0; j < 16 && p < pct.size (); j++) - sWorldLog.Log ("%.2X ", const_cast<WorldPacket&> (pct)[p++]); + for (uint32 j = 0; j < 16 && p < pct.size (); j++) + sWorldLog.Log ("%.2X ", const_cast<WorldPacket&>(pct)[p++]); - sWorldLog.Log ("\n"); + sWorldLog.Log ("\n"); } - sWorldLog.Log ("\n\n"); + sWorldLog.Log ("\n\n"); } - if (iSendPacket (pct) == -1) + if (iSendPacket (pct) == -1) { - WorldPacket* npct; + WorldPacket* npct; - ACE_NEW_RETURN (npct, WorldPacket (pct), -1); + ACE_NEW_RETURN (npct, WorldPacket (pct), -1); - // NOTE maybe check of the size of the queue can be good ? - // to make it bounded instead of unbounded - if (m_PacketQueue.enqueue_tail (npct) == -1) + // NOTE maybe check of the size of the queue can be good ? + // to make it bounded instead of unbounded + if (m_PacketQueue.enqueue_tail (npct) == -1) { - delete npct; - sLog.outError ("WorldSocket::SendPacket: m_PacketQueue.enqueue_tail failed"); - return -1; + delete npct; + sLog.outError ("WorldSocket::SendPacket: m_PacketQueue.enqueue_tail failed"); + return -1; } } - return 0; + return 0; } -long -WorldSocket::AddReference (void) +long WorldSocket::AddReference (void) { - return static_cast<long> (this->add_reference ()); + return static_cast<long> (add_reference ()); } -long -WorldSocket::RemoveReference (void) +long WorldSocket::RemoveReference (void) { - return static_cast<long> (this->remove_reference ()); + return static_cast<long> (remove_reference ()); } -int -WorldSocket::open (void *a) +int WorldSocket::open (void *a) { - ACE_UNUSED_ARG (a); + ACE_UNUSED_ARG (a); - // Prevent double call to this func. - if (m_OutBuffer) - return -1; + // Prevent double call to this func. + if (m_OutBuffer) + return -1; - // This will also prevent the socket from being Updated - // while we are initializing it. - m_OutActive = true; + // This will also prevent the socket from being Updated + // while we are initializing it. + m_OutActive = true; - // Hook for the manager. - if (sWorldSocketMgr->OnSocketOpen (this) == -1) - return -1; + // Hook for the manager. + if (sWorldSocketMgr->OnSocketOpen (this) == -1) + return -1; - // Allocate the buffer. - ACE_NEW_RETURN (m_OutBuffer, ACE_Message_Block (m_OutBufferSize), -1); + // Allocate the buffer. + ACE_NEW_RETURN (m_OutBuffer, ACE_Message_Block (m_OutBufferSize), -1); - // Store peer address. - ACE_INET_Addr remote_addr; + // Store peer address. + ACE_INET_Addr remote_addr; - if (this->peer ().get_remote_addr (remote_addr) == -1) + if (peer ().get_remote_addr (remote_addr) == -1) { - sLog.outError ("WorldSocket::open: peer ().get_remote_addr errno = %s", ACE_OS::strerror (errno)); - return -1; + sLog.outError ("WorldSocket::open: peer ().get_remote_addr errno = %s", ACE_OS::strerror (errno)); + return -1; } - m_Address = remote_addr.get_host_addr (); + m_Address = remote_addr.get_host_addr (); - // Send startup packet. - WorldPacket packet (SMSG_AUTH_CHALLENGE, 4); - packet << m_Seed; + // Send startup packet. + WorldPacket packet (SMSG_AUTH_CHALLENGE, 4); + packet << m_Seed; - if (SendPacket (packet) == -1) - return -1; + if (SendPacket (packet) == -1) + return -1; - // Register with ACE Reactor - if (this->reactor ()->register_handler - (this, - ACE_Event_Handler::READ_MASK | ACE_Event_Handler::WRITE_MASK) == -1) + // Register with ACE Reactor + if (reactor ()->register_handler(this, ACE_Event_Handler::READ_MASK | ACE_Event_Handler::WRITE_MASK) == -1) { - sLog.outError ("WorldSocket::open: unable to register client handler errno = %s", ACE_OS::strerror (errno)); - return -1; + sLog.outError ("WorldSocket::open: unable to register client handler errno = %s", ACE_OS::strerror (errno)); + return -1; } - // reactor takes care of the socket from now on - this->remove_reference (); + // reactor takes care of the socket from now on + remove_reference (); - return 0; + return 0; } -int -WorldSocket::close (int) +int WorldSocket::close (int) { - this->shutdown (); + shutdown (); - this->closing_ = true; + closing_ = true; - this->remove_reference (); + remove_reference (); - return 0; + return 0; } -int -WorldSocket::handle_input (ACE_HANDLE) +int WorldSocket::handle_input (ACE_HANDLE) { - if (this->closing_) - return -1; + if (closing_) + return -1; - switch (this->handle_input_missing_data ()) + switch (handle_input_missing_data ()) { - case -1 : - { - if ((errno == EWOULDBLOCK) || - (errno == EAGAIN)) - { - //return 0; - return this->Update (); // interesting line ,isnt it ? - } - - DEBUG_LOG ("WorldSocket::handle_input: Peer error closing connection errno = %s", ACE_OS::strerror (errno)); + case -1 : + { + if ((errno == EWOULDBLOCK) || + (errno == EAGAIN)) + { + return Update (); // interesting line ,isnt it ? + } - return -1; - } - case 0: - { - DEBUG_LOG ("WorldSocket::handle_input: Peer has closed connection\n"); + DEBUG_LOG ("WorldSocket::handle_input: Peer error closing connection errno = %s", ACE_OS::strerror (errno)); - errno = ECONNRESET; + errno = ECONNRESET; + return -1; + } + case 0: + { + DEBUG_LOG ("WorldSocket::handle_input: Peer has closed connection\n"); - return -1; - } - case 1: - return 1; + errno = ECONNRESET; + return -1; + } + case 1: + return 1; + default: + return Update (); // another interesting line ;) } - //return 0; - return this->Update (); // another interesting line ;) + ACE_NOTREACHED(return -1); } -int -WorldSocket::handle_output (ACE_HANDLE) +int WorldSocket::handle_output (ACE_HANDLE) { - ACE_GUARD_RETURN (LockType, Guard, m_OutBufferLock, -1); + ACE_GUARD_RETURN (LockType, Guard, m_OutBufferLock, -1); - if (this->closing_) - return -1; + if (closing_) + return -1; - const size_t send_len = m_OutBuffer->length (); + const size_t send_len = m_OutBuffer->length (); - if (send_len == 0) - return this->cancel_wakeup_output (Guard); + if (send_len == 0) + return cancel_wakeup_output (Guard); - // TODO SO_NOSIGPIPE on platforms that support it #ifdef MSG_NOSIGNAL - ssize_t n = this->peer ().send (m_OutBuffer->rd_ptr (), send_len, MSG_NOSIGNAL); + ssize_t n = peer ().send (m_OutBuffer->rd_ptr (), send_len, MSG_NOSIGNAL); #else - ssize_t n = this->peer ().send (m_OutBuffer->rd_ptr (), send_len); + ssize_t n = peer ().send (m_OutBuffer->rd_ptr (), send_len); #endif // MSG_NOSIGNAL - if (n == 0) - return -1; - else if (n == -1) + if (n == 0) + return -1; + else if (n == -1) { - if (errno == EWOULDBLOCK || errno == EAGAIN) - return this->schedule_wakeup_output (Guard); + if (errno == EWOULDBLOCK || errno == EAGAIN) + return schedule_wakeup_output (Guard); - return -1; + return -1; } - else if (n < send_len) //now n > 0 + else if (n < send_len) //now n > 0 { - m_OutBuffer->rd_ptr (static_cast<size_t> (n)); + m_OutBuffer->rd_ptr (static_cast<size_t> (n)); - // move the data to the base of the buffer - m_OutBuffer->crunch (); + // move the data to the base of the buffer + m_OutBuffer->crunch (); - return this->schedule_wakeup_output (Guard); + return schedule_wakeup_output (Guard); } - else //now n == send_len + else //now n == send_len { - m_OutBuffer->reset (); + m_OutBuffer->reset (); - if (!iFlushPacketQueue ()) - return this->cancel_wakeup_output (Guard); - else - return this->schedule_wakeup_output (Guard); + if (!iFlushPacketQueue ()) + return cancel_wakeup_output (Guard); + else + return schedule_wakeup_output (Guard); } - ACE_NOTREACHED (return 0); + ACE_NOTREACHED (return 0); } -int -WorldSocket::handle_close (ACE_HANDLE h, ACE_Reactor_Mask) +int WorldSocket::handle_close (ACE_HANDLE h, ACE_Reactor_Mask) { - // Critical section - { - ACE_GUARD_RETURN (LockType, Guard, m_OutBufferLock, -1); + // Critical section + { + ACE_GUARD_RETURN (LockType, Guard, m_OutBufferLock, -1); - this->closing_ = true; + closing_ = true; - if (h == ACE_INVALID_HANDLE) - this->peer ().close_writer (); - } + if (h == ACE_INVALID_HANDLE) + peer ().close_writer (); + } - // Critical section - { - ACE_GUARD_RETURN (LockType, Guard, m_SessionLock, -1); + // Critical section + { + ACE_GUARD_RETURN (LockType, Guard, m_SessionLock, -1); - m_Session = NULL; - } + m_Session = NULL; + } - return 0; + return 0; } -int -WorldSocket::Update (void) +int WorldSocket::Update (void) { - if (this->closing_) - return -1; + if (closing_) + return -1; - if (m_OutActive || m_OutBuffer->length () == 0) - return 0; + if (m_OutActive || m_OutBuffer->length () == 0) + return 0; - return this->handle_output (this->get_handle ()); + return handle_output (get_handle ()); } -int -WorldSocket::handle_input_header (void) +int WorldSocket::handle_input_header (void) { - ACE_ASSERT (m_RecvWPct == NULL); + ACE_ASSERT (m_RecvWPct == NULL); - if (m_Header.length () != sizeof (ClientPktHeader)) - { - sLog.outError ("WorldSocket::handle_input_header: internal error: invalid header"); - errno = EINVAL; - return -1; - } - - m_Crypt.DecryptRecv ((ACE_UINT8*) m_Header.rd_ptr (), sizeof (ClientPktHeader)); + ACE_ASSERT (m_Header.length () == sizeof (ClientPktHeader)); - ClientPktHeader& header = *((ClientPktHeader*) m_Header.rd_ptr ()); + m_Crypt.DecryptRecv ((ACE_UINT8*) m_Header.rd_ptr (), sizeof (ClientPktHeader)); - header.size = ACE_NTOHS (header.size); + ClientPktHeader& header = *((ClientPktHeader*) m_Header.rd_ptr ()); -#if ACE_BYTE_ORDER == ACE_BIG_ENDIAN - header.cmd = ACE_SWAP_LONG (header.cmd) -#endif // ACE_BIG_ENDIAN + EndianConvertReverse(header.size); + EndianConvert(header.cmd); - if ((header.size < 4) || - (header.size > 10240) || - (header.cmd <= 0) || - (header.cmd > 10240) - ) + if ((header.size < 4) || (header.size > 10240) || + (header.cmd < 0) || (header.cmd > 10240) ) { - sLog.outError ("WorldSocket::handle_input_header: client sent mailformed packet size = %d , cmd = %d", - header.size, - header.cmd); + sLog.outError ("WorldSocket::handle_input_header: client sent mailformed packet size = %d , cmd = %d", + header.size, header.cmd); - errno = EINVAL; - return -1; + errno = EINVAL; + return -1; } - header.size -= 4; + header.size -= 4; - ACE_NEW_RETURN (m_RecvWPct, WorldPacket ((uint16) header.cmd, header.size), -1); + ACE_NEW_RETURN (m_RecvWPct, WorldPacket ((uint16) header.cmd, header.size), -1); - if (header.size > 0) + if(header.size > 0) { - m_RecvWPct->resize (header.size); - m_RecvPct.base ((char*) m_RecvWPct->contents (), m_RecvWPct->size ()); + m_RecvWPct->resize (header.size); + m_RecvPct.base ((char*) m_RecvWPct->contents (), m_RecvWPct->size ()); } - else + else { - ACE_ASSERT (m_RecvPct.space () == 0); + ACE_ASSERT(m_RecvPct.space() == 0); } - - return 0; + return 0; } -int -WorldSocket::handle_input_payload (void) +int WorldSocket::handle_input_payload (void) { - // set errno properly here on error !!! - // now have a header and payload + // set errno properly here on error !!! + // now have a header and payload - ACE_ASSERT (m_RecvPct.space () == 0); - ACE_ASSERT (m_Header.space () == 0); - ACE_ASSERT (m_RecvWPct != NULL); + ACE_ASSERT (m_RecvPct.space () == 0); + ACE_ASSERT (m_Header.space () == 0); + ACE_ASSERT (m_RecvWPct != NULL); - const int ret = this->ProcessIncoming (m_RecvWPct); + const int ret = ProcessIncoming (m_RecvWPct); - m_RecvPct.base (NULL, 0); - m_RecvPct.reset (); - m_RecvWPct = NULL; + m_RecvPct.base (NULL, 0); + m_RecvPct.reset (); + m_RecvWPct = NULL; - m_Header.reset (); + m_Header.reset (); - if (ret == -1) - errno = EINVAL; + if (ret == -1) + errno = EINVAL; - return ret; + return ret; } -int -WorldSocket::handle_input_missing_data (void) +int WorldSocket::handle_input_missing_data (void) { - char buf [1024]; + char buf [1024]; - ACE_Data_Block db (sizeof (buf), - ACE_Message_Block::MB_DATA, - buf, - 0, - 0, - ACE_Message_Block::DONT_DELETE, - 0); + ACE_Data_Block db ( sizeof (buf), + ACE_Message_Block::MB_DATA, + buf, + 0, + 0, + ACE_Message_Block::DONT_DELETE, + 0); - ACE_Message_Block message_block (&db, - ACE_Message_Block::DONT_DELETE, - 0); + ACE_Message_Block message_block(&db, + ACE_Message_Block::DONT_DELETE, + 0); - const size_t recv_size = message_block.space (); + const size_t recv_size = message_block.space (); - const ssize_t n = this->peer ().recv (message_block.wr_ptr (), - recv_size); + const ssize_t n = peer ().recv (message_block.wr_ptr (), + recv_size); - if (n <= 0) - return n; + if (n <= 0) + return n; - message_block.wr_ptr (n); + message_block.wr_ptr (n); - while (message_block.length () > 0) + while (message_block.length () > 0) { - if (m_Header.space () > 0) + if (m_Header.space () > 0) { - //need to recieve the header - const size_t to_header = (message_block.length () > m_Header.space () ? m_Header.space () : message_block.length ()); - m_Header.copy (message_block.rd_ptr (), to_header); - message_block.rd_ptr (to_header); + //need to recieve the header + const size_t to_header = (message_block.length () > m_Header.space () ? m_Header.space () : message_block.length ()); + m_Header.copy (message_block.rd_ptr (), to_header); + message_block.rd_ptr (to_header); - if (m_Header.space () > 0) + if (m_Header.space () > 0) { - //couldnt recieve the whole header this time - ACE_ASSERT (message_block.length () == 0); - errno = EWOULDBLOCK; - return -1; + //couldnt recieve the whole header this time + ACE_ASSERT (message_block.length () == 0); + errno = EWOULDBLOCK; + return -1; } //we just recieved nice new header - if (this->handle_input_header () == -1) + if (handle_input_header () == -1) { - ACE_ASSERT ((errno != EWOULDBLOCK) && (errno != EAGAIN)); - return -1; + ACE_ASSERT ((errno != EWOULDBLOCK) && (errno != EAGAIN)); + return -1; } } - // Its possible on some error situations that this happens - // for example on closing when epoll recieves more chunked data and stuff - // hope this is not hack ,as proper m_RecvWPct is asserted around - if (!m_RecvWPct) + // Its possible on some error situations that this happens + // for example on closing when epoll recieves more chunked data and stuff + // hope this is not hack ,as proper m_RecvWPct is asserted around + if (!m_RecvWPct) { - sLog.outError ("Forsing close on input m_RecvWPct = NULL"); - errno = EINVAL; - return -1; + sLog.outError ("Forsing close on input m_RecvWPct = NULL"); + errno = EINVAL; + return -1; } - // We have full readed header, now check the data payload - if (m_RecvPct.space () > 0) + // We have full readed header, now check the data payload + if (m_RecvPct.space () > 0) { - //need more data in the payload - const size_t to_data = (message_block.length () > m_RecvPct.space () ? m_RecvPct.space () : message_block.length ()); - m_RecvPct.copy (message_block.rd_ptr (), to_data); - message_block.rd_ptr (to_data); + //need more data in the payload + const size_t to_data = (message_block.length () > m_RecvPct.space () ? m_RecvPct.space () : message_block.length ()); + m_RecvPct.copy (message_block.rd_ptr (), to_data); + message_block.rd_ptr (to_data); - if (m_RecvPct.space () > 0) + if (m_RecvPct.space () > 0) { - //couldnt recieve the whole data this time - ACE_ASSERT (message_block.length () == 0); - errno = EWOULDBLOCK; - return -1; + //couldnt recieve the whole data this time + ACE_ASSERT (message_block.length () == 0); + errno = EWOULDBLOCK; + return -1; } } - //just recieved fresh new payload - if (this->handle_input_payload () == -1) + //just recieved fresh new payload + if (handle_input_payload () == -1) { - ACE_ASSERT ((errno != EWOULDBLOCK) && (errno != EAGAIN)); - return -1; + ACE_ASSERT ((errno != EWOULDBLOCK) && (errno != EAGAIN)); + return -1; } } - return n == recv_size ? 1 : 2; + return n == recv_size ? 1 : 2; } -int -WorldSocket::cancel_wakeup_output (GuardType& g) +int WorldSocket::cancel_wakeup_output (GuardType& g) { - if (!m_OutActive) - return 0; + if (!m_OutActive) + return 0; - m_OutActive = false; + m_OutActive = false; - g.release (); + g.release (); - if (this->reactor ()->cancel_wakeup - (this, ACE_Event_Handler::WRITE_MASK) == -1) + if (reactor ()->cancel_wakeup + (this, ACE_Event_Handler::WRITE_MASK) == -1) { - // would be good to store errno from reactor with errno guard - sLog.outError ("WorldSocket::cancel_wakeup_output"); - return -1; + // would be good to store errno from reactor with errno guard + sLog.outError ("WorldSocket::cancel_wakeup_output"); + return -1; } - return 0; + return 0; } -int -WorldSocket::schedule_wakeup_output (GuardType& g) +int WorldSocket::schedule_wakeup_output (GuardType& g) { - if (m_OutActive) - return 0; + if (m_OutActive) + return 0; - m_OutActive = true; + m_OutActive = true; - g.release (); + g.release (); - if (this->reactor ()->schedule_wakeup - (this, ACE_Event_Handler::WRITE_MASK) == -1) + if (reactor ()->schedule_wakeup + (this, ACE_Event_Handler::WRITE_MASK) == -1) { - sLog.outError ("WorldSocket::schedule_wakeup_output"); - return -1; + sLog.outError ("WorldSocket::schedule_wakeup_output"); + return -1; } - return 0; + return 0; } -int -WorldSocket::ProcessIncoming (WorldPacket* new_pct) +int WorldSocket::ProcessIncoming (WorldPacket* new_pct) { - ACE_ASSERT (new_pct); + ACE_ASSERT (new_pct); - // manage memory ;) - ACE_Auto_Ptr<WorldPacket> aptr (new_pct); + // manage memory ;) + ACE_Auto_Ptr<WorldPacket> aptr (new_pct); - const ACE_UINT16 opcode = new_pct->GetOpcode (); + const ACE_UINT16 opcode = new_pct->GetOpcode (); - if (this->closing_) - return -1; + if (closing_) + return -1; - // dump recieved packet - if (sWorldLog.LogWorld ()) + // dump recieved packet + if (sWorldLog.LogWorld ()) { - sWorldLog.Log ("CLIENT:\nSOCKET: %u\nLENGTH: %u\nOPCODE: %s (0x%.4X)\nDATA:\n", + sWorldLog.Log ("CLIENT:\nSOCKET: %u\nLENGTH: %u\nOPCODE: %s (0x%.4X)\nDATA:\n", (uint32) get_handle (), new_pct->size (), LookupOpcodeName (new_pct->GetOpcode ()), new_pct->GetOpcode ()); - uint32 p = 0; - while (p < new_pct->size ()) + uint32 p = 0; + while (p < new_pct->size ()) { - for (uint32 j = 0; j < 16 && p < new_pct->size (); j++) - sWorldLog.Log ("%.2X ", (*new_pct)[p++]); - sWorldLog.Log ("\n"); + for (uint32 j = 0; j < 16 && p < new_pct->size (); j++) + sWorldLog.Log ("%.2X ", (*new_pct)[p++]); + sWorldLog.Log ("\n"); } - sWorldLog.Log ("\n\n"); + sWorldLog.Log ("\n\n"); } - // like one switch ;) - if (opcode == CMSG_PING) + // like one switch ;) + if (opcode == CMSG_PING) { - return HandlePing (*new_pct); + return HandlePing (*new_pct); } - else if (opcode == CMSG_AUTH_SESSION) + else if (opcode == CMSG_AUTH_SESSION) { - if (m_Session) + if (m_Session) { - sLog.outError ("WorldSocket::ProcessIncoming: Player send CMSG_AUTH_SESSION again"); - return -1; + sLog.outError ("WorldSocket::ProcessIncoming: Player send CMSG_AUTH_SESSION again"); + return -1; } - return HandleAuthSession (*new_pct); + return HandleAuthSession (*new_pct); } - else if (opcode == CMSG_KEEP_ALIVE) + else if (opcode == CMSG_KEEP_ALIVE) { - DEBUG_LOG ("CMSG_KEEP_ALIVE ,size: %d", new_pct->size ()); + DEBUG_LOG ("CMSG_KEEP_ALIVE ,size: %d", new_pct->size ()); - return 0; + return 0; } - else + else { - ACE_GUARD_RETURN (LockType, Guard, m_SessionLock, -1); + ACE_GUARD_RETURN (LockType, Guard, m_SessionLock, -1); - if (m_Session != NULL) + if (m_Session != NULL) { - // OK ,give the packet to WorldSession - aptr.release (); - // WARNINIG here we call it with locks held. - // Its possible to cause deadlock if QueuePacket calls back - m_Session->QueuePacket (new_pct); - return 0; + // OK ,give the packet to WorldSession + aptr.release (); + // WARNINIG here we call it with locks held. + // Its possible to cause deadlock if QueuePacket calls back + m_Session->QueuePacket (new_pct); + return 0; } - else + else { - sLog.outError ("WorldSocket::ProcessIncoming: Client not authed opcode = ", opcode); - return -1; + sLog.outError ("WorldSocket::ProcessIncoming: Client not authed opcode = ", opcode); + return -1; } } - ACE_NOTREACHED (return 0); + ACE_NOTREACHED (return 0); } -int -WorldSocket::HandleAuthSession (WorldPacket& recvPacket) +int WorldSocket::HandleAuthSession (WorldPacket& recvPacket) { - uint8 digest[20]; - uint32 clientSeed; - uint32 unk2; - uint32 BuiltNumberClient; - uint32 id, security; - //uint8 expansion = 0; - LocaleConstant locale; - std::string account; - Sha1Hash sha1; - BigNumber v, s, g, N, x, I; - WorldPacket packet, SendAddonPacked; - - BigNumber K; - - if (recvPacket.size () < (4 + 4 + 1 + 4 + 20)) - { - sLog.outError ("WorldSocket::HandleAuthSession: wrong packet size"); - return -1; + // NOTE: ATM the socket is singlethreaded, have this in mind ... + uint8 digest[20]; + uint32 clientSeed; + uint32 unk2; + uint32 BuiltNumberClient; + uint32 id, security; + //uint8 expansion = 0; + LocaleConstant locale; + std::string account; + Sha1Hash sha1; + BigNumber v, s, g, N, x, I; + WorldPacket packet, SendAddonPacked; + + BigNumber K; + + if (recvPacket.size () < (4 + 4 + 1 + 4 + 20)) + { + sLog.outError ("WorldSocket::HandleAuthSession: wrong packet size"); + return -1; } - // Read the content of the packet - recvPacket >> BuiltNumberClient; // for now no use - recvPacket >> unk2; - recvPacket >> account; + // Read the content of the packet + recvPacket >> BuiltNumberClient; // for now no use + recvPacket >> unk2; + recvPacket >> account; - if (recvPacket.size () < (4 + 4 + (account.size () + 1) + 4 + 20)) + if (recvPacket.size () < (4 + 4 + (account.size () + 1) + 4 + 20)) { - sLog.outError ("WorldSocket::HandleAuthSession: wrong packet size second check"); - return -1; + sLog.outError ("WorldSocket::HandleAuthSession: wrong packet size second check"); + return -1; } - recvPacket >> clientSeed; - recvPacket.read (digest, 20); + recvPacket >> clientSeed; + recvPacket.read (digest, 20); - DEBUG_LOG ("WorldSocket::HandleAuthSession: client %u, unk2 %u, account %s, clientseed %u", - BuiltNumberClient, - unk2, - account.c_str (), - clientSeed); + DEBUG_LOG ("WorldSocket::HandleAuthSession: client %u, unk2 %u, account %s, clientseed %u", + BuiltNumberClient, + unk2, + account.c_str (), + clientSeed); - // Get the account information from the realmd database - std::string safe_account = account; // Duplicate, else will screw the SHA hash verification below - loginDatabase.escape_string (safe_account); - // No SQL injection, username escaped. + // Get the account information from the realmd database + std::string safe_account = account; // Duplicate, else will screw the SHA hash verification below + loginDatabase.escape_string (safe_account); + // No SQL injection, username escaped. - QueryResult *result = + QueryResult *result = loginDatabase.PQuery ("SELECT " "id, " //0 "gmlevel, " //1 @@ -735,107 +700,106 @@ WorldSocket::HandleAuthSession (WorldPacket& recvPacket) "WHERE username = '%s'", safe_account.c_str ()); - // Stop if the account is not found - if (!result) + // Stop if the account is not found + if (!result) { - packet.Initialize (SMSG_AUTH_RESPONSE, 1); - packet << uint8 (AUTH_UNKNOWN_ACCOUNT); + packet.Initialize (SMSG_AUTH_RESPONSE, 1); + packet << uint8 (AUTH_UNKNOWN_ACCOUNT); - SendPacket (packet); + SendPacket (packet); - sLog.outError ("WorldSocket::HandleAuthSession: Sent Auth Response (unknown account)."); - return -1; + sLog.outError ("WorldSocket::HandleAuthSession: Sent Auth Response (unknown account)."); + return -1; } - Field* fields = result->Fetch (); + Field* fields = result->Fetch (); uint8 expansion = fields[8].GetUInt8(); uint32 world_expansion = sWorld.getConfig(CONFIG_EXPANSION); if(expansion > world_expansion) expansion = world_expansion; - N.SetHexStr ("894B645E89E1535BBDAD5B8B290650530801B18EBFBF5E8FAB3C82872A3E9BB7"); - g.SetDword (7); - I.SetHexStr (fields[5].GetString ()); + N.SetHexStr ("894B645E89E1535BBDAD5B8B290650530801B18EBFBF5E8FAB3C82872A3E9BB7"); + g.SetDword (7); + I.SetHexStr (fields[5].GetString ()); - //In case of leading zeros in the I hash, restore them - uint8 mDigest[SHA_DIGEST_LENGTH]; - memset (mDigest, 0, SHA_DIGEST_LENGTH); + //In case of leading zeros in the I hash, restore them + uint8 mDigest[SHA_DIGEST_LENGTH]; + memset (mDigest, 0, SHA_DIGEST_LENGTH); - if (I.GetNumBytes () <= SHA_DIGEST_LENGTH) - memcpy (mDigest, I.AsByteArray (), I.GetNumBytes ()); + if (I.GetNumBytes () <= SHA_DIGEST_LENGTH) + memcpy (mDigest, I.AsByteArray (), I.GetNumBytes ()); - std::reverse (mDigest, mDigest + SHA_DIGEST_LENGTH); + std::reverse (mDigest, mDigest + SHA_DIGEST_LENGTH); - s.SetHexStr (fields[7].GetString ()); - sha1.UpdateData (s.AsByteArray (), s.GetNumBytes ()); - sha1.UpdateData (mDigest, SHA_DIGEST_LENGTH); - sha1.Finalize (); - x.SetBinary (sha1.GetDigest (), sha1.GetLength ()); - v = g.ModExp (x, N); + s.SetHexStr (fields[7].GetString ()); + sha1.UpdateData (s.AsByteArray (), s.GetNumBytes ()); + sha1.UpdateData (mDigest, SHA_DIGEST_LENGTH); + sha1.Finalize (); + x.SetBinary (sha1.GetDigest (), sha1.GetLength ()); + v = g.ModExp (x, N); - const char* sStr = s.AsHexStr (); //Must be freed by OPENSSL_free() - const char* vStr = v.AsHexStr (); //Must be freed by OPENSSL_free() - const char* vold = fields[6].GetString (); + const char* sStr = s.AsHexStr (); //Must be freed by OPENSSL_free() + const char* vStr = v.AsHexStr (); //Must be freed by OPENSSL_free() + const char* vold = fields[6].GetString (); - DEBUG_LOG ("WorldSocket::HandleAuthSession: " - "(s,v) check s: %s v_old: %s v_new: %s", - sStr, - vold, - vStr); + DEBUG_LOG ("WorldSocket::HandleAuthSession: (s,v) check s: %s v_old: %s v_new: %s", + sStr, + vold, + vStr); - loginDatabase.PExecute ("UPDATE account " - "SET " - "v = '0', " - "s = '0' " - "WHERE username = '%s'", - safe_account.c_str ()); + loginDatabase.PExecute ("UPDATE account " + "SET " + "v = '0', " + "s = '0' " + "WHERE username = '%s'", + safe_account.c_str ()); - if (!vold || strcmp (vStr, vold)) + if (!vold || strcmp (vStr, vold)) { - packet.Initialize (SMSG_AUTH_RESPONSE, 1); - packet << uint8 (AUTH_UNKNOWN_ACCOUNT); - SendPacket (packet); - delete result; - OPENSSL_free ((void*) sStr); - OPENSSL_free ((void*) vStr); - - sLog.outBasic ("WorldSocket::HandleAuthSession: User not logged."); - return -1; + packet.Initialize (SMSG_AUTH_RESPONSE, 1); + packet << uint8 (AUTH_UNKNOWN_ACCOUNT); + SendPacket (packet); + delete result; + OPENSSL_free ((void*) sStr); + OPENSSL_free ((void*) vStr); + + sLog.outBasic ("WorldSocket::HandleAuthSession: User not logged."); + return -1; } - OPENSSL_free ((void*) sStr); - OPENSSL_free ((void*) vStr); + OPENSSL_free ((void*) sStr); + OPENSSL_free ((void*) vStr); - ///- Re-check ip locking (same check as in realmd). - if (fields[4].GetUInt8 () == 1) // if ip is locked + ///- Re-check ip locking (same check as in realmd). + if (fields[4].GetUInt8 () == 1) // if ip is locked { - if (strcmp (fields[3].GetString (), GetRemoteAddress ().c_str ())) + if (strcmp (fields[3].GetString (), GetRemoteAddress ().c_str ())) { - packet.Initialize (SMSG_AUTH_RESPONSE, 1); - packet << uint8 (AUTH_FAILED); - SendPacket (packet); + packet.Initialize (SMSG_AUTH_RESPONSE, 1); + packet << uint8 (AUTH_FAILED); + SendPacket (packet); - delete result; - sLog.outBasic ("WorldSocket::HandleAuthSession: Sent Auth Response (Account IP differs)."); - return -1; + delete result; + sLog.outBasic ("WorldSocket::HandleAuthSession: Sent Auth Response (Account IP differs)."); + return -1; } } - id = fields[0].GetUInt32 (); - security = fields[1].GetUInt16 (); - K.SetHexStr (fields[2].GetString ()); + id = fields[0].GetUInt32 (); + security = fields[1].GetUInt16 (); + K.SetHexStr (fields[2].GetString ()); - time_t mutetime = time_t (fields[9].GetUInt64 ()); + time_t mutetime = time_t (fields[9].GetUInt64 ()); - locale = LocaleConstant (fields[10].GetUInt8 ()); - if (locale >= MAX_LOCALE) - locale = LOCALE_enUS; + locale = LocaleConstant (fields[10].GetUInt8 ()); + if (locale >= MAX_LOCALE) + locale = LOCALE_enUS; - delete result; + delete result; - // Re-check account ban (same check as in realmd) - QueryResult *banresult = + // Re-check account ban (same check as in realmd) + QueryResult *banresult = loginDatabase.PQuery ("SELECT " "bandate, " "unbandate " @@ -844,219 +808,211 @@ WorldSocket::HandleAuthSession (WorldPacket& recvPacket) "AND active = 1", id); - if (banresult) // if account banned + if (banresult) // if account banned { - packet.Initialize (SMSG_AUTH_RESPONSE, 1); - packet << uint8 (AUTH_BANNED); - SendPacket (packet); + packet.Initialize (SMSG_AUTH_RESPONSE, 1); + packet << uint8 (AUTH_BANNED); + SendPacket (packet); - delete banresult; + delete banresult; - sLog.outBasic ("WorldSocket::HandleAuthSession: Sent Auth Response (Account banned)."); - return -1; + sLog.outError ("WorldSocket::HandleAuthSession: Sent Auth Response (Account banned)."); + return -1; } - // Check locked state for server - AccountTypes allowedAccountType = sWorld.GetPlayerSecurityLimit (); + // Check locked state for server + AccountTypes allowedAccountType = sWorld.GetPlayerSecurityLimit (); - if (allowedAccountType > SEC_PLAYER && security < allowedAccountType) + if (allowedAccountType > SEC_PLAYER && security < allowedAccountType) { - WorldPacket Packet (SMSG_AUTH_RESPONSE, 1); - Packet << uint8 (AUTH_UNAVAILABLE); + WorldPacket Packet (SMSG_AUTH_RESPONSE, 1); + Packet << uint8 (AUTH_UNAVAILABLE); - SendPacket (packet); + SendPacket (packet); - sLog.outBasic ("WorldSocket::HandleAuthSession: User tryes to login but his security level is not enough"); - return -1; + sLog.outBasic ("WorldSocket::HandleAuthSession: User tryes to login but his security level is not enough"); + return -1; } - // Check that Key and account name are the same on client and server - Sha1Hash sha; + // Check that Key and account name are the same on client and server + Sha1Hash sha; - uint32 t = 0; - uint32 seed = m_Seed; + uint32 t = 0; + uint32 seed = m_Seed; - sha.UpdateData (account); - sha.UpdateData ((uint8 *) & t, 4); - sha.UpdateData ((uint8 *) & clientSeed, 4); - sha.UpdateData ((uint8 *) & seed, 4); - sha.UpdateBigNumbers (&K, NULL); - sha.Finalize (); + sha.UpdateData (account); + sha.UpdateData ((uint8 *) & t, 4); + sha.UpdateData ((uint8 *) & clientSeed, 4); + sha.UpdateData ((uint8 *) & seed, 4); + sha.UpdateBigNumbers (&K, NULL); + sha.Finalize (); - if (memcmp (sha.GetDigest (), digest, 20)) + if (memcmp (sha.GetDigest (), digest, 20)) { - packet.Initialize (SMSG_AUTH_RESPONSE, 1); - packet << uint8 (AUTH_FAILED); + packet.Initialize (SMSG_AUTH_RESPONSE, 1); + packet << uint8 (AUTH_FAILED); - SendPacket (packet); + SendPacket (packet); - sLog.outBasic ("WorldSocket::HandleAuthSession: Sent Auth Response (authentification failed)."); - return -1; + sLog.outError ("WorldSocket::HandleAuthSession: Sent Auth Response (authentification failed)."); + return -1; } - std::string address = this->GetRemoteAddress (); + std::string address = GetRemoteAddress (); - DEBUG_LOG ("WorldSocket::HandleAuthSession: Client '%s' authenticated successfully from %s.", - account.c_str (), - address.c_str ()); + DEBUG_LOG ("WorldSocket::HandleAuthSession: Client '%s' authenticated successfully from %s.", + account.c_str (), + address.c_str ()); - // Update the last_ip in the database - // No SQL injection, username escaped. - loginDatabase.escape_string (address); + // Update the last_ip in the database + // No SQL injection, username escaped. + loginDatabase.escape_string (address); - loginDatabase.PExecute ("UPDATE account " - "SET last_ip = '%s' " - "WHERE username = '%s'", - address.c_str (), - safe_account.c_str ()); + loginDatabase.PExecute ("UPDATE account " + "SET last_ip = '%s' " + "WHERE username = '%s'", + address.c_str (), + safe_account.c_str ()); - // TODO protect here probably ? - // Althought atm the socket is singlethreaded - ACE_NEW_RETURN (m_Session, WorldSession (id, this, security, expansion, mutetime, locale), -1); + // NOTE ATM the socket is singlethreaded, have this in mind ... + ACE_NEW_RETURN (m_Session, WorldSession (id, this, security, expansion, mutetime, locale), -1); - m_Crypt.SetKey (&K); - m_Crypt.Init (); + m_Crypt.SetKey (&K); + m_Crypt.Init (); - // In case needed sometime the second arg is in microseconds 1 000 000 = 1 sec - ACE_OS::sleep (ACE_Time_Value (0, 10000)); + // In case needed sometime the second arg is in microseconds 1 000 000 = 1 sec + ACE_OS::sleep (ACE_Time_Value (0, 10000)); - // TODO error handling - sWorld.AddSession (this->m_Session); + sWorld.AddSession (m_Session); - // Create and send the Addon packet - if (sAddOnHandler.BuildAddonPacket (&recvPacket, &SendAddonPacked)) - SendPacket (SendAddonPacked); + // Create and send the Addon packet + if (sAddOnHandler.BuildAddonPacket (&recvPacket, &SendAddonPacked)) + SendPacket (SendAddonPacked); - return 0; + return 0; } -int -WorldSocket::HandlePing (WorldPacket& recvPacket) +int WorldSocket::HandlePing (WorldPacket& recvPacket) { - uint32 ping; - uint32 latency; + uint32 ping; + uint32 latency; - if (recvPacket.size () < 8) + if (recvPacket.size () < 8) { - sLog.outError ("WorldSocket::_HandlePing wrong packet size"); - return -1; + sLog.outError ("WorldSocket::_HandlePing wrong packet size"); + return -1; } - // Get the ping packet content - recvPacket >> ping; - recvPacket >> latency; + // Get the ping packet content + recvPacket >> ping; + recvPacket >> latency; - if (m_LastPingTime == ACE_Time_Value::zero) - m_LastPingTime = ACE_OS::gettimeofday (); // for 1st ping - else + if (m_LastPingTime == ACE_Time_Value::zero) + m_LastPingTime = ACE_OS::gettimeofday (); // for 1st ping + else { - ACE_Time_Value cur_time = ACE_OS::gettimeofday (); - ACE_Time_Value diff_time (cur_time); - diff_time -= m_LastPingTime; - m_LastPingTime = cur_time; + ACE_Time_Value cur_time = ACE_OS::gettimeofday (); + ACE_Time_Value diff_time (cur_time); + diff_time -= m_LastPingTime; + m_LastPingTime = cur_time; - if (diff_time < ACE_Time_Value (27)) + if (diff_time < ACE_Time_Value (27)) { - ++m_OverSpeedPings; + ++m_OverSpeedPings; - uint32 max_count = sWorld.getConfig (CONFIG_MAX_OVERSPEED_PINGS); + uint32 max_count = sWorld.getConfig (CONFIG_MAX_OVERSPEED_PINGS); - if (max_count && m_OverSpeedPings > max_count) + if (max_count && m_OverSpeedPings > max_count) { - ACE_GUARD_RETURN (LockType, Guard, m_SessionLock, -1); + ACE_GUARD_RETURN (LockType, Guard, m_SessionLock, -1); - if (m_Session && m_Session->GetSecurity () == SEC_PLAYER) + if (m_Session && m_Session->GetSecurity () == SEC_PLAYER) { - sLog.outError ("WorldSocket::HandlePing: Player kicked for " - "overspeeded pings adress = %s", - GetRemoteAddress ().c_str ()); + sLog.outError ("WorldSocket::HandlePing: Player kicked for " + "overspeeded pings adress = %s", + GetRemoteAddress ().c_str ()); - return -1; + return -1; } } } - else - m_OverSpeedPings = 0; + else + m_OverSpeedPings = 0; } - // critical section - { - ACE_GUARD_RETURN (LockType, Guard, m_SessionLock, -1); + // critical section + { + ACE_GUARD_RETURN (LockType, Guard, m_SessionLock, -1); - if (m_Session) - m_Session->SetLatency (latency); - else - { - sLog.outError ("WorldSocket::HandlePing: peer sent CMSG_PING, " - "but is not authenticated or got recently kicked," - " adress = %s", - this->GetRemoteAddress ().c_str ()); - return -1; - } - } + if (m_Session) + m_Session->SetLatency (latency); + else + { + sLog.outError ("WorldSocket::HandlePing: peer sent CMSG_PING, " + "but is not authenticated or got recently kicked," + " adress = %s", + GetRemoteAddress ().c_str ()); + return -1; + } + } - WorldPacket packet (SMSG_PONG, 4); - packet << ping; - return this->SendPacket (packet); + WorldPacket packet (SMSG_PONG, 4); + packet << ping; + return SendPacket (packet); } -int -WorldSocket::iSendPacket (const WorldPacket& pct) +int WorldSocket::iSendPacket (const WorldPacket& pct) { - if (m_OutBuffer->space () < pct.size () + sizeof (ServerPktHeader)) + if (m_OutBuffer->space () < pct.size () + sizeof (ServerPktHeader)) { - errno = ENOBUFS; - return -1; + errno = ENOBUFS; + return -1; } - ServerPktHeader header; - - header.cmd = pct.GetOpcode (); + ServerPktHeader header; -#if ACE_BYTE_ORDER == ACE_BIG_ENDIAN - header.cmd = ACE_SWAP_WORD (header.cmd) -#endif + header.cmd = pct.GetOpcode (); + EndianConvert(header.cmd); - header.size = (uint16) pct.size () + 2; - header.size = ACE_HTONS (header.size); + header.size = (uint16) pct.size () + 2; + EndianConvertReverse(header.size); - m_Crypt.EncryptSend ((uint8*) & header, sizeof (header)); + m_Crypt.EncryptSend ((uint8*) & header, sizeof (header)); - if (m_OutBuffer->copy ((char*) & header, sizeof (header)) == -1) - ACE_ASSERT (false); + if (m_OutBuffer->copy ((char*) & header, sizeof (header)) == -1) + ACE_ASSERT (false); - if (!pct.empty ()) - if (m_OutBuffer->copy ((char*) pct.contents (), pct.size ()) == -1) - ACE_ASSERT (false); + if (!pct.empty ()) + if (m_OutBuffer->copy ((char*) pct.contents (), pct.size ()) == -1) + ACE_ASSERT (false); - return 0; + return 0; } -bool -WorldSocket::iFlushPacketQueue () +bool WorldSocket::iFlushPacketQueue () { - WorldPacket *pct; - bool haveone = false; + WorldPacket *pct; + bool haveone = false; - while (m_PacketQueue.dequeue_head (pct) == 0) + while (m_PacketQueue.dequeue_head (pct) == 0) { - if (iSendPacket (*pct) == -1) + if (iSendPacket (*pct) == -1) { - if (m_PacketQueue.enqueue_head (pct) == -1) + if (m_PacketQueue.enqueue_head (pct) == -1) { - delete pct; - sLog.outError ("WorldSocket::iFlushPacketQueue m_PacketQueue->enqueue_head"); - return false; + delete pct; + sLog.outError ("WorldSocket::iFlushPacketQueue m_PacketQueue->enqueue_head"); + return false; } - break; + break; } - else + else { - haveone = true; - delete pct; + haveone = true; + delete pct; } } - return haveone; + return haveone; } diff --git a/src/game/WorldSocket.h b/src/game/WorldSocket.h index 9e97fe16401..10ac198b423 100644 --- a/src/game/WorldSocket.h +++ b/src/game/WorldSocket.h @@ -10,12 +10,12 @@ * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /** \addtogroup u2w User to World Communication @@ -25,7 +25,7 @@ */ #ifndef _WORLDSOCKET_H -#define _WORLDSOCKET_H +#define _WORLDSOCKET_H #include <ace/Basic_Types.h> #include <ace/Synch_Traits.h> @@ -43,7 +43,7 @@ #endif /* ACE_LACKS_PRAGMA_ONCE */ #include "Common.h" -#include "Auth/AuthCrypt.h" +#include "Auth/AuthCrypt.h" class ACE_Message_Block; class WorldPacket; @@ -54,182 +54,182 @@ typedef ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH> WorldHandler; /** * WorldSocket. - * - * This class is responsible for the comunication with + * + * This class is responsible for the comunication with * remote clients. - * Most methods return -1 on failure. + * Most methods return -1 on failure. * The class uses refferece counting. * - * For output the class uses one buffer (64K usually) and - * a queue where it stores packet if there is no place on - * the queue. The reason this is done, is because the server - * does realy a lot of small-size writes to it, and it doesn't - * scale well to allocate memory for every. When something is - * writen to the output buffer the socket is not immideately - * activated for output (again for the same reason), there - * is 10ms celling (thats why there is Update() method). - * This concept is simmilar to TCP_CORK, but TCP_CORK - * usses 200ms celling. As result overhead generated by - * sending packets from "producer" threads is minimal, + * For output the class uses one buffer (64K usually) and + * a queue where it stores packet if there is no place on + * the queue. The reason this is done, is because the server + * does realy a lot of small-size writes to it, and it doesn't + * scale well to allocate memory for every. When something is + * writen to the output buffer the socket is not immideately + * activated for output (again for the same reason), there + * is 10ms celling (thats why there is Update() method). + * This concept is simmilar to TCP_CORK, but TCP_CORK + * usses 200ms celling. As result overhead generated by + * sending packets from "producer" threads is minimal, * and doing a lot of writes with small size is tollerated. - * + * * The calls to Upate () method are managed by WorldSocketMgr * and ReactorRunnable. - * - * For input ,the class uses one 1024 bytes buffer on stack - * to which it does recv() calls. And then recieved data is - * distributed where its needed. 1024 matches pritey well the + * + * For input ,the class uses one 1024 bytes buffer on stack + * to which it does recv() calls. And then recieved data is + * distributed where its needed. 1024 matches pritey well the * traffic generated by client for now. - * - * The input/output do speculative reads/writes (AKA it tryes - * to read all data avaible in the kernel buffer or tryes to - * write everything avaible in userspace buffer), - * which is ok for using with Level and Edge Trigered IO + * + * The input/output do speculative reads/writes (AKA it tryes + * to read all data avaible in the kernel buffer or tryes to + * write everything avaible in userspace buffer), + * which is ok for using with Level and Edge Trigered IO * notification. - * + * */ class WorldSocket : protected WorldHandler { -public: - /// Declare some friends - friend class ACE_Acceptor< WorldSocket, ACE_SOCK_ACCEPTOR >; - friend class WorldSocketMgr; - friend class ReactorRunnable; + public: + /// Declare some friends + friend class ACE_Acceptor< WorldSocket, ACE_SOCK_ACCEPTOR >; + friend class WorldSocketMgr; + friend class ReactorRunnable; + + /// Declare the acceptor for this class + typedef ACE_Acceptor< WorldSocket, ACE_SOCK_ACCEPTOR > Acceptor; - /// Declare the acceptor for this class - typedef ACE_Acceptor< WorldSocket, ACE_SOCK_ACCEPTOR > Acceptor; + /// Mutex type used for various syncronizations. + typedef ACE_Thread_Mutex LockType; + typedef ACE_Guard<LockType> GuardType; - /// Mutex type used for various syncronizations. - typedef ACE_Thread_Mutex LockType; - typedef ACE_Guard<LockType> GuardType; + /// Queue for storing packets for which there is no space. + typedef ACE_Unbounded_Queue< WorldPacket* > PacketQueueT; - /// Queue for storing packets for which there is no space. - typedef ACE_Unbounded_Queue< WorldPacket* > PacketQueueT; + /// Check if socket is closed. + bool IsClosed (void) const; - /// Check if socket is closed. - bool IsClosed (void) const; + /// Close the socket. + void CloseSocket (void); - /// Close the socket. - void CloseSocket (void); + /// Get address of connected peer. + const std::string& GetRemoteAddress (void) const; - /// Get address of connected peer. - const std::string& GetRemoteAddress (void) const; + /// Send A packet on the socket, this function is reentrant. + /// @param pct packet to send + /// @return -1 of failure + int SendPacket (const WorldPacket& pct); - /// Send A packet on the socket, this function is reentrant. - /// @param pct packet to send - /// @return -1 of failure - int SendPacket (const WorldPacket& pct); + /// Add refference to this object. + long AddReference (void); - /// Add refference to this object. - long AddReference (void); + /// Remove refference to this object. + long RemoveReference (void); - /// Remove refference to this object. - long RemoveReference (void); + protected: + /// things called by ACE framework. + WorldSocket (void); + virtual ~WorldSocket (void); -protected: - /// things called by ACE framework. - WorldSocket (void); - virtual ~WorldSocket (void); + /// Called on open ,the void* is the acceptor. + virtual int open (void *); - /// Called on open ,the void* is the acceptor. - virtual int open (void *); + /// Called on failures inside of the acceptor, don't call from your code. + virtual int close (int); - /// Called on failures inside of the acceptor, don't call from your code. - virtual int close (int); + /// Called when we can read from the socket. + virtual int handle_input (ACE_HANDLE = ACE_INVALID_HANDLE); - /// Called when we can read from the socket. - virtual int handle_input (ACE_HANDLE = ACE_INVALID_HANDLE); + /// Called when the socket can write. + virtual int handle_output (ACE_HANDLE = ACE_INVALID_HANDLE); - /// Called when the socket can write. - virtual int handle_output (ACE_HANDLE = ACE_INVALID_HANDLE); + /// Called when connection is closed or error happens. + virtual int handle_close (ACE_HANDLE = ACE_INVALID_HANDLE, + ACE_Reactor_Mask = ACE_Event_Handler::ALL_EVENTS_MASK); - /// Called when connection is closed or error happens. - virtual int handle_close (ACE_HANDLE = ACE_INVALID_HANDLE, - ACE_Reactor_Mask = ACE_Event_Handler::ALL_EVENTS_MASK); + /// Called by WorldSocketMgr/ReactorRunnable. + int Update (void); - /// Called by WorldSocketMgr/ReactorRunnable. - int Update (void); + private: + /// Helper functions for processing incoming data. + int handle_input_header (void); + int handle_input_payload (void); + int handle_input_missing_data (void); -private: - /// Helper functions for processing incoming data. - int handle_input_header (void); - int handle_input_payload (void); - int handle_input_missing_data (void); + /// Help functions to mark/unmark the socket for output. + /// @param g the guard is for m_OutBufferLock, the function will release it + int cancel_wakeup_output (GuardType& g); + int schedule_wakeup_output (GuardType& g); - /// Help functions to mark/unmark the socket for output. - /// @param g the guard is for m_OutBufferLock, the function will release it - int cancel_wakeup_output (GuardType& g); - int schedule_wakeup_output (GuardType& g); + /// process one incoming packet. + /// @param new_pct received packet ,note that you need to delete it. + int ProcessIncoming (WorldPacket* new_pct); - /// process one incoming packet. - /// @param new_pct received packet ,note that you need to delete it. - int ProcessIncoming (WorldPacket* new_pct); + /// Called by ProcessIncoming() on CMSG_AUTH_SESSION. + int HandleAuthSession (WorldPacket& recvPacket); - /// Called by ProcessIncoming() on CMSG_AUTH_SESSION. - int HandleAuthSession (WorldPacket& recvPacket); + /// Called by ProcessIncoming() on CMSG_PING. + int HandlePing (WorldPacket& recvPacket); - /// Called by ProcessIncoming() on CMSG_PING. - int HandlePing (WorldPacket& recvPacket); + /// Try to write WorldPacket to m_OutBuffer ,return -1 if no space + /// Need to be called with m_OutBufferLock lock held + int iSendPacket (const WorldPacket& pct); - /// Try to write WorldPacket to m_OutBuffer ,return -1 if no space - /// Need to be called with m_OutBufferLock lock held - int iSendPacket (const WorldPacket& pct); + /// Flush m_PacketQueue if there are packets in it + /// Need to be called with m_OutBufferLock lock held + /// @return true if it wrote to the buffer ( AKA you need + /// to mark the socket for output ). + bool iFlushPacketQueue (); - /// Flush m_PacketQueue if there are packets in it - /// Need to be called with m_OutBufferLock lock held - /// @return true if it wrote to the buffer ( AKA you need - /// to mark the socket for output ). - bool iFlushPacketQueue (); + private: + /// Time in which the last ping was received + ACE_Time_Value m_LastPingTime; -private: - /// Time in which the last ping was received - ACE_Time_Value m_LastPingTime; - - /// Keep track of overspeed pings ,to prevent ping flood. - uint32 m_OverSpeedPings; + /// Keep track of overspeed pings ,to prevent ping flood. + uint32 m_OverSpeedPings; - /// Address of the remote peer - std::string m_Address; + /// Address of the remote peer + std::string m_Address; - /// Class used for managing encryption of the headers - AuthCrypt m_Crypt; + /// Class used for managing encryption of the headers + AuthCrypt m_Crypt; - /// Mutex lock to protect m_Session - LockType m_SessionLock; + /// Mutex lock to protect m_Session + LockType m_SessionLock; - /// Session to which recieved packets are routed - WorldSession* m_Session; + /// Session to which recieved packets are routed + WorldSession* m_Session; - /// here are stored the fragmens of the recieved data - WorldPacket* m_RecvWPct; + /// here are stored the fragmens of the recieved data + WorldPacket* m_RecvWPct; - /// This block actually refers to m_RecvWPct contents, - /// which alows easy and safe writing to it. - /// It wont free memory when its deleted. m_RecvWPct takes care of freeing. - ACE_Message_Block m_RecvPct; + /// This block actually refers to m_RecvWPct contents, + /// which alows easy and safe writing to it. + /// It wont free memory when its deleted. m_RecvWPct takes care of freeing. + ACE_Message_Block m_RecvPct; - /// Fragment of the recieved header. - ACE_Message_Block m_Header; + /// Fragment of the recieved header. + ACE_Message_Block m_Header; - /// Mutex for protecting otuput related data. - LockType m_OutBufferLock; + /// Mutex for protecting otuput related data. + LockType m_OutBufferLock; - /// Buffer used for writing output. - ACE_Message_Block *m_OutBuffer; + /// Buffer used for writing output. + ACE_Message_Block *m_OutBuffer; - /// Size of the m_OutBuffer. - size_t m_OutBufferSize; + /// Size of the m_OutBuffer. + size_t m_OutBufferSize; - /// Here are stored packets for which there was no space on m_OutBuffer, - /// this alows not-to kick player if its buffer is overflowed. - PacketQueueT m_PacketQueue; + /// Here are stored packets for which there was no space on m_OutBuffer, + /// this alows not-to kick player if its buffer is overflowed. + PacketQueueT m_PacketQueue; - /// True if the socket is registered with the reactor for output - bool m_OutActive; + /// True if the socket is registered with the reactor for output + bool m_OutActive; - uint32 m_Seed; + uint32 m_Seed; }; -#endif /* _WORLDSOCKET_H */ +#endif /* _WORLDSOCKET_H */ /// @} |