Skip to content

Commit

Permalink
Network: Remove built in nagle algo and fully go async (#653)
Browse files Browse the repository at this point in the history
* Network: Remove built in nagle algo and fully go async

* Fix compilation due to printf error

* Fix hang on shutdown

* Add changes requested in comments

* Make last session key update in sequence sync to avoid race condition with db
  • Loading branch information
killerwife committed Mar 24, 2024
1 parent 9652235 commit 0da1a50
Show file tree
Hide file tree
Showing 23 changed files with 941 additions and 1,277 deletions.
2 changes: 1 addition & 1 deletion src/game/Grids/GridNotifiersImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ void MaNGOS::LocalizedPacketDo<Builder>::operator()(Player* p)
if (i_data_cache.size() < cache_idx + 1)
i_data_cache.resize(cache_idx + 1);

auto data = std::unique_ptr<WorldPacket>(new WorldPacket());
std::unique_ptr<WorldPacket> data = std::make_unique<WorldPacket>();

i_builder(*data, loc_idx);

Expand Down
64 changes: 32 additions & 32 deletions src/game/Server/WorldSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ bool WorldSessionFilter::Process(WorldPacket const& packet) const
/// WorldSession constructor
WorldSession::WorldSession(uint32 id, WorldSocket* sock, AccountTypes sec, time_t mute_time, LocaleConstant locale, std::string accountName, uint32 accountFlags) :
m_muteTime(mute_time),
_player(nullptr), m_Socket(sock ? sock->shared<WorldSocket>() : nullptr), m_requestSocket(nullptr), m_sessionState(WORLD_SESSION_STATE_CREATED),
_player(nullptr), m_socket(sock ? sock->shared_from_this() : nullptr), m_requestSocket(nullptr), m_sessionState(WORLD_SESSION_STATE_CREATED),
_security(sec), _accountId(id), m_accountName(accountName), m_accountFlags(accountFlags),
m_clientOS(CLIENT_OS_UNKNOWN), m_clientPlatform(CLIENT_PLATFORM_UNKNOWN), m_orderCounter(0),
_logoutTime(0), m_playerSave(true), m_inQueue(false), m_playerLoading(false), m_kickSession(false), m_playerLogout(false), m_playerRecentlyLogout(false),
Expand All @@ -116,12 +116,12 @@ WorldSession::~WorldSession()

// marks this session as finalized in the socket which references (BUT DOES NOT OWN) it.
// this lets the socket handling code know that the socket can be safely deleted
if (m_Socket)
if (m_socket)
{
if (!m_Socket->IsClosed())
m_Socket->Close();
if (!m_socket->IsClosed())
m_socket->Close();

m_Socket->FinalizeSession();
m_socket->FinalizeSession();
}
}

Expand All @@ -135,22 +135,22 @@ void WorldSession::SetOffline()
}

// be sure its closed (may occur when second session is opened)
if (m_Socket)
if (m_socket)
{
if (!m_Socket->IsClosed())
m_Socket->Close();
if (!m_socket->IsClosed())
m_socket->Close();

// unexpected socket close, let it be deleted
m_Socket->FinalizeSession();
m_Socket = nullptr;
m_socket->FinalizeSession();
m_socket = nullptr;
}

m_sessionState = WORLD_SESSION_STATE_OFFLINE;
}

