[svn] Merge from Mangos:

3c7ac5bd3e20c33a22ac57c5c3bac23a0798dc9e 2008-10-23 19:06:27
Some endianess related fixes and cleanups. By VladimirMangos.

--HG--
branch : trunk
This commit is contained in:
megamage
2008-11-06 16:10:28 -06:00
parent 0aada07187
commit 37d4724c97
5 changed files with 906 additions and 948 deletions
+7 -1
View File
@@ -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
+4 -3
View File
@@ -230,7 +230,7 @@ World::AddSession_ (WorldSession* s)
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
packet << uint8 (s->Expansion()); // 0 - normal, 1 - TBC, must be set in database manually for each account
s->SendPacket (&packet);
UpdateMaxSessionCounters ();
@@ -2304,7 +2304,7 @@ BanReturn World::BanAccount(BanMode mode, std::string nameOrIP, std::string dura
if(!resultAccounts)
{
if(mode==BAN_IP)
return BAN_SUCCESS;
return BAN_SUCCESS; // ip correctly banned but nobody affected (yet)
else
return BAN_NOTFOUND; // Nobody to ban
}
@@ -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());
}
}
@@ -2507,6 +2507,7 @@ void World::ProcessCliCommands()
return;
CliCommandHolder::Print* zprint;
while (!cliCmdQueue.empty())
{
sLog.outDebug("CLI command under processing...");
+2 -7
View File
@@ -78,7 +78,6 @@ WorldSession::~WorldSession()
WorldPacket *packet = _recvQueue.next ();
delete packet;
}
}
void WorldSession::SizeError(WorldPacket const& packet, uint32 size) const
@@ -133,7 +132,7 @@ 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 ();
@@ -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.
@@ -521,7 +520,3 @@ void WorldSession::SendAuthWaitQue(uint32 position)
SendPacket(&packet);
}
}
+85 -129
View File
@@ -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,7 +83,7 @@ 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)
@@ -94,33 +94,31 @@ WorldSocket::~WorldSocket (void)
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;
}
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_)
if (closing_)
return;
this->closing_ = true;
closing_ = true;
this->peer ().close_writer ();
peer ().close_writer ();
}
{
@@ -128,21 +126,18 @@ WorldSocket::CloseSocket (void)
m_Session = NULL;
}
}
const std::string&
WorldSocket::GetRemoteAddress (void) const
const std::string& WorldSocket::GetRemoteAddress (void) const
{
return m_Address;
}
int
WorldSocket::SendPacket (const WorldPacket& pct)
int WorldSocket::SendPacket (const WorldPacket& pct)
{
ACE_GUARD_RETURN (LockType, Guard, m_OutBufferLock, -1);
if (this->closing_)
if (closing_)
return -1;
// Dump outgoing packet.
@@ -185,20 +180,17 @@ WorldSocket::SendPacket (const WorldPacket& pct)
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);
@@ -220,7 +212,7 @@ WorldSocket::open (void *a)
// 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;
@@ -236,51 +228,47 @@ WorldSocket::open (void *a)
return -1;
// Register with ACE Reactor
if (this->reactor ()->register_handler
(this,
ACE_Event_Handler::READ_MASK | ACE_Event_Handler::WRITE_MASK) == -1)
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;
}
// reactor takes care of the socket from now on
this->remove_reference ();
remove_reference ();
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;
}
int
WorldSocket::handle_input (ACE_HANDLE)
int WorldSocket::handle_input (ACE_HANDLE)
{
if (this->closing_)
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 ?
return Update (); // interesting line ,isnt it ?
}
DEBUG_LOG ("WorldSocket::handle_input: Peer error closing connection errno = %s", ACE_OS::strerror (errno));
errno = ECONNRESET;
return -1;
}
case 0:
@@ -288,35 +276,33 @@ WorldSocket::handle_input (ACE_HANDLE)
DEBUG_LOG ("WorldSocket::handle_input: Peer has closed connection\n");
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);
if (this->closing_)
if (closing_)
return -1;
const size_t send_len = m_OutBuffer->length ();
if (send_len == 0)
return this->cancel_wakeup_output (Guard);
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)
@@ -324,7 +310,7 @@ WorldSocket::handle_output (ACE_HANDLE)
else if (n == -1)
{
if (errno == EWOULDBLOCK || errno == EAGAIN)
return this->schedule_wakeup_output (Guard);
return schedule_wakeup_output (Guard);
return -1;
}
@@ -335,32 +321,31 @@ WorldSocket::handle_output (ACE_HANDLE)
// 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
{
m_OutBuffer->reset ();
if (!iFlushPacketQueue ())
return this->cancel_wakeup_output (Guard);
return cancel_wakeup_output (Guard);
else
return this->schedule_wakeup_output (Guard);
return schedule_wakeup_output (Guard);
}
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);
this->closing_ = true;
closing_ = true;
if (h == ACE_INVALID_HANDLE)
this->peer ().close_writer ();
peer ().close_writer ();
}
// Critical section
@@ -373,49 +358,35 @@ WorldSocket::handle_close (ACE_HANDLE h, ACE_Reactor_Mask)
return 0;
}
int
WorldSocket::Update (void)
int WorldSocket::Update (void)
{
if (this->closing_)
if (closing_)
return -1;
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);
if (m_Header.length () != sizeof (ClientPktHeader))
{
sLog.outError ("WorldSocket::handle_input_header: internal error: invalid header");
errno = EINVAL;
return -1;
}
ACE_ASSERT (m_Header.length () == sizeof (ClientPktHeader));
m_Crypt.DecryptRecv ((ACE_UINT8*) m_Header.rd_ptr (), sizeof (ClientPktHeader));
ClientPktHeader& header = *((ClientPktHeader*) m_Header.rd_ptr ());
header.size = ACE_NTOHS (header.size);
EndianConvertReverse(header.size);
EndianConvert(header.cmd);
#if ACE_BYTE_ORDER == ACE_BIG_ENDIAN
header.cmd = ACE_SWAP_LONG (header.cmd)
#endif // ACE_BIG_ENDIAN
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);
header.size, header.cmd);
errno = EINVAL;
return -1;
@@ -435,12 +406,10 @@ WorldSocket::handle_input_header (void)
ACE_ASSERT(m_RecvPct.space() == 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
@@ -449,7 +418,7 @@ WorldSocket::handle_input_payload (void)
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 ();
@@ -463,8 +432,7 @@ WorldSocket::handle_input_payload (void)
return ret;
}
int
WorldSocket::handle_input_missing_data (void)
int WorldSocket::handle_input_missing_data (void)
{
char buf [1024];
@@ -482,7 +450,7 @@ WorldSocket::handle_input_missing_data (void)
const size_t recv_size = message_block.space ();
const ssize_t n = this->peer ().recv (message_block.wr_ptr (),
const ssize_t n = peer ().recv (message_block.wr_ptr (),
recv_size);
if (n <= 0)
@@ -508,7 +476,7 @@ WorldSocket::handle_input_missing_data (void)
}
//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;
@@ -543,7 +511,7 @@ WorldSocket::handle_input_missing_data (void)
}
//just recieved fresh new payload
if (this->handle_input_payload () == -1)
if (handle_input_payload () == -1)
{
ACE_ASSERT ((errno != EWOULDBLOCK) && (errno != EAGAIN));
return -1;
@@ -553,8 +521,7 @@ WorldSocket::handle_input_missing_data (void)
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;
@@ -563,7 +530,7 @@ WorldSocket::cancel_wakeup_output (GuardType& g)
g.release ();
if (this->reactor ()->cancel_wakeup
if (reactor ()->cancel_wakeup
(this, ACE_Event_Handler::WRITE_MASK) == -1)
{
// would be good to store errno from reactor with errno guard
@@ -574,8 +541,7 @@ WorldSocket::cancel_wakeup_output (GuardType& g)
return 0;
}
int
WorldSocket::schedule_wakeup_output (GuardType& g)
int WorldSocket::schedule_wakeup_output (GuardType& g)
{
if (m_OutActive)
return 0;
@@ -584,7 +550,7 @@ WorldSocket::schedule_wakeup_output (GuardType& g)
g.release ();
if (this->reactor ()->schedule_wakeup
if (reactor ()->schedule_wakeup
(this, ACE_Event_Handler::WRITE_MASK) == -1)
{
sLog.outError ("WorldSocket::schedule_wakeup_output");
@@ -594,8 +560,7 @@ WorldSocket::schedule_wakeup_output (GuardType& g)
return 0;
}
int
WorldSocket::ProcessIncoming (WorldPacket* new_pct)
int WorldSocket::ProcessIncoming (WorldPacket* new_pct)
{
ACE_ASSERT (new_pct);
@@ -604,7 +569,7 @@ WorldSocket::ProcessIncoming (WorldPacket* new_pct)
const ACE_UINT16 opcode = new_pct->GetOpcode ();
if (this->closing_)
if (closing_)
return -1;
// dump recieved packet
@@ -670,9 +635,9 @@ WorldSocket::ProcessIncoming (WorldPacket* new_pct)
ACE_NOTREACHED (return 0);
}
int
WorldSocket::HandleAuthSession (WorldPacket& recvPacket)
int WorldSocket::HandleAuthSession (WorldPacket& recvPacket)
{
// NOTE: ATM the socket is singlethreaded, have this in mind ...
uint8 digest[20];
uint32 clientSeed;
uint32 unk2;
@@ -778,8 +743,7 @@ WorldSocket::HandleAuthSession (WorldPacket& recvPacket)
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",
DEBUG_LOG ("WorldSocket::HandleAuthSession: (s,v) check s: %s v_old: %s v_new: %s",
sStr,
vold,
vStr);
@@ -852,7 +816,7 @@ WorldSocket::HandleAuthSession (WorldPacket& recvPacket)
delete banresult;
sLog.outBasic ("WorldSocket::HandleAuthSession: Sent Auth Response (Account banned).");
sLog.outError ("WorldSocket::HandleAuthSession: Sent Auth Response (Account banned).");
return -1;
}
@@ -890,11 +854,11 @@ WorldSocket::HandleAuthSession (WorldPacket& recvPacket)
SendPacket (packet);
sLog.outBasic ("WorldSocket::HandleAuthSession: Sent Auth Response (authentification failed).");
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 (),
@@ -910,8 +874,7 @@ WorldSocket::HandleAuthSession (WorldPacket& recvPacket)
address.c_str (),
safe_account.c_str ());
// TODO protect here probably ?
// Althought atm the socket is singlethreaded
// 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);
@@ -920,8 +883,7 @@ WorldSocket::HandleAuthSession (WorldPacket& recvPacket)
// 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))
@@ -930,8 +892,7 @@ WorldSocket::HandleAuthSession (WorldPacket& recvPacket)
return 0;
}
int
WorldSocket::HandlePing (WorldPacket& recvPacket)
int WorldSocket::HandlePing (WorldPacket& recvPacket)
{
uint32 ping;
uint32 latency;
@@ -990,18 +951,17 @@ WorldSocket::HandlePing (WorldPacket& recvPacket)
sLog.outError ("WorldSocket::HandlePing: peer sent CMSG_PING, "
"but is not authenticated or got recently kicked,"
" adress = %s",
this->GetRemoteAddress ().c_str ());
GetRemoteAddress ().c_str ());
return -1;
}
}
WorldPacket packet (SMSG_PONG, 4);
packet << ping;
return this->SendPacket (packet);
return SendPacket (packet);
}
int
WorldSocket::iSendPacket (const WorldPacket& pct)
int WorldSocket::iSendPacket (const WorldPacket& pct)
{
if (m_OutBuffer->space () < pct.size () + sizeof (ServerPktHeader))
{
@@ -1012,13 +972,10 @@ WorldSocket::iSendPacket (const WorldPacket& pct)
ServerPktHeader header;
header.cmd = pct.GetOpcode ();
#if ACE_BYTE_ORDER == ACE_BIG_ENDIAN
header.cmd = ACE_SWAP_WORD (header.cmd)
#endif
EndianConvert(header.cmd);
header.size = (uint16) pct.size () + 2;
header.size = ACE_HTONS (header.size);
EndianConvertReverse(header.size);
m_Crypt.EncryptSend ((uint8*) & header, sizeof (header));
@@ -1032,8 +989,7 @@ WorldSocket::iSendPacket (const WorldPacket& pct)
return 0;
}
bool
WorldSocket::iFlushPacketQueue ()
bool WorldSocket::iFlushPacketQueue ()
{
WorldPacket *pct;
bool haveone = false;