Cancel functionality works again.

Also async operations are now cancelled when the dbconnection is closed and if needed also a cancel is
send before closing the connection.
This commit is contained in:
Eelke Klein 2017-08-25 08:37:18 +02:00
parent f11f9545ac
commit 16676aa910
2 changed files with 65 additions and 51 deletions

View file

@ -8,9 +8,9 @@ using namespace boost::asio;
ASyncDBConnection::ASyncDBConnection(boost::asio::io_service &ios) ASyncDBConnection::ASyncDBConnection(boost::asio::io_service &ios)
: m_asioSock(ios) : m_asioSock(ios)
{ {}
} ASyncDBConnection::~ASyncDBConnection() = default;
ASyncDBConnection::State ASyncDBConnection::state() const ASyncDBConnection::State ASyncDBConnection::state() const
{ {
@ -46,6 +46,8 @@ void ASyncDBConnection::setupConnection(const ConnectionConfig &config)
void ASyncDBConnection::async_connect_handler(boost::system::error_code ec, std::size_t s) void ASyncDBConnection::async_connect_handler(boost::system::error_code ec, std::size_t s)
{ {
// boost::asio::error::operation_aborted
if (ec == boost::system::errc::success) {
auto poll_state = m_connection.connectPoll(); auto poll_state = m_connection.connectPoll();
if (poll_state == PGRES_POLLING_OK) { if (poll_state == PGRES_POLLING_OK) {
// if connected return true // if connected return true
@ -68,12 +70,14 @@ void ASyncDBConnection::async_connect_handler(boost::system::error_code ec, std:
{ async_connect_handler(ec, s); } { async_connect_handler(ec, s); }
); );
} }
}
} }
void ASyncDBConnection::doStateCallback(State state) void ASyncDBConnection::doStateCallback(State state)
{ {
m_state = state; m_state = state;
if (state == State::Connected) { if (state == State::Connected) {
m_canceller = m_connection.getCancel();
m_connection.setNoticeReceiver( m_connection.setNoticeReceiver(
[this](const PGresult *result) { processNotice(result); }); [this](const PGresult *result) { processNotice(result); });
} }
@ -93,7 +97,12 @@ void ASyncDBConnection::closeConnection()
// m_thread.join(); // m_thread.join();
// } // }
// SHould this be async too???? // SHould this be async too????
if (m_state == State::QuerySend) {
m_canceller.cancel(nullptr);
}
m_asioSock.close();
m_connection.close(); m_connection.close();
doStateCallback(State::NotConnected);
} }
bool ASyncDBConnection::send(const std::string &command, on_result_callback on_result) bool ASyncDBConnection::send(const std::string &command, on_result_callback on_result)
@ -133,6 +142,7 @@ bool ASyncDBConnection::send(const std::string &command, Pgsql::Params params, o
void ASyncDBConnection::async_query_handler(boost::system::error_code ec, std::size_t s, on_result_callback on_result) void ASyncDBConnection::async_query_handler(boost::system::error_code ec, std::size_t s, on_result_callback on_result)
{ {
if (ec == boost::system::errc::success) {
bool finished = false; bool finished = false;
if (m_connection.consumeInput()) { if (m_connection.consumeInput()) {
while ( ! finished && ! m_connection.isBusy()) { while ( ! finished && ! m_connection.isBusy()) {
@ -159,11 +169,12 @@ void ASyncDBConnection::async_query_handler(boost::system::error_code ec, std::s
{ async_query_handler(ec, s, on_result); } { async_query_handler(ec, s, on_result); }
); );
} }
}
} }
bool ASyncDBConnection::cancel() bool ASyncDBConnection::cancel()
{ {
return false; //m_threadData.cancel(); return m_canceller.cancel(nullptr);
} }
void ASyncDBConnection::setStateCallback(on_state_callback state_callback) void ASyncDBConnection::setStateCallback(on_state_callback state_callback)

View file

@ -24,17 +24,18 @@ public:
enum class State { enum class State {
NotConnected, NotConnected,
Connecting, Connecting,
Connected, Connected, ///< connected and idle
QuerySend, QuerySend, ///< connected query send expecting result
CancelSend, CancelSend, ///< cancel send expecting result
Terminating Terminating ///< shutting down
}; };
using on_result_callback = std::function<void(std::shared_ptr<Pgsql::Result>, qint64)>; using on_result_callback = std::function<void(std::shared_ptr<Pgsql::Result>, qint64)>;
using on_state_callback = std::function<void(State)>; using on_state_callback = std::function<void(State)>;
using on_notice_callback = std::function<void(Pgsql::ErrorDetails)>; using on_notice_callback = std::function<void(Pgsql::ErrorDetails)>;
ASyncDBConnection(boost::asio::io_service &ios); explicit ASyncDBConnection(boost::asio::io_service &ios);
~ASyncDBConnection();
State state() const; State state() const;
// void setupConnection(const std::string &connstring); // void setupConnection(const std::string &connstring);
@ -60,10 +61,12 @@ private:
boost::asio::ip::tcp::socket m_asioSock; boost::asio::ip::tcp::socket m_asioSock;
ConnectionConfig m_config; ConnectionConfig m_config;
State m_state = State::NotConnected; State m_state = State::NotConnected;
Pgsql::Canceller m_canceller;
struct { struct {
std::mutex m_mutex; std::mutex m_mutex;
on_state_callback m_func; on_state_callback m_func;
} m_stateCallback; } m_stateCallback;
struct { struct {
std::mutex m_mutex; std::mutex m_mutex;
on_notice_callback m_func; on_notice_callback m_func;