| 150 | } |
| 151 | |
| 152 | bool LocalPeer::sendMessage(const QByteArray& message, int timeout) |
| 153 | { |
| 154 | if (!isClient()) |
| 155 | return false; |
| 156 | |
| 157 | QLocalSocket socket; |
| 158 | bool connOk = false; |
| 159 | int tries = 2; |
| 160 | for (int i = 0; i < tries; i++) { |
| 161 | // Try twice, in case the other instance is just starting up |
| 162 | socket.connectToServer(socketName); |
| 163 | connOk = socket.waitForConnected(timeout / 2); |
| 164 | if (!connOk && i < (tries - 1)) { |
| 165 | std::this_thread::sleep_for(std::chrono::milliseconds(250)); |
| 166 | } |
| 167 | } |
| 168 | if (!connOk) { |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | QByteArray uMsg(message); |
| 173 | QDataStream ds(&socket); |
| 174 | |
| 175 | ds.writeBytes(uMsg.constData(), uMsg.size()); |
| 176 | if (!socket.waitForBytesWritten(timeout)) { |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | // wait for 'ack' |
| 181 | if (!socket.waitForReadyRead(timeout)) { |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | // make sure we got 'ack' |
| 186 | if (!(socket.read(qstrlen(ack)) == ack)) { |
| 187 | return false; |
| 188 | } |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | void LocalPeer::receiveConnection() |
| 193 | { |
no test coverage detected