fix minor connection leak

master
Eduardo Bart 12 years ago
parent eb308997d4
commit f57d46de0e

@ -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<std::string>(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();

@ -39,8 +39,8 @@ class Connection : public std::enable_shared_from_this<Connection>, 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);

@ -35,6 +35,7 @@ class Protocol;
class Server;
typedef std::shared_ptr<Connection> ConnectionPtr;
typedef std::weak_ptr<Connection> ConnectionWeakPtr;
typedef std::shared_ptr<Protocol> ProtocolPtr;
typedef std::shared_ptr<Server> ServerPtr;

@ -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);

@ -33,6 +33,7 @@ class Protocol : public LuaObject
{
public:
Protocol();
virtual ~Protocol();
void connect(const std::string& host, uint16 port);
void disconnect();

@ -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()

Loading…
Cancel
Save