2017-12-16 21:41:46 +01:00
|
|
|
|
#include "ASyncDBConnection.h"
|
2017-08-23 08:10:01 +02:00
|
|
|
|
#include "ScopeGuard.h"
|
2019-09-16 19:24:39 +02:00
|
|
|
|
#include "util.h"
|
2019-11-06 20:03:27 +01:00
|
|
|
|
#include "Pgsql_PgException.h"
|
|
|
|
|
|
#include "Win32Event.h"
|
|
|
|
|
|
#include "WaitHandleList.h"
|
2017-01-06 07:23:40 +01:00
|
|
|
|
#include <chrono>
|
2019-11-06 20:03:27 +01:00
|
|
|
|
#include <queue>
|
|
|
|
|
|
#include <WinSock2.h>
|
2020-04-04 07:34:31 +02:00
|
|
|
|
#include <QDebug>
|
2017-08-24 21:12:32 +02:00
|
|
|
|
|
2018-01-08 20:54:03 +01:00
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
|
|
class registerMetaTypes {
|
|
|
|
|
|
public:
|
|
|
|
|
|
registerMetaTypes()
|
|
|
|
|
|
{
|
|
|
|
|
|
qRegisterMetaType<ASyncDBConnection::State>();
|
|
|
|
|
|
qRegisterMetaType<Pgsql::ErrorDetails>();
|
2018-01-09 20:39:43 +01:00
|
|
|
|
qRegisterMetaType<std::shared_ptr<Pgsql::Result>>();
|
|
|
|
|
|
}
|
2018-01-08 20:54:03 +01:00
|
|
|
|
} registerMetaTypes_instance;
|
2017-09-03 14:37:12 +02:00
|
|
|
|
|
2019-11-06 20:03:27 +01:00
|
|
|
|
class Command {
|
|
|
|
|
|
public:
|
|
|
|
|
|
std::string command;
|
|
|
|
|
|
Pgsql::Params params;
|
|
|
|
|
|
ASyncDBConnection::on_result_callback on_result;
|
|
|
|
|
|
|
|
|
|
|
|
Command() = default;
|
|
|
|
|
|
Command(const std::string &cmd, ASyncDBConnection::on_result_callback cb)
|
|
|
|
|
|
: command(cmd), on_result(cb)
|
|
|
|
|
|
{}
|
|
|
|
|
|
Command(const std::string &cmd, Pgsql::Params &&p, ASyncDBConnection::on_result_callback cb)
|
|
|
|
|
|
: command(cmd), params(p), on_result(cb)
|
|
|
|
|
|
{}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2018-01-08 20:54:03 +01:00
|
|
|
|
}
|
2017-09-03 14:37:12 +02:00
|
|
|
|
|
2019-11-06 20:03:27 +01:00
|
|
|
|
class ASyncDBConnectionThread {
|
|
|
|
|
|
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;
|
|
|
|
|
|
Win32Event m_newEvent;
|
|
|
|
|
|
t_Command()
|
|
|
|
|
|
: m_newEvent(Win32Event::Reset::Auto, Win32Event::Initial::Clear)
|
|
|
|
|
|
{}
|
|
|
|
|
|
} m_commandQueue;
|
|
|
|
|
|
|
|
|
|
|
|
// std::string m_initString;
|
|
|
|
|
|
ConnectionConfig m_config;
|
|
|
|
|
|
ASyncDBConnection::State m_state = ASyncDBConnection::State::NotConnected;
|
|
|
|
|
|
|
|
|
|
|
|
ASyncDBConnectionThread(ASyncDBConnection *asco);
|
|
|
|
|
|
|
|
|
|
|
|
/// Is started as a seperate thread by ASyncDBConnection
|
|
|
|
|
|
void run();
|
|
|
|
|
|
|
|
|
|
|
|
/// Sends a cancel request to the DB server
|
|
|
|
|
|
bool cancel();
|
|
|
|
|
|
|
|
|
|
|
|
void stop();
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
ASyncDBConnection *asyncConnObject = nullptr;
|
|
|
|
|
|
|
|
|
|
|
|
/// \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(ASyncDBConnection::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();
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ASyncDBConnectionThread::ASyncDBConnectionThread(ASyncDBConnection *asco)
|
|
|
|
|
|
: asyncConnObject(asco)
|
|
|
|
|
|
, m_stopEvent(Win32Event::Reset::Manual, Win32Event::Initial::Clear)
|
2017-08-25 08:37:18 +02:00
|
|
|
|
{}
|
2017-01-03 07:22:36 +01:00
|
|
|
|
|
2019-11-06 20:03:27 +01:00
|
|
|
|
void ASyncDBConnectionThread::run()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_terminated = false;
|
|
|
|
|
|
SCOPE_EXIT {
|
|
|
|
|
|
m_state = ASyncDBConnection::State::NotConnected;
|
|
|
|
|
|
m_terminated = true;
|
|
|
|
|
|
};
|
|
|
|
|
|
while (!terminateRequested) {
|
|
|
|
|
|
|
|
|
|
|
|
// make or recover connection
|
|
|
|
|
|
if (makeConnection()) {
|
|
|
|
|
|
m_connection.setNoticeReceiver(
|
|
|
|
|
|
[this](const PGresult *result) { processNotice(result); });
|
|
|
|
|
|
m_canceller = m_connection.getCancel();
|
2017-01-03 07:22:36 +01:00
|
|
|
|
|
2019-11-06 20:03:27 +01:00
|
|
|
|
|
|
|
|
|
|
// send commands and receive results
|
|
|
|
|
|
communicate();
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
// It is not possible to determine the source of the problem.
|
|
|
|
|
|
// Accept for PQconnectionNeedsPassword
|
|
|
|
|
|
|
|
|
|
|
|
// Pass problem to main thread and stop this thread
|
|
|
|
|
|
|
|
|
|
|
|
// Main thread needs to know it has to restart connecting if it want's to.
|
|
|
|
|
|
// TODO: add status functions to help main thread so it doesn't have to remember
|
|
|
|
|
|
// everything reported through callbacks.
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// close connection
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ASyncDBConnectionThread::cancel()
|
2017-01-15 21:38:07 +01:00
|
|
|
|
{
|
2019-11-06 20:03:27 +01:00
|
|
|
|
return m_canceller.cancel(nullptr);
|
2017-01-15 21:38:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-11-06 20:03:27 +01:00
|
|
|
|
bool ASyncDBConnectionThread::makeConnection()
|
2017-01-06 07:23:40 +01:00
|
|
|
|
{
|
2019-11-06 20:03:27 +01:00
|
|
|
|
using namespace std::literals::chrono_literals;
|
|
|
|
|
|
|
|
|
|
|
|
// start connecting
|
2019-09-16 19:24:39 +02:00
|
|
|
|
// auto keywords = m_config.getKeywords();
|
2019-11-06 20:03:27 +01:00
|
|
|
|
// auto values = m_config.getValues();
|
|
|
|
|
|
QString conn_string = m_config.connectionString();
|
|
|
|
|
|
#if false
|
|
|
|
|
|
try {
|
|
|
|
|
|
m_connection.connect(conn_string); //keywords, values, 0);
|
|
|
|
|
|
doStateCallback(ASyncDBConnection::State::Connected);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Pgsql::PgConnectionError &) {
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
#else
|
|
|
|
|
|
while (!terminateRequested) {
|
|
|
|
|
|
|
|
|
|
|
|
bool ok = m_connection.connectStart(conn_string); //keywords, values);
|
|
|
|
|
|
auto start = std::chrono::steady_clock::now();
|
|
|
|
|
|
if (ok && m_connection.status() != CONNECTION_BAD) {
|
|
|
|
|
|
Win32Event socket_event(Win32Event::Reset::Auto, Win32Event::Initial::Clear);
|
|
|
|
|
|
|
|
|
|
|
|
long fd = FD_WRITE;
|
|
|
|
|
|
while (true) {
|
|
|
|
|
|
// poll till complete or failed (we can get an abort command)
|
2019-11-06 20:16:15 +01:00
|
|
|
|
int sock = m_connection.socket();
|
2019-11-06 20:03:27 +01:00
|
|
|
|
WSAEventSelect(sock, socket_event.handle(), fd);
|
|
|
|
|
|
WaitHandleList whl;
|
|
|
|
|
|
auto wait_result_socket_event = whl.add(socket_event);
|
|
|
|
|
|
auto wait_result_stop = whl.add(m_stopEvent);
|
|
|
|
|
|
|
|
|
|
|
|
auto nu = std::chrono::steady_clock::now();
|
|
|
|
|
|
std::chrono::duration<float, std::milli> diff = -(nu-start);
|
|
|
|
|
|
diff += 30s;
|
|
|
|
|
|
DWORD timeout = diff.count();
|
|
|
|
|
|
|
|
|
|
|
|
DWORD res = MsgWaitForMultipleObjectsEx(
|
|
|
|
|
|
whl.count(), // _In_ DWORD nCount,
|
|
|
|
|
|
whl, // _In_ const HANDLE *pHandles,
|
|
|
|
|
|
timeout, // _In_ DWORD dwMilliseconds,
|
|
|
|
|
|
0, // _In_ DWORD dwWakeMask,
|
|
|
|
|
|
0 // _In_ DWORD dwFlags
|
|
|
|
|
|
);
|
|
|
|
|
|
if (res == wait_result_socket_event) {
|
|
|
|
|
|
auto poll_state = m_connection.connectPoll();
|
|
|
|
|
|
if (poll_state == PGRES_POLLING_OK) {
|
|
|
|
|
|
// if connected return true
|
|
|
|
|
|
doStateCallback(ASyncDBConnection::State::Connected);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (poll_state == PGRES_POLLING_FAILED) {
|
|
|
|
|
|
doStateCallback(ASyncDBConnection::State::NotConnected);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (poll_state == PGRES_POLLING_READING) {
|
|
|
|
|
|
doStateCallback(ASyncDBConnection::State::Connecting);
|
|
|
|
|
|
fd = FD_READ;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (poll_state == PGRES_POLLING_WRITING) {
|
|
|
|
|
|
doStateCallback(ASyncDBConnection::State::Connecting);
|
|
|
|
|
|
fd = FD_WRITE;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (res == wait_result_stop) {
|
2019-11-09 12:56:02 +01:00
|
|
|
|
m_connection.close();
|
|
|
|
|
|
return false;
|
2019-11-06 20:03:27 +01:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
} // end while (true)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ASyncDBConnectionThread::communicate()
|
|
|
|
|
|
{
|
|
|
|
|
|
while (!terminateRequested) {
|
|
|
|
|
|
// wait for something to do:
|
|
|
|
|
|
// - command to send to server
|
|
|
|
|
|
// - wait for results and (notifies can also come in)
|
|
|
|
|
|
// - pass each result on to the completion routine
|
|
|
|
|
|
// - notify comming in from the server
|
|
|
|
|
|
// - pass to notify callback
|
|
|
|
|
|
// - connection raises an error
|
|
|
|
|
|
// - return
|
|
|
|
|
|
// - stop signal
|
|
|
|
|
|
// - return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (m_state == ASyncDBConnection::State::Connected) {
|
|
|
|
|
|
waitForAndSendCommand();
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (m_state == ASyncDBConnection::State::QuerySend || m_state == ASyncDBConnection::State::CancelSend) {
|
|
|
|
|
|
// Wait for result, even after a cancel we should wait, for all results
|
|
|
|
|
|
// New command's are not excepted when one has been send
|
|
|
|
|
|
waitForResult();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ASyncDBConnectionThread::stop()
|
|
|
|
|
|
{
|
|
|
|
|
|
terminateRequested = true;
|
|
|
|
|
|
m_stopEvent.set();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ASyncDBConnectionThread::doStateCallback(ASyncDBConnection::State state)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_state = state;
|
|
|
|
|
|
// std::lock_guard<std::mutex> lg(m_stateCallback.m_mutex);
|
|
|
|
|
|
// if (m_stateCallback.m_func) {
|
|
|
|
|
|
// m_stateCallback.m_func(state);
|
|
|
|
|
|
// }
|
2022-05-24 18:56:39 +02:00
|
|
|
|
Q_EMIT asyncConnObject->onStateChanged(state);
|
2019-11-06 20:03:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ASyncDBConnectionThread::waitForAndSendCommand()
|
|
|
|
|
|
{
|
|
|
|
|
|
#if false
|
|
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
|
// lock the data
|
|
|
|
|
|
std::unique_lock<std::mutex> lk(m_commandQueue.m_mutex);
|
|
|
|
|
|
if (m_commandQueue.m_queue.empty()) {
|
|
|
|
|
|
// no data wait till there is data
|
|
|
|
|
|
m_commandQueue.m_newEvent.wait_for(lk, 1000ms);
|
|
|
|
|
|
// can we use the predicate to reimplement the stop function???, []{return ready;});
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
doNewCommand();
|
|
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
WaitHandleList whl;
|
|
|
|
|
|
auto wait_result_new_command = whl.add(m_commandQueue.m_newEvent);
|
|
|
|
|
|
auto wait_result_stop = whl.add(m_stopEvent);
|
|
|
|
|
|
|
|
|
|
|
|
DWORD res = MsgWaitForMultipleObjectsEx(
|
|
|
|
|
|
whl.count(), // _In_ DWORD nCount,
|
|
|
|
|
|
whl, // _In_ const HANDLE *pHandles,
|
|
|
|
|
|
INFINITE, // _In_ DWORD dwMilliseconds,
|
|
|
|
|
|
0, // _In_ DWORD dwWakeMask,
|
|
|
|
|
|
0 // _In_ DWORD dwFlags
|
2019-09-16 19:24:39 +02:00
|
|
|
|
);
|
2019-11-06 20:03:27 +01:00
|
|
|
|
if (res == wait_result_new_command) {
|
|
|
|
|
|
doNewCommand();
|
2019-09-16 19:24:39 +02:00
|
|
|
|
}
|
2019-11-06 20:03:27 +01:00
|
|
|
|
if (res == wait_result_stop)
|
|
|
|
|
|
return;
|
|
|
|
|
|
#endif
|
2017-01-06 07:23:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-11-06 20:03:27 +01:00
|
|
|
|
void ASyncDBConnectionThread::doNewCommand()
|
|
|
|
|
|
{
|
|
|
|
|
|
// get command from top of queue (but leave it in the queue, we need the callback)
|
|
|
|
|
|
if (! m_commandQueue.m_queue.empty()) {
|
|
|
|
|
|
const Command &command = m_commandQueue.m_queue.front();
|
2019-12-18 19:35:17 +01:00
|
|
|
|
bool query_send = false;
|
|
|
|
|
|
if (command.params.empty())
|
|
|
|
|
|
query_send = m_connection.sendQuery(command.command.c_str());
|
|
|
|
|
|
else
|
|
|
|
|
|
query_send = m_connection.sendQueryParams(command.command.c_str(), command.params);
|
|
|
|
|
|
|
|
|
|
|
|
if (query_send) {
|
|
|
|
|
|
m_timer.start();
|
|
|
|
|
|
doStateCallback(ASyncDBConnection::State::QuerySend);
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
std::string error = m_connection.getErrorMessage();
|
2020-04-04 07:34:31 +02:00
|
|
|
|
qDebug() << "Error sending query " << QString::fromStdString(error);
|
2019-12-18 19:35:17 +01:00
|
|
|
|
// todo: need to report the error
|
2019-11-06 20:03:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ASyncDBConnectionThread::consumeResultInput()
|
|
|
|
|
|
{
|
|
|
|
|
|
bool finished = false;
|
|
|
|
|
|
if (m_connection.consumeInput()) {
|
|
|
|
|
|
while ( ! finished && ! m_connection.isBusy()) {
|
2019-11-20 19:18:51 +01:00
|
|
|
|
auto res(m_connection.getResultNoThrow());
|
2019-11-06 20:03:27 +01:00
|
|
|
|
{
|
|
|
|
|
|
qint64 ms = m_timer.restart();
|
|
|
|
|
|
std::lock_guard<std::mutex> lg(m_commandQueue.m_mutex);
|
|
|
|
|
|
m_commandQueue.m_queue.front().on_result(res, ms);
|
|
|
|
|
|
if (res == nullptr) {
|
|
|
|
|
|
m_timer.invalidate();
|
|
|
|
|
|
m_commandQueue.m_queue.pop();
|
|
|
|
|
|
doStateCallback(ASyncDBConnection::State::Connected);
|
|
|
|
|
|
finished = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
// else is still waiting for more data
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
// error during consume
|
2019-11-20 19:18:51 +01:00
|
|
|
|
auto error_msg = m_connection.getErrorMessage();
|
2020-04-04 07:34:31 +02:00
|
|
|
|
qDebug() << "error while communicating with server " << QString::fromStdString(error_msg);
|
|
|
|
|
|
doStateCallback(ASyncDBConnection::State::NotConnected);
|
|
|
|
|
|
finished = true;
|
|
|
|
|
|
stop();
|
2019-11-06 20:03:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
return finished;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ASyncDBConnectionThread::waitForResult()
|
|
|
|
|
|
{
|
2020-04-04 07:34:31 +02:00
|
|
|
|
#if false
|
2019-11-06 20:03:27 +01:00
|
|
|
|
int sock = m_connection.socket();
|
|
|
|
|
|
|
|
|
|
|
|
fd_set readfds;
|
|
|
|
|
|
timeval timeout;
|
|
|
|
|
|
bool finished = false;
|
|
|
|
|
|
while (!finished && !terminateRequested) {
|
|
|
|
|
|
FD_ZERO(&readfds);
|
|
|
|
|
|
FD_SET(sock, &readfds);
|
|
|
|
|
|
|
|
|
|
|
|
timeout.tv_sec = 5;
|
|
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
|
|
|
|
|
|
|
int select_result = select(sock + 1, &readfds, nullptr, nullptr, &timeout);
|
|
|
|
|
|
if (select_result > 0) {
|
|
|
|
|
|
if (FD_ISSET(sock, &readfds)) {
|
|
|
|
|
|
if (consumeResultInput()) {
|
|
|
|
|
|
finished = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2020-04-04 07:34:31 +02:00
|
|
|
|
#else
|
|
|
|
|
|
SOCKET sock = static_cast<SOCKET>(m_connection.socket());
|
2019-11-06 20:03:27 +01:00
|
|
|
|
Win32Event socket_event(Win32Event::Reset::Manual, Win32Event::Initial::Clear);
|
|
|
|
|
|
|
|
|
|
|
|
long fd = FD_READ | FD_CLOSE;
|
|
|
|
|
|
|
|
|
|
|
|
bool finished = false;
|
|
|
|
|
|
while ( ! finished) {
|
|
|
|
|
|
WSAEventSelect(sock, socket_event.handle(), fd);
|
|
|
|
|
|
|
|
|
|
|
|
WaitHandleList whl;
|
|
|
|
|
|
auto wait_result_socket = whl.add(socket_event);
|
2020-04-04 07:34:31 +02:00
|
|
|
|
auto wait_result_stop = whl.add(m_stopEvent);
|
2019-11-06 20:03:27 +01:00
|
|
|
|
|
|
|
|
|
|
DWORD res = MsgWaitForMultipleObjectsEx(
|
|
|
|
|
|
whl.count(), // _In_ DWORD nCount,
|
|
|
|
|
|
whl, // _In_ const HANDLE *pHandles,
|
|
|
|
|
|
INFINITE, // _In_ DWORD dwMilliseconds,
|
|
|
|
|
|
0, // _In_ DWORD dwWakeMask,
|
|
|
|
|
|
0 // _In_ DWORD dwFlags
|
|
|
|
|
|
);
|
|
|
|
|
|
if (res == wait_result_socket) {
|
|
|
|
|
|
WSANETWORKEVENTS net_events;
|
|
|
|
|
|
WSAEnumNetworkEvents(sock, socket_event.handle(), &net_events);
|
|
|
|
|
|
|
|
|
|
|
|
if (net_events.lNetworkEvents & FD_READ) {
|
|
|
|
|
|
if (consumeResultInput()) {
|
|
|
|
|
|
finished = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (res == wait_result_stop) {
|
|
|
|
|
|
// Send cancel, close connection and terminate thread
|
|
|
|
|
|
cancel();
|
2020-04-04 07:34:31 +02:00
|
|
|
|
doStateCallback(ASyncDBConnection::State::Terminating);
|
2019-11-06 20:03:27 +01:00
|
|
|
|
finished = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
} // end while
|
|
|
|
|
|
// When last result received, remove command from queue
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ASyncDBConnectionThread::processNotice(const PGresult *result)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Pgsql::Result res(result);
|
|
|
|
|
|
// std::lock_guard<std::mutex> lg(m_noticeCallback.m_mutex);
|
|
|
|
|
|
// if (m_noticeCallback.m_func) {
|
|
|
|
|
|
// Pgsql::ErrorDetails details = Pgsql::ErrorDetails::createErrorDetailsFromPGresult(result);
|
|
|
|
|
|
// m_noticeCallback.m_func(details);
|
|
|
|
|
|
// }
|
|
|
|
|
|
Pgsql::ErrorDetails details = Pgsql::ErrorDetails::createErrorDetailsFromPGresult(result);
|
2022-05-24 18:56:39 +02:00
|
|
|
|
Q_EMIT asyncConnObject->onNotice(details);
|
2017-01-06 07:23:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-05 16:02:06 +02:00
|
|
|
|
|
2019-11-06 20:03:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ASyncDBConnection::ASyncDBConnection()
|
|
|
|
|
|
: m_threadData(std::make_unique<ASyncDBConnectionThread>(this))
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
ASyncDBConnection::~ASyncDBConnection()
|
|
|
|
|
|
{
|
|
|
|
|
|
closeConnection();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ASyncDBConnection::State ASyncDBConnection::state() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_threadData->m_state;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ASyncDBConnection::setupConnection(const ConnectionConfig &config)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_thread.joinable()) {
|
|
|
|
|
|
m_threadData->stop();
|
|
|
|
|
|
m_thread.join();
|
|
|
|
|
|
}
|
2020-04-04 07:49:44 +02:00
|
|
|
|
|
|
|
|
|
|
m_threadData.reset(new ASyncDBConnectionThread(this));
|
2019-11-06 20:03:27 +01:00
|
|
|
|
m_threadData->m_config = config;
|
|
|
|
|
|
m_thread = std::thread([this] () { m_threadData->run(); });
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-24 21:12:32 +02:00
|
|
|
|
void ASyncDBConnection::doStateCallback(State state)
|
2017-01-06 07:23:40 +01:00
|
|
|
|
{
|
2017-09-02 11:55:47 +02:00
|
|
|
|
m_state = state;
|
2017-08-24 21:12:32 +02:00
|
|
|
|
if (state == State::Connected) {
|
2017-08-25 08:37:18 +02:00
|
|
|
|
m_canceller = m_connection.getCancel();
|
2017-08-24 21:12:32 +02:00
|
|
|
|
m_connection.setNoticeReceiver(
|
|
|
|
|
|
[this](const PGresult *result) { processNotice(result); });
|
|
|
|
|
|
}
|
2022-05-24 18:56:39 +02:00
|
|
|
|
Q_EMIT onStateChanged(state);
|
2017-10-05 16:02:06 +02:00
|
|
|
|
}
|
2017-01-08 09:58:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-08-24 21:12:32 +02:00
|
|
|
|
void ASyncDBConnection::closeConnection()
|
|
|
|
|
|
{
|
2019-11-06 20:03:27 +01:00
|
|
|
|
// doStateCallback(State::NotConnected);
|
|
|
|
|
|
// TODO also send cancel???
|
|
|
|
|
|
m_threadData->stop();
|
|
|
|
|
|
if (m_thread.joinable()) {
|
|
|
|
|
|
m_thread.join();
|
2017-09-02 11:55:47 +02:00
|
|
|
|
}
|
2019-11-06 20:03:27 +01:00
|
|
|
|
|
2017-01-08 09:58:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-24 21:12:32 +02:00
|
|
|
|
bool ASyncDBConnection::send(const std::string &command, on_result_callback on_result)
|
2017-01-08 09:58:34 +01:00
|
|
|
|
{
|
2019-12-18 19:35:17 +01:00
|
|
|
|
if (command.empty())
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
2019-11-06 20:03:27 +01:00
|
|
|
|
{
|
|
|
|
|
|
std::lock_guard<std::mutex> lg(m_threadData->m_commandQueue.m_mutex);
|
|
|
|
|
|
m_threadData->m_commandQueue.m_queue.emplace(command, on_result);
|
|
|
|
|
|
m_threadData->m_commandQueue.m_newEvent.set();
|
|
|
|
|
|
}
|
2017-08-24 21:12:32 +02:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2017-02-19 11:12:43 +01:00
|
|
|
|
|
2017-08-24 21:12:32 +02:00
|
|
|
|
bool ASyncDBConnection::send(const std::string &command, Pgsql::Params params, on_result_callback on_result)
|
|
|
|
|
|
{
|
2019-11-06 20:03:27 +01:00
|
|
|
|
{
|
|
|
|
|
|
std::lock_guard<std::mutex> lg(m_threadData->m_commandQueue.m_mutex);
|
|
|
|
|
|
m_threadData->m_commandQueue.m_queue.emplace(command, std::move(params), on_result);
|
|
|
|
|
|
m_threadData->m_commandQueue.m_newEvent.set();
|
|
|
|
|
|
}
|
2017-08-24 21:12:32 +02:00
|
|
|
|
return true;
|
2017-01-08 09:58:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-24 21:12:32 +02:00
|
|
|
|
bool ASyncDBConnection::cancel()
|
2017-08-23 13:27:23 +02:00
|
|
|
|
{
|
2019-11-06 20:03:27 +01:00
|
|
|
|
return m_threadData->cancel();
|
2017-08-24 21:12:32 +02:00
|
|
|
|
}
|
2017-01-08 09:58:34 +01:00
|
|
|
|
|
2017-08-24 21:12:32 +02:00
|
|
|
|
void ASyncDBConnection::processNotice(const PGresult *result)
|
2017-01-08 10:29:21 +01:00
|
|
|
|
{
|
2017-09-03 10:06:32 +02:00
|
|
|
|
Pgsql::ErrorDetails details = Pgsql::ErrorDetails::createErrorDetailsFromPGresult(result);
|
2022-05-24 18:56:39 +02:00
|
|
|
|
Q_EMIT onNotice(details);
|
2017-01-08 10:29:21 +01:00
|
|
|
|
}
|