1 /*
2 * barrier -- mouse and keyboard sharing utility
3 * Copyright (C) 2012-2016 Symless Ltd.
4 * Copyright (C) 2012 Nick Bolton
5 *
6 * This package is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * found in the file LICENSE that should have accompanied this file.
9 *
10 * This package is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "IpcClient.h"
20 #include <QTcpSocket>
21 #include <QHostAddress>
22 #include <iostream>
23 #include <QTimer>
24 #include "IpcReader.h"
25 #include "Ipc.h"
26 #include <QDataStream>
27
IpcClient()28 IpcClient::IpcClient() :
29 m_ReaderStarted(false),
30 m_Enabled(false)
31 {
32 m_Socket = new QTcpSocket(this);
33 connect(m_Socket, SIGNAL(connected()), this, SLOT(connected()));
34 connect(m_Socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error(QAbstractSocket::SocketError)));
35
36 m_Reader = new IpcReader(m_Socket);
37 connect(m_Reader, SIGNAL(readLogLine(const QString&)), this, SLOT(handleReadLogLine(const QString&)));
38 }
39
~IpcClient()40 IpcClient::~IpcClient()
41 {
42 }
43
connected()44 void IpcClient::connected()
45 {
46 sendHello();
47 infoMessage("connection established");
48 }
49
connectToHost()50 void IpcClient::connectToHost()
51 {
52 m_Enabled = true;
53
54 infoMessage("connecting to service...");
55 m_Socket->connectToHost(QHostAddress(QHostAddress::LocalHost), IPC_PORT);
56
57 if (!m_ReaderStarted) {
58 m_Reader->start();
59 m_ReaderStarted = true;
60 }
61 }
62
disconnectFromHost()63 void IpcClient::disconnectFromHost()
64 {
65 infoMessage("service disconnect");
66 m_Reader->stop();
67 m_Socket->close();
68 }
69
error(QAbstractSocket::SocketError error)70 void IpcClient::error(QAbstractSocket::SocketError error)
71 {
72 QString text;
73 switch (error) {
74 case 0: text = "connection refused"; break;
75 case 1: text = "remote host closed"; break;
76 default: text = QString("code=%1").arg(error); break;
77 }
78
79 errorMessage(QString("ipc connection error, %1").arg(text));
80
81 QTimer::singleShot(1000, this, SLOT(retryConnect()));
82 }
83
retryConnect()84 void IpcClient::retryConnect()
85 {
86 if (m_Enabled) {
87 connectToHost();
88 }
89 }
90
sendHello()91 void IpcClient::sendHello()
92 {
93 QDataStream stream(m_Socket);
94 stream.writeRawData(kIpcMsgHello, 4);
95
96 char typeBuf[1];
97 typeBuf[0] = kIpcClientGui;
98 stream.writeRawData(typeBuf, 1);
99 }
100
sendCommand(const QString & command,ElevateMode const elevate)101 void IpcClient::sendCommand(const QString& command, ElevateMode const elevate)
102 {
103 QDataStream stream(m_Socket);
104
105 stream.writeRawData(kIpcMsgCommand, 4);
106
107 std::string stdStringCommand = command.toStdString();
108 const char* charCommand = stdStringCommand.c_str();
109 int length = (int)strlen(charCommand);
110
111 char lenBuf[4];
112 intToBytes(length, lenBuf, 4);
113 stream.writeRawData(lenBuf, 4);
114 stream.writeRawData(charCommand, length);
115
116 char elevateBuf[1];
117 // Refer to enum ElevateMode documentation for why this flag is mapped this way
118 elevateBuf[0] = (elevate == ElevateAlways) ? 1 : 0;
119 stream.writeRawData(elevateBuf, 1);
120 }
121
handleReadLogLine(const QString & text)122 void IpcClient::handleReadLogLine(const QString& text)
123 {
124 readLogLine(text);
125 }
126
127 // TODO: qt must have a built in way of converting int to bytes.
intToBytes(int value,char * buffer,int size)128 void IpcClient::intToBytes(int value, char *buffer, int size)
129 {
130 if (size == 1) {
131 buffer[0] = value & 0xff;
132 }
133 else if (size == 2) {
134 buffer[0] = (value >> 8) & 0xff;
135 buffer[1] = value & 0xff;
136 }
137 else if (size == 4) {
138 buffer[0] = (value >> 24) & 0xff;
139 buffer[1] = (value >> 16) & 0xff;
140 buffer[2] = (value >> 8) & 0xff;
141 buffer[3] = value & 0xff;
142 }
143 else {
144 // TODO: other sizes, if needed.
145 }
146 }
147