2013-07-24 16:41:12 +00:00
|
|
|
/*
|
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
2024-07-16 08:30:35 +00:00
|
|
|
* Copyright (C) 2012 Symless Ltd.
|
2013-07-24 16:41:12 +00:00
|
|
|
*
|
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
2015-05-03 02:33:52 +00:00
|
|
|
* found in the file LICENSE that should have accompanied this file.
|
2013-07-24 16:41:12 +00:00
|
|
|
*
|
|
|
|
|
* 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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-07-16 08:30:35 +00:00
|
|
|
#include "QIpcClient.h"
|
2024-07-23 19:17:40 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
#include "IpcReader.h"
|
2024-07-26 22:53:52 +00:00
|
|
|
#include "global/Ipc.h"
|
2024-07-16 08:30:35 +00:00
|
|
|
|
2014-04-09 10:19:13 +00:00
|
|
|
#include <QDataStream>
|
2024-07-02 19:07:06 +00:00
|
|
|
#include <QHostAddress>
|
|
|
|
|
#include <QTimer>
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2024-07-16 08:30:35 +00:00
|
|
|
QIpcClient::QIpcClient(const StreamProvider &streamProvider)
|
2024-07-16 13:36:44 +00:00
|
|
|
: m_ReaderStarted(false),
|
|
|
|
|
m_Enabled(false),
|
2024-07-16 08:30:35 +00:00
|
|
|
m_StreamProvider(streamProvider) {
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
m_Socket = new QTcpSocket(this);
|
2024-07-16 08:30:35 +00:00
|
|
|
|
|
|
|
|
if (!m_StreamProvider) {
|
|
|
|
|
m_StreamProvider = [this]() {
|
|
|
|
|
return std::make_shared<QDataStreamProxy>(m_Socket);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
connect(m_Socket, SIGNAL(connected()), this, SLOT(connected()));
|
2024-07-16 13:36:44 +00:00
|
|
|
connect(
|
|
|
|
|
m_Socket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this,
|
|
|
|
|
SLOT(error(QAbstractSocket::SocketError)));
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
m_Reader = new IpcReader(m_Socket);
|
2024-07-16 13:36:44 +00:00
|
|
|
connect(
|
|
|
|
|
m_Reader, SIGNAL(readLogLine(const QString &)), this,
|
|
|
|
|
SLOT(handleReadLogLine(const QString &)));
|
2013-07-24 16:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-16 08:30:35 +00:00
|
|
|
QIpcClient::~QIpcClient() {
|
|
|
|
|
delete m_Reader;
|
|
|
|
|
delete m_Socket;
|
|
|
|
|
}
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2024-07-16 08:30:35 +00:00
|
|
|
void QIpcClient::connected() {
|
2024-07-18 16:51:06 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
sendHello();
|
|
|
|
|
infoMessage("connection established");
|
2013-07-24 16:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-16 08:30:35 +00:00
|
|
|
void QIpcClient::connectToHost() {
|
2024-07-02 19:07:06 +00:00
|
|
|
m_Enabled = true;
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
infoMessage("connecting to service...");
|
2024-07-18 07:04:39 +00:00
|
|
|
const auto port = static_cast<quint16>(kIpcPort);
|
|
|
|
|
m_Socket->connectToHost(QHostAddress(QHostAddress::LocalHost), port);
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
if (!m_ReaderStarted) {
|
|
|
|
|
m_Reader->start();
|
|
|
|
|
m_ReaderStarted = true;
|
|
|
|
|
}
|
2013-07-24 16:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-16 08:30:35 +00:00
|
|
|
void QIpcClient::disconnectFromHost() {
|
2024-07-02 19:07:06 +00:00
|
|
|
infoMessage("service disconnect");
|
|
|
|
|
m_Reader->stop();
|
|
|
|
|
m_Socket->close();
|
2013-07-24 16:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-16 08:30:35 +00:00
|
|
|
void QIpcClient::error(QAbstractSocket::SocketError error) {
|
2024-07-02 19:07:06 +00:00
|
|
|
QString text;
|
|
|
|
|
switch (error) {
|
|
|
|
|
case 0:
|
|
|
|
|
text = "connection refused";
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
text = "remote host closed";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
text = QString("code=%1").arg(error);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
errorMessage(QString("ipc connection error, %1").arg(text));
|
|
|
|
|
|
|
|
|
|
QTimer::singleShot(1000, this, SLOT(retryConnect()));
|
2013-07-24 16:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-16 08:30:35 +00:00
|
|
|
void QIpcClient::retryConnect() {
|
2024-07-02 19:07:06 +00:00
|
|
|
if (m_Enabled) {
|
|
|
|
|
connectToHost();
|
|
|
|
|
}
|
2013-07-24 16:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-16 08:30:35 +00:00
|
|
|
void QIpcClient::sendHello() {
|
|
|
|
|
auto stream = m_StreamProvider();
|
|
|
|
|
stream->writeRawData(kIpcMsgHello, 4);
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
char typeBuf[1];
|
2024-07-18 07:04:39 +00:00
|
|
|
typeBuf[0] = static_cast<char>(IpcClientType::GUI);
|
2024-07-16 08:30:35 +00:00
|
|
|
stream->writeRawData(typeBuf, 1);
|
2013-07-24 16:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-16 13:36:44 +00:00
|
|
|
void QIpcClient::sendCommand(
|
|
|
|
|
const QString &command, ElevateMode const elevate) {
|
2024-07-16 08:30:35 +00:00
|
|
|
auto stream = m_StreamProvider();
|
|
|
|
|
stream->writeRawData(kIpcMsgCommand, 4);
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
std::string stdStringCommand = command.toStdString();
|
|
|
|
|
const char *charCommand = stdStringCommand.c_str();
|
2024-07-16 08:30:35 +00:00
|
|
|
auto length = static_cast<int>(stdStringCommand.length());
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
char lenBuf[4];
|
|
|
|
|
intToBytes(length, lenBuf, 4);
|
2024-07-16 08:30:35 +00:00
|
|
|
stream->writeRawData(lenBuf, 4);
|
|
|
|
|
stream->writeRawData(charCommand, length);
|
2013-07-24 16:41:12 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
char elevateBuf[1];
|
|
|
|
|
// Refer to enum ElevateMode documentation for why this flag is mapped this
|
|
|
|
|
// way
|
|
|
|
|
elevateBuf[0] = (elevate == ElevateAlways) ? 1 : 0;
|
2024-07-16 08:30:35 +00:00
|
|
|
stream->writeRawData(elevateBuf, 1);
|
2013-07-24 16:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-16 08:30:35 +00:00
|
|
|
void QIpcClient::handleReadLogLine(const QString &text) { readLogLine(text); }
|
2013-07-24 16:41:12 +00:00
|
|
|
|
|
|
|
|
// TODO: qt must have a built in way of converting int to bytes.
|
2024-07-16 08:30:35 +00:00
|
|
|
void QIpcClient::intToBytes(int value, char *buffer, int size) {
|
2024-07-02 19:07:06 +00:00
|
|
|
if (size == 1) {
|
|
|
|
|
buffer[0] = value & 0xff;
|
|
|
|
|
} else if (size == 2) {
|
|
|
|
|
buffer[0] = (value >> 8) & 0xff;
|
|
|
|
|
buffer[1] = value & 0xff;
|
|
|
|
|
} else if (size == 4) {
|
|
|
|
|
buffer[0] = (value >> 24) & 0xff;
|
|
|
|
|
buffer[1] = (value >> 16) & 0xff;
|
|
|
|
|
buffer[2] = (value >> 8) & 0xff;
|
|
|
|
|
buffer[3] = value & 0xff;
|
|
|
|
|
} else {
|
|
|
|
|
// TODO: other sizes, if needed.
|
|
|
|
|
}
|
2013-07-24 16:41:12 +00:00
|
|
|
}
|