void tst_QL2capServer::tst_pendingConnections() { QFETCH(int, maxConnections); { QL2capServer server; server.setMaxPendingConnections(maxConnections); bool result = server.listen(); QVERIFY(result); QVERIFY(server.isListening()); qDebug() << "Listening on L2CAP channel:" << server.serverPort(); QCOMPARE(server.maxPendingConnections(), maxConnections); QVERIFY(!server.hasPendingConnections()); QVERIFY(server.nextPendingConnection() == 0); { /* wait for maxConnections simultaneous connections */ qDebug() << "Waiting for" << maxConnections << "simultaneous connections."; QSignalSpy connectionSpy(&server, SIGNAL(newConnection())); int connectTime = MaxConnectTime; while (connectionSpy.count() < maxConnections && connectTime > 0) { QTest::qWait(1000); connectTime -= 1000; } QList<QBluetoothSocket *> sockets; while (server.hasPendingConnections()) sockets.append(server.nextPendingConnection()); QCOMPARE(connectionSpy.count(), maxConnections); QCOMPARE(sockets.count(), maxConnections); foreach (QBluetoothSocket *socket, sockets) { qDebug() << socket->state(); QVERIFY(socket->state() == QBluetoothSocket::ConnectedState); QVERIFY(socket->openMode() == QIODevice::ReadWrite); } QVERIFY(!server.hasPendingConnections()); QVERIFY(server.nextPendingConnection() == 0); while (!sockets.isEmpty()) { QBluetoothSocket *socket = sockets.takeFirst(); socket->close(); delete socket; } } server.close(); }
BtLocalDevice::~BtLocalDevice() { while (!serverSockets.isEmpty()) { QBluetoothSocket* s = serverSockets.takeFirst(); s->abort(); s->deleteLater(); } }
//! [clientConnected] void ChatServer::clientConnected() { QBluetoothSocket *socket = rfcommServer->nextPendingConnection(); if (!socket) return; connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket())); connect(socket, SIGNAL(disconnected()), this, SLOT(clientDisconnected())); clientSockets.append(socket); emit clientConnected(socket->peerName()); }
//! [readSocket] void ChatServer::readSocket() { QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender()); if (!socket) return; while (socket->canReadLine()) { QByteArray line = socket->readLine().trimmed(); emit messageReceived(socket->peerName(), QString::fromUtf8(line.constData(), line.length())); } }
//! [clientDisconnected] void ChatServer::clientDisconnected() { QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender()); if (!socket) return; emit clientDisconnected(socket->peerName()); clientSockets.removeOne(socket); socket->deleteLater(); }
//! [clientDisconnected] void TennisServer::clientDisconnected() { qDebug() << Q_FUNC_INFO << "client closing!"; lagTimer.stop(); lagReplyTimeout = 0; QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender()); if (!socket) return; emit clientDisconnected(socket->peerName()); clientSocket->deleteLater(); clientSocket = 0; delete stream; stream = 0; // socket->deleteLater(); }