Adding in boost::asio support

main starts a thread that keep a global io_service object
running and makes sure it is stopped when everything else is
stopped.
This commit is contained in:
Eelke Klein 2017-08-24 19:45:00 +02:00
parent a6755c20f2
commit 4beea05ba6
7 changed files with 50 additions and 9 deletions

View file

@ -0,0 +1,7 @@
#include "GlobalIoService.h"
std::shared_ptr<boost::asio::io_service> getGlobalAsioIoService()
{
static auto ios = std::make_shared<boost::asio::io_service>();
return ios;
}

6
pglab/GlobalIoService.h Normal file
View file

@ -0,0 +1,6 @@
#pragma once
#include <memory>
#include <boost/asio.hpp>
std::shared_ptr<boost::asio::io_service> getGlobalAsioIoService();

View file

@ -2,8 +2,10 @@
#define MASTERCONTROLLER_H
#include <QObject>
#include <future>
#include <map>
class ConnectionConfig;
class ConnectionList;
class ConnectionListModel;
@ -30,7 +32,7 @@ public:
void openSqlWindowForConnection(int connection_index);
void openServerWindowForConnection(int connection_index);
void openBackupDlgForConnection(int connection_index);
signals:
public slots:
@ -39,6 +41,7 @@ private:
ConnectionList *m_connectionList = nullptr;
ConnectionListModel *m_connectionListModel = nullptr;
ConnectionManagerWindow *m_connectionManagerWindow = nullptr;
};
#endif // MASTERCONTROLLER_H

View file

@ -20,7 +20,6 @@
#include "PgsqlDatabaseCatalogue.h"
#include "util.h"
QueryParamListController::QueryParamListController(QTableView *tv,
OpenDatabase *opendb, QWidget *parent)
: QObject(parent)

View file

@ -4,12 +4,13 @@
# include <winsock2.h>
#endif
#include <memory>
#include "GlobalIoService.h"
int main(int argc, char *argv[])
{
/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
#ifdef _WIN32
#ifdef WIN32
WORD wVersionRequested = MAKEWORD(2, 2);
WSADATA wsaData;
int err = WSAStartup(wVersionRequested, &wsaData);
@ -20,16 +21,27 @@ int main(int argc, char *argv[])
return 1;
}
#endif
QApplication a(argc, argv);
QCoreApplication::setOrganizationName("pglab");
QCoreApplication::setOrganizationDomain("eelkeklein.nl");
QCoreApplication::setApplicationName("pglab");
auto master_controller = std::make_unique<MasterController>();
master_controller->init();
int result = a.exec();
#ifdef _WIN32
std::thread asio_service_thread;
int result = -1;
{
auto ios = getGlobalAsioIoService();
boost::asio::io_service::work work(*ios); // Prevent service from running out of work so run doesn't return
asio_service_thread = std::thread([ios](){ ios->run(); });
// make sure the io_service is stopped before we wait on the future
auto master_controller = std::make_unique<MasterController>();
master_controller->init();
result = a.exec();
}
asio_service_thread.join();
#ifdef WIN32
WSACleanup();
#endif
return result;