From 690dd7a33ddc90678367d73adf313533536e10f2 Mon Sep 17 00:00:00 2001 From: Alexey Sokolov Date: Fri, 29 Apr 2016 23:57:30 +0100 Subject: Apply clang-format --- src/modules/upnp/SsdpConnection.cpp | 186 +++++++++++++++++------------------- 1 file changed, 90 insertions(+), 96 deletions(-) (limited to 'src/modules/upnp/SsdpConnection.cpp') diff --git a/src/modules/upnp/SsdpConnection.cpp b/src/modules/upnp/SsdpConnection.cpp index 7440d10d1..11a316ce6 100644 --- a/src/modules/upnp/SsdpConnection.cpp +++ b/src/modules/upnp/SsdpConnection.cpp @@ -43,110 +43,104 @@ namespace UPnP { -SsdpConnection::SsdpConnection() -: QObject() -{ - m_pSocket = new QUdpSocket(); - connect(m_pSocket, SIGNAL(readyRead()), this, SLOT(slotDataReceived())); -} - -SsdpConnection::~SsdpConnection() -{ - if(m_pSocket != 0) + SsdpConnection::SsdpConnection() + : QObject() { - m_pSocket->close(); - delete m_pSocket; + m_pSocket = new QUdpSocket(); + connect(m_pSocket, SIGNAL(readyRead()), this, SLOT(slotDataReceived())); } -} - - - -// Data was received by the socket -void SsdpConnection::slotDataReceived() -{ - qDebug() << "UPnP::SsdpConnection: received " << m_pSocket->bytesAvailable() << " bytes." << endl; - - // Response from Diederik's Acatel router: - // - // HTTP/1.1 200 OK - // CACHE-CONTROL:max-age=1800 - // EXT: - // LOCATION:http://10.0.0.138:80/IGD.xml - // SERVER:SpeedTouch 510 4.0.2.0.0 UPnP/1.0 (0313QZ6S2) - // ST:upnp:rootdevice - // USN:uuid:UPnP-SpeedTouch510-1_00-90-D0-8E-A1-6F::upnp:rootdevice - // - - // This is from a Zyxel router: - // - // HTTP/1.1 200 OK - // ST:urn:schemas-upnp-org:device:InternetGatewayDevice:1 - // USN:uuid:11111111-%04x-c0a8-fcb30002cf618c::urn:schemas-upnp-org:device:InternetGatewayDevice:1 - // Location:http://192.168.252.179:80/DeviceDescription.xml - // Cache-Control: max-age=480 - // Server: ZyXEL-RomPlug/4.51 UPnP/1.0 IGD/1.00 - // Ext: - - while (m_pSocket->hasPendingDatagrams()) { - QByteArray datagram; - datagram.resize(m_pSocket->pendingDatagramSize()); - m_pSocket->readDatagram(datagram.data(), datagram.size()); - - qDebug("Received datagram: %s\n",datagram.data()); - - QString sspdResponse = QString::fromUtf8(datagram.data(), datagram.size()); - - // Find the location field manually, MimeMessage is not required - int locationStart = sspdResponse.indexOf("LOCATION:",0,Qt::CaseInsensitive); - int locationEnd = sspdResponse.indexOf("\r\n",locationStart,Qt::CaseSensitive); - - locationStart += 9; // length of field name - QString location = sspdResponse.mid(locationStart, locationEnd - locationStart); - - // Parse the URL syntax using KURL - QUrl url(location.trimmed()); - - qDebug("Found internet gateway: %s\n", location.toUtf8().data()); - // Emit success - emit deviceFound(url.host(), url.port(), url.path()); - + SsdpConnection::~SsdpConnection() + { + if(m_pSocket != 0) + { + m_pSocket->close(); + delete m_pSocket; + } } -} - -// Send a broadcast to detect all devices -void SsdpConnection::queryDevices(int bindPort) -{ - qDebug() << "UPnP::SsdpConnection: sending broadcast packet." << endl; - - // Send a packet to a broadcast address - QHostAddress address("239.255.255.250"); - - QString data = "M-SEARCH * HTTP/1.1\r\n" - "Host:239.255.255.250:1900\r\n" - "ST:urn:schemas-upnp-org:device:InternetGatewayDevice:1\r\n" - "Man:\"ssdp:discover\"\r\n" - "MX:3\r\n" - "\r\n"; - - // Bind the socket to a certain port - bool success = m_pSocket->bind(bindPort); - if(! success) + // Data was received by the socket + void SsdpConnection::slotDataReceived() { - qDebug() << "UPnP::SsdpConnection: failed to bind to port " << bindPort << "." << endl; + qDebug() << "UPnP::SsdpConnection: received " << m_pSocket->bytesAvailable() << " bytes." << endl; + + // Response from Diederik's Acatel router: + // + // HTTP/1.1 200 OK + // CACHE-CONTROL:max-age=1800 + // EXT: + // LOCATION:http://10.0.0.138:80/IGD.xml + // SERVER:SpeedTouch 510 4.0.2.0.0 UPnP/1.0 (0313QZ6S2) + // ST:upnp:rootdevice + // USN:uuid:UPnP-SpeedTouch510-1_00-90-D0-8E-A1-6F::upnp:rootdevice + // + + // This is from a Zyxel router: + // + // HTTP/1.1 200 OK + // ST:urn:schemas-upnp-org:device:InternetGatewayDevice:1 + // USN:uuid:11111111-%04x-c0a8-fcb30002cf618c::urn:schemas-upnp-org:device:InternetGatewayDevice:1 + // Location:http://192.168.252.179:80/DeviceDescription.xml + // Cache-Control: max-age=480 + // Server: ZyXEL-RomPlug/4.51 UPnP/1.0 IGD/1.00 + // Ext: + + while(m_pSocket->hasPendingDatagrams()) + { + QByteArray datagram; + datagram.resize(m_pSocket->pendingDatagramSize()); + m_pSocket->readDatagram(datagram.data(), datagram.size()); + + qDebug("Received datagram: %s\n", datagram.data()); + + QString sspdResponse = QString::fromUtf8(datagram.data(), datagram.size()); + + // Find the location field manually, MimeMessage is not required + int locationStart = sspdResponse.indexOf("LOCATION:", 0, Qt::CaseInsensitive); + int locationEnd = sspdResponse.indexOf("\r\n", locationStart, Qt::CaseSensitive); + + locationStart += 9; // length of field name + QString location = sspdResponse.mid(locationStart, locationEnd - locationStart); + + // Parse the URL syntax using KURL + QUrl url(location.trimmed()); + + qDebug("Found internet gateway: %s\n", location.toUtf8().data()); + // Emit success + emit deviceFound(url.host(), url.port(), url.path()); + } } - // Send the data - QByteArray dataBlock = data.toUtf8(); - int bytesWritten = m_pSocket->writeDatagram(dataBlock.data(), dataBlock.size(), address, 1900); - - if(bytesWritten == -1) + // Send a broadcast to detect all devices + void SsdpConnection::queryDevices(int bindPort) { - qDebug() << "UPnP::SsdpConnection: failed to send the UPnP broadcast packet." << endl; + qDebug() << "UPnP::SsdpConnection: sending broadcast packet." << endl; + + // Send a packet to a broadcast address + QHostAddress address("239.255.255.250"); + + QString data = "M-SEARCH * HTTP/1.1\r\n" + "Host:239.255.255.250:1900\r\n" + "ST:urn:schemas-upnp-org:device:InternetGatewayDevice:1\r\n" + "Man:\"ssdp:discover\"\r\n" + "MX:3\r\n" + "\r\n"; + + // Bind the socket to a certain port + bool success = m_pSocket->bind(bindPort); + if(!success) + { + qDebug() << "UPnP::SsdpConnection: failed to bind to port " << bindPort << "." << endl; + } + + // Send the data + QByteArray dataBlock = data.toUtf8(); + int bytesWritten = m_pSocket->writeDatagram(dataBlock.data(), dataBlock.size(), address, 1900); + + if(bytesWritten == -1) + { + qDebug() << "UPnP::SsdpConnection: failed to send the UPnP broadcast packet." << endl; + } } -} - - -} // end of namespace +} // end of namespace -- cgit v1.3.1-10-gc9f91