From 19ccba81267175990951f2d7e669756d2cddd389 Mon Sep 17 00:00:00 2001 From: Nick Bolton Date: Wed, 11 Jul 2012 18:28:28 +0000 Subject: [PATCH] made gui compatible with new version of ipc protocol. --- src/gui/gui.pro | 3 ++- src/gui/src/Ipc.cpp | 23 +++++++++++++++++++++++ src/gui/src/Ipc.h | 5 +++++ src/gui/src/IpcClient.cpp | 24 ++++++++++++++++-------- src/gui/src/IpcReader.cpp | 38 ++++++++++++++++++-------------------- 5 files changed, 64 insertions(+), 29 deletions(-) create mode 100644 src/gui/src/Ipc.cpp diff --git a/src/gui/gui.pro b/src/gui/gui.pro index 67f8901d..527e7116 100644 --- a/src/gui/gui.pro +++ b/src/gui/gui.pro @@ -37,7 +37,8 @@ SOURCES += src/main.cpp \ src/VersionChecker.cpp \ src/SetupWizard.cpp \ src/IpcClient.cpp \ - src/IpcReader.cpp + src/IpcReader.cpp \ + src/Ipc.cpp HEADERS += src/MainWindow.h \ src/AboutDialog.h \ src/ServerConfig.h \ diff --git a/src/gui/src/Ipc.cpp b/src/gui/src/Ipc.cpp new file mode 100644 index 00000000..c7e45030 --- /dev/null +++ b/src/gui/src/Ipc.cpp @@ -0,0 +1,23 @@ +/* + * 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 . + */ + +#include "Ipc.h" + +const char* kIpcMsgHello = "IHEL%1i"; +const char* kIpcMsgLogLine = "ILOG%s"; +const char* kIpcMsgCommand = "ICMD%s"; +const char* kIpcMsgShutdown = "ISDN"; diff --git a/src/gui/src/Ipc.h b/src/gui/src/Ipc.h index 7bc0099b..473699ef 100644 --- a/src/gui/src/Ipc.h +++ b/src/gui/src/Ipc.h @@ -32,3 +32,8 @@ enum qIpcClientType { kIpcClientGui, kIpcClientNode, }; + +extern const char* kIpcMsgHello; +extern const char* kIpcMsgLogLine; +extern const char* kIpcMsgCommand; +extern const char* kIpcMsgShutdown; diff --git a/src/gui/src/IpcClient.cpp b/src/gui/src/IpcClient.cpp index 3b301da3..c83b7b69 100644 --- a/src/gui/src/IpcClient.cpp +++ b/src/gui/src/IpcClient.cpp @@ -71,19 +71,18 @@ void IpcClient::write(int code, int length, const char* data) { QDataStream stream(m_Socket); - char codeBuf[1]; - codeBuf[0] = code; - stream.writeRawData(codeBuf, 1); - switch (code) { case kIpcHello: + stream.writeRawData(kIpcMsgHello, 4); stream.writeRawData(data, 1); break; case kIpcCommand: { - char lenBuf[2]; - intToBytes(length, lenBuf, 2); - stream.writeRawData(lenBuf, 2); + char lenBuf[4]; + intToBytes(length, lenBuf, 4); + + stream.writeRawData(kIpcMsgCommand, 4); + stream.writeRawData(lenBuf, 4); stream.writeRawData(data, length); break; } @@ -102,10 +101,19 @@ void IpcClient::handleReadLogLine(const QString& text) // TODO: qt must have a built in way of converting int to bytes. void IpcClient::intToBytes(int value, char *buffer, int size) { - if (size == 2) { + 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. } diff --git a/src/gui/src/IpcReader.cpp b/src/gui/src/IpcReader.cpp index 94d82c16..eade55cf 100644 --- a/src/gui/src/IpcReader.cpp +++ b/src/gui/src/IpcReader.cpp @@ -44,30 +44,28 @@ void IpcReader::read() while (m_Socket->bytesAvailable()) { std::cout << "bytes available" << std::endl; - char codeBuf[1]; - readStream(codeBuf, 1); - int code = bytesToInt(codeBuf, 1); + char codeBuf[5]; + readStream(codeBuf, 4); + codeBuf[4] = 0; + std::cout << "ipc read: " << codeBuf << std::endl; - switch (code) { - case kIpcLogLine: { - std::cout << "reading log line" << std::endl; + if (memcmp(codeBuf, kIpcMsgLogLine, 4) == 0) { + std::cout << "reading log line" << std::endl; - char lenBuf[4]; - readStream(lenBuf, 4); - int len = bytesToInt(lenBuf, 4); + char lenBuf[4]; + readStream(lenBuf, 4); + int len = bytesToInt(lenBuf, 4); - char* data = new char[len]; - readStream(data, len); - QString line = QString::fromUtf8(data, len); - delete data; + char* data = new char[len]; + readStream(data, len); + QString line = QString::fromUtf8(data, len); + delete data; - readLogLine(line); - break; - } - - default: - std::cerr << "aborting, message invalid: " << code << std::endl; - return; + readLogLine(line); + } + else { + std::cerr << "aborting, message invalid" << std::endl; + return; } }