2012-06-28 07:29:06 +00:00
|
|
|
/*
|
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
2012-09-04 02:09:56 +00:00
|
|
|
* Copyright (C) 2012 Bolton Software 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
|
|
|
|
|
* 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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "ipc/IpcClient.h"
|
|
|
|
|
#include "ipc/Ipc.h"
|
|
|
|
|
#include "ipc/IpcServerProxy.h"
|
|
|
|
|
#include "ipc/IpcMessage.h"
|
|
|
|
|
#include "base/TMethodEventJob.h"
|
2012-07-05 18:05:35 +00:00
|
|
|
|
2013-06-29 14:17:49 +00:00
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// IpcClient
|
2013-06-29 14:17:49 +00:00
|
|
|
//
|
|
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
IpcClient::IpcClient(IEventQueue* events, SocketMultiplexer* socketMultiplexer) :
|
|
|
|
|
m_serverAddress(NetworkAddress(IPC_HOST, IPC_PORT)),
|
2013-07-16 19:02:30 +00:00
|
|
|
m_socket(events, socketMultiplexer),
|
2014-02-25 15:03:43 +00:00
|
|
|
m_server(nullptr),
|
2013-06-29 14:17:49 +00:00
|
|
|
m_events(events)
|
2012-07-11 12:07:13 +00:00
|
|
|
{
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
IpcClient::IpcClient(IEventQueue* events, SocketMultiplexer* socketMultiplexer, int port) :
|
|
|
|
|
m_serverAddress(NetworkAddress(IPC_HOST, port)),
|
2013-07-16 19:02:30 +00:00
|
|
|
m_socket(events, socketMultiplexer),
|
2014-02-25 15:03:43 +00:00
|
|
|
m_server(nullptr),
|
2013-06-29 14:17:49 +00:00
|
|
|
m_events(events)
|
2012-07-11 12:07:13 +00:00
|
|
|
{
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2014-11-11 13:51:47 +00:00
|
|
|
IpcClient::init()
|
2012-06-28 07:29:06 +00:00
|
|
|
{
|
2012-06-29 11:33:21 +00:00
|
|
|
m_serverAddress.resolve();
|
2012-06-28 07:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
IpcClient::~IpcClient()
|
2012-06-28 07:29:06 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2014-11-11 13:51:47 +00:00
|
|
|
IpcClient::connect()
|
2012-06-28 07:29:06 +00:00
|
|
|
{
|
2013-06-29 14:17:49 +00:00
|
|
|
m_events->adoptHandler(
|
|
|
|
|
m_events->forIDataSocket().connected(), m_socket.getEventTarget(),
|
2014-11-11 13:51:47 +00:00
|
|
|
new TMethodEventJob<IpcClient>(
|
|
|
|
|
this, &IpcClient::handleConnected));
|
2012-07-10 11:23:08 +00:00
|
|
|
|
2012-06-29 11:33:21 +00:00
|
|
|
m_socket.connect(m_serverAddress);
|
2014-11-11 13:51:47 +00:00
|
|
|
m_server = new IpcServerProxy(m_socket, m_events);
|
2012-07-10 01:51:51 +00:00
|
|
|
|
2013-06-29 14:17:49 +00:00
|
|
|
m_events->adoptHandler(
|
2014-11-11 13:51:47 +00:00
|
|
|
m_events->forIpcServerProxy().messageReceived(), m_server,
|
|
|
|
|
new TMethodEventJob<IpcClient>(
|
|
|
|
|
this, &IpcClient::handleMessageReceived));
|
2012-07-10 01:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2014-11-11 13:51:47 +00:00
|
|
|
IpcClient::disconnect()
|
2012-07-10 01:51:51 +00:00
|
|
|
{
|
2013-06-29 14:17:49 +00:00
|
|
|
m_events->removeHandler(m_events->forIDataSocket().connected(), m_socket.getEventTarget());
|
2014-11-11 13:51:47 +00:00
|
|
|
m_events->removeHandler(m_events->forIpcServerProxy().messageReceived(), m_server);
|
2012-07-10 11:23:08 +00:00
|
|
|
|
2012-07-10 01:51:51 +00:00
|
|
|
m_server->disconnect();
|
|
|
|
|
delete m_server;
|
|
|
|
|
m_server = nullptr;
|
2012-06-28 07:29:06 +00:00
|
|
|
}
|
2012-07-02 13:45:52 +00:00
|
|
|
|
|
|
|
|
void
|
2014-11-11 13:51:47 +00:00
|
|
|
IpcClient::send(const IpcMessage& message)
|
2012-07-02 13:45:52 +00:00
|
|
|
{
|
2012-07-10 01:51:51 +00:00
|
|
|
assert(m_server != nullptr);
|
2012-07-02 15:28:23 +00:00
|
|
|
m_server->send(message);
|
2012-07-02 13:45:52 +00:00
|
|
|
}
|
2012-07-05 18:05:35 +00:00
|
|
|
|
|
|
|
|
void
|
2014-11-11 13:51:47 +00:00
|
|
|
IpcClient::handleConnected(const Event&, void*)
|
2012-07-05 18:05:35 +00:00
|
|
|
{
|
2014-11-11 13:51:47 +00:00
|
|
|
m_events->addEvent(Event(
|
|
|
|
|
m_events->forIpcClient().connected(), this, m_server, Event::kDontFreeData));
|
2012-07-05 18:05:35 +00:00
|
|
|
|
2014-11-11 13:51:47 +00:00
|
|
|
IpcHelloMessage message(kIpcClientNode);
|
2012-07-05 18:05:35 +00:00
|
|
|
send(message);
|
|
|
|
|
}
|
2012-07-10 01:51:51 +00:00
|
|
|
|
|
|
|
|
void
|
2014-11-11 13:51:47 +00:00
|
|
|
IpcClient::handleMessageReceived(const Event& e, void*)
|
2012-07-10 01:51:51 +00:00
|
|
|
{
|
2014-11-11 13:51:47 +00:00
|
|
|
Event event(m_events->forIpcClient().messageReceived(), this);
|
2012-07-10 01:51:51 +00:00
|
|
|
event.setDataObject(e.getDataObject());
|
2013-06-29 14:17:49 +00:00
|
|
|
m_events->addEvent(event);
|
2012-07-10 01:51:51 +00:00
|
|
|
}
|