mirror of
https://github.com/debauchee/barrier.git
synced 2026-05-09 07:22:21 +08:00
fully replaced gui/daemon named pipes ipc with tcp ipc.
This commit is contained in:
@@ -36,7 +36,8 @@ SOURCES += src/main.cpp \
|
||||
src/QSynergyApplication.cpp \
|
||||
src/VersionChecker.cpp \
|
||||
src/SetupWizard.cpp \
|
||||
src/IpcLogReader.cpp
|
||||
src/IpcLogReader.cpp \
|
||||
src/IpcClient.cpp
|
||||
HEADERS += src/MainWindow.h \
|
||||
src/AboutDialog.h \
|
||||
src/ServerConfig.h \
|
||||
@@ -59,7 +60,8 @@ HEADERS += src/MainWindow.h \
|
||||
src/QSynergyApplication.h \
|
||||
src/VersionChecker.h \
|
||||
src/SetupWizard.h \
|
||||
src/IpcLogReader.h
|
||||
src/IpcLogReader.h \
|
||||
src/IpcClient.h
|
||||
RESOURCES += res/Synergy.qrc
|
||||
RC_FILE = res/win/Synergy.rc
|
||||
TRANSLATIONS = res/lang/nl_NL.ts
|
||||
|
||||
83
src/gui/src/IpcClient.cpp
Normal file
83
src/gui/src/IpcClient.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* synergy -- mouse and keyboard sharing utility
|
||||
* Copyright (C) 2012 Nick Bolton
|
||||
*
|
||||
* This package is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* found in the file COPYING that should have accompanied this file.
|
||||
*
|
||||
* This package is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "IpcClient.h"
|
||||
#include <QTcpSocket>
|
||||
#include <QHostAddress>
|
||||
|
||||
IpcClient::IpcClient()
|
||||
{
|
||||
m_Socket = new QTcpSocket(this);
|
||||
connect(m_Socket, SIGNAL(readyRead()), this, SLOT(read()));
|
||||
connect(m_Socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error(QAbstractSocket::SocketError)));
|
||||
}
|
||||
|
||||
IpcClient::~IpcClient()
|
||||
{
|
||||
}
|
||||
|
||||
void IpcClient::connectToHost()
|
||||
{
|
||||
m_Socket->connectToHost(QHostAddress(QHostAddress::LocalHost), IPC_PORT);
|
||||
}
|
||||
|
||||
void IpcClient::read()
|
||||
{
|
||||
QDataStream stream(m_Socket);
|
||||
|
||||
char codeBuf[1];
|
||||
stream.readRawData(codeBuf, 1);
|
||||
|
||||
switch (codeBuf[0]) {
|
||||
case kIpcLogLine: {
|
||||
char lenBuf[1];
|
||||
stream.readRawData(lenBuf, 1);
|
||||
|
||||
char* data = new char[lenBuf[0] + 1];
|
||||
stream.readRawData(data, lenBuf[0]);
|
||||
data[(int)lenBuf[0]] = 0;
|
||||
|
||||
QString s(data);
|
||||
readLogLine(s);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void IpcClient::error(QAbstractSocket::SocketError error)
|
||||
{
|
||||
errorMessage("ERROR: Could not connect to background service.");
|
||||
}
|
||||
|
||||
void IpcClient::write(unsigned char code, unsigned char length, const char* data)
|
||||
{
|
||||
QDataStream stream(m_Socket);
|
||||
|
||||
char codeBuf[1];
|
||||
codeBuf[0] = code;
|
||||
stream.writeRawData(codeBuf, 1);
|
||||
|
||||
switch (code) {
|
||||
case kIpcCommand: {
|
||||
char lenBuf[1];
|
||||
lenBuf[0] = length;
|
||||
stream.writeRawData(lenBuf, 1);
|
||||
stream.writeRawData(data, length);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
53
src/gui/src/IpcClient.h
Normal file
53
src/gui/src/IpcClient.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* synergy -- mouse and keyboard sharing utility
|
||||
* Copyright (C) 2012 Nick Bolton
|
||||
*
|
||||
* This package is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* found in the file COPYING that should have accompanied this file.
|
||||
*
|
||||
* This package is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QAbstractSocket>
|
||||
|
||||
#define IPC_PORT 24801
|
||||
|
||||
class QTcpSocket;
|
||||
|
||||
class IpcClient : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
IpcClient();
|
||||
virtual ~IpcClient();
|
||||
|
||||
void connectToHost();
|
||||
void write(unsigned char code, unsigned char length, const char* data);
|
||||
|
||||
private slots:
|
||||
void read();
|
||||
void error(QAbstractSocket::SocketError error);
|
||||
|
||||
signals:
|
||||
void readLogLine(const QString& text);
|
||||
void errorMessage(const QString& text);
|
||||
|
||||
private:
|
||||
QTcpSocket* m_Socket;
|
||||
};
|
||||
|
||||
enum EIpcMessage {
|
||||
kIpcLogLine,
|
||||
kIpcCommand
|
||||
};
|
||||
@@ -17,6 +17,8 @@
|
||||
|
||||
#define WEBSITE_ADDRESS "synergy-foss.org"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "MainWindow.h"
|
||||
#include "AboutDialog.h"
|
||||
#include "ServerConfigDialog.h"
|
||||
@@ -88,8 +90,10 @@ MainWindow::MainWindow(QSettings& settings, AppConfig& appConfig) :
|
||||
|
||||
if (appConfig.processMode() == Service)
|
||||
{
|
||||
connect(&m_IpcLogReader, SIGNAL(receivedLine(const QString&)), this, SLOT(appendLog(const QString&)));
|
||||
m_IpcLogReader.start();
|
||||
connect(&m_IpcClient, SIGNAL(readLogLine(const QString&)), this, SLOT(appendLog(const QString&)));
|
||||
connect(&m_IpcClient, SIGNAL(errorMessage(const QString&)), this, SLOT(appendLog(const QString&)));
|
||||
m_IpcClient.connectToHost();
|
||||
appendLog("INFO: Connecting to background service...");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -665,55 +669,9 @@ void MainWindow::on_m_pButtonConfigureServer_clicked()
|
||||
|
||||
void MainWindow::sendDaemonCommand(const QString& command, bool showErrors)
|
||||
{
|
||||
sendIpcMessage(Command, command.toStdString().c_str(), showErrors);
|
||||
}
|
||||
|
||||
// TODO: put this in an IPC client class.
|
||||
void MainWindow::sendIpcMessage(qIpcMessage type, const char* data, bool showErrors)
|
||||
{
|
||||
#if defined(Q_OS_WIN)
|
||||
|
||||
const WCHAR* name = L"\\\\.\\pipe\\Synergy";
|
||||
char message[1024];
|
||||
message[0] = type;
|
||||
char* messagePtr = message;
|
||||
messagePtr++;
|
||||
strcpy(messagePtr, data);
|
||||
|
||||
HANDLE pipe = CreateFile(
|
||||
name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
|
||||
|
||||
if (showErrors && pipe == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
appendLog(QString("ERROR: could not connect to service, error: ") +
|
||||
QString::number(GetLastError()));
|
||||
return;
|
||||
}
|
||||
|
||||
DWORD dwMode = PIPE_READMODE_MESSAGE;
|
||||
BOOL stateSuccess = SetNamedPipeHandleState(pipe, &dwMode, NULL, NULL);
|
||||
|
||||
if (showErrors && !stateSuccess)
|
||||
{
|
||||
appendLog(QString("ERROR: could not set service pipe state, error: ") +
|
||||
QString::number(GetLastError()));
|
||||
return;
|
||||
}
|
||||
|
||||
DWORD written;
|
||||
BOOL writeSuccess = WriteFile(
|
||||
pipe, message, strlen(message), &written, NULL);
|
||||
|
||||
if (showErrors && !writeSuccess)
|
||||
{
|
||||
appendLog(QString("ERROR: could not write to service pipe, error: ") +
|
||||
QString::number(GetLastError()));
|
||||
return;
|
||||
}
|
||||
|
||||
CloseHandle(pipe);
|
||||
|
||||
#endif
|
||||
std::string s = command.toStdString();
|
||||
const char* data = s.c_str();
|
||||
m_IpcClient.write(Command, strlen(data), data);
|
||||
}
|
||||
|
||||
void MainWindow::on_m_pActionWizard_triggered()
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
#include "ServerConfig.h"
|
||||
#include "AppConfig.h"
|
||||
#include "VersionChecker.h"
|
||||
#include "IpcLogReader.h"
|
||||
#include "IpcClient.h"
|
||||
|
||||
class QAction;
|
||||
class QMenu;
|
||||
@@ -142,7 +142,7 @@ class MainWindow : public QMainWindow, public Ui::MainWindowBase
|
||||
bool m_alreadyHidden;
|
||||
VersionChecker m_versionChecker;
|
||||
SetupWizard* m_SetupWizard;
|
||||
IpcLogReader m_IpcLogReader;
|
||||
IpcClient m_IpcClient;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user