void ClientSktUdp::newData() { QUdpSocket* s = qobject_cast<QUdpSocket*>(sender()); if (!s) return; qint64 bufLen = s->bytesAvailable(); char* buf = TK::createBuffer(bufLen, MAXBUFFER); if (!buf) return; qint64 readLen = 0; qint64 ioLen = s->read(buf, bufLen); while (ioLen > 0) { readLen += ioLen; ioLen = s->read(buf+readLen, bufLen-readLen); } if (ioLen >= 0) { recordRecv(readLen); dump(buf, readLen, false); } TK::releaseBuffer(buf); }
// TODO: sendCommand should only take one argument. The second is redundant. inline static void sendCommand( ScreenCommand & command, ScreenCommand::WaitMode wait = ScreenCommand::WAIT_BEFORE_UPDATE) { // qDebug("sendCommand at %s", qPrintable(QTime::currentTime().toString("mm:ss.zzz"))); command.wait_flags = wait; socket_.write(reinterpret_cast<const char *>(&command), sizeof(command)); if (wait & ScreenCommand::WAIT_COMMAND_FINISH) { if (socket_.waitForReadyRead(3000)) { socket_.read(reinterpret_cast<char *>(&command), sizeof(command)); } } }
void tst_QUdpSocket::writeToNonExistingPeer() { QSKIP("Connected-mode UDP sockets and their behaviour are erratic", SkipAll); QFETCH(QHostAddress, peerAddress); quint16 peerPort = 34534; qRegisterMetaType<QAbstractSocket::SocketError>("QAbstractSocket::SocketError"); QUdpSocket sConnected; QSignalSpy sConnectedReadyReadSpy(&sConnected, SIGNAL(readyRead())); QSignalSpy sConnectedErrorSpy(&sConnected, SIGNAL(error(QAbstractSocket::SocketError))); sConnected.connectToHost(peerAddress, peerPort, QIODevice::ReadWrite); // the first write succeeds... QCOMPARE(sConnected.write("", 1), qint64(1)); // the second one should fail! QTest::qSleep(1000); // do not process events QCOMPARE(sConnected.write("", 1), qint64(-1)); QCOMPARE(int(sConnected.error()), int(QUdpSocket::ConnectionRefusedError)); // the third one will succeed... QCOMPARE(sConnected.write("", 1), qint64(1)); QTestEventLoop::instance().enterLoop(1); QCOMPARE(sConnectedReadyReadSpy.count(), 0); QCOMPARE(sConnectedErrorSpy.count(), 1); QCOMPARE(int(sConnected.error()), int(QUdpSocket::ConnectionRefusedError)); // we should now get a read error QCOMPARE(sConnected.write("", 1), qint64(1)); QTest::qSleep(1000); // do not process events char buf[2]; QVERIFY(!sConnected.hasPendingDatagrams()); QCOMPARE(sConnected.bytesAvailable(), Q_INT64_C(0)); QCOMPARE(sConnected.pendingDatagramSize(), Q_INT64_C(-1)); QCOMPARE(sConnected.readDatagram(buf, 2), Q_INT64_C(-1)); QCOMPARE(int(sConnected.error()), int(QUdpSocket::ConnectionRefusedError)); QCOMPARE(sConnected.write("", 1), qint64(1)); QTest::qSleep(1000); // do not process events QCOMPARE(sConnected.read(buf, 2), Q_INT64_C(0)); QCOMPARE(int(sConnected.error()), int(QUdpSocket::ConnectionRefusedError)); // we should still be connected QCOMPARE(int(sConnected.state()), int(QUdpSocket::ConnectedState)); }
int udp_ping(QStringList command) { qDebug() << "udp_ping(" << command.join(" ") << ")" << endl; qDebug() << "udp_ping(" << command.join(" ") << ")" << endl; /** * Check input */ QTextStream errorStream(stderr); if(command.size() != 3 || command.at(0)!="ping" || command.at(1)!="udp" ) { errorStream << "Error: udp_ping(" << command.join(" ") << ") is no valid call (ping udp <ip_address> <port> rzv|max|random|default)" << endl; return 1; } /** * <functionality> */ QByteArray byteArray; /** * CIP for "rzv" */ if(command.at(2)=="rzv") { qDebug() << "rzv" << endl; byteArray.append(QByteArray(42, '\0')); } /** * Sent via TCP */ QUdpSocket *udpSocket; udpSocket = new QUdpSocket(); QTextStream outStream(stdout); QString out; udpSocket->abort(); udpSocket->connectToHost("127.0.0.1", 22366); qDebug() << "waitForConnected!"; if (udpSocket->waitForConnected(5000)) { qDebug() << "Connected!"; } else { errorStream << "Error: udp_ping(" << command.join(" ") << "): No connection available!" << endl; return 1; } qDebug() << QString("BytesWritten: %1").arg(udpSocket->write(byteArray, byteArray.length())); udpSocket->flush(); int numRead = 0, numReadTotal = 0; char buffer[MAXMSG]; forever { numRead = udpSocket->read(buffer, MAXMSG); qDebug() << "read buffer: " << numRead; numReadTotal += numRead; if (numRead <= 0 && !udpSocket->waitForReadyRead(30)) break; } qDebug() << numReadTotal << " bytes red"; if(numReadTotal==-1) { errorStream << "Error: udp_ping(" << command.join(" ") << "): " << udpSocket->errorString() << endl; return 1; } else { for(int i=0; i < numReadTotal; i++) { qDebug() << QString("receipt[%1]: %2\n").arg(i).arg(buffer[i], 8, 2, QLatin1Char('0')) << endl;; } QByteArray receipt(buffer); qDebug() << "receipt.size(): " << receipt.size(); for(int i = 0; i < receipt.size();++i) { qDebug() << QString("receipt[%1]: %2\n").arg(i).arg(receipt.at(i), 8, 2, QLatin1Char('0')) << endl;; } qDebug() << "buffer: " << buffer; } udpSocket->disconnectFromHost(); udpSocket->close(); outStream << out << endl; return 0; }