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 "CIpcClientProxy.h"
|
2012-07-01 21:18:21 +00:00
|
|
|
#include "IStream.h"
|
2012-07-02 13:45:52 +00:00
|
|
|
#include "TMethodEventJob.h"
|
|
|
|
#include "Ipc.h"
|
|
|
|
#include "CLog.h"
|
|
|
|
#include "CIpcMessage.h"
|
2012-07-03 17:33:19 +00:00
|
|
|
#include "CProtocolUtil.h"
|
2012-07-06 14:46:46 +00:00
|
|
|
#include "CArch.h"
|
2012-07-02 13:45:52 +00:00
|
|
|
|
|
|
|
CEvent::Type CIpcClientProxy::s_messageReceivedEvent = CEvent::kUnknown;
|
2012-07-06 12:27:22 +00:00
|
|
|
CEvent::Type CIpcClientProxy::s_disconnectedEvent = CEvent::kUnknown;
|
2012-06-28 07:29:06 +00:00
|
|
|
|
2012-07-05 18:05:35 +00:00
|
|
|
CIpcClientProxy::CIpcClientProxy(synergy::IStream& stream) :
|
2012-07-03 14:15:05 +00:00
|
|
|
m_stream(stream),
|
2012-07-06 12:27:22 +00:00
|
|
|
m_clientType(kIpcClientUnknown),
|
2012-07-09 12:09:24 +00:00
|
|
|
m_disconnecting(false),
|
|
|
|
m_readMutex(ARCH->newMutex()),
|
|
|
|
m_writeMutex(ARCH->newMutex())
|
2012-06-28 07:29:06 +00:00
|
|
|
{
|
2012-07-05 18:05:35 +00:00
|
|
|
EVENTQUEUE->adoptHandler(
|
|
|
|
m_stream.getInputReadyEvent(), stream.getEventTarget(),
|
2012-07-02 13:45:52 +00:00
|
|
|
new TMethodEventJob<CIpcClientProxy>(
|
2012-07-06 12:27:22 +00:00
|
|
|
this, &CIpcClientProxy::handleData));
|
|
|
|
|
2012-07-06 14:46:46 +00:00
|
|
|
EVENTQUEUE->adoptHandler(
|
|
|
|
m_stream.getOutputErrorEvent(), stream.getEventTarget(),
|
|
|
|
new TMethodEventJob<CIpcClientProxy>(
|
|
|
|
this, &CIpcClientProxy::handleWriteError));
|
|
|
|
|
2012-07-06 12:27:22 +00:00
|
|
|
EVENTQUEUE->adoptHandler(
|
|
|
|
m_stream.getInputShutdownEvent(), stream.getEventTarget(),
|
|
|
|
new TMethodEventJob<CIpcClientProxy>(
|
|
|
|
this, &CIpcClientProxy::handleDisconnect));
|
|
|
|
|
|
|
|
EVENTQUEUE->adoptHandler(
|
|
|
|
m_stream.getOutputShutdownEvent(), stream.getEventTarget(),
|
|
|
|
new TMethodEventJob<CIpcClientProxy>(
|
|
|
|
this, &CIpcClientProxy::handleWriteError));
|
2012-06-28 07:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CIpcClientProxy::~CIpcClientProxy()
|
|
|
|
{
|
2012-07-05 18:05:35 +00:00
|
|
|
EVENTQUEUE->removeHandler(
|
|
|
|
m_stream.getInputReadyEvent(), m_stream.getEventTarget());
|
2012-07-06 14:46:46 +00:00
|
|
|
EVENTQUEUE->removeHandler(
|
|
|
|
m_stream.getOutputErrorEvent(), m_stream.getEventTarget());
|
2012-07-06 12:27:22 +00:00
|
|
|
EVENTQUEUE->removeHandler(
|
|
|
|
m_stream.getInputShutdownEvent(), m_stream.getEventTarget());
|
|
|
|
EVENTQUEUE->removeHandler(
|
|
|
|
m_stream.getOutputShutdownEvent(), m_stream.getEventTarget());
|
2012-07-08 17:49:45 +00:00
|
|
|
|
|
|
|
// don't delete the stream while it's being used.
|
2012-07-09 12:09:24 +00:00
|
|
|
ARCH->lockMutex(m_readMutex);
|
|
|
|
ARCH->lockMutex(m_writeMutex);
|
2012-07-06 12:27:22 +00:00
|
|
|
delete &m_stream;
|
2012-07-09 12:09:24 +00:00
|
|
|
ARCH->unlockMutex(m_readMutex);
|
|
|
|
ARCH->unlockMutex(m_writeMutex);
|
|
|
|
|
|
|
|
ARCH->closeMutex(m_readMutex);
|
|
|
|
ARCH->closeMutex(m_writeMutex);
|
2012-07-06 12:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CIpcClientProxy::handleDisconnect(const CEvent&, void*)
|
|
|
|
{
|
|
|
|
disconnect();
|
|
|
|
LOG((CLOG_DEBUG "ipc client disconnected"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CIpcClientProxy::handleWriteError(const CEvent&, void*)
|
|
|
|
{
|
|
|
|
disconnect();
|
|
|
|
LOG((CLOG_DEBUG "ipc client write error"));
|
2012-07-02 13:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CIpcClientProxy::handleData(const CEvent&, void*)
|
|
|
|
{
|
2012-07-08 17:49:45 +00:00
|
|
|
// don't allow the dtor to destroy the stream while we're using it.
|
2012-07-09 12:09:24 +00:00
|
|
|
CArchMutexLock lock(m_readMutex);
|
2012-07-08 17:49:45 +00:00
|
|
|
|
2012-07-11 18:06:10 +00:00
|
|
|
LOG((CLOG_DEBUG "start ipc handle data"));
|
|
|
|
|
|
|
|
UInt8 code[4];
|
|
|
|
UInt32 n = m_stream.read(code, 4);
|
2012-07-02 13:45:52 +00:00
|
|
|
while (n != 0) {
|
|
|
|
|
2012-07-11 18:06:10 +00:00
|
|
|
LOG((CLOG_DEBUG "ipc read: %c%c%c%c",
|
|
|
|
code[0], code[1], code[2], code[3]));
|
|
|
|
|
2012-07-10 01:51:51 +00:00
|
|
|
CIpcMessage* m = nullptr;
|
2012-07-11 18:06:10 +00:00
|
|
|
if (memcmp(code, kIpcMsgHello, 4) == 0) {
|
2012-07-10 01:51:51 +00:00
|
|
|
m = parseHello();
|
2012-07-11 18:06:10 +00:00
|
|
|
}
|
|
|
|
else if (memcmp(code, kIpcMsgCommand, 4) == 0) {
|
2012-07-10 01:51:51 +00:00
|
|
|
m = parseCommand();
|
2012-07-11 18:06:10 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-07-13 17:34:59 +00:00
|
|
|
LOG((CLOG_ERR "invalid ipc message"));
|
2012-07-02 13:45:52 +00:00
|
|
|
disconnect();
|
|
|
|
}
|
2012-07-11 18:06:10 +00:00
|
|
|
|
2012-07-10 01:51:51 +00:00
|
|
|
// 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);
|
2012-07-11 18:06:10 +00:00
|
|
|
|
|
|
|
n = m_stream.read(code, 4);
|
2012-07-02 13:45:52 +00:00
|
|
|
}
|
2012-07-11 18:06:10 +00:00
|
|
|
|
|
|
|
LOG((CLOG_DEBUG "finished ipc handle data"));
|
2012-07-02 13:45:52 +00:00
|
|
|
}
|
|
|
|
|
2012-07-02 15:28:23 +00:00
|
|
|
void
|
|
|
|
CIpcClientProxy::send(const CIpcMessage& message)
|
|
|
|
{
|
2012-07-06 22:17:26 +00:00
|
|
|
// don't allow other threads to write until we've finished the entire
|
|
|
|
// message. stream write is locked, but only for that single write.
|
2012-07-08 17:49:45 +00:00
|
|
|
// also, don't allow the dtor to destroy the stream while we're using it.
|
2012-07-09 12:09:24 +00:00
|
|
|
CArchMutexLock lock(m_writeMutex);
|
2012-07-06 22:17:26 +00:00
|
|
|
|
2012-07-11 18:06:10 +00:00
|
|
|
LOG((CLOG_DEBUG "ipc write: %d", message.type()));
|
2012-07-06 14:46:46 +00:00
|
|
|
|
2012-07-10 01:51:51 +00:00
|
|
|
switch (message.type()) {
|
2012-07-06 16:18:21 +00:00
|
|
|
case kIpcLogLine: {
|
2012-07-10 01:51:51 +00:00
|
|
|
const CIpcLogLineMessage& llm = static_cast<const CIpcLogLineMessage&>(message);
|
|
|
|
CString logLine = llm.logLine();
|
2012-07-11 18:06:10 +00:00
|
|
|
CProtocolUtil::writef(&m_stream, kIpcMsgLogLine, &logLine);
|
2012-07-06 16:18:21 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-07-05 18:05:35 +00:00
|
|
|
|
2012-07-06 16:18:21 +00:00
|
|
|
case kIpcShutdown:
|
2012-07-11 18:06:10 +00:00
|
|
|
CProtocolUtil::writef(&m_stream, kIpcMsgShutdown);
|
2012-07-06 16:18:21 +00:00
|
|
|
break;
|
2012-07-02 15:28:23 +00:00
|
|
|
|
2012-07-06 16:18:21 +00:00
|
|
|
default:
|
2012-07-13 17:34:59 +00:00
|
|
|
LOG((CLOG_ERR "ipc message not supported: %d", message.type()));
|
2012-07-06 16:18:21 +00:00
|
|
|
break;
|
2012-07-02 15:28:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-10 01:51:51 +00:00
|
|
|
CIpcHelloMessage*
|
2012-07-05 18:05:35 +00:00
|
|
|
CIpcClientProxy::parseHello()
|
|
|
|
{
|
2012-07-11 18:06:10 +00:00
|
|
|
UInt8 type;
|
|
|
|
CProtocolUtil::readf(&m_stream, kIpcMsgHello + 4, &type);
|
|
|
|
|
|
|
|
m_clientType = static_cast<EIpcClientType>(type);
|
2012-07-10 01:51:51 +00:00
|
|
|
|
|
|
|
// must be deleted by event handler.
|
|
|
|
return new CIpcHelloMessage(m_clientType);
|
2012-07-05 18:05:35 +00:00
|
|
|
}
|
|
|
|
|
2012-07-10 01:51:51 +00:00
|
|
|
CIpcCommandMessage*
|
2012-07-02 13:45:52 +00:00
|
|
|
CIpcClientProxy::parseCommand()
|
|
|
|
{
|
2012-07-11 18:06:10 +00:00
|
|
|
CString command;
|
2012-07-28 02:59:20 +00:00
|
|
|
UInt8 elevate;
|
|
|
|
CProtocolUtil::readf(&m_stream, kIpcMsgCommand + 4, &command, &elevate);
|
2012-07-02 13:45:52 +00:00
|
|
|
|
2012-07-10 01:51:51 +00:00
|
|
|
// must be deleted by event handler.
|
2012-07-28 02:59:20 +00:00
|
|
|
return new CIpcCommandMessage(command, elevate != 0);
|
2012-07-02 13:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CIpcClientProxy::disconnect()
|
|
|
|
{
|
2012-07-13 17:34:59 +00:00
|
|
|
LOG((CLOG_DEBUG "ipc disconnect, closing stream"));
|
2012-07-06 12:27:22 +00:00
|
|
|
m_disconnecting = true;
|
2012-07-13 17:08:00 +00:00
|
|
|
m_stream.close();
|
2012-07-06 12:27:22 +00:00
|
|
|
EVENTQUEUE->addEvent(CEvent(getDisconnectedEvent(), this));
|
2012-07-02 13:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CEvent::Type
|
|
|
|
CIpcClientProxy::getMessageReceivedEvent()
|
|
|
|
{
|
|
|
|
return EVENTQUEUE->registerTypeOnce(
|
|
|
|
s_messageReceivedEvent, "CIpcClientProxy::messageReceived");
|
2012-06-28 07:29:06 +00:00
|
|
|
}
|
2012-07-06 12:27:22 +00:00
|
|
|
|
|
|
|
CEvent::Type
|
|
|
|
CIpcClientProxy::getDisconnectedEvent()
|
|
|
|
{
|
|
|
|
return EVENTQUEUE->registerTypeOnce(
|
|
|
|
s_disconnectedEvent, "CIpcClientProxy::disconnected");
|
|
|
|
}
|