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 "CIpcServer.h"
|
2012-06-29 11:33:21 +00:00
|
|
|
#include "Ipc.h"
|
2012-07-01 21:18:21 +00:00
|
|
|
#include "IEventQueue.h"
|
|
|
|
#include "TMethodEventJob.h"
|
|
|
|
#include "CEvent.h"
|
|
|
|
#include "CLog.h"
|
|
|
|
#include "CIpcClientProxy.h"
|
|
|
|
#include "IStream.h"
|
|
|
|
#include "IDataSocket.h"
|
2012-07-02 15:28:23 +00:00
|
|
|
#include "CIpcMessage.h"
|
2012-07-01 21:18:21 +00:00
|
|
|
|
|
|
|
CEvent::Type CIpcServer::s_clientConnectedEvent = CEvent::kUnknown;
|
2012-06-28 07:29:06 +00:00
|
|
|
|
2012-06-29 11:33:21 +00:00
|
|
|
CIpcServer::CIpcServer() :
|
|
|
|
m_address(CNetworkAddress(IPC_HOST, IPC_PORT))
|
2012-06-28 07:29:06 +00:00
|
|
|
{
|
2012-06-29 11:33:21 +00:00
|
|
|
m_address.resolve();
|
2012-07-01 21:18:21 +00:00
|
|
|
|
|
|
|
EVENTQUEUE->adoptHandler(
|
|
|
|
IListenSocket::getConnectingEvent(), &m_socket,
|
|
|
|
new TMethodEventJob<CIpcServer>(
|
2012-07-02 13:45:52 +00:00
|
|
|
this, &CIpcServer::handleClientConnecting));
|
2012-06-28 07:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CIpcServer::~CIpcServer()
|
|
|
|
{
|
2012-07-01 21:18:21 +00:00
|
|
|
EVENTQUEUE->removeHandler(IListenSocket::getConnectingEvent(), &m_socket);
|
2012-06-28 07:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CIpcServer::listen()
|
|
|
|
{
|
2012-06-29 11:33:21 +00:00
|
|
|
m_socket.bind(m_address);
|
2012-06-28 07:29:06 +00:00
|
|
|
}
|
2012-07-01 21:18:21 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
CIpcServer::handleClientConnecting(const CEvent&, void*)
|
|
|
|
{
|
|
|
|
IStream* stream = m_socket.accept();
|
|
|
|
if (stream == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOG((CLOG_NOTE "accepted ipc client connection"));
|
|
|
|
|
|
|
|
// TODO: delete on disconnect
|
|
|
|
CIpcClientProxy* proxy = new CIpcClientProxy(*stream);
|
|
|
|
m_clients.insert(proxy);
|
|
|
|
|
|
|
|
EVENTQUEUE->addEvent(CEvent(getClientConnectedEvent(), this, proxy));
|
|
|
|
}
|
|
|
|
|
|
|
|
CEvent::Type
|
|
|
|
CIpcServer::getClientConnectedEvent()
|
|
|
|
{
|
|
|
|
return EVENTQUEUE->registerTypeOnce(
|
|
|
|
s_clientConnectedEvent, "CIpcServer::clientConnected");
|
|
|
|
}
|
2012-07-02 15:28:23 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
CIpcServer::send(const CIpcMessage& message)
|
|
|
|
{
|
|
|
|
CClientSet::iterator it;
|
|
|
|
for (it = m_clients.begin(); it != m_clients.end(); it++) {
|
|
|
|
(*it)->send(message);
|
|
|
|
}
|
|
|
|
}
|