2012-06-28 07:29:06 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2012 Symless Ltd.
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2012-06-28 07:29:06 +00:00
|
|
|
*/
|
|
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "ipc/IpcClientProxy.h"
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
#include "base/Log.h"
|
|
|
|
|
#include "base/TMethodEventJob.h"
|
2024-08-09 13:03:48 +00:00
|
|
|
#include "common/ipc.h"
|
2024-09-17 19:00:25 +00:00
|
|
|
#include "deskflow/ProtocolUtil.h"
|
2024-07-02 19:07:06 +00:00
|
|
|
#include "io/IStream.h"
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "ipc/IpcMessage.h"
|
2022-02-11 09:28:39 +00:00
|
|
|
#include "ipc/IpcSettingMessage.h"
|
2012-07-02 13:45:52 +00:00
|
|
|
|
2013-06-29 14:17:49 +00:00
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// IpcClientProxy
|
2013-06-29 14:17:49 +00:00
|
|
|
//
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
IpcClientProxy::IpcClientProxy(deskflow::IStream &stream, IEventQueue *events) : m_stream(stream), m_events(events)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
m_events->adoptHandler(
|
|
|
|
|
m_events->forIStream().inputReady(), stream.getEventTarget(),
|
2024-10-17 18:04:35 +00:00
|
|
|
new TMethodEventJob<IpcClientProxy>(this, &IpcClientProxy::handleData)
|
|
|
|
|
);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
2024-07-16 13:36:44 +00:00
|
|
|
m_events->adoptHandler(
|
|
|
|
|
m_events->forIStream().outputError(), stream.getEventTarget(),
|
2024-10-17 18:04:35 +00:00
|
|
|
new TMethodEventJob<IpcClientProxy>(this, &IpcClientProxy::handleWriteError)
|
|
|
|
|
);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
2024-07-16 13:36:44 +00:00
|
|
|
m_events->adoptHandler(
|
|
|
|
|
m_events->forIStream().inputShutdown(), stream.getEventTarget(),
|
2024-10-17 18:04:35 +00:00
|
|
|
new TMethodEventJob<IpcClientProxy>(this, &IpcClientProxy::handleDisconnect)
|
|
|
|
|
);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
2024-07-16 13:36:44 +00:00
|
|
|
m_events->adoptHandler(
|
|
|
|
|
m_events->forIStream().outputShutdown(), stream.getEventTarget(),
|
2024-10-17 18:04:35 +00:00
|
|
|
new TMethodEventJob<IpcClientProxy>(this, &IpcClientProxy::handleWriteError)
|
|
|
|
|
);
|
2012-06-28 07:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
IpcClientProxy::~IpcClientProxy()
|
|
|
|
|
{
|
|
|
|
|
m_events->removeHandler(m_events->forIStream().inputReady(), m_stream.getEventTarget());
|
|
|
|
|
m_events->removeHandler(m_events->forIStream().outputError(), m_stream.getEventTarget());
|
|
|
|
|
m_events->removeHandler(m_events->forIStream().inputShutdown(), m_stream.getEventTarget());
|
|
|
|
|
m_events->removeHandler(m_events->forIStream().outputShutdown(), m_stream.getEventTarget());
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
// don't delete the stream while it's being used.
|
|
|
|
|
ARCH->lockMutex(m_readMutex);
|
|
|
|
|
ARCH->lockMutex(m_writeMutex);
|
|
|
|
|
delete &m_stream;
|
|
|
|
|
ARCH->unlockMutex(m_readMutex);
|
|
|
|
|
ARCH->unlockMutex(m_writeMutex);
|
|
|
|
|
|
|
|
|
|
ARCH->closeMutex(m_readMutex);
|
|
|
|
|
ARCH->closeMutex(m_writeMutex);
|
2012-07-06 12:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void IpcClientProxy::handleDisconnect(const Event &, void *)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
disconnect();
|
|
|
|
|
LOG((CLOG_DEBUG "ipc client disconnected"));
|
2012-07-06 12:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void IpcClientProxy::handleWriteError(const Event &, void *)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
disconnect();
|
|
|
|
|
LOG((CLOG_DEBUG "ipc client write error"));
|
2012-07-02 13:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void IpcClientProxy::handleData(const Event &, void *)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
// don't allow the dtor to destroy the stream while we're using it.
|
|
|
|
|
ArchMutexLock lock(m_readMutex);
|
|
|
|
|
|
|
|
|
|
LOG((CLOG_DEBUG "start ipc handle data"));
|
|
|
|
|
|
2025-01-11 00:09:43 +00:00
|
|
|
uint8_t code[4];
|
2025-01-11 02:13:19 +00:00
|
|
|
uint32_t n = m_stream.read(code, 4);
|
2024-07-02 19:07:06 +00:00
|
|
|
while (n != 0) {
|
|
|
|
|
|
|
|
|
|
LOG((CLOG_DEBUG "ipc read: %c%c%c%c", code[0], code[1], code[2], code[3]));
|
|
|
|
|
|
|
|
|
|
IpcMessage *m = nullptr;
|
|
|
|
|
if (memcmp(code, kIpcMsgHello, 4) == 0) {
|
|
|
|
|
m = parseHello();
|
|
|
|
|
} else if (memcmp(code, kIpcMsgCommand, 4) == 0) {
|
|
|
|
|
m = parseCommand();
|
|
|
|
|
} else if (memcmp(code, kIpcMsgSetting, 4) == 0) {
|
|
|
|
|
m = parseSetting();
|
|
|
|
|
} else {
|
|
|
|
|
LOG((CLOG_ERR "invalid ipc message"));
|
|
|
|
|
disconnect();
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// don't delete with this event; the data is passed to a new event.
|
2024-10-17 18:04:35 +00:00
|
|
|
Event e(m_events->forIpcClientProxy().messageReceived(), this, NULL, Event::kDontFreeData);
|
2024-07-02 19:07:06 +00:00
|
|
|
e.setDataObject(m);
|
|
|
|
|
m_events->addEvent(e);
|
|
|
|
|
|
|
|
|
|
n = m_stream.read(code, 4);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG((CLOG_DEBUG "finished ipc handle data"));
|
2012-07-02 13:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void IpcClientProxy::send(const IpcMessage &message)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +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.
|
|
|
|
|
// also, don't allow the dtor to destroy the stream while we're using it.
|
|
|
|
|
ArchMutexLock lock(m_writeMutex);
|
|
|
|
|
|
|
|
|
|
LOG((CLOG_DEBUG4 "ipc write: %d", message.type()));
|
|
|
|
|
|
|
|
|
|
switch (message.type()) {
|
2024-07-18 07:04:39 +00:00
|
|
|
case IpcMessageType::LogLine: {
|
2024-10-17 18:04:35 +00:00
|
|
|
const IpcLogLineMessage &llm = static_cast<const IpcLogLineMessage &>(message);
|
2025-01-06 00:55:07 +00:00
|
|
|
const std::string logLine = llm.logLine();
|
2024-07-02 19:07:06 +00:00
|
|
|
ProtocolUtil::writef(&m_stream, kIpcMsgLogLine, &logLine);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-18 07:04:39 +00:00
|
|
|
case IpcMessageType::Shutdown:
|
2024-07-02 19:07:06 +00:00
|
|
|
ProtocolUtil::writef(&m_stream, kIpcMsgShutdown);
|
|
|
|
|
break;
|
|
|
|
|
|
2024-07-18 07:04:39 +00:00
|
|
|
case IpcMessageType::HelloBack:
|
2024-07-02 19:07:06 +00:00
|
|
|
ProtocolUtil::writef(&m_stream, kIpcMsgHelloBack);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
LOG((CLOG_ERR "ipc message not supported: %d", message.type()));
|
|
|
|
|
break;
|
|
|
|
|
}
|
2012-07-02 15:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
IpcHelloMessage *IpcClientProxy::parseHello()
|
|
|
|
|
{
|
2025-01-11 00:09:43 +00:00
|
|
|
uint8_t type;
|
2024-07-02 19:07:06 +00:00
|
|
|
ProtocolUtil::readf(&m_stream, kIpcMsgHello + 4, &type);
|
2012-07-11 18:06:10 +00:00
|
|
|
|
2024-07-18 07:04:39 +00:00
|
|
|
m_clientType = static_cast<IpcClientType>(type);
|
2012-07-10 01:51:51 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// must be deleted by event handler.
|
|
|
|
|
return new IpcHelloMessage(m_clientType);
|
2012-07-05 18:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
IpcCommandMessage *IpcClientProxy::parseCommand()
|
|
|
|
|
{
|
2025-01-06 00:55:07 +00:00
|
|
|
std::string command;
|
2025-01-11 00:09:43 +00:00
|
|
|
uint8_t elevate;
|
2024-07-02 19:07:06 +00:00
|
|
|
ProtocolUtil::readf(&m_stream, kIpcMsgCommand + 4, &command, &elevate);
|
2012-07-02 13:45:52 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// must be deleted by event handler.
|
|
|
|
|
return new IpcCommandMessage(command, elevate != 0);
|
2012-07-02 13:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
IpcSettingMessage *IpcClientProxy::parseSetting() const
|
|
|
|
|
{
|
2025-01-06 00:55:07 +00:00
|
|
|
std::string name;
|
|
|
|
|
std::string value;
|
2022-02-11 09:28:39 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
ProtocolUtil::readf(&m_stream, kIpcMsgSetting + 4, &name, &value);
|
2022-02-11 09:28:39 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// must be deleted by event handler.
|
|
|
|
|
return new IpcSettingMessage(name, value);
|
2022-02-11 09:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void IpcClientProxy::disconnect()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
LOG((CLOG_DEBUG "ipc disconnect, closing stream"));
|
|
|
|
|
m_disconnecting = true;
|
|
|
|
|
m_stream.close();
|
|
|
|
|
m_events->addEvent(Event(m_events->forIpcClientProxy().disconnected(), this));
|
2012-07-06 12:27:22 +00:00
|
|
|
}
|