barrier/src/lib/ipc/IpcServerProxy.cpp

123 lines
3.0 KiB
C++
Raw Normal View History

2012-06-28 07:29:06 +00:00
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2012 Bolton Software Ltd.
2012-06-28 07:29:06 +00:00
* 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 "CIpcServerProxy.h"
2012-07-02 15:28:23 +00:00
#include "IStream.h"
#include "TMethodEventJob.h"
#include "CLog.h"
#include "CIpcMessage.h"
#include "Ipc.h"
2012-07-03 17:33:19 +00:00
#include "CProtocolUtil.h"
2012-06-28 07:29:06 +00:00
//
// CIpcServerProxy
//
2012-07-02 15:28:23 +00:00
CIpcServerProxy::CIpcServerProxy(synergy::IStream& stream, IEventQueue* events) :
2014-02-25 15:03:43 +00:00
m_stream(stream),
m_events(events)
2012-06-28 07:29:06 +00:00
{
m_events->adoptHandler(m_events->forIStream().inputReady(),
2012-07-02 15:28:23 +00:00
stream.getEventTarget(),
new TMethodEventJob<CIpcServerProxy>(
this, &CIpcServerProxy::handleData));
2012-06-28 07:29:06 +00:00
}
CIpcServerProxy::~CIpcServerProxy()
{
m_events->removeHandler(m_events->forIStream().inputReady(),
2012-07-02 15:28:23 +00:00
m_stream.getEventTarget());
}
void
CIpcServerProxy::handleData(const CEvent&, void*)
{
LOG((CLOG_DEBUG "start ipc handle data"));
2012-07-02 15:28:23 +00:00
UInt8 code[4];
UInt32 n = m_stream.read(code, 4);
while (n != 0) {
2012-07-02 15:28:23 +00:00
LOG((CLOG_DEBUG "ipc read: %c%c%c%c",
code[0], code[1], code[2], code[3]));
CIpcMessage* m = nullptr;
if (memcmp(code, kIpcMsgLogLine, 4) == 0) {
m = parseLogLine();
}
else if (memcmp(code, kIpcMsgShutdown, 4) == 0) {
m = new CIpcShutdownMessage();
}
else {
2012-07-13 17:34:59 +00:00
LOG((CLOG_ERR "invalid ipc message"));
2012-07-02 15:28:23 +00:00
disconnect();
}
// don't delete with this event; the data is passed to a new event.
CEvent e(m_events->forCIpcServerProxy().messageReceived(), this, NULL, CEvent::kDontFreeData);
e.setDataObject(m);
m_events->addEvent(e);
n = m_stream.read(code, 4);
2012-07-02 15:28:23 +00:00
}
LOG((CLOG_DEBUG "finished ipc handle data"));
2012-07-02 15:28:23 +00:00
}
void
CIpcServerProxy::send(const CIpcMessage& message)
{
LOG((CLOG_DEBUG "ipc write: %d", message.type()));
2012-07-02 15:28:23 +00:00
switch (message.type()) {
case kIpcHello: {
const CIpcHelloMessage& hm = static_cast<const CIpcHelloMessage&>(message);
CProtocolUtil::writef(&m_stream, kIpcMsgHello, hm.clientType());
break;
}
2012-07-02 15:28:23 +00:00
case kIpcCommand: {
const CIpcCommandMessage& cm = static_cast<const CIpcCommandMessage&>(message);
CString command = cm.command();
CProtocolUtil::writef(&m_stream, kIpcMsgCommand, &command);
2012-07-02 15:28:23 +00:00
break;
}
2012-07-02 15:28:23 +00:00
default:
2012-07-13 17:34:59 +00:00
LOG((CLOG_ERR "ipc message not supported: %d", message.type()));
2012-07-02 15:28:23 +00:00
break;
}
}
CIpcLogLineMessage*
2012-07-02 15:28:23 +00:00
CIpcServerProxy::parseLogLine()
{
CString logLine;
CProtocolUtil::readf(&m_stream, kIpcMsgLogLine + 4, &logLine);
// must be deleted by event handler.
return new CIpcLogLineMessage(logLine);
2012-07-02 15:28:23 +00:00
}
void
CIpcServerProxy::disconnect()
{
2012-07-13 17:34:59 +00:00
LOG((CLOG_DEBUG "ipc disconnect, closing stream"));
2012-07-02 15:28:23 +00:00
m_stream.close();
}