void WorldSession::SetOnline()
{
if (_player && m_Socket && !m_Socket->IsClosed())
if (_player && m_socket && !m_socket->IsClosed())
{
m_sessionState = WORLD_SESSION_STATE_READY;
m_kickTime = 0;
Expand All @@ -169,7 +169,7 @@ bool WorldSession::RequestNewSocket(WorldSocket* socket)
if (m_requestSocket)
return false;

m_requestSocket = socket->shared<WorldSocket>();
m_requestSocket = socket->shared_from_this();
m_sessionState = WORLD_SESSION_STATE_CREATED;
return true;
}
Expand Down Expand Up @@ -208,7 +208,7 @@ void WorldSession::SendPacket(WorldPacket const& packet, bool forcedSend /*= fal
}
#endif

if (!m_Socket || (m_sessionState != WORLD_SESSION_STATE_READY && !forcedSend))
if (!m_socket || (m_sessionState != WORLD_SESSION_STATE_READY && !forcedSend))
{
//sLog.outDebug("Refused to send %s to %s", packet.GetOpcodeName(), _player ? _player->GetName() : "UKNOWN");
return;
Expand Down Expand Up @@ -250,7 +250,7 @@ void WorldSession::SendPacket(WorldPacket const& packet, bool forcedSend /*= fal

#endif // !MANGOS_DEBUG

m_Socket->SendPacket(packet);
m_socket->SendPacket(packet);
}

/// Add an incoming packet to the queue
Expand Down Expand Up @@ -358,7 +358,7 @@ bool WorldSession::Update(uint32 diff)
std::swap(recvQueueCopy, m_recvQueue);
}

if (m_Socket && !m_Socket->IsClosed() && m_anticheat)
if (m_socket && !m_socket->IsClosed() && m_anticheat)
{
auto const now = WorldTimer::getMSTime();
m_anticheat->Update(WorldTimer::getMSTimeDiff(m_lastAnticheatUpdate, now));
Expand All @@ -367,7 +367,7 @@ bool WorldSession::Update(uint32 diff)

///- Retrieve packets from the receive queue and call the appropriate handlers
/// not process packets if socket already closed
while (m_Socket && !m_Socket->IsClosed() && !recvQueueCopy.empty())
while (m_socket && !m_socket->IsClosed() && !recvQueueCopy.empty())
{
// sLog.outError("MOEP: %s (0x%.4X)", packet->GetOpcodeName(), packet->GetOpcode());

Expand Down Expand Up @@ -484,9 +484,9 @@ bool WorldSession::Update(uint32 diff)
if (!IsOffline())
SetOffline();

m_Socket = m_requestSocket;
m_socket = m_requestSocket;
m_requestSocket = nullptr;
sLog.outDetail("New Session key %s", m_Socket->GetSessionKey().AsHexStr());
sLog.outDetail("New Session key %s", m_socket->GetSessionKey().AsHexStr());
SendAuthOk();
}
else
Expand All @@ -504,7 +504,7 @@ bool WorldSession::Update(uint32 diff)

// waiting to go online
// TODO:: Maybe check if have to send queue update?
if (!m_Socket || (m_Socket && m_Socket->IsClosed()))
if (!m_socket || (m_socket && m_socket->IsClosed()))
{
// directly remove this session
return false;
Expand All @@ -523,7 +523,7 @@ bool WorldSession::Update(uint32 diff)

case WORLD_SESSION_STATE_READY:
{
if (m_Socket && m_Socket->IsClosed())
if (m_socket && m_socket->IsClosed())
{
if (!_player)
return false;
Expand All @@ -542,7 +542,7 @@ bool WorldSession::Update(uint32 diff)
if (ShouldDisconnect(time(nullptr))) // check if delayed logout is fired
{
LogoutPlayer();
if (!m_requestSocket && (!m_Socket || m_Socket->IsClosed()))
if (!m_requestSocket && (!m_socket || m_socket->IsClosed()))
return false;
}

Expand All @@ -563,7 +563,7 @@ void WorldSession::UpdateMap(uint32 diff)
std::swap(recvQueueMapCopy, m_recvQueueMap);
}

while (m_Socket && !m_Socket->IsClosed() && recvQueueMapCopy.size())
while (m_socket && !m_socket->IsClosed() && recvQueueMapCopy.size())
{
auto const packet = std::move(recvQueueMapCopy.front());
recvQueueMapCopy.pop_front();
Expand Down Expand Up @@ -734,7 +734,7 @@ void WorldSession::LogoutPlayer()

// remove player from the group if he is:
// a) in group; b) not in raid group; c) logging out normally (not being kicked or disconnected)
if (_player->GetGroup() && !_player->GetGroup()->IsRaidGroup() && m_Socket && !m_Socket->IsClosed())
if (_player->GetGroup() && !_player->GetGroup()->IsRaidGroup() && m_socket && !m_socket->IsClosed())
_player->RemoveFromGroup();
#endif

Expand Down Expand Up @@ -808,10 +808,10 @@ void WorldSession::LogoutPlayer()

if (m_kickSession)
{
if (m_Socket)
if (m_socket)
{
m_Socket->Close();
m_Socket = nullptr;
m_socket->Close();
m_socket = nullptr;
}
m_kickSession = false;
}
Expand Down Expand Up @@ -1234,24 +1234,24 @@ void WorldSession::SynchronizeMovement(MovementInfo& movementInfo)

std::deque<uint32> WorldSession::GetOutOpcodeHistory()
{
if (m_Socket)
return m_Socket->GetOutOpcodeHistory();
if (m_socket)
return m_socket->GetOutOpcodeHistory();
else
return std::deque<uint32>();
}

std::deque<uint32> WorldSession::GetIncOpcodeHistory()
{
if (m_Socket)
return m_Socket->GetIncOpcodeHistory();
if (m_socket)
return m_socket->GetIncOpcodeHistory();
else
return std::deque<uint32>();
}

void WorldSession::SetPacketLogging(bool state)
{
if (m_Socket)
m_Socket->SetPacketLogging(state);
if (m_socket)
m_socket->SetPacketLogging(state);
}

void WorldSession::SendAuthOk() const
Expand Down
6 changes: 3 additions & 3 deletions src/game/Server/WorldSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ class WorldSession
void SetSecurity(AccountTypes security) { _security = security; }
#if defined(BUILD_DEPRECATED_PLAYERBOT) || defined(ENABLE_PLAYERBOTS)
// Players connected without socket are bot
const std::string GetRemoteAddress() const { return m_Socket ? m_Socket->GetRemoteAddress() : "disconnected/bot"; }
const std::string GetRemoteAddress() const { return m_socket ? m_socket->GetRemoteAddress() : "disconnected/bot"; }
#else
const std::string GetRemoteAddress() const { return m_Socket ? m_Socket->GetRemoteAddress() : "disconnected"; }
const std::string GetRemoteAddress() const { return m_socket ? m_socket->GetRemoteAddress() : "disconnected"; }
#endif
const std::string& GetLocalAddress() const { return m_localAddress; }

Expand Down Expand Up @@ -823,7 +823,7 @@ class WorldSession

uint32 m_GUIDLow; // set logged or recently logout player (while m_playerRecentlyLogout set)
Player* _player;
std::shared_ptr<WorldSocket> m_Socket; // socket pointer is owned by the network thread which created it
std::shared_ptr<WorldSocket> m_socket; // socket pointer is owned by the network thread which created it
std::shared_ptr<WorldSocket> m_requestSocket; // a new socket for this session is requested (double connection)
std::string m_localAddress;
WorldSessionState m_sessionState; // this session state
Expand Down
Loading

0 comments on commit 0da1a50

Please sign in to comment.