Fixed the crashes

This commit is contained in:
Subv
2014-07-05 21:38:29 -05:00
parent 42b2041aeb
commit b5d5768d44
6 changed files with 35 additions and 31 deletions
+5 -5
View File
@@ -400,35 +400,35 @@ void ScriptMgr::OnNetworkStop()
FOREACH_SCRIPT(ServerScript)->OnNetworkStop();
}
void ScriptMgr::OnSocketOpen(WorldTcpSession* socket)
void ScriptMgr::OnSocketOpen(std::shared_ptr<WorldTcpSession> socket)
{
ASSERT(socket);
FOREACH_SCRIPT(ServerScript)->OnSocketOpen(socket);
}
void ScriptMgr::OnSocketClose(WorldTcpSession* socket, bool wasNew)
void ScriptMgr::OnSocketClose(std::shared_ptr<WorldTcpSession> socket, bool wasNew)
{
ASSERT(socket);
FOREACH_SCRIPT(ServerScript)->OnSocketClose(socket, wasNew);
}
void ScriptMgr::OnPacketReceive(WorldTcpSession* socket, WorldPacket packet)
void ScriptMgr::OnPacketReceive(std::shared_ptr<WorldTcpSession> socket, WorldPacket packet)
{
ASSERT(socket);
FOREACH_SCRIPT(ServerScript)->OnPacketReceive(socket, packet);
}
void ScriptMgr::OnPacketSend(WorldTcpSession* socket, WorldPacket packet)
void ScriptMgr::OnPacketSend(std::shared_ptr<WorldTcpSession> socket, WorldPacket packet)
{
ASSERT(socket);
FOREACH_SCRIPT(ServerScript)->OnPacketSend(socket, packet);
}
void ScriptMgr::OnUnknownPacketReceive(WorldTcpSession* socket, WorldPacket packet)
void ScriptMgr::OnUnknownPacketReceive(std::shared_ptr<WorldTcpSession> socket, WorldPacket packet)
{
ASSERT(socket);
+10 -10
View File
@@ -221,23 +221,23 @@ class ServerScript : public ScriptObject
virtual void OnNetworkStop() { }
// Called when a remote socket establishes a connection to the server. Do not store the socket object.
virtual void OnSocketOpen(WorldTcpSession* /*socket*/) { }
virtual void OnSocketOpen(std::shared_ptr<WorldTcpSession> /*socket*/) { }
// 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(WorldTcpSession* /*socket*/, bool /*wasNew*/) { }
virtual void OnSocketClose(std::shared_ptr<WorldTcpSession> /*socket*/, bool /*wasNew*/) { }
// 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(WorldTcpSession* /*socket*/, WorldPacket& /*packet*/) { }
virtual void OnPacketSend(std::shared_ptr<WorldTcpSession> /*socket*/, 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(WorldTcpSession* /*socket*/, WorldPacket& /*packet*/) { }
virtual void OnPacketReceive(std::shared_ptr<WorldTcpSession> /*socket*/, 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(WorldTcpSession* /*socket*/, WorldPacket& /*packet*/) { }
virtual void OnUnknownPacketReceive(std::shared_ptr<WorldTcpSession> /*socket*/, WorldPacket& /*packet*/) { }
};
class WorldScript : public ScriptObject
@@ -908,11 +908,11 @@ class ScriptMgr
void OnNetworkStart();
void OnNetworkStop();
void OnSocketOpen(WorldTcpSession* socket);
void OnSocketClose(WorldTcpSession* socket, bool wasNew);
void OnPacketReceive(WorldTcpSession* socket, WorldPacket packet);
void OnPacketSend(WorldTcpSession* socket, WorldPacket packet);
void OnUnknownPacketReceive(WorldTcpSession* socket, WorldPacket packet);
void OnSocketOpen(std::shared_ptr<WorldTcpSession> socket);
void OnSocketClose(std::shared_ptr<WorldTcpSession> socket, bool wasNew);
void OnPacketReceive(std::shared_ptr<WorldTcpSession> socket, WorldPacket packet);
void OnPacketSend(std::shared_ptr<WorldTcpSession> socket, WorldPacket packet);
void OnUnknownPacketReceive(std::shared_ptr<WorldTcpSession> socket, WorldPacket packet);
public: /* WorldScript */
+3 -3
View File
@@ -97,7 +97,7 @@ bool WorldSessionFilter::Process(WorldPacket* packet)
}
/// WorldSession constructor
WorldSession::WorldSession(uint32 id, WorldTcpSession* sock, AccountTypes sec, uint8 expansion, time_t mute_time, LocaleConstant locale, uint32 recruiter, bool isARecruiter):
WorldSession::WorldSession(uint32 id, std::shared_ptr<WorldTcpSession> sock, AccountTypes sec, uint8 expansion, time_t mute_time, LocaleConstant locale, uint32 recruiter, bool isARecruiter):
m_muteTime(mute_time),
m_timeOutTime(0),
AntiDOS(this),
@@ -149,7 +149,7 @@ WorldSession::~WorldSession()
if (m_Socket)
{
m_Socket->CloseSocket();
m_Socket = NULL;
m_Socket = nullptr;
}
delete _warden;
@@ -420,7 +420,7 @@ bool WorldSession::Update(uint32 diff, PacketFilter& updater)
expireTime -= expireTime > diff ? diff : expireTime;
if (expireTime < diff || forceExit)
{
m_Socket = NULL;
m_Socket = nullptr;
}
}
+2 -2
View File
@@ -208,7 +208,7 @@ struct PacketCounter
class WorldSession
{
public:
WorldSession(uint32 id, WorldTcpSession* sock, AccountTypes sec, uint8 expansion, time_t mute_time, LocaleConstant locale, uint32 recruiter, bool isARecruiter);
WorldSession(uint32 id, std::shared_ptr<WorldTcpSession> sock, AccountTypes sec, uint8 expansion, time_t mute_time, LocaleConstant locale, uint32 recruiter, bool isARecruiter);
~WorldSession();
bool PlayerLoading() const { return m_playerLoading; }
@@ -981,7 +981,7 @@ class WorldSession
uint32 m_GUIDLow; // set logined or recently logout player (while m_playerRecentlyLogout set)
Player* _player;
WorldTcpSession* m_Socket;
std::shared_ptr<WorldTcpSession> m_Socket;
std::string m_Address; // Current Remote Address
// std::string m_LAddress; // Last Attempted Remote Adress - we can not set attempted ip for a non-existing session!
+13 -6
View File
@@ -28,6 +28,11 @@
using boost::asio::ip::tcp;
using boost::asio::streambuf;
WorldTcpSession::WorldTcpSession(tcp::socket socket)
: _socket(std::move(socket)), _authSeed(static_cast<uint32>(rand32())), _worldSession(nullptr)
{
}
void WorldTcpSession::Start()
{
AsyncReadHeader();
@@ -54,7 +59,8 @@ void WorldTcpSession::HandleSendAuthSession()
void WorldTcpSession::AsyncReadHeader()
{
_socket.async_read_some(boost::asio::buffer(_readBuffer, sizeof(ClientPktHeader)), [this](boost::system::error_code error, size_t transferedBytes)
auto self(shared_from_this());
_socket.async_read_some(boost::asio::buffer(_readBuffer, sizeof(ClientPktHeader)), [this, self](boost::system::error_code error, size_t transferedBytes)
{
if (!error && transferedBytes == sizeof(ClientPktHeader))
{
@@ -74,7 +80,8 @@ void WorldTcpSession::AsyncReadHeader()
void WorldTcpSession::AsyncReadData(size_t dataSize)
{
_socket.async_read_some(boost::asio::buffer(&_readBuffer[sizeof(ClientPktHeader)], dataSize), [this, dataSize](boost::system::error_code error, size_t transferedBytes)
auto self(shared_from_this());
_socket.async_read_some(boost::asio::buffer(&_readBuffer[sizeof(ClientPktHeader)], dataSize), [this, dataSize, self](boost::system::error_code error, size_t transferedBytes)
{
if (!error && transferedBytes == dataSize)
{
@@ -104,12 +111,12 @@ void WorldTcpSession::AsyncReadData(size_t dataSize)
break;
}
sScriptMgr->OnPacketReceive(this, packet);
sScriptMgr->OnPacketReceive(shared_from_this(), packet);
HandleAuthSession(packet);
break;
case CMSG_KEEP_ALIVE:
TC_LOG_DEBUG("network", "%s", opcodeName.c_str());
sScriptMgr->OnPacketReceive(this, packet);
sScriptMgr->OnPacketReceive(shared_from_this(), packet);
break;
default:
{
@@ -366,7 +373,7 @@ void WorldTcpSession::HandleAuthSession(WorldPacket& recvPacket)
LoginDatabase.Execute(stmt);
// NOTE ATM the socket is single-threaded, have this in mind ...
_worldSession = new WorldSession(id, this, AccountTypes(security), expansion, mutetime, locale, recruiter, isRecruiter);
_worldSession = new WorldSession(id, shared_from_this(), AccountTypes(security), expansion, mutetime, locale, recruiter, isRecruiter);
_authCrypt.Init(&k);
@@ -391,4 +398,4 @@ void WorldTcpSession::SendAuthResponseError(uint8 code)
packet << uint8(code);
AsyncWrite(packet);
}
}
+2 -5
View File
@@ -40,13 +40,10 @@ struct ClientPktHeader
#pragma pack(pop)
class WorldTcpSession
class WorldTcpSession : public std::enable_shared_from_this<WorldTcpSession>
{
public:
WorldTcpSession(tcp::socket socket) :
_socket(std::move(socket)), _authSeed(static_cast<uint32> (rand32()))
{
}
WorldTcpSession(tcp::socket socket);
WorldTcpSession(WorldTcpSession const& right) = delete;
WorldTcpSession& operator=(WorldTcpSession const& right) = delete;