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

View File

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

View File

@ -24,6 +24,7 @@ set(inc
) )
set(src set(src
Ipc.cpp
CIpcServer.cpp CIpcServer.cpp
CIpcClient.cpp CIpcClient.cpp
CIpcServerProxy.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, kIpcClientGui,
kIpcClientNode, kIpcClientNode,
}; };
extern const char* kIpcMsgHello;
extern const char* kIpcMsgLogLine;
extern const char* kIpcMsgCommand;
extern const char* kIpcMsgShutdown;