diff --git a/src/framework/net/connection.cpp b/src/framework/net/connection.cpp index 30176938..efa99e52 100644 --- a/src/framework/net/connection.cpp +++ b/src/framework/net/connection.cpp @@ -37,6 +37,11 @@ Connection::Connection() : m_connecting = false; } +Connection::~Connection() +{ + close(); +} + void Connection::poll() { g_ioService.poll(); @@ -55,7 +60,21 @@ void Connection::connect(const std::string& host, uint16 port, const SimpleCallb m_connectCallback = connectCallback; asio::ip::tcp::resolver::query query(host, Fw::unsafeCast(port)); - m_resolver.async_resolve(query, std::bind(&Connection::onResolve, shared_from_this(), _1, _2)); + + auto weakSelf = ConnectionWeakPtr(shared_from_this()); + m_resolver.async_resolve(query, [=](const boost::system::error_code& error, asio::ip::tcp::resolver::iterator endpointIterator) { + if(!weakSelf.lock()) + return; + m_readTimer.cancel(); + + if(!error) { + m_socket.async_connect(*endpointIterator, std::bind(&Connection::onConnect, shared_from_this(), _1)); + + m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT)); + m_readTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), _1)); + } else + handleError(error); + }); m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT)); m_readTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), _1)); @@ -72,6 +91,7 @@ void Connection::close() m_errorCallback = nullptr; m_recvCallback = nullptr; + m_resolver.cancel(); m_readTimer.cancel(); m_writeTimer.cancel(); @@ -130,19 +150,6 @@ void Connection::read_some(const RecvCallback& callback) m_readTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), _1)); } -void Connection::onResolve(const boost::system::error_code& error, asio::ip::tcp::resolver::iterator endpointIterator) -{ - m_readTimer.cancel(); - - if(!error) { - m_socket.async_connect(*endpointIterator, std::bind(&Connection::onConnect, shared_from_this(), _1)); - - m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT)); - m_readTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), _1)); - } else - handleError(error); -} - void Connection::onConnect(const boost::system::error_code& error) { m_readTimer.cancel(); diff --git a/src/framework/net/connection.h b/src/framework/net/connection.h index 7fda70d7..0ee2975f 100644 --- a/src/framework/net/connection.h +++ b/src/framework/net/connection.h @@ -39,8 +39,8 @@ class Connection : public std::enable_shared_from_this, boost::nonco public: Connection(); + ~Connection(); - static void init(); static void poll(); static void terminate(); @@ -57,7 +57,6 @@ public: bool isConnected() const { return m_connected; } protected: - void onResolve(const boost::system::error_code& error, asio::ip::tcp::resolver::iterator endpointIterator); void onConnect(const boost::system::error_code& error); void onWrite(const boost::system::error_code& error, size_t); void onRecv(const boost::system::error_code& error, size_t recvSize); diff --git a/src/framework/net/declarations.h b/src/framework/net/declarations.h index 6b228aa1..55f9dd2e 100644 --- a/src/framework/net/declarations.h +++ b/src/framework/net/declarations.h @@ -35,6 +35,7 @@ class Protocol; class Server; typedef std::shared_ptr ConnectionPtr; +typedef std::weak_ptr ConnectionWeakPtr; typedef std::shared_ptr ProtocolPtr; typedef std::shared_ptr ServerPtr; diff --git a/src/framework/net/protocol.cpp b/src/framework/net/protocol.cpp index 94c777eb..e17471ee 100644 --- a/src/framework/net/protocol.cpp +++ b/src/framework/net/protocol.cpp @@ -29,6 +29,11 @@ Protocol::Protocol() m_checksumEnabled = false; } +Protocol::~Protocol() +{ + disconnect(); +} + void Protocol::connect(const std::string& host, uint16 port) { m_connection = ConnectionPtr(new Connection); diff --git a/src/framework/net/protocol.h b/src/framework/net/protocol.h index 0d8df4fc..5938de41 100644 --- a/src/framework/net/protocol.h +++ b/src/framework/net/protocol.h @@ -33,6 +33,7 @@ class Protocol : public LuaObject { public: Protocol(); + virtual ~Protocol(); void connect(const std::string& host, uint16 port); void disconnect(); diff --git a/src/otclient/net/protocollogin.cpp b/src/otclient/net/protocollogin.cpp index a01e24d9..530569f6 100644 --- a/src/otclient/net/protocollogin.cpp +++ b/src/otclient/net/protocollogin.cpp @@ -83,6 +83,7 @@ void ProtocolLogin::onRecv(InputMessage& inputMessage) void ProtocolLogin::onError(const boost::system::error_code& error) { callLuaField("onError", error.message(), true); + disconnect(); } void ProtocolLogin::sendLoginPacket()