fix minor connection leak

This commit is contained in:
Eduardo Bart 2012-01-13 13:41:57 -02:00
parent eb308997d4
commit f57d46de0e
6 changed files with 30 additions and 16 deletions

View File

@ -37,6 +37,11 @@ Connection::Connection() :
m_connecting = false; m_connecting = false;
} }
Connection::~Connection()
{
close();
}
void Connection::poll() void Connection::poll()
{ {
g_ioService.poll(); g_ioService.poll();
@ -55,7 +60,21 @@ void Connection::connect(const std::string& host, uint16 port, const SimpleCallb
m_connectCallback = connectCallback; m_connectCallback = connectCallback;
asio::ip::tcp::resolver::query query(host, Fw::unsafeCast<std::string>(port)); 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.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT));
m_readTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), _1)); m_readTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), _1));
@ -72,6 +91,7 @@ void Connection::close()
m_errorCallback = nullptr; m_errorCallback = nullptr;
m_recvCallback = nullptr; m_recvCallback = nullptr;
m_resolver.cancel();
m_readTimer.cancel(); m_readTimer.cancel();
m_writeTimer.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)); 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) void Connection::onConnect(const boost::system::error_code& error)
{ {
m_readTimer.cancel(); m_readTimer.cancel();

View File

@ -39,8 +39,8 @@ class Connection : public std::enable_shared_from_this<Connection>, boost::nonco
public: public:
Connection(); Connection();
~Connection();
static void init();
static void poll(); static void poll();
static void terminate(); static void terminate();
@ -57,7 +57,6 @@ public:
bool isConnected() const { return m_connected; } bool isConnected() const { return m_connected; }
protected: protected:
void onResolve(const boost::system::error_code& error, asio::ip::tcp::resolver::iterator endpointIterator);
void onConnect(const boost::system::error_code& error); void onConnect(const boost::system::error_code& error);
void onWrite(const boost::system::error_code& error, size_t); void onWrite(const boost::system::error_code& error, size_t);
void onRecv(const boost::system::error_code& error, size_t recvSize); void onRecv(const boost::system::error_code& error, size_t recvSize);

View File

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

View File

@ -29,6 +29,11 @@ Protocol::Protocol()
m_checksumEnabled = false; m_checksumEnabled = false;
} }
Protocol::~Protocol()
{
disconnect();
}
void Protocol::connect(const std::string& host, uint16 port) void Protocol::connect(const std::string& host, uint16 port)
{ {
m_connection = ConnectionPtr(new Connection); m_connection = ConnectionPtr(new Connection);

View File

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

View File

@ -83,6 +83,7 @@ void ProtocolLogin::onRecv(InputMessage& inputMessage)
void ProtocolLogin::onError(const boost::system::error_code& error) void ProtocolLogin::onError(const boost::system::error_code& error)
{ {
callLuaField("onError", error.message(), true); callLuaField("onError", error.message(), true);
disconnect();
} }
void ProtocolLogin::sendLoginPacket() void ProtocolLogin::sendLoginPacket()