bool QtLocalPeer::sendMessage(const QString &message, int timeout) { if (!isClient()) return false; QLocalSocket socket; bool connOk = false; for (int i = 0; i < 2; i++) { // Try twice, in case the other instance is just starting up socket.connectToServer(socketName); connOk = socket.waitForConnected(timeout/2); if (connOk || i) break; int ms = 250; #if defined(Q_OS_WIN) Sleep(DWORD(ms)); #else struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 }; nanosleep(&ts, NULL); #endif } if (!connOk) return false; QByteArray uMsg(message.toUtf8()); QDataStream ds(&socket); ds.writeBytes(uMsg.constData(), uMsg.size()); bool res = socket.waitForBytesWritten(timeout); res &= socket.waitForReadyRead(timeout); // wait for ack res &= (socket.read(qstrlen(ack)) == ack); return res; }
unsigned int LocalPeer::get_hwnd_and_activate() { unsigned int hwnd = 0; #ifdef _WIN32 QLocalSocket socket; bool connOk = false; int timeout = 5000; for(int i = 0; i < 2; i++) { socket.connectToServer(socket_name_); connOk = socket.waitForConnected(timeout/2); if (connOk || i) break; int ms = 250; Sleep(DWORD(ms)); } if (!connOk) return false; QByteArray uMsg((QString(crossprocess_message_get_hwnd_activate)).toUtf8()); QDataStream ds(&socket); ds.writeBytes(uMsg.constData(), uMsg.size()); if (socket.waitForBytesWritten(timeout)) { if (socket.waitForReadyRead(timeout)) { QByteArray data_read = socket.readAll(); if (data_read.size() == sizeof(hwnd)) { hwnd = *(unsigned int*) data_read.data(); } } } #endif _WIN32 return hwnd; }