made switching between desktop and service mode more robust.

This commit is contained in:
Nick Bolton 2012-07-13 14:38:58 +00:00
parent aec76ce083
commit db3ea4ab9c
6 changed files with 66 additions and 22 deletions

View File

@ -24,7 +24,8 @@
#include "Ipc.h"
IpcClient::IpcClient() :
m_ReaderStarted(false)
m_ReaderStarted(false),
m_Enabled(false)
{
m_Socket = new QTcpSocket(this);
connect(m_Socket, SIGNAL(connected()), this, SLOT(connected()));
@ -49,6 +50,8 @@ void IpcClient::connected()
void IpcClient::connectToHost()
{
m_Enabled = true;
infoMessage("connecting to service...");
m_Socket->connectToHost(QHostAddress(QHostAddress::LocalHost), IPC_PORT);
@ -58,6 +61,13 @@ void IpcClient::connectToHost()
}
}
void IpcClient::disconnectFromHost()
{
infoMessage("service disconnect");
m_Reader->stop();
m_Socket->close();
}
void IpcClient::error(QAbstractSocket::SocketError error)
{
QString text;
@ -69,7 +79,14 @@ void IpcClient::error(QAbstractSocket::SocketError error)
errorMessage(QString("ipc connection error, %1").arg(text));
QTimer::singleShot(1000, this, SLOT(connectToHost()));
QTimer::singleShot(1000, this, SLOT(retryConnect()));
}
void IpcClient::retryConnect()
{
if (m_Enabled) {
connectToHost();
}
}
void IpcClient::write(int code, int length, const char* data)

View File

@ -32,9 +32,11 @@ public:
virtual ~IpcClient();
void write(int code, int length, const char* data);
void connectToHost();
void disconnectFromHost();
public slots:
void connectToHost();
void retryConnect();
private:
void intToBytes(int value, char* buffer, int size);
@ -53,4 +55,5 @@ private:
QTcpSocket* m_Socket;
IpcReader* m_Reader;
bool m_ReaderStarted;
bool m_Enabled;
};

View File

@ -36,6 +36,11 @@ void IpcReader::start()
connect(m_Socket, SIGNAL(readyRead()), this, SLOT(read()));
}
void IpcReader::stop()
{
disconnect(m_Socket, SIGNAL(readyRead()), this, SLOT(read()));
}
void IpcReader::read()
{
QMutexLocker locker(&m_Mutex);

View File

@ -30,6 +30,7 @@ public:
IpcReader(QTcpSocket* socket);
virtual ~IpcReader();
void start();
void stop();
signals:
void readLogLine(const QString& text);

View File

@ -77,7 +77,13 @@ MainWindow::MainWindow(QSettings& settings, AppConfig& appConfig) :
m_versionChecker.setApp(appPath(appConfig.synergycName()));
m_SetupWizard = new SetupWizard(*this, false);
connect(m_SetupWizard, SIGNAL(finished(int)), this, SLOT(refreshStartButton()));
connect(m_SetupWizard, SIGNAL(finished(int)), this, SLOT(onModeChanged()));
// ipc must always be enabled, so that we can disable command when switching to desktop mode.
connect(&m_IpcClient, SIGNAL(readLogLine(const QString&)), this, SLOT(appendLogRaw(const QString&)));
connect(&m_IpcClient, SIGNAL(errorMessage(const QString&)), this, SLOT(appendLogError(const QString&)));
connect(&m_IpcClient, SIGNAL(infoMessage(const QString&)), this, SLOT(appendLogInfo(const QString&)));
m_IpcClient.connectToHost();
}
MainWindow::~MainWindow()
@ -89,21 +95,7 @@ MainWindow::~MainWindow()
void MainWindow::start(bool firstRun)
{
refreshStartButton();
if (appConfig().processMode() == Service)
{
connect(&m_IpcClient, SIGNAL(readLogLine(const QString&)), this, SLOT(appendLogRaw(const QString&)));
connect(&m_IpcClient, SIGNAL(errorMessage(const QString&)), this, SLOT(appendLogError(const QString&)));
connect(&m_IpcClient, SIGNAL(infoMessage(const QString&)), this, SLOT(appendLogInfo(const QString&)));
m_IpcClient.connectToHost();
startSynergy();
}
if (appConfig().processMode() == Desktop && !firstRun && appConfig().autoConnect())
{
startSynergy();
}
onModeChanged(firstRun);
createTrayIcon();
@ -113,6 +105,33 @@ void MainWindow::start(bool firstRun)
m_versionChecker.checkLatest();
}
void MainWindow::onModeChanged()
{
onModeChanged(false);
}
void MainWindow::onModeChanged(bool firstRun)
{
refreshStartButton();
stopSynergy();
if (appConfig().processMode() == Service)
{
startSynergy();
}
else
{
// cause the service to stop creating processes.
sendDaemonCommand("", false);
}
if (appConfig().processMode() == Desktop && !firstRun && appConfig().autoConnect())
{
startSynergy();
}
}
void MainWindow::refreshStartButton()
{
if (appConfig().processMode() == Service)
@ -322,9 +341,6 @@ void MainWindow::startSynergy()
if (desktopMode)
{
// cause the service to stop creating processes.
sendDaemonCommand("", false);
stopSynergy();
setSynergyState(synergyConnecting);
}

View File

@ -112,6 +112,8 @@ class MainWindow : public QMainWindow, public Ui::MainWindowBase
void logError();
void updateFound(const QString& version);
void refreshStartButton();
void onModeChanged();
void onModeChanged(bool firstRun);
protected:
QSettings& settings() { return m_Settings; }