diff --git a/src/lib/base/CEventQueue.cpp b/src/lib/base/CEventQueue.cpp index 639e8bac..63e41c6a 100644 --- a/src/lib/base/CEventQueue.cpp +++ b/src/lib/base/CEventQueue.cpp @@ -54,6 +54,18 @@ CEventQueue::~CEventQueue() setInstance(NULL); } +void +CEventQueue::loop() +{ + CEvent event; + getEvent(event); + while (event.getType() != CEvent::kQuit) { + dispatchEvent(event); + CEvent::deleteData(event); + getEvent(event); + } +} + CEvent::Type CEventQueue::registerType(const char* name) { diff --git a/src/lib/base/CEventQueue.h b/src/lib/base/CEventQueue.h index f23bd563..2c694fa2 100644 --- a/src/lib/base/CEventQueue.h +++ b/src/lib/base/CEventQueue.h @@ -37,6 +37,7 @@ public: virtual ~CEventQueue(); // IEventQueue overrides + virtual void loop(); virtual void adoptBuffer(IEventQueueBuffer*); virtual bool getEvent(CEvent& event, double timeout = -1.0); virtual bool dispatchEvent(const CEvent& event); diff --git a/src/lib/base/IEventQueue.h b/src/lib/base/IEventQueue.h index edb7a691..2a5dc91f 100644 --- a/src/lib/base/IEventQueue.h +++ b/src/lib/base/IEventQueue.h @@ -49,6 +49,12 @@ public: //! @name manipulators //@{ + //! Loop the event queue until quit + /*! + Dequeues and dispatches events until the kQuit event is found. + */ + virtual void loop() = 0; + //! Set the buffer /*! Replace the current event queue buffer. Any queued events are diff --git a/src/lib/ipc/CIpcClient.cpp b/src/lib/ipc/CIpcClient.cpp index 7a1e95bd..ecb5d70f 100644 --- a/src/lib/ipc/CIpcClient.cpp +++ b/src/lib/ipc/CIpcClient.cpp @@ -16,9 +16,12 @@ */ #include "CIpcClient.h" +#include "Ipc.h" -CIpcClient::CIpcClient() +CIpcClient::CIpcClient() : +m_serverAddress(CNetworkAddress(IPC_HOST, IPC_PORT)) { + m_serverAddress.resolve(); } CIpcClient::~CIpcClient() @@ -28,4 +31,5 @@ CIpcClient::~CIpcClient() void CIpcClient::connect() { + m_socket.connect(m_serverAddress); } diff --git a/src/lib/ipc/CIpcClient.h b/src/lib/ipc/CIpcClient.h index c064add5..d203c8f6 100644 --- a/src/lib/ipc/CIpcClient.h +++ b/src/lib/ipc/CIpcClient.h @@ -17,6 +17,9 @@ #pragma once +#include "CNetworkAddress.h" +#include "CTCPSocket.h" + //! IPC client for communication between daemon and GUI. /*! * See \ref CIpcServer description. @@ -28,4 +31,8 @@ public: //! Connects to the IPC server at localhost. void connect(); + +private: + CNetworkAddress m_serverAddress; + CTCPSocket m_socket; }; diff --git a/src/lib/ipc/CIpcServer.cpp b/src/lib/ipc/CIpcServer.cpp index fa61ba79..1e16c17c 100644 --- a/src/lib/ipc/CIpcServer.cpp +++ b/src/lib/ipc/CIpcServer.cpp @@ -16,9 +16,12 @@ */ #include "CIpcServer.h" +#include "Ipc.h" -CIpcServer::CIpcServer() +CIpcServer::CIpcServer() : +m_address(CNetworkAddress(IPC_HOST, IPC_PORT)) { + m_address.resolve(); } CIpcServer::~CIpcServer() @@ -28,4 +31,5 @@ CIpcServer::~CIpcServer() void CIpcServer::listen() { + m_socket.bind(m_address); } diff --git a/src/lib/ipc/CIpcServer.h b/src/lib/ipc/CIpcServer.h index ddbbfa06..f4ab5395 100644 --- a/src/lib/ipc/CIpcServer.h +++ b/src/lib/ipc/CIpcServer.h @@ -17,6 +17,9 @@ #pragma once +#include "CTCPListenSocket.h" +#include "CNetworkAddress.h" + //! IPC server for communication between daemon and GUI. /*! * The IPC server listens on localhost. The IPC client runs on both the @@ -31,4 +34,8 @@ public: //! Opens a TCP socket only allowing local connections void listen(); + +private: + CTCPListenSocket m_socket; + CNetworkAddress m_address; }; diff --git a/src/lib/ipc/CMakeLists.txt b/src/lib/ipc/CMakeLists.txt index 1f1a7808..fa9ea57f 100644 --- a/src/lib/ipc/CMakeLists.txt +++ b/src/lib/ipc/CMakeLists.txt @@ -14,6 +14,7 @@ # along with this program. If not, see . set(inc + Ipc.h CIpcServer.h CIpcClient.h CIpcServerProxy.h @@ -32,6 +33,11 @@ if (WIN32) endif() set(inc + ../arch + ../base + ../common + ../io + ../mt ../net ) @@ -45,5 +51,5 @@ include_directories(${inc}) add_library(ipc STATIC ${src}) if (UNIX) - target_link_libraries(net) + target_link_libraries(arch base common mt io net) endif() diff --git a/src/lib/ipc/Ipc.h b/src/lib/ipc/Ipc.h new file mode 100644 index 00000000..69c285be --- /dev/null +++ b/src/lib/ipc/Ipc.h @@ -0,0 +1,21 @@ +/* + * 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 . + */ + +#pragma once + +#define IPC_HOST "127.0.0.1" +#define IPC_PORT 24801 diff --git a/src/lib/net/CSocketMultiplexer.cpp b/src/lib/net/CSocketMultiplexer.cpp index 76808bd7..272a8cf6 100644 --- a/src/lib/net/CSocketMultiplexer.cpp +++ b/src/lib/net/CSocketMultiplexer.cpp @@ -82,6 +82,7 @@ CSocketMultiplexer::~CSocketMultiplexer() CSocketMultiplexer* CSocketMultiplexer::getInstance() { + assert(s_instance != NULL); return s_instance; } diff --git a/src/lib/synergy/CClientApp.cpp b/src/lib/synergy/CClientApp.cpp index 51ded42a..01e6985b 100644 --- a/src/lib/synergy/CClientApp.cpp +++ b/src/lib/synergy/CClientApp.cpp @@ -538,14 +538,8 @@ CClientApp::mainLoop() // run event loop. if startClient() failed we're supposed to retry // later. the timer installed by startClient() will take care of // that. - CEvent event; DAEMON_RUNNING(true); - EVENTQUEUE->getEvent(event); - while (event.getType() != CEvent::kQuit) { - EVENTQUEUE->dispatchEvent(event); - CEvent::deleteData(event); - EVENTQUEUE->getEvent(event); - } + EVENTQUEUE->loop(); DAEMON_RUNNING(false); // close down diff --git a/src/lib/synergy/CDaemonApp.cpp b/src/lib/synergy/CDaemonApp.cpp index dd6ea8ad..23155423 100644 --- a/src/lib/synergy/CDaemonApp.cpp +++ b/src/lib/synergy/CDaemonApp.cpp @@ -190,13 +190,7 @@ CDaemonApp::mainLoop(bool logToFile) m_relauncher.startAsync(); #endif - CEvent event; - EVENTQUEUE->getEvent(event); - while (event.getType() != CEvent::kQuit) { - EVENTQUEUE->dispatchEvent(event); - CEvent::deleteData(event); - EVENTQUEUE->getEvent(event); - } + EVENTQUEUE->loop(); #if SYSAPI_WIN32 m_relauncher.stop(); diff --git a/src/lib/synergy/CServerApp.cpp b/src/lib/synergy/CServerApp.cpp index 993d64f2..4ee3eeb4 100644 --- a/src/lib/synergy/CServerApp.cpp +++ b/src/lib/synergy/CServerApp.cpp @@ -356,13 +356,9 @@ CServerApp::closeServer(CServer* server) new TMethodEventJob(this, &CServerApp::handleClientsDisconnected)); EVENTQUEUE->adoptHandler(CServer::getDisconnectedEvent(), server, new TMethodEventJob(this, &CServerApp::handleClientsDisconnected)); - CEvent event; - EVENTQUEUE->getEvent(event); - while (event.getType() != CEvent::kQuit) { - EVENTQUEUE->dispatchEvent(event); - CEvent::deleteData(event); - EVENTQUEUE->getEvent(event); - } + + EVENTQUEUE->loop(); + EVENTQUEUE->removeHandler(CEvent::kTimer, timer); EVENTQUEUE->deleteTimer(timer); EVENTQUEUE->removeHandler(CServer::getDisconnectedEvent(), server); @@ -815,14 +811,8 @@ CServerApp::mainLoop() // run event loop. if startServer() failed we're supposed to retry // later. the timer installed by startServer() will take care of // that. - CEvent event; DAEMON_RUNNING(true); - EVENTQUEUE->getEvent(event); - while (event.getType() != CEvent::kQuit) { - EVENTQUEUE->dispatchEvent(event); - CEvent::deleteData(event); - EVENTQUEUE->getEvent(event); - } + EVENTQUEUE->loop(); DAEMON_RUNNING(false); // close down diff --git a/src/test/integtests/CIpcTests.cpp b/src/test/integtests/CIpcTests.cpp index e3be8bcf..8bbcd700 100644 --- a/src/test/integtests/CIpcTests.cpp +++ b/src/test/integtests/CIpcTests.cpp @@ -18,21 +18,19 @@ #include #include "CIpcServer.h" #include "CIpcClient.h" +#include "CSocketMultiplexer.h" +#include "CEventQueue.h" -TEST(CIpcTests, serverSend) +TEST(CIpcTests, connectToServer) { + CSocketMultiplexer multiplexer; + CEventQueue eventQueue; + CIpcServer server; server.listen(); CIpcClient client; client.connect(); -} -TEST(CIpcTests, clientSend) -{ - CIpcServer server; - server.listen(); - - CIpcClient client; - client.connect(); + eventQueue.loop(); } diff --git a/src/test/unittests/synergy/CMockEventQueue.h b/src/test/unittests/synergy/CMockEventQueue.h index ae6145b6..9ea396b9 100644 --- a/src/test/unittests/synergy/CMockEventQueue.h +++ b/src/test/unittests/synergy/CMockEventQueue.h @@ -24,6 +24,7 @@ class CMockEventQueue : public IEventQueue { public: + MOCK_METHOD0(loop, void()); MOCK_METHOD2(newOneShotTimer, CEventQueueTimer*(double, void*)); MOCK_METHOD2(newTimer, CEventQueueTimer*(double, void*)); MOCK_METHOD2(getEvent, bool(CEvent&, double));