ASyncDBConnection is now based of boost::asio instead of using it's own thread.
connection: tested OK querying: tested OK notices: should be working cancel: todo
This commit is contained in:
parent
4beea05ba6
commit
f11f9545ac
4 changed files with 519 additions and 432 deletions
|
|
@ -5,12 +5,14 @@
|
|||
#include "Pgsql_Params.h"
|
||||
#include "ConnectionConfig.h"
|
||||
#include <QElapsedTimer>
|
||||
#include <functional>
|
||||
#include <condition_variable>
|
||||
// #include <functional>
|
||||
// #include <condition_variable>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
// #include <queue>
|
||||
// #include <vector>
|
||||
// #include <thread>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/io_service.hpp>
|
||||
|
||||
/** \brief Class that handles asynchronous execution of queries.
|
||||
*
|
||||
|
|
@ -32,7 +34,7 @@ public:
|
|||
using on_state_callback = std::function<void(State)>;
|
||||
using on_notice_callback = std::function<void(Pgsql::ErrorDetails)>;
|
||||
|
||||
ASyncDBConnection();
|
||||
ASyncDBConnection(boost::asio::io_service &ios);
|
||||
|
||||
State state() const;
|
||||
// void setupConnection(const std::string &connstring);
|
||||
|
|
@ -54,88 +56,106 @@ public:
|
|||
bool cancel();
|
||||
|
||||
private:
|
||||
Pgsql::Connection m_connection;
|
||||
boost::asio::ip::tcp::socket m_asioSock;
|
||||
ConnectionConfig m_config;
|
||||
State m_state = State::NotConnected;
|
||||
struct {
|
||||
std::mutex m_mutex;
|
||||
on_state_callback m_func;
|
||||
} m_stateCallback;
|
||||
struct {
|
||||
std::mutex m_mutex;
|
||||
on_notice_callback m_func;
|
||||
} m_noticeCallback;
|
||||
QElapsedTimer m_timer;
|
||||
|
||||
void async_connect_handler(boost::system::error_code ec, std::size_t s);
|
||||
void async_query_handler(boost::system::error_code ec, std::size_t s, on_result_callback on_result);
|
||||
void doStateCallback(State state);
|
||||
void processNotice(const PGresult *result);
|
||||
|
||||
class Command {
|
||||
public:
|
||||
std::string command;
|
||||
Pgsql::Params params;
|
||||
on_result_callback on_result;
|
||||
|
||||
Command() = default;
|
||||
Command(const std::string &cmd, on_result_callback cb)
|
||||
: command(cmd), on_result(cb)
|
||||
{}
|
||||
Command(const std::string &cmd, Pgsql::Params &&p, on_result_callback cb)
|
||||
: command(cmd), params(p), on_result(cb)
|
||||
{}
|
||||
};
|
||||
|
||||
/// Contains all the members accessed by the thread
|
||||
class Thread {
|
||||
public:
|
||||
using t_CommandQueue = std::queue<Command>;
|
||||
struct {
|
||||
std::mutex m_mutex;
|
||||
on_state_callback m_func;
|
||||
} m_stateCallback;
|
||||
struct {
|
||||
std::mutex m_mutex;
|
||||
on_notice_callback m_func;
|
||||
} m_noticeCallback;
|
||||
|
||||
struct t_Command {
|
||||
std::mutex m_mutex;
|
||||
t_CommandQueue m_queue;
|
||||
std::condition_variable m_newEvent;
|
||||
t_Command()
|
||||
{}
|
||||
} m_commandQueue;
|
||||
|
||||
// std::string m_initString;
|
||||
ConnectionConfig m_config;
|
||||
State m_state = State::NotConnected;
|
||||
|
||||
Thread();
|
||||
|
||||
/// Is started as a seperate thread by ASyncDBConnection
|
||||
void run();
|
||||
|
||||
/// Sends a cancel request to the DB server
|
||||
bool cancel();
|
||||
|
||||
void stop();
|
||||
|
||||
private:
|
||||
|
||||
/// \todo Implement new method to stop the thread
|
||||
//Win32Event m_stopEvent;
|
||||
Pgsql::Connection m_connection;
|
||||
bool terminateRequested = false; ///< is set when the thread should stop
|
||||
bool m_terminated = true;
|
||||
Pgsql::Canceller m_canceller;
|
||||
QElapsedTimer m_timer;
|
||||
|
||||
|
||||
bool makeConnection();
|
||||
void communicate();
|
||||
|
||||
void doStateCallback(State state);
|
||||
/// Wait's for a command to come in and send's it to the server
|
||||
void waitForAndSendCommand();
|
||||
void doNewCommand();
|
||||
void waitForResult();
|
||||
|
||||
|
||||
void processNotice(const PGresult *result);
|
||||
/** Function to call when after sending a command the socket is ready for reading.
|
||||
*
|
||||
* It might take several consumes before all data is read.
|
||||
*/
|
||||
bool consumeResultInput();
|
||||
};
|
||||
|
||||
Thread m_threadData;
|
||||
std::thread m_thread;
|
||||
// class Command {
|
||||
// public:
|
||||
// std::string command;
|
||||
// Pgsql::Params params;
|
||||
// on_result_callback on_result;
|
||||
//
|
||||
// Command() = default;
|
||||
// Command(const std::string &cmd, on_result_callback cb)
|
||||
// : command(cmd), on_result(cb)
|
||||
// {}
|
||||
// Command(const std::string &cmd, Pgsql::Params &&p, on_result_callback cb)
|
||||
// : command(cmd), params(p), on_result(cb)
|
||||
// {}
|
||||
// };
|
||||
//
|
||||
// /// Contains all the members accessed by the thread
|
||||
// class Thread {
|
||||
// public:
|
||||
// using t_CommandQueue = std::queue<Command>;
|
||||
// struct {
|
||||
// std::mutex m_mutex;
|
||||
// on_state_callback m_func;
|
||||
// } m_stateCallback;
|
||||
// struct {
|
||||
// std::mutex m_mutex;
|
||||
// on_notice_callback m_func;
|
||||
// } m_noticeCallback;
|
||||
//
|
||||
// struct t_Command {
|
||||
// std::mutex m_mutex;
|
||||
// t_CommandQueue m_queue;
|
||||
// std::condition_variable m_newEvent;
|
||||
// t_Command()
|
||||
// {}
|
||||
// } m_commandQueue;
|
||||
//
|
||||
// // std::string m_initString;
|
||||
// ConnectionConfig m_config;
|
||||
// State m_state = State::NotConnected;
|
||||
//
|
||||
// Thread();
|
||||
//
|
||||
// /// Is started as a seperate thread by ASyncDBConnection
|
||||
// void run();
|
||||
//
|
||||
// /// Sends a cancel request to the DB server
|
||||
// bool cancel();
|
||||
//
|
||||
// void stop();
|
||||
//
|
||||
// private:
|
||||
//
|
||||
// /// \todo Implement new method to stop the thread
|
||||
// //Win32Event m_stopEvent;
|
||||
// Pgsql::Connection m_connection;
|
||||
// bool terminateRequested = false; ///< is set when the thread should stop
|
||||
// bool m_terminated = true;
|
||||
// Pgsql::Canceller m_canceller;
|
||||
// QElapsedTimer m_timer;
|
||||
//
|
||||
//
|
||||
// bool makeConnection();
|
||||
// void communicate();
|
||||
//
|
||||
// void doStateCallback(State state);
|
||||
// /// Wait's for a command to come in and send's it to the server
|
||||
// void waitForAndSendCommand();
|
||||
// void doNewCommand();
|
||||
// void waitForResult();
|
||||
//
|
||||
//
|
||||
// void processNotice(const PGresult *result);
|
||||
// /** Function to call when after sending a command the socket is ready for reading.
|
||||
// *
|
||||
// * It might take several consumes before all data is read.
|
||||
// */
|
||||
// bool consumeResultInput();
|
||||
// };
|
||||
//
|
||||
// Thread m_threadData;
|
||||
// std::thread m_thread;
|
||||
};
|
||||
|
||||
#endif // ASYNCDBCONNECTION_H
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue