changed ipc protocol to be more like original synergy protocol (4 byte message codes and better use of protocol util class).

This commit is contained in:
Nick Bolton 2012-07-11 18:06:10 +00:00
parent 0a7c7f5073
commit 82d91605fa
5 changed files with 78 additions and 82 deletions

View File

@ -94,44 +94,39 @@ CIpcClientProxy::handleWriteError(const CEvent&, void*)
void
CIpcClientProxy::handleData(const CEvent&, void*)
{
LOG((CLOG_DEBUG "start ipc client proxy handle data"));
// don't allow the dtor to destroy the stream while we're using it.
CArchMutexLock lock(m_readMutex);
UInt8 codeBuf[1];
UInt32 n = m_stream.read(codeBuf, 1);
int code = codeBuf[0];
LOG((CLOG_DEBUG "start ipc handle data"));
UInt8 code[4];
UInt32 n = m_stream.read(code, 4);
while (n != 0) {
LOG((CLOG_DEBUG "ipc client proxy read: %d", code));
LOG((CLOG_DEBUG "ipc read: %c%c%c%c",
code[0], code[1], code[2], code[3]));
CIpcMessage* m = nullptr;
switch (code) {
case kIpcHello:
if (memcmp(code, kIpcMsgHello, 4) == 0) {
m = parseHello();
break;
case kIpcCommand:
m = parseCommand();
break;
default:
disconnect();
return;
}
else if (memcmp(code, kIpcMsgCommand, 4) == 0) {
m = parseCommand();
}
else {
LOG((CLOG_ERR "invalid message"));
disconnect();
}
// don't delete with this event; the data is passed to a new event.
CEvent e(getMessageReceivedEvent(), this, NULL, CEvent::kDontFreeData);
e.setDataObject(m);
EVENTQUEUE->addEvent(e);
n = m_stream.read(codeBuf, 1);
code = codeBuf[0];
n = m_stream.read(code, 4);
}
LOG((CLOG_DEBUG "finished ipc client proxy handle data"));
LOG((CLOG_DEBUG "finished ipc handle data"));
}
void
@ -142,25 +137,18 @@ CIpcClientProxy::send(const CIpcMessage& message)
// also, don't allow the dtor to destroy the stream while we're using it.
CArchMutexLock lock(m_writeMutex);
LOG((CLOG_DEBUG "ipc client proxy write: %d", message.type()));
CProtocolUtil::writef(&m_stream, "%1i", message.type());
LOG((CLOG_DEBUG "ipc write: %d", message.type()));
switch (message.type()) {
case kIpcLogLine: {
const CIpcLogLineMessage& llm = static_cast<const CIpcLogLineMessage&>(message);
CString logLine = llm.logLine();
const char* data = logLine.c_str();
int len = strlen(data);
CProtocolUtil::writef(&m_stream, "%4i", len);
m_stream.write(data, len);
CProtocolUtil::writef(&m_stream, kIpcMsgLogLine, &logLine);
break;
}
case kIpcShutdown:
// no data.
CProtocolUtil::writef(&m_stream, kIpcMsgShutdown);
break;
default:
@ -172,9 +160,10 @@ CIpcClientProxy::send(const CIpcMessage& message)
CIpcHelloMessage*
CIpcClientProxy::parseHello()
{
UInt8 buffer[1];
m_stream.read(buffer, 1);
m_clientType = static_cast<EIpcClientType>(buffer[0]);
UInt8 type;
CProtocolUtil::readf(&m_stream, kIpcMsgHello + 4, &type);
m_clientType = static_cast<EIpcClientType>(type);
// must be deleted by event handler.
return new CIpcHelloMessage(m_clientType);
@ -183,16 +172,11 @@ CIpcClientProxy::parseHello()
CIpcCommandMessage*
CIpcClientProxy::parseCommand()
{
int len = 0;
CProtocolUtil::readf(&m_stream, "%2i", &len);
char* buffer = new char[len];
m_stream.read(buffer, len);
CString s(buffer, len);
delete buffer;
CString command;
CProtocolUtil::readf(&m_stream, kIpcMsgCommand + 4, &command);
// must be deleted by event handler.
return new CIpcCommandMessage(s);
return new CIpcCommandMessage(command);
}
void

View File

@ -45,29 +45,25 @@ CIpcServerProxy::~CIpcServerProxy()
void
CIpcServerProxy::handleData(const CEvent&, void*)
{
LOG((CLOG_DEBUG "start ipc server proxy handle data"));
UInt8 codeBuf[1];
UInt32 n = m_stream.read(codeBuf, 1);
int code = codeBuf[0];
LOG((CLOG_DEBUG "start ipc handle data"));
UInt8 code[4];
UInt32 n = m_stream.read(code, 4);
while (n != 0) {
LOG((CLOG_DEBUG "ipc server proxy read: %d", code));
LOG((CLOG_DEBUG "ipc read: %c%c%c%c",
code[0], code[1], code[2], code[3]));
CIpcMessage* m = nullptr;
switch (code) {
case kIpcLogLine:
if (memcmp(code, kIpcMsgLogLine, 4) == 0) {
m = parseLogLine();
break;
case kIpcShutdown:
}
else if (memcmp(code, kIpcMsgShutdown, 4) == 0) {
m = new CIpcShutdownMessage();
break;
default:
}
else {
LOG((CLOG_ERR "invalid message"));
disconnect();
return;
}
// don't delete with this event; the data is passed to a new event.
@ -75,36 +71,28 @@ CIpcServerProxy::handleData(const CEvent&, void*)
e.setDataObject(m);
EVENTQUEUE->addEvent(e);
n = m_stream.read(codeBuf, 1);
code = codeBuf[0];
n = m_stream.read(code, 4);
}
LOG((CLOG_DEBUG "finished ipc server proxy handle data"));
LOG((CLOG_DEBUG "finished ipc handle data"));
}
void
CIpcServerProxy::send(const CIpcMessage& message)
{
LOG((CLOG_DEBUG "ipc server proxy write: %d", message.type()));
CProtocolUtil::writef(&m_stream, "%1i", message.type());
LOG((CLOG_DEBUG "ipc write: %d", message.type()));
switch (message.type()) {
case kIpcHello: {
const CIpcHelloMessage& hm = static_cast<const CIpcHelloMessage&>(message);
CProtocolUtil::writef(&m_stream, "%1i", hm.clientType());
CProtocolUtil::writef(&m_stream, kIpcMsgHello, hm.clientType());
break;
}
case kIpcCommand: {
const CIpcCommandMessage& cm = static_cast<const CIpcCommandMessage&>(message);
CString command = cm.command();
const char* data = command.c_str();
int len = strlen(data);
CProtocolUtil::writef(&m_stream, "%2i", len);
m_stream.write(data, len);
CProtocolUtil::writef(&m_stream, kIpcMsgCommand, &command);
break;
}
@ -117,16 +105,11 @@ CIpcServerProxy::send(const CIpcMessage& message)
CIpcLogLineMessage*
CIpcServerProxy::parseLogLine()
{
int len = 0;
CProtocolUtil::readf(&m_stream, "%4i", &len);
char* buffer = new char[len];
m_stream.read(buffer, len);
CString s(buffer, len);
delete buffer;
CString logLine;
CProtocolUtil::readf(&m_stream, kIpcMsgLogLine + 4, &logLine);
// must be deleted by event handler.
return new CIpcLogLineMessage(s);
return new CIpcLogLineMessage(logLine);
}
void

View File

@ -24,6 +24,7 @@ set(inc
)
set(src
Ipc.cpp
CIpcServer.cpp
CIpcClient.cpp
CIpcServerProxy.cpp

23
src/lib/ipc/Ipc.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "Ipc.h"
const char* kIpcMsgHello = "IHEL%1i";
const char* kIpcMsgLogLine = "ILOG%s";
const char* kIpcMsgCommand = "ICMD%s";
const char* kIpcMsgShutdown = "ISDN";

View File

@ -32,3 +32,8 @@ enum EIpcClientType {
kIpcClientGui,
kIpcClientNode,
};
extern const char* kIpcMsgHello;
extern const char* kIpcMsgLogLine;
extern const char* kIpcMsgCommand;
extern const char* kIpcMsgShutdown;