deskflow/src/lib/ipc/CIpcServerProxy.cpp

145 lines
3.4 KiB
C++
Raw Normal View History

2012-06-28 07:29:06 +00:00
/*
* 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 "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
2012-07-02 15:28:23 +00:00
CEvent::Type CIpcServerProxy::s_messageReceivedEvent = CEvent::kUnknown;
CIpcServerProxy::CIpcServerProxy(synergy::IStream& stream) :
2012-07-02 15:28:23 +00:00
m_stream(stream)
2012-06-28 07:29:06 +00:00
{
2012-07-02 15:28:23 +00:00
EVENTQUEUE->adoptHandler(m_stream.getInputReadyEvent(),
stream.getEventTarget(),
new TMethodEventJob<CIpcServerProxy>(
this, &CIpcServerProxy::handleData));
2012-06-28 07:29:06 +00:00
}
CIpcServerProxy::~CIpcServerProxy()
{
2012-07-02 15:28:23 +00:00
EVENTQUEUE->removeHandler(m_stream.getInputReadyEvent(),
m_stream.getEventTarget());
m_stream.close();
2012-07-02 15:28:23 +00:00
}
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];
2012-07-02 15:28:23 +00:00
while (n != 0) {
2012-07-02 15:28:23 +00:00
LOG((CLOG_DEBUG "ipc server proxy read: %d", code));
CIpcMessage* m = nullptr;
switch (code) {
2012-07-02 15:28:23 +00:00
case kIpcLogLine:
m = parseLogLine();
2012-07-02 15:28:23 +00:00
break;
case kIpcShutdown:
m = new CIpcShutdownMessage();
break;
2012-07-02 15:28:23 +00:00
default:
disconnect();
return;
}
// 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];
2012-07-02 15:28:23 +00:00
}
LOG((CLOG_DEBUG "finished ipc server proxy handle data"));
2012-07-02 15:28:23 +00:00
}
void
CIpcServerProxy::send(const CIpcMessage& message)
{
LOG((CLOG_DEBUG "ipc server proxy write: %d", message.type()));
2012-07-02 15:28:23 +00:00
CProtocolUtil::writef(&m_stream, "%1i", 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, "%1i", 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();
const char* data = command.c_str();
int len = strlen(data);
2012-07-02 15:28:23 +00:00
CProtocolUtil::writef(&m_stream, "%2i", len);
m_stream.write(data, len);
2012-07-02 15:28:23 +00:00
break;
}
2012-07-02 15:28:23 +00:00
default:
LOG((CLOG_ERR "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()
{
2012-07-03 17:33:19 +00:00
int len = 0;
CProtocolUtil::readf(&m_stream, "%4i", &len);
2012-07-02 15:28:23 +00:00
char* buffer = new char[len];
2012-07-03 17:33:19 +00:00
m_stream.read(buffer, len);
CString s(buffer, len);
delete buffer;
// must be deleted by event handler.
return new CIpcLogLineMessage(s);
2012-07-02 15:28:23 +00:00
}
void
CIpcServerProxy::disconnect()
{
LOG((CLOG_NOTE "disconnect, closing stream"));
m_stream.close();
}
CEvent::Type
CIpcServerProxy::getMessageReceivedEvent()
{
return EVENTQUEUE->registerTypeOnce(
s_messageReceivedEvent, "CIpcServerProxy::messageReceived");
2012-06-28 07:29:06 +00:00
}