| 132 | } |
| 133 | |
| 134 | bool QtLocalPeer::sendMessage(const QByteArray &message, int timeout) |
| 135 | { |
| 136 | if (!isClient()) |
| 137 | return false; |
| 138 | |
| 139 | QLocalSocket socket; |
| 140 | bool connOk = false; |
| 141 | for (int i = 0; i < 2; i++) |
| 142 | { |
| 143 | // Try twice, in case the other instance is just starting up |
| 144 | socket.connectToServer(socketName); |
| 145 | connOk = socket.waitForConnected(timeout / 2); |
| 146 | if (connOk || i) |
| 147 | break; |
| 148 | int ms = 250; |
| 149 | #if defined(Q_OS_WIN) |
| 150 | Sleep(DWORD(ms)); |
| 151 | #else |
| 152 | struct timespec ts = {ms / 1000, (ms % 1000) * 1000 * 1000}; |
| 153 | nanosleep(&ts, nullptr); |
| 154 | #endif |
| 155 | } |
| 156 | if (!connOk) |
| 157 | return false; |
| 158 | |
| 159 | QDataStream ds(&socket); |
| 160 | ds.writeBytes(message.constData(), message.size()); |
| 161 | bool res = socket.waitForBytesWritten(timeout); |
| 162 | if (res) |
| 163 | { |
| 164 | res &= socket.waitForReadyRead(timeout); // wait for ack |
| 165 | if (res) |
| 166 | res &= (socket.read(qstrlen(ack)) == ack); |
| 167 | } |
| 168 | return res; |
| 169 | } |
| 170 | |
| 171 | void QtLocalPeer::receiveConnection() |
| 172 | { |
no test coverage detected