2012-06-28 07:29:06 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
2016-09-07 14:24:00 +00:00
|
|
|
* Copyright (C) 2012-2016 Symless 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
|
2015-05-03 02:33:52 +00:00
|
|
|
* found in the file LICENSE that should have accompanied this file.
|
2012-06-28 07:29:06 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "ipc/IpcServerProxy.h"
|
|
|
|
|
|
|
|
#include "ipc/IpcMessage.h"
|
|
|
|
#include "ipc/Ipc.h"
|
|
|
|
#include "synergy/ProtocolUtil.h"
|
|
|
|
#include "io/IStream.h"
|
|
|
|
#include "base/TMethodEventJob.h"
|
|
|
|
#include "base/Log.h"
|
2012-06-28 07:29:06 +00:00
|
|
|
|
2013-06-29 14:17:49 +00:00
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// IpcServerProxy
|
2013-06-29 14:17:49 +00:00
|
|
|
//
|
2012-07-02 15:28:23 +00:00
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
IpcServerProxy::IpcServerProxy(synergy::IStream& stream, IEventQueue* events) :
|
2016-12-28 11:50:32 +00:00
|
|
|
m_stream(stream),
|
|
|
|
m_events(events)
|
2012-06-28 07:29:06 +00:00
|
|
|
{
|
2016-12-28 11:50:32 +00:00
|
|
|
m_events->adoptHandler(m_events->forIStream().inputReady(),
|
|
|
|
stream.getEventTarget(),
|
|
|
|
new TMethodEventJob<IpcServerProxy>(
|
|
|
|
this, &IpcServerProxy::handleData));
|
2012-06-28 07:29:06 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
IpcServerProxy::~IpcServerProxy()
|
2012-06-28 07:29:06 +00:00
|
|
|
{
|
2016-12-28 11:50:32 +00:00
|
|
|
m_events->removeHandler(m_events->forIStream().inputReady(),
|
|
|
|
m_stream.getEventTarget());
|
2012-07-02 15:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-11-11 13:51:47 +00:00
|
|
|
IpcServerProxy::handleData(const Event&, void*)
|
2012-07-02 15:28:23 +00:00
|
|
|
{
|
2016-12-28 11:50:32 +00:00
|
|
|
LOG((CLOG_DEBUG "start ipc handle data"));
|
2012-07-02 15:28:23 +00:00
|
|
|
|
2016-12-28 11:50:32 +00:00
|
|
|
UInt8 code[4];
|
|
|
|
UInt32 n = m_stream.read(code, 4);
|
|
|
|
while (n != 0) {
|
2012-07-02 15:28:23 +00:00
|
|
|
|
2016-12-28 11:50:32 +00:00
|
|
|
LOG((CLOG_DEBUG "ipc read: %c%c%c%c",
|
|
|
|
code[0], code[1], code[2], code[3]));
|
|
|
|
|
|
|
|
IpcMessage* m = nullptr;
|
|
|
|
if (memcmp(code, kIpcMsgLogLine, 4) == 0) {
|
|
|
|
m = parseLogLine();
|
|
|
|
}
|
|
|
|
else if (memcmp(code, kIpcMsgShutdown, 4) == 0) {
|
|
|
|
m = new IpcShutdownMessage();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG((CLOG_ERR "invalid ipc message"));
|
|
|
|
disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't delete with this event; the data is passed to a new event.
|
|
|
|
Event e(m_events->forIpcServerProxy().messageReceived(), this, NULL, Event::kDontFreeData);
|
|
|
|
e.setDataObject(m);
|
|
|
|
m_events->addEvent(e);
|
2012-07-10 01:51:51 +00:00
|
|
|
|
2016-12-28 11:50:32 +00:00
|
|
|
n = m_stream.read(code, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG((CLOG_DEBUG "finished ipc handle data"));
|
2012-07-02 15:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-11-11 13:51:47 +00:00
|
|
|
IpcServerProxy::send(const IpcMessage& message)
|
2012-07-02 15:28:23 +00:00
|
|
|
{
|
2016-12-28 11:50:32 +00:00
|
|
|
LOG((CLOG_DEBUG4 "ipc write: %d", message.type()));
|
2012-07-02 15:28:23 +00:00
|
|
|
|
2016-12-28 11:50:32 +00:00
|
|
|
switch (message.type()) {
|
|
|
|
case kIpcHello: {
|
|
|
|
const IpcHelloMessage& hm = static_cast<const IpcHelloMessage&>(message);
|
|
|
|
ProtocolUtil::writef(&m_stream, kIpcMsgHello, hm.clientType());
|
|
|
|
break;
|
|
|
|
}
|
2012-07-05 18:05:35 +00:00
|
|
|
|
2016-12-28 11:50:32 +00:00
|
|
|
case kIpcCommand: {
|
|
|
|
const IpcCommandMessage& cm = static_cast<const IpcCommandMessage&>(message);
|
|
|
|
const String command = cm.command();
|
|
|
|
ProtocolUtil::writef(&m_stream, kIpcMsgCommand, &command);
|
|
|
|
break;
|
|
|
|
}
|
2012-07-02 15:28:23 +00:00
|
|
|
|
2016-12-28 11:50:32 +00:00
|
|
|
default:
|
|
|
|
LOG((CLOG_ERR "ipc message not supported: %d", message.type()));
|
|
|
|
break;
|
|
|
|
}
|
2012-07-02 15:28:23 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
IpcLogLineMessage*
|
|
|
|
IpcServerProxy::parseLogLine()
|
2012-07-02 15:28:23 +00:00
|
|
|
{
|
2016-12-28 11:50:32 +00:00
|
|
|
String logLine;
|
|
|
|
ProtocolUtil::readf(&m_stream, kIpcMsgLogLine + 4, &logLine);
|
|
|
|
|
|
|
|
// must be deleted by event handler.
|
|
|
|
return new IpcLogLineMessage(logLine);
|
2012-07-02 15:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-11-11 13:51:47 +00:00
|
|
|
IpcServerProxy::disconnect()
|
2012-07-02 15:28:23 +00:00
|
|
|
{
|
2016-12-28 11:50:32 +00:00
|
|
|
LOG((CLOG_DEBUG "ipc disconnect, closing stream"));
|
|
|
|
m_stream.close();
|
2012-07-02 15:28:23 +00:00
|
|
|
}
|