diff --git a/src/framework/net/connection.cpp b/src/framework/net/connection.cpp index 9625df7d..b6619cb5 100644 --- a/src/framework/net/connection.cpp +++ b/src/framework/net/connection.cpp @@ -58,6 +58,7 @@ void Connection::connect(const std::string& host, uint16 port, const SimpleCallb { m_connected = false; m_connecting = true; + m_error.clear(); m_connectCallback = connectCallback; asio::ip::tcp::resolver::query query(host, Fw::unsafeCast(port)); @@ -86,6 +87,10 @@ void Connection::close() if(!m_connected && !m_connecting) 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_connected = false; 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) { if(error != asio::error::operation_aborted) { + m_error = error; if(m_errorCallback) m_errorCallback(error); if(m_connected || m_connecting) diff --git a/src/framework/net/connection.h b/src/framework/net/connection.h index 386e6197..f78a7e07 100644 --- a/src/framework/net/connection.h +++ b/src/framework/net/connection.h @@ -57,6 +57,7 @@ public: void setErrorCallback(const ErrorCallback& errorCallback) { m_errorCallback = errorCallback; } + boost::system::error_code getError() const { return m_error; } bool isConnecting() const { return m_connecting; } bool isConnected() const { return m_connected; } @@ -80,6 +81,7 @@ protected: uint8 m_recvBuffer[RECV_BUFFER_SIZE]; bool m_connected; bool m_connecting; + boost::system::error_code m_error; int m_sendBufferSize; Timer m_sendTimer; ScheduledEventPtr m_sendEvent;