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-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-05 18:05:35 +00:00
|
|
|
m_enableLog(false),
|
2012-07-06 12:27:22 +00:00
|
|
|
m_clientType(kIpcClientUnknown),
|
|
|
|
m_disconnecting(false)
|
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));
|
|
|
|
|
|
|
|
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 12:27:22 +00:00
|
|
|
EVENTQUEUE->removeHandler(
|
|
|
|
m_stream.getInputShutdownEvent(), m_stream.getEventTarget());
|
|
|
|
EVENTQUEUE->removeHandler(
|
|
|
|
m_stream.getOutputShutdownEvent(), m_stream.getEventTarget());
|
|
|
|
|
|
|
|
delete &m_stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
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*)
|
|
|
|
{
|
|
|
|
UInt8 code[1];
|
|
|
|
UInt32 n = m_stream.read(code, 1);
|
|
|
|
while (n != 0) {
|
2012-07-03 14:15:05 +00:00
|
|
|
UInt8 type = code[0];
|
2012-07-02 13:45:52 +00:00
|
|
|
|
|
|
|
CIpcMessage* m = new CIpcMessage();
|
2012-07-03 14:15:05 +00:00
|
|
|
m->m_type = type;
|
2012-07-06 12:27:22 +00:00
|
|
|
m->m_source = this;
|
2012-07-02 13:45:52 +00:00
|
|
|
|
2012-07-03 14:15:05 +00:00
|
|
|
if (m_enableLog) {
|
|
|
|
LOG((CLOG_DEBUG "ipc client proxy read: %d", code[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
2012-07-05 18:05:35 +00:00
|
|
|
case kIpcHello:
|
|
|
|
parseHello();
|
|
|
|
break;
|
|
|
|
|
2012-07-02 13:45:52 +00:00
|
|
|
case kIpcCommand:
|
|
|
|
m->m_data = parseCommand();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
delete m;
|
|
|
|
disconnect();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// event deletes data.
|
|
|
|
EVENTQUEUE->addEvent(CEvent(getMessageReceivedEvent(), this, m));
|
|
|
|
|
|
|
|
n = m_stream.read(code, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-02 15:28:23 +00:00
|
|
|
void
|
|
|
|
CIpcClientProxy::send(const CIpcMessage& message)
|
|
|
|
{
|
2012-07-03 14:15:05 +00:00
|
|
|
if (m_enableLog) {
|
|
|
|
LOG((CLOG_DEBUG "ipc client proxy write: %d", message.m_type));
|
|
|
|
}
|
2012-07-02 15:28:23 +00:00
|
|
|
|
|
|
|
UInt8 code[1];
|
|
|
|
code[0] = message.m_type;
|
|
|
|
m_stream.write(code, 1);
|
|
|
|
|
|
|
|
switch (message.m_type) {
|
|
|
|
case kIpcLogLine: {
|
2012-07-05 18:05:35 +00:00
|
|
|
CString* s = (CString*)message.m_data;
|
|
|
|
const char* data = s->c_str();
|
|
|
|
|
|
|
|
int len = strlen(data);
|
|
|
|
CProtocolUtil::writef(&m_stream, "%2i", len);
|
2012-07-02 15:28:23 +00:00
|
|
|
|
2012-07-05 18:05:35 +00:00
|
|
|
m_stream.write(data, len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case kIpcShutdown:
|
|
|
|
// no data.
|
2012-07-02 15:28:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2012-07-03 14:15:05 +00:00
|
|
|
if (m_enableLog) {
|
|
|
|
LOG((CLOG_ERR "message not supported: %d", message.m_type));
|
|
|
|
}
|
2012-07-02 15:28:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-05 18:05:35 +00:00
|
|
|
void
|
|
|
|
CIpcClientProxy::parseHello()
|
|
|
|
{
|
|
|
|
UInt8 buffer[1];
|
|
|
|
m_stream.read(buffer, 1);
|
|
|
|
m_clientType = static_cast<EIpcClientType>(buffer[0]);
|
|
|
|
}
|
|
|
|
|
2012-07-02 13:45:52 +00:00
|
|
|
void*
|
|
|
|
CIpcClientProxy::parseCommand()
|
|
|
|
{
|
2012-07-03 17:33:19 +00:00
|
|
|
int len = 0;
|
|
|
|
CProtocolUtil::readf(&m_stream, "%2i", &len);
|
2012-07-02 13:45:52 +00:00
|
|
|
|
2012-07-03 17:33:19 +00:00
|
|
|
UInt8* buffer = new UInt8[len];
|
|
|
|
m_stream.read(buffer, len);
|
2012-07-02 13:45:52 +00:00
|
|
|
|
2012-07-05 18:05:35 +00:00
|
|
|
// delete by event cleanup.
|
2012-07-03 17:33:19 +00:00
|
|
|
return new CString((const char*)buffer, len);
|
2012-07-02 13:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CIpcClientProxy::disconnect()
|
|
|
|
{
|
2012-07-06 12:27:22 +00:00
|
|
|
m_disconnecting = true;
|
|
|
|
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");
|
|
|
|
}
|