| 1 | /***************************************************************************** |
| 2 | ** QNapi |
| 3 | ** Copyright (C) 2008-2017 Piotr Krzemiński <[email protected]> |
| 4 | ** |
| 5 | ** This program is free software; you can redistribute it and/or modify |
| 6 | ** it under the terms of the GNU General Public License as published by |
| 7 | ** the Free Software Foundation; either version 2 of the License, or |
| 8 | ** (at your option) any later version. |
| 9 | ** |
| 10 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
| 11 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | ** |
| 13 | *****************************************************************************/ |
| 14 | |
| 15 | #include "qinterprocesschannel_win32.h" |
| 16 | |
| 17 | /*! |
| 18 | \class QInterProcessChannel |
| 19 | |
| 20 | \brief A generic network-based communication channel for applications. |
| 21 | |
| 22 | QInterProcessChannel offers communication facilities between several |
| 23 | instances of the same application. It initializes itself so that the first |
| 24 | created instance acts as a "server" which recieve messages from the others |
| 25 | instances. This is particularly handy when creating a single-instance |
| 26 | application which, for example, uses file association through a command line |
| 27 | interface. |
| 28 | |
| 29 | \see QSingleApplication |
| 30 | */ |
| 31 | |
| 32 | /*! |
| 33 | \brief Constructor |
| 34 | |
| 35 | Check for a valid server and create one if none found. |
| 36 | */ |
| 37 | QInterProcessChannel::QInterProcessChannel(QObject* p) : QThread(p) { |
| 38 | uniqPrefix = QApplication::applicationName(); |
| 39 | pipeName = |
| 40 | QString("\\\\.\\pipe\\%1%2").arg(uniqPrefix).arg("_uniquePipeName"); |
| 41 | globalMutexStr = uniqPrefix + QString("_globalMutexId"); |
| 42 | blockerMutexStr = uniqPrefix + QString("_blockerMutexId"); |
| 43 | init(); |
| 44 | } |
| 45 | |
| 46 | /*! |
| 47 | \brief Destructor |
| 48 | */ |
| 49 | QInterProcessChannel::~QInterProcessChannel() { |
| 50 | // while(isRunning()) |
| 51 | // quit(); |
| 52 | |
| 53 | close(); |
| 54 | } |
| 55 | |
| 56 | /*! |
| 57 | \return Wether the instance has a server role |
| 58 | */ |
| 59 | bool QInterProcessChannel::isServer() const { return serverMode; } |
| 60 | |
| 61 | /*! |
| 62 | \return The current buffered message |
| 63 | */ |
| 64 | QString QInterProcessChannel::messageBuffer() const { return sMsg; } |
| 65 | |
| 66 | /*! |
| 67 | \brief Sets a buffered message |
| 68 | */ |
| 69 | void QInterProcessChannel::setMessageBuffer(const QString& s) { sMsg = s; } |
| 70 | |
| 71 | /*! |
| 72 | \overload |
| 73 | |
| 74 | Sends the content of the current message buffer |
| 75 | */ |
| 76 | void QInterProcessChannel::sendMessage() { |
| 77 | sendMessage(sMsg); |
| 78 | sMsg.clear(); |
| 79 | } |
| 80 | |
| 81 | /*! |
| 82 | \overload |
| 83 | */ |
| 84 | void QInterProcessChannel::sendMessage(const QString& s) { |
| 85 | sendMessage(s.toUtf8()); |
| 86 | } |
| 87 | /*! |
| 88 | \brief Send a message to server instance |
| 89 | */ |
| 90 | void QInterProcessChannel::sendMessage(const QByteArray& msg) { |
| 91 | if (msg.count()) { |
| 92 | // a lot of simultaneous clients cause that pipe sometimes omits |
| 93 | // some of these clients despite results of writing functions are ok ;/ |
| 94 | // workaround: reserve mutex, wait 100ms before every write operation |
| 95 | |
| 96 | HANDLE mutex = CreateMutexA(NULL, FALSE, qPrintable(blockerMutexStr)); |
| 97 | WaitForSingleObject(mutex, INFINITE); |
| 98 | Sleep(100); |
| 99 | |
| 100 | HANDLE hPipe; |
| 101 | |
| 102 | // loop to get pipe's handle |
| 103 | for (;;) { |
| 104 | hPipe = CreateFileA(qPrintable(pipeName), GENERIC_WRITE, 0, NULL, |
| 105 | OPEN_EXISTING, 0, NULL); |
| 106 | |
| 107 | if (hPipe != INVALID_HANDLE_VALUE) break; |
| 108 | |
| 109 | if (GetLastError() != ERROR_PIPE_BUSY) { |
| 110 | ReleaseMutex(mutex); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | // wait max 1s and retry |
| 115 | WaitNamedPipeA(qPrintable(pipeName), 1000); |
| 116 | } |
| 117 | |
| 118 | // ok, now we have pipe's handle, we can prepare buffers and write to the |
| 119 | // pipe |
| 120 | |
| 121 | char buffer[bufferSize]; |
| 122 | strncpy(buffer, msg.constData(), bufferSize); |
| 123 | buffer[bufferSize - 1] = 0; |
| 124 | |
| 125 | DWORD bytesWritten; |
| 126 | bool writeSuccess = |
| 127 | WriteFile(hPipe, buffer, strlen(buffer) + 1, &bytesWritten, NULL); |
| 128 | |
| 129 | if (!writeSuccess) { |
| 130 | ReleaseMutex(mutex); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | CloseHandle(hPipe); |
| 135 | ReleaseMutex(mutex); |
| 136 | } else { |
| 137 | // qWarning("::sendMessage(%d): Empty messages are not carried out...", |
| 138 | // GetCurrentProcessId()); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /*! |
| 143 | \brief Closes the communication channel |
| 144 | */ |
| 145 | void QInterProcessChannel::close() { |
| 146 | if (serverMode) { |
| 147 | // if we want to quit pipe server thread, we have to send --quit message |
| 148 | sendMessage(QString("--quit")); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /*! |
| 153 | \internal |
| 154 | */ |
| 155 | void QInterProcessChannel::run() { |
| 156 | if (!serverMode) return; |
| 157 | |
| 158 | HANDLE hPipe; |
| 159 | char buffer[bufferSize]; |
| 160 | bool connected, readSuccess; |
| 161 | DWORD bytesRead; |
| 162 | |
| 163 | // thread loop |
| 164 | for (;;) { |
| 165 | hPipe = |
| 166 | CreateNamedPipeA(qPrintable(pipeName), PIPE_ACCESS_INBOUND, |
| 167 | PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, |
| 168 | 1, 0, bufferSize, 0, NULL); |
| 169 | |
| 170 | if (hPipe == INVALID_HANDLE_VALUE) break; |
| 171 | |
| 172 | connected = ConnectNamedPipe(hPipe, NULL) |
| 173 | ? TRUE |
| 174 | : (GetLastError() == ERROR_PIPE_CONNECTED); |
| 175 | |
| 176 | if (!connected) { |
| 177 | CloseHandle(hPipe); |
| 178 | continue; |
| 179 | } |
| 180 | |
| 181 | readSuccess = ReadFile(hPipe, buffer, bufferSize, &bytesRead, NULL); |
| 182 | buffer[bufferSize - 1] = 0; |
| 183 | |
| 184 | if (!readSuccess || !bytesRead) { |
| 185 | CloseHandle(hPipe); |
| 186 | continue; |
| 187 | } |
| 188 | |
| 189 | QStringList argv = QManagedRequest::splitArguments(QString(buffer)); |
| 190 | const QString cmd = argv.at(0); |
| 191 | |
| 192 | // if server instance is going to suspend thread... |
| 193 | if (cmd == "--quit") { |
| 194 | CloseHandle(hPipe); |
| 195 | break; |
| 196 | } else if (cmd == "--request") { |
| 197 | argv.removeAt(0); |
| 198 | emit request(argv); |
| 199 | } else { |
| 200 | emit message(QString(buffer)); |
| 201 | } |
| 202 | |
| 203 | CloseHandle(hPipe); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /*! |
| 208 | \internal |
| 209 | */ |
| 210 | void QInterProcessChannel::init() { |
| 211 | // while(isRunning()) |
| 212 | // quit(); |
| 213 | |
| 214 | // create global mutex to check single instance |
| 215 | CreateMutexA(NULL, FALSE, qPrintable(globalMutexStr)); |
| 216 | serverMode = !(GetLastError() == ERROR_ALREADY_EXISTS); |
| 217 | |
| 218 | // start listening thread in single instance mode |
| 219 | if (serverMode) start(); |
| 220 | } |