Query, Explain and Cancel are going throught the asyncdbconnection now.

Todo: Notice processing and error reporting.
This commit is contained in:
Eelke Klein 2017-01-08 09:58:34 +01:00
parent fce51a7b7e
commit a36bf5f7f4
11 changed files with 335 additions and 217 deletions

View file

@ -5,6 +5,7 @@
#include "win32event.h"
#include <functional>
#include <mutex>
#include <queue>
#include <vector>
#include <thread>
@ -15,10 +16,11 @@ public:
Connecting,
Connected,
QuerySend,
CancelSend
CancelSend,
Terminating
};
using on_result_callback = std::function<void(Pgsql::Result)>;
using on_result_callback = std::function<void(std::shared_ptr<Pgsql::Result>)>;
using on_state_callback = std::function<void(State)>;
ASyncDBConnection();
@ -30,26 +32,44 @@ public:
/** Sends command to the server.
When the result is in and no result_task_queue was passed then on_result will be
called directly within the thread.
When the result is in on_result will be called directly within the thread.
If the command gives multiple results on_result will be called for each result.
*/
bool send(const std::string &command, on_result_callback on_result);
bool cancel();
private:
class Command {
public:
std::string command;
on_result_callback on_result;
Command() = default;
Command(const std::string &cmd, on_result_callback cb)
: command(cmd), on_result(cb)
{}
};
/// Contains all the members accessed by the thread
class Thread {
public:
on_state_callback m_stateCallback;
std::mutex m_stateCallbackMutex;
using t_CommandQueue = std::queue<Command>;
struct {
std::mutex m_mutex;
on_state_callback m_func;
} m_stateCallback;
struct t_Command {
std::mutex m_mutex;
t_CommandQueue m_queue;
Win32Event m_newEvent;
t_Command()
: m_newEvent(Win32Event::Reset::Auto, Win32Event::Initial::Clear)
{}
} m_commandQueue;
std::string m_initString;
@ -59,21 +79,29 @@ private:
void run();
/// Sends a cancel request to the DB server
void cancel();
bool cancel();
void stop();
private:
State m_state = State::NotConnected;
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;
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();
};
Thread m_threadData;