From ddb2d7feebbbfdc94b1b7d37312272585d6048cd Mon Sep 17 00:00:00 2001 From: Nick Bolton Date: Thu, 5 Jul 2012 19:10:04 +0000 Subject: [PATCH] fixed: ipc client connected event was being wiped out daemon now communicates graceful shutdown message through new ipc system. --- src/gui/src/IpcClient.cpp | 31 ++++++++++++++--------- src/gui/src/IpcClient.h | 6 ++--- src/gui/src/MainWindow.cpp | 2 +- src/gui/src/MainWindow.h | 6 +---- src/lib/base/CEventQueue.cpp | 8 ++++++ src/lib/ipc/CIpcClient.cpp | 2 +- src/lib/ipc/CIpcServer.cpp | 1 + src/lib/platform/CMSWindowsRelauncher.cpp | 3 ++- src/lib/synergy/CDaemonApp.cpp | 8 ++---- src/lib/synergy/CServerApp.cpp | 15 +++++------ 10 files changed, 44 insertions(+), 38 deletions(-) diff --git a/src/gui/src/IpcClient.cpp b/src/gui/src/IpcClient.cpp index a8cba815..885615e7 100644 --- a/src/gui/src/IpcClient.cpp +++ b/src/gui/src/IpcClient.cpp @@ -68,13 +68,12 @@ void IpcClient::read() stream.readRawData(data, len); readLogLine(QString::fromUtf8(data, len)); + break; } - break; - default: { - std::cout << "invalid code: " << codeBuf[0] << std::endl; - } - break; + default: + std::cerr << "message type not supported: " << codeBuf[0] << std::endl; + break; } } } @@ -93,7 +92,7 @@ void IpcClient::error(QAbstractSocket::SocketError error) QTimer::singleShot(1000, this, SLOT(connectToHost())); } -void IpcClient::write(unsigned char code, unsigned char length, const char* data) +void IpcClient::write(int code, int length, const char* data) { QDataStream stream(m_Socket); @@ -102,12 +101,20 @@ void IpcClient::write(unsigned char code, unsigned char length, const char* data stream.writeRawData(codeBuf, 1); switch (code) { - case kIpcCommand: { - char lenBuf[2]; - intToBytes(length, lenBuf, 2); - stream.writeRawData(lenBuf, 2); - stream.writeRawData(data, length); - } + case kIpcHello: + stream.writeRawData(data, 1); + break; + + case kIpcCommand: { + char lenBuf[2]; + intToBytes(length, lenBuf, 2); + stream.writeRawData(lenBuf, 2); + stream.writeRawData(data, length); + break; + } + + default: + std::cerr << "message type not supported: " << code << std::endl; break; } } diff --git a/src/gui/src/IpcClient.h b/src/gui/src/IpcClient.h index 19fd9778..f8f6bcc5 100644 --- a/src/gui/src/IpcClient.h +++ b/src/gui/src/IpcClient.h @@ -32,7 +32,7 @@ public: IpcClient(); virtual ~IpcClient(); - void write(unsigned char code, unsigned char length, const char* data); + void write(int code, int length, const char* data); public slots: void connectToHost(); @@ -55,14 +55,14 @@ private: QTcpSocket* m_Socket; }; -enum IpcMessageType { +enum qIpcMessageType { kIpcHello, kIpcLogLine, kIpcCommand, kIpcShutdown, }; -enum IpcClientType { +enum qIpcClientType { kIpcClientUnknown, kIpcClientGui, kIpcClientNode, diff --git a/src/gui/src/MainWindow.cpp b/src/gui/src/MainWindow.cpp index ffb3d443..b0d8d554 100644 --- a/src/gui/src/MainWindow.cpp +++ b/src/gui/src/MainWindow.cpp @@ -686,7 +686,7 @@ void MainWindow::sendDaemonCommand(const QString& command, bool showErrors) { std::string s = command.toStdString(); const char* data = s.c_str(); - m_IpcClient.write(Command, strlen(data), data); + m_IpcClient.write(kIpcCommand, strlen(data), data); } void MainWindow::on_m_pActionWizard_triggered() diff --git a/src/gui/src/MainWindow.h b/src/gui/src/MainWindow.h index a330ad57..1525d8b5 100644 --- a/src/gui/src/MainWindow.h +++ b/src/gui/src/MainWindow.h @@ -69,10 +69,6 @@ class MainWindow : public QMainWindow, public Ui::MainWindowBase synergyServer }; - enum qIpcMessage { - Command = 1 - }; - enum qLevel { Error, Info @@ -135,7 +131,7 @@ class MainWindow : public QMainWindow, public Ui::MainWindowBase bool serverArgs(QStringList& args, QString& app); void setStatus(const QString& status); void sendDaemonCommand(const QString& command, bool showErrors); - void sendIpcMessage(qIpcMessage type, const char* buffer, bool showErrors); + void sendIpcMessage(qIpcMessageType type, const char* buffer, bool showErrors); private: QSettings& m_Settings; diff --git a/src/lib/base/CEventQueue.cpp b/src/lib/base/CEventQueue.cpp index 63e41c6a..d5af656b 100644 --- a/src/lib/base/CEventQueue.cpp +++ b/src/lib/base/CEventQueue.cpp @@ -121,6 +121,14 @@ CEventQueue::adoptBuffer(IEventQueueBuffer* buffer) { CArchMutexLock lock(m_mutex); + LOG((CLOG_DEBUG "adopting new buffer")); + + if (m_events.size() != 0) { + // this can come as a nasty surprise to programmers expecting + // their events to be raised, only to have them deleted. + LOG((CLOG_WARN "discarding %d event(s), sorry", m_events.size())); + } + // discard old buffer and old events delete m_buffer; for (CEventTable::iterator i = m_events.begin(); i != m_events.end(); ++i) { diff --git a/src/lib/ipc/CIpcClient.cpp b/src/lib/ipc/CIpcClient.cpp index 50515afb..9cbf5fd2 100644 --- a/src/lib/ipc/CIpcClient.cpp +++ b/src/lib/ipc/CIpcClient.cpp @@ -30,7 +30,7 @@ m_server(nullptr) m_serverAddress.resolve(); EVENTQUEUE->adoptHandler( - m_socket.getConnectedEvent(), &m_socket, + m_socket.getConnectedEvent(), m_socket.getEventTarget(), new TMethodEventJob( this, &CIpcClient::handleConnected)); } diff --git a/src/lib/ipc/CIpcServer.cpp b/src/lib/ipc/CIpcServer.cpp index ada173fc..45ce2b78 100644 --- a/src/lib/ipc/CIpcServer.cpp +++ b/src/lib/ipc/CIpcServer.cpp @@ -45,6 +45,7 @@ CIpcServer::~CIpcServer() for (it = m_clients.begin(); it != m_clients.end(); it++) { delete *it; } + m_clients.empty(); EVENTQUEUE->removeHandler(m_socket.getConnectingEvent(), &m_socket); } diff --git a/src/lib/platform/CMSWindowsRelauncher.cpp b/src/lib/platform/CMSWindowsRelauncher.cpp index 34fcb250..4f4ebf0c 100644 --- a/src/lib/platform/CMSWindowsRelauncher.cpp +++ b/src/lib/platform/CMSWindowsRelauncher.cpp @@ -437,7 +437,8 @@ CMSWindowsRelauncher::shutdownProcess(const PROCESS_INFORMATION& pi, int timeout { GetExitCodeProcess(pi.hProcess, &exitCode); if (exitCode != STILL_ACTIVE) { - LOG((CLOG_INFO "process %d was shutdown successfully", pi.dwProcessId)); + // yay, we got a graceful shutdown. there should be no hook in use errors! + LOG((CLOG_INFO "process %d was shutdown gracefully", pi.dwProcessId)); break; } else { diff --git a/src/lib/synergy/CDaemonApp.cpp b/src/lib/synergy/CDaemonApp.cpp index 84242125..d821b590 100644 --- a/src/lib/synergy/CDaemonApp.cpp +++ b/src/lib/synergy/CDaemonApp.cpp @@ -221,6 +221,8 @@ CDaemonApp::mainLoop(bool logToFile) eventQueue.removeHandler( CIpcServer::getClientConnectedEvent(), m_ipcServer); + CLOG->remove(m_ipcLogOutputter); + delete m_ipcLogOutputter; delete m_ipcServer; DAEMON_RUNNING(false); @@ -278,8 +280,6 @@ void CDaemonApp::handleIpcMessage(const CEvent& e, void*) { CIpcMessage& m = *static_cast(e.getData()); - LOG((CLOG_DEBUG "ipc message, type=%d", m.m_type)); - switch (m.m_type) { case kIpcCommand: { CString& command = *static_cast(m.m_data); @@ -300,9 +300,5 @@ CDaemonApp::handleIpcMessage(const CEvent& e, void*) m_relauncher->command(command); } break; - - default: - LOG((CLOG_ERR "ipc message not supported, type=%d", m.m_type)); - break; } } diff --git a/src/lib/synergy/CServerApp.cpp b/src/lib/synergy/CServerApp.cpp index 35a2d80e..b954a9b3 100644 --- a/src/lib/synergy/CServerApp.cpp +++ b/src/lib/synergy/CServerApp.cpp @@ -760,15 +760,6 @@ CServerApp::mainLoop() // create the event queue CEventQueue eventQueue; -/* - bool a = true; - while (a) { - ARCH->sleep(1); - }*/ - - if (argsBase().m_enableIpc) { - initIpcClient(); - } // if configuration has no screens then add this system // as the default @@ -795,6 +786,12 @@ CServerApp::mainLoop() // start server, etc appUtil().startNode(); + + // init ipc client after node start, since create a new screen wipes out + // the event queue (the screen ctors call adoptBuffer). + if (argsBase().m_enableIpc) { + initIpcClient(); + } // load all available plugins. ARCH->plugin().init(s_serverScreen->getEventTarget());