fix possible issue when disconnecting from the server

This commit is contained in:
Eduardo Bart 2012-02-03 00:54:33 -02:00
parent 05f649cdeb
commit f1cba66b1e
2 changed files with 8 additions and 0 deletions

View File

@ -58,6 +58,7 @@ void Connection::connect(const std::string& host, uint16 port, const SimpleCallb
{ {
m_connected = false; m_connected = false;
m_connecting = true; m_connecting = true;
m_error.clear();
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));
@ -86,6 +87,10 @@ void Connection::close()
if(!m_connected && !m_connecting) if(!m_connected && !m_connecting)
return; return;
// flush send data before disconnecting on clean connections
if(m_connected && !m_error && m_sendBufferSize > 0 && m_sendEvent)
m_sendEvent->execute();
m_connecting = false; m_connecting = false;
m_connected = false; m_connected = false;
m_connectCallback = nullptr; m_connectCallback = nullptr;
@ -221,6 +226,7 @@ void Connection::onTimeout(const boost::system::error_code& error)
void Connection::handleError(const boost::system::error_code& error) void Connection::handleError(const boost::system::error_code& error)
{ {
if(error != asio::error::operation_aborted) { if(error != asio::error::operation_aborted) {
m_error = error;
if(m_errorCallback) if(m_errorCallback)
m_errorCallback(error); m_errorCallback(error);
if(m_connected || m_connecting) if(m_connected || m_connecting)

View File

@ -57,6 +57,7 @@ public:
void setErrorCallback(const ErrorCallback& errorCallback) { m_errorCallback = errorCallback; } void setErrorCallback(const ErrorCallback& errorCallback) { m_errorCallback = errorCallback; }
boost::system::error_code getError() const { return m_error; }
bool isConnecting() const { return m_connecting; } bool isConnecting() const { return m_connecting; }
bool isConnected() const { return m_connected; } bool isConnected() const { return m_connected; }
@ -80,6 +81,7 @@ protected:
uint8 m_recvBuffer[RECV_BUFFER_SIZE]; uint8 m_recvBuffer[RECV_BUFFER_SIZE];
bool m_connected; bool m_connected;
bool m_connecting; bool m_connecting;
boost::system::error_code m_error;
int m_sendBufferSize; int m_sendBufferSize;
Timer m_sendTimer; Timer m_sendTimer;
ScheduledEventPtr m_sendEvent; ScheduledEventPtr m_sendEvent;