From 0537bbdfad8d048e5000d3a62de6b0569fec1f13 Mon Sep 17 00:00:00 2001 From: Nick Bolton Date: Tue, 3 Jul 2012 17:33:19 +0000 Subject: [PATCH] increased ipc message length. --- src/gui/src/IpcClient.cpp | 26 +++++++++++++++----------- src/gui/src/MainWindow.cpp | 2 +- src/lib/ipc/CIpcClientProxy.cpp | 17 ++++++++--------- src/lib/ipc/CIpcServerProxy.cpp | 21 +++++++++++---------- src/lib/ipc/CMakeLists.txt | 3 ++- src/lib/synergy/CDaemonApp.cpp | 9 +++++---- 6 files changed, 42 insertions(+), 36 deletions(-) diff --git a/src/gui/src/IpcClient.cpp b/src/gui/src/IpcClient.cpp index ee0f9012..a0554474 100644 --- a/src/gui/src/IpcClient.cpp +++ b/src/gui/src/IpcClient.cpp @@ -44,14 +44,15 @@ void IpcClient::read() switch (codeBuf[0]) { case kIpcLogLine: { - char lenBuf[1]; - stream.readRawData(lenBuf, 1); + // TODO: qt must have a built in way of converting bytes to int. + char lenBuf[2]; + stream.readRawData(lenBuf, 2); + int len = (lenBuf[0] << 8) + lenBuf[1]; - char* data = new char[lenBuf[0] + 1]; - stream.readRawData(data, lenBuf[0]); - data[(int)lenBuf[0]] = 0; - - QString s(data); + char* data = new char[len]; + stream.readRawData(data, len); + + QString s = QString::fromUtf8(data, len); readLogLine(s); } break; @@ -60,7 +61,7 @@ void IpcClient::read() void IpcClient::error(QAbstractSocket::SocketError error) { - errorMessage("ERROR: Could not connect to background service."); + errorMessage(QString("ERROR: could not connect to background service, code=%1").arg(error)); } void IpcClient::write(unsigned char code, unsigned char length, const char* data) @@ -73,9 +74,12 @@ void IpcClient::write(unsigned char code, unsigned char length, const char* data switch (code) { case kIpcCommand: { - char lenBuf[1]; - lenBuf[0] = length; - stream.writeRawData(lenBuf, 1); + // TODO: qt must have a built in way of converting int to bytes. + char lenBuf[2]; + lenBuf[0] = (length >> 8) & 0xff; + lenBuf[1] = length & 0xff; + stream.writeRawData(lenBuf, 2); + stream.writeRawData(data, length); } break; diff --git a/src/gui/src/MainWindow.cpp b/src/gui/src/MainWindow.cpp index 8574490d..87cc441e 100644 --- a/src/gui/src/MainWindow.cpp +++ b/src/gui/src/MainWindow.cpp @@ -93,7 +93,7 @@ MainWindow::MainWindow(QSettings& settings, AppConfig& appConfig) : 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..."); + appendLog("INFO: connecting to background service..."); } } diff --git a/src/lib/ipc/CIpcClientProxy.cpp b/src/lib/ipc/CIpcClientProxy.cpp index ca4967c4..4f3fb542 100644 --- a/src/lib/ipc/CIpcClientProxy.cpp +++ b/src/lib/ipc/CIpcClientProxy.cpp @@ -21,6 +21,7 @@ #include "Ipc.h" #include "CLog.h" #include "CIpcMessage.h" +#include "CProtocolUtil.h" CEvent::Type CIpcClientProxy::s_messageReceivedEvent = CEvent::kUnknown; @@ -88,11 +89,9 @@ CIpcClientProxy::send(const CIpcMessage& message) case kIpcLogLine: { CString* s = (CString*)message.m_data; const char* data = s->c_str(); + int len = strlen(data); - - UInt8 lenBuf[1]; - lenBuf[0] = len; - m_stream.write(lenBuf, 1); + CProtocolUtil::writef(&m_stream, "%2i", len); m_stream.write(data, len); } @@ -109,13 +108,13 @@ CIpcClientProxy::send(const CIpcMessage& message) void* CIpcClientProxy::parseCommand() { - UInt8 len[1]; - m_stream.read(len, 1); + int len = 0; + CProtocolUtil::readf(&m_stream, "%2i", &len); - UInt8* buffer = new UInt8[len[0]]; - m_stream.read(buffer, len[0]); + UInt8* buffer = new UInt8[len]; + m_stream.read(buffer, len); - return new CString((const char*)buffer, len[0]); + return new CString((const char*)buffer, len); } void diff --git a/src/lib/ipc/CIpcServerProxy.cpp b/src/lib/ipc/CIpcServerProxy.cpp index 0a7c7869..70aef8d7 100644 --- a/src/lib/ipc/CIpcServerProxy.cpp +++ b/src/lib/ipc/CIpcServerProxy.cpp @@ -21,6 +21,7 @@ #include "CLog.h" #include "CIpcMessage.h" #include "Ipc.h" +#include "CProtocolUtil.h" CEvent::Type CIpcServerProxy::s_messageReceivedEvent = CEvent::kUnknown; @@ -80,12 +81,12 @@ CIpcServerProxy::send(const CIpcMessage& message) switch (message.m_type) { case kIpcCommand: { CString* s = (CString*)message.m_data; + const char* data = s->c_str(); + + int len = strlen(data); + CProtocolUtil::writef(&m_stream, "%2i", len); - UInt8 len[1]; - len[0] = s->size(); - m_stream.write(len, 1); - - m_stream.write(s->c_str(), s->size()); + m_stream.write(data, len); } break; @@ -98,13 +99,13 @@ CIpcServerProxy::send(const CIpcMessage& message) void* CIpcServerProxy::parseLogLine() { - UInt8 len[1]; - m_stream.read(len, 1); + int len = 0; + CProtocolUtil::readf(&m_stream, "%2i", &len); - UInt8* buffer = new UInt8[len[0]]; - m_stream.read(buffer, len[0]); + UInt8* buffer = new UInt8[len]; + m_stream.read(buffer, len); - return new CString((const char*)buffer, len[0]); + return new CString((const char*)buffer, len); } void diff --git a/src/lib/ipc/CMakeLists.txt b/src/lib/ipc/CMakeLists.txt index 7a1c4daf..211f80f2 100644 --- a/src/lib/ipc/CMakeLists.txt +++ b/src/lib/ipc/CMakeLists.txt @@ -43,6 +43,7 @@ set(inc ../io ../mt ../net + ../synergy ) if (UNIX) @@ -55,5 +56,5 @@ include_directories(${inc}) add_library(ipc STATIC ${src}) if (UNIX) - target_link_libraries(arch base common mt io net) + target_link_libraries(arch base common mt io net synergy) endif() diff --git a/src/lib/synergy/CDaemonApp.cpp b/src/lib/synergy/CDaemonApp.cpp index a5f47911..ebc5192a 100644 --- a/src/lib/synergy/CDaemonApp.cpp +++ b/src/lib/synergy/CDaemonApp.cpp @@ -277,11 +277,12 @@ CDaemonApp::handleIpcMessage(const CEvent& e, void*) { CIpcMessage& m = *reinterpret_cast(e.getData()); - LOG((CLOG_DEBUG "ipc message: %d", m.m_type)); + LOG((CLOG_DEBUG "ipc message, type=%d", m.m_type)); switch (m.m_type) { case kIpcCommand: { CString& command = *reinterpret_cast(m.m_data); + LOG((CLOG_DEBUG "got new command: %s", command.c_str())); try { // store command in system settings. this is used when the daemon @@ -289,9 +290,9 @@ CDaemonApp::handleIpcMessage(const CEvent& e, void*) ARCH->setting("Command", command); } catch (XArch& e) { - //LOG((CLOG_ERR "failed to save setting: %s", e.what().c_str())); + LOG((CLOG_ERR "failed to save setting, %s", e.what().c_str())); } - + // tell the relauncher about the new command. this causes the // relauncher to stop the existing command and start the new // command. @@ -300,7 +301,7 @@ CDaemonApp::handleIpcMessage(const CEvent& e, void*) break; default: - LOG((CLOG_ERR "ipc message not supported: %d", m.m_type)); + LOG((CLOG_ERR "ipc message not supported, type=%d", m.m_type)); break; } }