本文整理汇总了C++中QUdpSocket::hasPendingDatagrams方法的典型用法代码示例。如果您正苦于以下问题:C++ QUdpSocket::hasPendingDatagrams方法的具体用法?C++ QUdpSocket::hasPendingDatagrams怎么用?C++ QUdpSocket::hasPendingDatagrams使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QUdpSocket
的用法示例。
在下文中一共展示了QUdpSocket::hasPendingDatagrams方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: slReceive
void Connection::slReceive() {
QUdpSocket* sock = (QUdpSocket*) QObject::sender();
const bool main = (sock->localPort() == sets.portMain) ? true : false;
const bool aux = (sock->localPort() == sets.portAux) ? true : false;
if (sock->hasPendingDatagrams()) {
if (main) emit sgRxMain();
if (aux) emit sgRxAux();
}
int size = 0;
bool enabled = false;
if (main) enabled = sets.main;
if (aux) enabled = sets.aux;
while (sock->hasPendingDatagrams() && run && enabled) {
size = sock->pendingDatagramSize();
convertor->setBufSize(size);
sock->readDatagram(convertor->getBuf(), size);
if (convertor->convert()) emit sgWr();
else emit sgNoWr();
if (main) timerRxMain->start();
if (aux) timerRxAux->start();
}
}
示例2: broadcasting
void tst_QUdpSocket::broadcasting()
{
QFETCH_GLOBAL(bool, setProxy);
if (setProxy) {
#ifdef TEST_QNETWORK_PROXY
QFETCH_GLOBAL(int, proxyType);
if (proxyType == QNetworkProxy::Socks5Proxy) {
QSKIP("With socks5 Broadcast is not supported.", SkipAll);
}
#endif
}
#ifdef Q_OS_AIX
QSKIP("Broadcast does not work on darko", SkipAll);
#endif
const char *message[] = {"Yo mista", "", "Yo", "Wassap"};
for (int i = 0; i < 4; ++i) {
QUdpSocket serverSocket;
QVERIFY2(serverSocket.bind(QHostAddress::Any, 5000), serverSocket.errorString().toLatin1().constData());
QCOMPARE(serverSocket.state(), QUdpSocket::BoundState);
connect(&serverSocket, SIGNAL(readyRead()), SLOT(empty_readyReadSlot()));
QUdpSocket broadcastSocket;
for (int j = 0; j < 100; ++j) {
broadcastSocket.writeDatagram(message[i], strlen(message[i]),
QHostAddress::Broadcast, 5000);
QTestEventLoop::instance().enterLoop(15);
if (QTestEventLoop::instance().timeout()) {
#if defined(Q_OS_FREEBSD)
QEXPECT_FAIL("",
"Broadcasting to 255.255.255.255 does not work on FreeBSD",
Abort);
QVERIFY(false); // seems that QFAIL() doesn't respect the QEXPECT_FAIL() :/
#endif
QFAIL("Network operation timed out");
}
QVERIFY(serverSocket.hasPendingDatagrams());
do {
QByteArray arr;
arr.resize(serverSocket.pendingDatagramSize() + 1);
QHostAddress host;
quint16 port;
QCOMPARE((int) serverSocket.readDatagram(arr.data(), arr.size() - 1, &host, &port),
(int) strlen(message[i]));
arr.resize(strlen(message[i]));
QCOMPARE(arr, QByteArray(message[i]));
} while (serverSocket.hasPendingDatagrams());
}
}
}
示例3: onStunReadyRead
void StunClient::onStunReadyRead()
{
QUdpSocket *sock = (QUdpSocket*)(sender());
qDebug()<<""<<sock;
u08bits rbuf[STUN_BUFFER_SIZE];
while (sock->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(sock->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
sock->readDatagram(datagram.data(), datagram.size(),
&sender, &senderPort);
{
m_sending_udp = false;
m_sending_timer->stop();
}
qDebug()<<"read: "<<sender<<senderPort<<datagram.length()<<datagram.toHex().left(20)<<"...";
for (int i = 0; i < datagram.length(); i ++) {
char c = datagram.at(i);
fprintf(stderr, "%c", isprint(c) ? c : '.');
if (i > 375) break;
}
fprintf(stderr, " ...]\n");
this->processResponse(datagram, QString("%1:%2").arg(sender.toString()).arg(senderPort));
// processTheDatagram(datagram);
}
}
示例4: hasDmx
void QDmxE131Controler::readDatagram()
{
if(sender())
{
QUdpSocket* socket = qobject_cast<QUdpSocket*>(sender());
if(socket)
{
while(socket->hasPendingDatagrams())
{
QNetworkDatagram datagram = socket->receiveDatagram();
if(datagram.senderAddress().isInSubnet(_address, _entry.prefixLength()))
{
QByteArray rawData = datagram.data();
if(_packetizer->checkPacket(rawData))
{
QByteArray dmx;
quint32 u;
_packetizer->fillDMXdata(rawData, dmx, u);
u--;
if(_listenedUniverse.contains(u))
emit hasDmx(u, dmx);
}
}
}
}
}
}
示例5: controlMessageReceived
void MainWindow::controlMessageReceived() {
qDebug() << "message received";
QUdpSocket* udpSocket = qobject_cast<QUdpSocket*>(sender());
while (udpSocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
udpSocket->readDatagram(datagram.data(), datagram.size(),
&sender, &senderPort);
if (datagram=="left") {
drone->moveLeft();
} else if (datagram== "right") {
drone->moveRight();
} else if (datagram=="stayx" && datagram=="stayy"){
drone->stay();
} else if (datagram=="up") {
drone->higher();
} else if (datagram=="down") {
drone->lower();
} else {
qDebug() << "invalid command received";
}
}
}
示例6: next
// Called whenever there is data available on the device.
void SignalChunker::next(QIODevice* device)
{
// Get a pointer to the UDP socket.
QUdpSocket* udpSocket = static_cast<QUdpSocket*>(device);
_bytesRead = 0;
// Get writable buffer space for the chunk.
WritableData writableData = getDataStorage(_chunkSize);
if (writableData.isValid()) {
// Get pointer to start of writable memory.
char* ptr = (char*) (writableData.ptr());
// Read datagrams for chunk from the UDP socket.
while (isActive() && _bytesRead < _chunkSize) {
// Read the datagram, but avoid using pendingDatagramSize().
if (!udpSocket->hasPendingDatagrams()) {
// MUST WAIT for the next datagram.
udpSocket->waitForReadyRead(100);
continue;
}
qint64 maxlen = _chunkSize - _bytesRead;
qint64 length = udpSocket->readDatagram(ptr + _bytesRead, maxlen);
if (length > 0)
_bytesRead += length;
}
}
// Must discard the datagram if there is no available space.
else {
udpSocket->readDatagram(0, 0);
}
}
示例7: processIncomingWaves
void OceanSocket::processIncomingWaves()
{
QUdpSocket* socket = dynamic_cast<QUdpSocket*>(QObject::sender());
while (socket->hasPendingDatagrams())
{
QByteArray wave;
QHostAddress host;
wave.resize(socket->pendingDatagramSize());
socket->readDatagram(wave.data(), wave.size(), &host);
for (int i = 0; i < boundAddresses.count(); i++) {
if (host == boundAddresses.at(i))
// skip processing since this is our own packet
return;
}
if (wave.startsWith(NET_OCEAN_HEARTBEAT))
emit shipHeartbeat(host, wave.mid(1).data());
else if (wave.startsWith(NET_OCEAN_COURSE_CHANGE))
emit shipChangedCourse(host, wave.mid(1).data());
else if (wave.startsWith(NET_OCEAN_RETURN_TO_PORT))
emit shipDocked(host, wave.mid(1).data());
else if (wave.startsWith(NET_OCEAN_ADMIN_LOCK_REQ))
emit adminLockRequested(host, wave.mid(1).data());
else if (wave.startsWith(NET_OCEAN_ADMIN_LOCK_RESP))
emit adminLockRequestReponse(host, (atoi(wave.mid(1,1).data()) == NET_OCEAN_ACKNOWLEDGE), wave.mid(2).data());
else if (wave.startsWith(NET_OCEAN_MESSAGE))
this->processMessage(host, wave.mid(1).data());
else if (wave.startsWith(NET_OCEAN_SAIL))
emit shipSailed(host, wave.mid(1).data());
}
}
示例8: readPendingAudioRcvrData
void AudioReceiver::readPendingAudioRcvrData() {
QUdpSocket *socket = qobject_cast<QUdpSocket *>(sender());
while (socket->hasPendingDatagrams()) {
m_datagram.resize(socket->pendingDatagramSize());
if (socket->readDatagram(m_datagram.data(), m_datagram.size()) < 0) {
AUDIO_RECEIVER << "read client" << m_client << "socket failed.";
if (io->rcveIQ_toggle) { // toggles the rcveIQ signal
emit rcveIQEvent(this, 2);
io->rcveIQ_toggle = false;
}
}
else {
io->au_queue.enqueue(m_datagram);
if (!io->rcveIQ_toggle) { // toggles the rcveIQ signal
emit rcveIQEvent(this, 1);
io->rcveIQ_toggle = true;
}
}
}
}
示例9: readyReadMessage
void SocketTestWindow::readyReadMessage()
{
if(m_tcpEnable)
{
QByteArray data = m_socketClient->readAll();
QString rd = data;
// char logbuf[BUFSIZ] = {0};
// sprintf(logbuf,"receive data from %s:%d %s",)
// QLogProvider::getLog()->log(logbuf);
ui->m_teRecvData->setText(rd);
}
else
{
QUdpSocket* udpSocket = dynamic_cast<QUdpSocket*>(m_socketClient);
while (udpSocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(), datagram.size(),
&m_remoteHost, &m_remotePort);
char logbuf[BUFSIZ] = {0};
sprintf(logbuf,"receive datagram from %s:%d",m_remoteHost.toString().toStdString().c_str(),m_remotePort);
ui->m_teRecvData->setText(datagram);
QLogProvider::getLog()->log(logbuf);
}
}
}
示例10: performance
void tst_QUdpSocket::performance()
{
QUdpSocket server;
QVERIFY2(server.bind(), server.errorString().toLatin1().constData());
QHostAddress serverAddress = QHostAddress::LocalHost;
if (!(server.localAddress() == QHostAddress::Any))
serverAddress = server.localAddress();
QUdpSocket client;
client.connectToHost(serverAddress, server.localPort());
QByteArray arr(8192, '@');
QTime stopWatch;
stopWatch.start();
qint64 nbytes = 0;
while (stopWatch.elapsed() < 5000) {
for (int i = 0; i < 100; ++i) {
if (client.write(arr.data(), arr.size()) > 0) {
do {
nbytes += server.readDatagram(arr.data(), arr.size());
} while (server.hasPendingDatagrams());
}
}
}
float secs = stopWatch.elapsed() / 1000.0;
qDebug("\t%.2fMB/%.2fs: %.2fMB/s", float(nbytes / (1024.0*1024.0)),
secs, float(nbytes / (1024.0*1024.0)) / secs);
}
示例11: processPendingDatagrams
void ICServer::processPendingDatagrams()
{
QByteArray datagram;
QUdpSocket *socket = isRunning ? udpSocket : testUdpSocket;
do {
QHostAddress *address = new QHostAddress;
datagram.resize(socket->pendingDatagramSize());
socket->readDatagram(datagram.data(), datagram.size(), address);
ICMessageHandler *messageHandler = new ICMessageHandler(this);
connect(messageHandler, SIGNAL(connectionOffer(QString)), this, SLOT(processSidCollision(QString)));
connect(messageHandler, SIGNAL(serverDiscovery(QHostAddress,QString)), this, SLOT(processDiscovery(QHostAddress,QString)));
connect(messageHandler, SIGNAL(questionRequest(QHostAddress)), this, SLOT(processRequest(QHostAddress)));
connect(messageHandler, SIGNAL(answerReady(ICAnswer,QString)), this, SLOT(processAnswer(ICAnswer,QString)));
ICMessageProcessor *processor = new ICMessageProcessor(messageHandler, *address, datagram);
QThreadPool::globalInstance()->start(processor);
qDebug()<< "[1] Received from " << *address << datagram;
delete address;
} while (socket->hasPendingDatagrams());
}
示例12: pendingDatagramSize
void tst_QUdpSocket::pendingDatagramSize()
{
QUdpSocket server;
QVERIFY2(server.bind(), server.errorString().toLatin1().constData());
QHostAddress serverAddress = QHostAddress::LocalHost;
if (!(server.localAddress() == QHostAddress::Any))
serverAddress = server.localAddress();
QUdpSocket client;
QVERIFY(client.writeDatagram("this is", 7, serverAddress, server.localPort()) == 7);
QVERIFY(client.writeDatagram(0, 0, serverAddress, server.localPort()) == 0);
QVERIFY(client.writeDatagram("3 messages", 10, serverAddress, server.localPort()) == 10);
char c = 0;
QVERIFY(server.waitForReadyRead());
if (server.hasPendingDatagrams()) {
#if defined Q_OS_HPUX && defined __ia64
QEXPECT_FAIL("", "HP-UX 11i v2 can't determine the datagram size correctly.", Abort);
#endif
QCOMPARE(server.pendingDatagramSize(), qint64(7));
c = '\0';
QCOMPARE(server.readDatagram(&c, 1), qint64(1));
QCOMPARE(c, 't');
c = '\0';
} else {
QSKIP("does not have the 1st datagram", SkipSingle);
}
if (server.hasPendingDatagrams()) {
QCOMPARE(server.pendingDatagramSize(), qint64(0));
QCOMPARE(server.readDatagram(&c, 1), qint64(0));
QCOMPARE(c, '\0'); // untouched
c = '\0';
} else {
QSKIP("does not have the 2nd datagram", SkipSingle);
}
if (server.hasPendingDatagrams()) {
QCOMPARE(server.pendingDatagramSize(), qint64(10));
QCOMPARE(server.readDatagram(&c, 1), qint64(1));
QCOMPARE(c, '3');
} else {
QSKIP("does not have the 3rd datagram", SkipSingle);
}
}
示例13: DataMayRead
void CNetwork::DataMayRead( )
{
QByteArray byData;
QUdpSocket* udpServer = qobject_cast< QUdpSocket* > ( sender( ) );
while ( udpServer->hasPendingDatagrams( ) ) {
byData.resize( udpServer->pendingDatagramSize( ) );
udpServer->readDatagram( byData.data( ), byData.size( ) );
ProcessData( byData );
}
}
示例14: zeroLengthDatagram
void tst_QUdpSocket::zeroLengthDatagram()
{
QFETCH_GLOBAL(bool, setProxy);
if (setProxy)
return;
QUdpSocket receiver;
QVERIFY(receiver.bind());
QVERIFY(!receiver.waitForReadyRead(100));
QVERIFY(!receiver.hasPendingDatagrams());
QUdpSocket sender;
QCOMPARE(sender.writeDatagram(QByteArray(), QHostAddress::LocalHost, receiver.localPort()), qint64(0));
QVERIFY(receiver.waitForReadyRead(1000));
QVERIFY(receiver.hasPendingDatagrams());
char buf;
QCOMPARE(receiver.readDatagram(&buf, 1), qint64(0));
}
示例15: newUdpDatagram
void ServerPool::newUdpDatagram(void)
{
QUdpSocket *socket = dynamic_cast<QUdpSocket*>(sender());
while (socket->state() == QAbstractSocket::BoundState &&
socket->hasPendingDatagrams())
{
QByteArray buffer;
buffer.resize(socket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
socket->readDatagram(buffer.data(), buffer.size(),
&sender, &senderPort);
emit newDatagram(buffer, sender, senderPort);
}
}