Core/NetworkIO: Restore networking related scripting hooks
Closes #12607
This commit is contained in:
@@ -32,6 +32,7 @@
|
||||
#include "CreatureAIImpl.h"
|
||||
#include "Player.h"
|
||||
#include "WorldPacket.h"
|
||||
#include "WorldSession.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
@@ -407,44 +408,42 @@ void ScriptMgr::OnSocketOpen(std::shared_ptr<WorldSocket> socket)
|
||||
FOREACH_SCRIPT(ServerScript)->OnSocketOpen(socket);
|
||||
}
|
||||
|
||||
void ScriptMgr::OnSocketClose(std::shared_ptr<WorldSocket> socket, bool wasNew)
|
||||
void ScriptMgr::OnSocketClose(std::shared_ptr<WorldSocket> socket)
|
||||
{
|
||||
ASSERT(socket);
|
||||
|
||||
FOREACH_SCRIPT(ServerScript)->OnSocketClose(socket, wasNew);
|
||||
FOREACH_SCRIPT(ServerScript)->OnSocketClose(socket);
|
||||
}
|
||||
|
||||
void ScriptMgr::OnPacketReceive(std::shared_ptr<WorldSocket> socket, WorldPacket const& packet)
|
||||
void ScriptMgr::OnPacketReceive(WorldSession* session, WorldPacket const& packet)
|
||||
{
|
||||
ASSERT(socket);
|
||||
if (SCR_REG_LST(ServerScript).empty())
|
||||
return;
|
||||
|
||||
WorldPacket copy(packet);
|
||||
FOREACH_SCRIPT(ServerScript)->OnPacketReceive(session, copy);
|
||||
}
|
||||
|
||||
void ScriptMgr::OnPacketSend(WorldSession* session, WorldPacket const& packet)
|
||||
{
|
||||
ASSERT(session);
|
||||
|
||||
if (SCR_REG_LST(ServerScript).empty())
|
||||
return;
|
||||
|
||||
WorldPacket copy(packet);
|
||||
FOREACH_SCRIPT(ServerScript)->OnPacketReceive(socket, copy);
|
||||
FOREACH_SCRIPT(ServerScript)->OnPacketSend(session, copy);
|
||||
}
|
||||
|
||||
void ScriptMgr::OnPacketSend(std::shared_ptr<WorldSocket> socket, WorldPacket const& packet)
|
||||
void ScriptMgr::OnUnknownPacketReceive(WorldSession* session, WorldPacket const& packet)
|
||||
{
|
||||
ASSERT(socket);
|
||||
ASSERT(session);
|
||||
|
||||
if (SCR_REG_LST(ServerScript).empty())
|
||||
return;
|
||||
|
||||
WorldPacket copy(packet);
|
||||
FOREACH_SCRIPT(ServerScript)->OnPacketSend(socket, copy);
|
||||
}
|
||||
|
||||
void ScriptMgr::OnUnknownPacketReceive(std::shared_ptr<WorldSocket> socket, WorldPacket const& packet)
|
||||
{
|
||||
ASSERT(socket);
|
||||
|
||||
if (SCR_REG_LST(ServerScript).empty())
|
||||
return;
|
||||
|
||||
WorldPacket copy(packet);
|
||||
FOREACH_SCRIPT(ServerScript)->OnUnknownPacketReceive(socket, copy);
|
||||
FOREACH_SCRIPT(ServerScript)->OnUnknownPacketReceive(session, copy);
|
||||
}
|
||||
|
||||
void ScriptMgr::OnOpenStateChange(bool open)
|
||||
|
||||
@@ -59,6 +59,7 @@ class Vehicle;
|
||||
class WorldPacket;
|
||||
class WorldSocket;
|
||||
class WorldObject;
|
||||
class WorldSession;
|
||||
|
||||
struct AchievementCriteriaData;
|
||||
struct AuctionEntry;
|
||||
@@ -225,19 +226,19 @@ class ServerScript : public ScriptObject
|
||||
|
||||
// Called when a socket is closed. Do not store the socket object, and do not rely on the connection
|
||||
// being open; it is not.
|
||||
virtual void OnSocketClose(std::shared_ptr<WorldSocket> /*socket*/, bool /*wasNew*/) { }
|
||||
virtual void OnSocketClose(std::shared_ptr<WorldSocket> /*socket*/) { }
|
||||
|
||||
// Called when a packet is sent to a client. The packet object is a copy of the original packet, so reading
|
||||
// and modifying it is safe.
|
||||
virtual void OnPacketSend(std::shared_ptr<WorldSocket> /*socket*/, WorldPacket& /*packet*/) { }
|
||||
virtual void OnPacketSend(WorldSession* /*session*/, WorldPacket& /*packet*/) { }
|
||||
|
||||
// Called when a (valid) packet is received by a client. The packet object is a copy of the original packet, so
|
||||
// reading and modifying it is safe.
|
||||
virtual void OnPacketReceive(std::shared_ptr<WorldSocket> /*socket*/, WorldPacket& /*packet*/) { }
|
||||
// reading and modifying it is safe. Make sure to check WorldSession pointer before usage, it might be null in case of auth packets
|
||||
virtual void OnPacketReceive(WorldSession* /*session*/, WorldPacket& /*packet*/) { }
|
||||
|
||||
// Called when an invalid (unknown opcode) packet is received by a client. The packet is a reference to the orignal
|
||||
// packet; not a copy. This allows you to actually handle unknown packets (for whatever purpose).
|
||||
virtual void OnUnknownPacketReceive(std::shared_ptr<WorldSocket> /*socket*/, WorldPacket& /*packet*/) { }
|
||||
virtual void OnUnknownPacketReceive(WorldSession* /*session*/, WorldPacket& /*packet*/) { }
|
||||
};
|
||||
|
||||
class WorldScript : public ScriptObject
|
||||
@@ -909,10 +910,10 @@ class ScriptMgr
|
||||
void OnNetworkStart();
|
||||
void OnNetworkStop();
|
||||
void OnSocketOpen(std::shared_ptr<WorldSocket> socket);
|
||||
void OnSocketClose(std::shared_ptr<WorldSocket> socket, bool wasNew);
|
||||
void OnPacketReceive(std::shared_ptr<WorldSocket> socket, WorldPacket const& packet);
|
||||
void OnPacketSend(std::shared_ptr<WorldSocket> socket, WorldPacket const& packet);
|
||||
void OnUnknownPacketReceive(std::shared_ptr<WorldSocket> socket, WorldPacket const& packet);
|
||||
void OnSocketClose(std::shared_ptr<WorldSocket> socket);
|
||||
void OnPacketReceive(WorldSession* session, WorldPacket const& packet);
|
||||
void OnPacketSend(WorldSession* session, WorldPacket const& packet);
|
||||
void OnUnknownPacketReceive(WorldSession* session, WorldPacket const& packet);
|
||||
|
||||
public: /* WorldScript */
|
||||
|
||||
|
||||
@@ -225,6 +225,8 @@ void WorldSession::SendPacket(WorldPacket* packet)
|
||||
}
|
||||
#endif // !TRINITY_DEBUG
|
||||
|
||||
sScriptMgr->OnPacketSend(this, *packet);
|
||||
|
||||
m_Socket->AsyncWrite(*packet);
|
||||
}
|
||||
|
||||
@@ -288,7 +290,7 @@ bool WorldSession::Update(uint32 diff, PacketFilter& updater)
|
||||
{
|
||||
TC_LOG_ERROR("network.opcode", "Received non-existed opcode %s from %s", GetOpcodeNameForLogging(packet->GetOpcode()).c_str()
|
||||
, GetPlayerInfo().c_str());
|
||||
sScriptMgr->OnUnknownPacketReceive(m_Socket, *packet);
|
||||
sScriptMgr->OnUnknownPacketReceive(this, *packet);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -318,7 +320,7 @@ bool WorldSession::Update(uint32 diff, PacketFilter& updater)
|
||||
}
|
||||
else if (_player->IsInWorld())
|
||||
{
|
||||
sScriptMgr->OnPacketReceive(m_Socket, *packet);
|
||||
sScriptMgr->OnPacketReceive(this, *packet);
|
||||
(this->*opHandle.handler)(*packet);
|
||||
LogUnprocessedTail(packet);
|
||||
}
|
||||
@@ -331,7 +333,7 @@ bool WorldSession::Update(uint32 diff, PacketFilter& updater)
|
||||
else
|
||||
{
|
||||
// not expected _player or must checked in packet handler
|
||||
sScriptMgr->OnPacketReceive(m_Socket, *packet);
|
||||
sScriptMgr->OnPacketReceive(this, *packet);
|
||||
(this->*opHandle.handler)(*packet);
|
||||
LogUnprocessedTail(packet);
|
||||
}
|
||||
@@ -343,7 +345,7 @@ bool WorldSession::Update(uint32 diff, PacketFilter& updater)
|
||||
LogUnexpectedOpcode(packet, "STATUS_TRANSFER", "the player is still in world");
|
||||
else
|
||||
{
|
||||
sScriptMgr->OnPacketReceive(m_Socket, *packet);
|
||||
sScriptMgr->OnPacketReceive(this, *packet);
|
||||
(this->*opHandle.handler)(*packet);
|
||||
LogUnprocessedTail(packet);
|
||||
}
|
||||
@@ -361,7 +363,7 @@ bool WorldSession::Update(uint32 diff, PacketFilter& updater)
|
||||
if (packet->GetOpcode() == CMSG_CHAR_ENUM)
|
||||
m_playerRecentlyLogout = false;
|
||||
|
||||
sScriptMgr->OnPacketReceive(m_Socket, *packet);
|
||||
sScriptMgr->OnPacketReceive(this, *packet);
|
||||
(this->*opHandle.handler)(*packet);
|
||||
LogUnprocessedTail(packet);
|
||||
break;
|
||||
|
||||
@@ -33,6 +33,7 @@ WorldSocket::WorldSocket(tcp::socket&& socket)
|
||||
|
||||
void WorldSocket::Start()
|
||||
{
|
||||
sScriptMgr->OnSocketOpen(shared_from_this());
|
||||
AsyncReadHeader();
|
||||
HandleSendAuthSession();
|
||||
}
|
||||
@@ -94,12 +95,11 @@ void WorldSocket::ReadDataHandler()
|
||||
break;
|
||||
}
|
||||
|
||||
sScriptMgr->OnPacketReceive(shared_from_this(), packet);
|
||||
HandleAuthSession(packet);
|
||||
break;
|
||||
case CMSG_KEEP_ALIVE:
|
||||
TC_LOG_DEBUG("network", "%s", opcodeName.c_str());
|
||||
sScriptMgr->OnPacketReceive(shared_from_this(), packet);
|
||||
sScriptMgr->OnPacketReceive(_worldSession, packet);
|
||||
break;
|
||||
default:
|
||||
{
|
||||
@@ -437,3 +437,10 @@ void WorldSocket::HandlePing(WorldPacket& recvPacket)
|
||||
packet << ping;
|
||||
return AsyncWrite(packet);
|
||||
}
|
||||
|
||||
void WorldSocket::CloseSocket()
|
||||
{
|
||||
sScriptMgr->OnSocketClose(shared_from_this());
|
||||
|
||||
Socket::CloseSocket();
|
||||
}
|
||||
|
||||
@@ -104,6 +104,8 @@ public:
|
||||
|
||||
void Start() override;
|
||||
|
||||
void CloseSocket() override;
|
||||
|
||||
using Base::AsyncWrite;
|
||||
void AsyncWrite(WorldPacket& packet);
|
||||
|
||||
|
||||
@@ -107,7 +107,7 @@ public:
|
||||
|
||||
bool IsOpen() const { return !_closed; }
|
||||
|
||||
void CloseSocket()
|
||||
virtual void CloseSocket()
|
||||
{
|
||||
if (_closed.exchange(true))
|
||||
return;
|
||||
|
||||
@@ -221,7 +221,7 @@ extern int main(int argc, char** argv)
|
||||
|
||||
AsyncAcceptor<WorldSocket> worldAcceptor(_ioService, worldListener, worldPort, tcpNoDelay);
|
||||
|
||||
sScriptMgr->OnStartup();
|
||||
sScriptMgr->OnNetworkStart();
|
||||
|
||||
// Set server online (allow connecting now)
|
||||
LoginDatabase.DirectPExecute("UPDATE realmlist SET flag = flag & ~%u, population = 0 WHERE id = '%u'", REALM_FLAG_INVALID, realmID);
|
||||
@@ -237,6 +237,8 @@ extern int main(int argc, char** argv)
|
||||
|
||||
TC_LOG_INFO("server.worldserver", "%s (worldserver-daemon) ready...", _FULLVERSION);
|
||||
|
||||
sScriptMgr->OnStartup();
|
||||
|
||||
WorldUpdateLoop();
|
||||
|
||||
// Shutdown starts here
|
||||
@@ -296,6 +298,8 @@ extern int main(int argc, char** argv)
|
||||
|
||||
void ShutdownThreadPool(std::vector<std::thread>& threadPool)
|
||||
{
|
||||
sScriptMgr->OnNetworkStop();
|
||||
|
||||
_ioService.stop();
|
||||
|
||||
for (auto& thread : threadPool)
|
||||
|
||||
Reference in New Issue
Block a user