Changeset View
Changeset View
Standalone View
Standalone View
core/backends/bluetooth/bluetoothlinkprovider.cpp
Show First 20 Lines • Show All 166 Lines • ▼ Show 20 Line(s) | 164 | { | |||
---|---|---|---|---|---|
167 | 167 | | |||
168 | QByteArray identityArray; | 168 | QByteArray identityArray; | ||
169 | while (socket->bytesAvailable() > 0 || !identityArray.contains('\n')) { | 169 | while (socket->bytesAvailable() > 0 || !identityArray.contains('\n')) { | ||
170 | identityArray += socket->readAll(); | 170 | identityArray += socket->readAll(); | ||
171 | } | 171 | } | ||
172 | 172 | | |||
173 | disconnect(socket, SIGNAL(readyRead()), this, SLOT(clientIdentityReceived())); | 173 | disconnect(socket, SIGNAL(readyRead()), this, SLOT(clientIdentityReceived())); | ||
174 | 174 | | |||
175 | NetworkPackage receivedPackage(""); | 175 | NetworkPacket receivedPacket(""); | ||
176 | bool success = NetworkPackage::unserialize(identityArray, &receivedPackage); | 176 | bool success = NetworkPacket::unserialize(identityArray, &receivedPacket); | ||
177 | 177 | | |||
178 | if (!success || receivedPackage.type() != PACKAGE_TYPE_IDENTITY) { | 178 | if (!success || receivedPacket.type() != PACKET_TYPE_IDENTITY) { | ||
179 | qCWarning(KDECONNECT_CORE) << "Not an identity package"; | 179 | qCWarning(KDECONNECT_CORE) << "Not an identity packet"; | ||
180 | mSockets.remove(socket->peerAddress()); | 180 | mSockets.remove(socket->peerAddress()); | ||
181 | socket->close(); | 181 | socket->close(); | ||
182 | socket->deleteLater(); | 182 | socket->deleteLater(); | ||
183 | return; | 183 | return; | ||
184 | } | 184 | } | ||
185 | 185 | | |||
186 | qCDebug(KDECONNECT_CORE()) << "Received identity package from" << socket->peerAddress(); | 186 | qCDebug(KDECONNECT_CORE()) << "Received identity packet from" << socket->peerAddress(); | ||
187 | 187 | | |||
188 | disconnect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(connectError())); | 188 | disconnect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(connectError())); | ||
189 | 189 | | |||
190 | const QString& deviceId = receivedPackage.get<QString>("deviceId"); | 190 | const QString& deviceId = receivedPacket.get<QString>("deviceId"); | ||
191 | BluetoothDeviceLink* deviceLink = new BluetoothDeviceLink(deviceId, this, socket); | 191 | BluetoothDeviceLink* deviceLink = new BluetoothDeviceLink(deviceId, this, socket); | ||
192 | 192 | | |||
193 | NetworkPackage np2(""); | 193 | NetworkPacket np2(""); | ||
194 | NetworkPackage::createIdentityPackage(&np2); | 194 | NetworkPacket::createIdentityPacket(&np2); | ||
195 | success = deviceLink->sendPackage(np2); | 195 | success = deviceLink->sendPacket(np2); | ||
196 | 196 | | |||
197 | if (success) { | 197 | if (success) { | ||
198 | 198 | | |||
199 | qCDebug(KDECONNECT_CORE) << "Handshaking done (I'm the new device)"; | 199 | qCDebug(KDECONNECT_CORE) << "Handshaking done (I'm the new device)"; | ||
200 | 200 | | |||
201 | connect(deviceLink, SIGNAL(destroyed(QObject*)), | 201 | connect(deviceLink, SIGNAL(destroyed(QObject*)), | ||
202 | this, SLOT(deviceLinkDestroyed(QObject*))); | 202 | this, SLOT(deviceLinkDestroyed(QObject*))); | ||
203 | 203 | | |||
204 | Q_EMIT onConnectionReceived(receivedPackage, deviceLink); | 204 | Q_EMIT onConnectionReceived(receivedPacket, deviceLink); | ||
205 | 205 | | |||
206 | //We kill any possible link from this same device | 206 | //We kill any possible link from this same device | ||
207 | addLink(deviceLink, deviceId); | 207 | addLink(deviceLink, deviceId); | ||
208 | 208 | | |||
209 | } else { | 209 | } else { | ||
210 | // Connection might be lost. Delete it. | 210 | // Connection might be lost. Delete it. | ||
211 | delete deviceLink; | 211 | delete deviceLink; | ||
212 | } | 212 | } | ||
Show All 14 Lines | 224 | if (mSockets.contains(socket->peerAddress())) { | |||
227 | socket->deleteLater(); | 227 | socket->deleteLater(); | ||
228 | return; | 228 | return; | ||
229 | } | 229 | } | ||
230 | 230 | | |||
231 | connect(socket, SIGNAL(readyRead()), this, SLOT(serverDataReceived())); | 231 | connect(socket, SIGNAL(readyRead()), this, SLOT(serverDataReceived())); | ||
232 | connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(connectError())); | 232 | connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(connectError())); | ||
233 | connect(socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected())); | 233 | connect(socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected())); | ||
234 | 234 | | |||
235 | NetworkPackage np2(""); | 235 | NetworkPacket np2(""); | ||
236 | NetworkPackage::createIdentityPackage(&np2); | 236 | NetworkPacket::createIdentityPacket(&np2); | ||
237 | socket->write(np2.serialize()); | 237 | socket->write(np2.serialize()); | ||
238 | 238 | | |||
239 | qCDebug(KDECONNECT_CORE()) << "Sent identity package to" << socket->peerAddress(); | 239 | qCDebug(KDECONNECT_CORE()) << "Sent identity packet to" << socket->peerAddress(); | ||
240 | 240 | | |||
241 | mSockets.insert(socket->peerAddress(), socket); | 241 | mSockets.insert(socket->peerAddress(), socket); | ||
242 | } | 242 | } | ||
243 | 243 | | |||
244 | //I'm the existing device and this is the answer to my identity package (data received) | 244 | //I'm the existing device and this is the answer to my identity packet (data received) | ||
245 | void BluetoothLinkProvider::serverDataReceived() | 245 | void BluetoothLinkProvider::serverDataReceived() | ||
246 | { | 246 | { | ||
247 | QBluetoothSocket* socket = qobject_cast<QBluetoothSocket*>(sender()); | 247 | QBluetoothSocket* socket = qobject_cast<QBluetoothSocket*>(sender()); | ||
248 | if (!socket) return; | 248 | if (!socket) return; | ||
249 | 249 | | |||
250 | QByteArray identityArray; | 250 | QByteArray identityArray; | ||
251 | while (socket->bytesAvailable() > 0 || !identityArray.contains('\n')) { | 251 | while (socket->bytesAvailable() > 0 || !identityArray.contains('\n')) { | ||
252 | identityArray += socket->readAll(); | 252 | identityArray += socket->readAll(); | ||
253 | } | 253 | } | ||
254 | 254 | | |||
255 | disconnect(socket, SIGNAL(readyRead()), this, SLOT(serverDataReceived())); | 255 | disconnect(socket, SIGNAL(readyRead()), this, SLOT(serverDataReceived())); | ||
256 | disconnect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(connectError())); | 256 | disconnect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(connectError())); | ||
257 | 257 | | |||
258 | NetworkPackage receivedPackage(""); | 258 | NetworkPacket receivedPacket(""); | ||
259 | bool success = NetworkPackage::unserialize(identityArray, &receivedPackage); | 259 | bool success = NetworkPacket::unserialize(identityArray, &receivedPacket); | ||
260 | 260 | | |||
261 | if (!success || receivedPackage.type() != PACKAGE_TYPE_IDENTITY) { | 261 | if (!success || receivedPacket.type() != PACKET_TYPE_IDENTITY) { | ||
262 | qCWarning(KDECONNECT_CORE) << "Not an identity package."; | 262 | qCWarning(KDECONNECT_CORE) << "Not an identity packet."; | ||
263 | mSockets.remove(socket->peerAddress()); | 263 | mSockets.remove(socket->peerAddress()); | ||
264 | socket->close(); | 264 | socket->close(); | ||
265 | socket->deleteLater(); | 265 | socket->deleteLater(); | ||
266 | return; | 266 | return; | ||
267 | } | 267 | } | ||
268 | 268 | | |||
269 | qCDebug(KDECONNECT_CORE()) << "Received identity package from" << socket->peerAddress(); | 269 | qCDebug(KDECONNECT_CORE()) << "Received identity packet from" << socket->peerAddress(); | ||
270 | 270 | | |||
271 | const QString& deviceId = receivedPackage.get<QString>("deviceId"); | 271 | const QString& deviceId = receivedPacket.get<QString>("deviceId"); | ||
272 | BluetoothDeviceLink* deviceLink = new BluetoothDeviceLink(deviceId, this, socket); | 272 | BluetoothDeviceLink* deviceLink = new BluetoothDeviceLink(deviceId, this, socket); | ||
273 | 273 | | |||
274 | connect(deviceLink, SIGNAL(destroyed(QObject*)), | 274 | connect(deviceLink, SIGNAL(destroyed(QObject*)), | ||
275 | this, SLOT(deviceLinkDestroyed(QObject*))); | 275 | this, SLOT(deviceLinkDestroyed(QObject*))); | ||
276 | 276 | | |||
277 | Q_EMIT onConnectionReceived(receivedPackage, deviceLink); | 277 | Q_EMIT onConnectionReceived(receivedPacket, deviceLink); | ||
278 | 278 | | |||
279 | addLink(deviceLink, deviceId); | 279 | addLink(deviceLink, deviceId); | ||
280 | } | 280 | } | ||
281 | 281 | | |||
282 | void BluetoothLinkProvider::deviceLinkDestroyed(QObject* destroyedDeviceLink) | 282 | void BluetoothLinkProvider::deviceLinkDestroyed(QObject* destroyedDeviceLink) | ||
283 | { | 283 | { | ||
284 | const QString id = destroyedDeviceLink->property("deviceId").toString(); | 284 | const QString id = destroyedDeviceLink->property("deviceId").toString(); | ||
285 | qCDebug(KDECONNECT_CORE()) << "Device disconnected:" << id; | 285 | qCDebug(KDECONNECT_CORE()) << "Device disconnected:" << id; | ||
Show All 20 Lines |