本文整理汇总了C++中QUdpSocket::pendingDatagramSize方法的典型用法代码示例。如果您正苦于以下问题:C++ QUdpSocket::pendingDatagramSize方法的具体用法?C++ QUdpSocket::pendingDatagramSize怎么用?C++ QUdpSocket::pendingDatagramSize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QUdpSocket
的用法示例。
在下文中一共展示了QUdpSocket::pendingDatagramSize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Broadcast
QString connection::Broadcast() {
// Om ip adressen te ontdekken
QNetworkAddressEntry inter;
// sockets aanmaken en verbinden met enerzijds broadcast en anderzijds een luister poort
QUdpSocket udpSocketSend;
QUdpSocket udpSocketGet;
udpSocketSend.connectToHost(inter.broadcast(), 40000);
// udpSocketGet->bind(inter->ip(),667);
// udpSocketGet->bind(QHostAddress::Any,667)
if(udpSocketGet.bind(udpgetport,QUdpSocket::ShareAddress))
Label->setText(Label->text() + "[INFO] Could properly bind udpSocketget to " + QString::number(udpgetport) + "\n");
else Label->setText(Label->text() + "[INFO] Couldn't properly bind udpSocketget to " + QString::number(udpgetport) + "\n");
// Pakket verzenden
QByteArray send_datagram = "DISCOVER-STRATEGO-SERVER";
// Optimalisatie voor in de loop
QByteArray receive_datagram;
quint16 serverPort;
forever{
udpSocketSend.writeDatagram(send_datagram, QHostAddress::Broadcast, 40000);
if(udpSocketGet.waitForReadyRead(3000)){
receive_datagram.resize(udpSocketGet.pendingDatagramSize());
udpSocketGet.readDatagram(receive_datagram.data(),receive_datagram.size(),&server,&serverPort);
if(QString::fromUtf8(receive_datagram.data()) == "DISCOVERED-STRATEGO-SERVER")
{
receive_datagram.resize(udpSocketGet.pendingDatagramSize());
udpSocketGet.readDatagram(receive_datagram.data(),receive_datagram.size(),&server,&serverPort);
Label->setText(Label->text() +"[INFO] PLAYER DATA: "+ receive_datagram.data() +"\n");
speler = receive_datagram.toInt();
if(speler==1)
{
attacker=true;
}
else
{
attacker=false;
}
receive_datagram.resize(udpSocketGet.pendingDatagramSize());
udpSocketGet.readDatagram(receive_datagram.data(),receive_datagram.size(),&server,&serverPort);
Label->setText(Label->text() + "[INFO] GAME DATA: "+receive_datagram.data()+"\n");
spel = receive_datagram.toInt();
Label->setText(Label->text() + " SPEL:" + QString::number(spel) + "\n");
Label->setText(Label->text() + "[INFO] Found STRATEGO-SERVER on " + server.toString().toUtf8().constData() + "\n");
return server.toString();
}
}
else
{
Label->setText(Label->text() + "[INFO] UDP Discover TimeOut!\n");
static int timeout=0;
timeout++;
if(timeout==5)
{
Label->setText(Label->text() + "[ERROR] Server is not online. Please try again later!");
return "";
}
}
}
return "";
}
示例2: 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();
}
}
示例3: testSendKeyInputPacket
void TestClient::testSendKeyInputPacket(){
Client* client = new Client(NULL);
QUdpSocket* socket = new QUdpSocket();
socket->bind(INCOMING_PORT);
client->setDirection(1);
client->setIsJumping(false);
client->setIsLaunching(true);
client->sendDatagram(KEY_INPUT_PACKET);
QHostAddress *address = NULL;
quint16 *port = NULL;
QByteArray *datagram = new QByteArray(socket->pendingDatagramSize(), (char) 0);
socket->readDatagram(datagram->data(), datagram->size(), address, port);
QCOMPARE(datagram->at(0), KEY_INPUT_PACKET);
QCOMPARE((int)datagram->at(1), -1);
QCOMPARE((int)datagram->at(2), 1);
QCOMPARE((bool)datagram->at(3), false);
QCOMPARE((bool)datagram->at(4), true);
delete socket;
}
示例4: 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";
}
}
}
示例5: 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());
}
}
示例6: 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;
}
}
}
}
示例7: 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);
}
}
}
示例8: 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);
}
}
示例9: 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());
}
示例10: onClientUdpSocketReadyRead
void UdpRelay::onClientUdpSocketReadyRead()
{
QUdpSocket *sock = qobject_cast<QUdpSocket *>(sender());
if (!sock) {
emit info("Fatal. A false object calling onClientUdpSocketReadyRead.");
return;
}
if (sock->pendingDatagramSize() > RecvSize) {
emit info("[UDP] Datagram is too large. Discarded.");
return;
}
QByteArray data;
data.resize(sock->pendingDatagramSize());
QHostAddress r_addr;
quint16 r_port;
sock->readDatagram(data.data(), RecvSize, &r_addr, &r_port);
QByteArray response;
if (isLocal) {
data = encryptor->decryptAll(data);
Address destAddr;
int header_length = 0;
bool _auth;
Common::parseHeader(data, destAddr, header_length, _auth);
if (header_length == 0) {
emit info("[UDP] Can't parse header. "
"Wrong encryption method or password?");
return;
}
response = QByteArray(3, static_cast<char>(0)) + data;
} else {
data.prepend(Common::packAddress(r_addr, r_port));
response = encryptor->encryptAll(data);
}
Address clientAddress = cache.key(sock);
if (clientAddress.getPort() != 0) {
listenSocket.writeDatagram(response,
clientAddress.getFirstIP(),
clientAddress.getPort());
} else {
emit debug("[UDP] Drop a packet from somewhere else we know.");
}
}
示例11: 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);
}
}
示例12: 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 );
}
}
示例13: 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());
}
}
}
示例14: newData
void ServerSktUdp::newData()
{
QUdpSocket* s = qobject_cast<QUdpSocket*>(sender());
if (!s) return;
qint64 bufLen = s->pendingDatagramSize();
char* buf = TK::createBuffer(bufLen, MAXBUFFER);
if (!buf) return;
QHostAddress addr; quint16 port(0);
qint64 readLen = 0;
qint64 ioLen = s->readDatagram(buf, bufLen, &addr, &port);
//while (ioLen > 0)
//{
readLen += ioLen;
// ioLen = s->readDatagram(buf+readLen, bufLen-readLen, &addr, &port);
//}
if (ioLen >= 0)
{
Conn* conn = (Conn*)getCookie(TK::ipstr(addr, port, false));
if (!conn)
{
conn = new Conn;
if (conn)
{
conn->key = TK::ipstr(addr, port, false);
conn->addr = addr;
conn->port = port;
setCookie(conn->key, conn);
}
}
if (conn)
{
recordRecv(readLen);
conn->stamp = QDateTime::currentDateTime();
dump(buf, readLen, false, conn->key);
}
}
TK::releaseBuffer(buf);
}
示例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);
}
}