Make incomming UDP packets work.

This commit is contained in:
Andrius Štikonas 2015-01-13 17:34:28 +00:00
parent efcb702473
commit 216ac17338
2 changed files with 46 additions and 55 deletions

View File

@ -4,7 +4,8 @@ project(s20)
# Find includes in corresponding build directories # Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed. # Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON) # set(CMAKE_AUTOMOC OFF)
SET(CMAKE_CXX_FLAGS "-std=gnu++11")
# Find the QtWidgets library # Find the QtWidgets library
find_package(Qt5Network) find_package(Qt5Network)

98
s20.cpp
View File

@ -20,78 +20,68 @@
#include <QHostAddress> #include <QHostAddress>
#include <QUdpSocket> #include <QUdpSocket>
class Server : public QObject class Socket : public QObject
{ {
Q_OBJECT
public: public:
Server(); Socket();
bool received;
private slots:
void readPendingDatagrams();
private: private:
QByteArray* makeNewDatagram(); void readPendingDatagrams();
QByteArray* makeNewDatagram();
QUdpSocket *udpSocketSend; QUdpSocket *udpSocketSend;
QUdpSocket *udpSocketGet; QUdpSocket *udpSocketGet;
QHostAddress socketIPAddress;
enum {Discover, Subscribe, PowerOff, PowerOn};
QByteArray datagram[4] = {
QByteArray::fromHex("68 64 00 06 71 61"), // global discovery
QByteArray::fromHex("68 64 00 1e 63 6c ac cf 23 35 f5 8c 20 20 20 20 20 20 8c f5 35 23 cf ac 20 20 20 20 20 20"), // subscribe
QByteArray::fromHex("68 64 00 17 64 63 ac cf 23 35 f5 8c 20 20 20 20 20 20 00 00 00 00 00"), // power off
QByteArray::fromHex("68 64 00 17 64 63 ac cf 23 35 f5 8c 20 20 20 20 20 20 00 00 00 00 01") // power on
};
}; };
Server::Server(){ Socket::Socket() {
received = false; udpSocketSend = new QUdpSocket();
udpSocketSend = new QUdpSocket(); udpSocketGet = new QUdpSocket();
udpSocketGet = new QUdpSocket(); // QHostAddress *host = new QHostAddress("192.168.1.212");
QHostAddress *host = new QHostAddress("192.168.1.212"); QHostAddress *bcast = new QHostAddress("192.168.1.255");
QHostAddress *bcast = new QHostAddress("192.168.1.255");
udpSocketSend->connectToHost(*bcast, 10000); udpSocketSend->connectToHost(*bcast, 10000);
udpSocketGet->bind(*host, 10000); udpSocketGet->bind(QHostAddress::Any, 10000);
connect(udpSocketGet, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams())); // QObject::connect(udpSocketGet, &QIODevice::readyRead, this, &Socket::readPendingDatagrams);
QByteArray *datagram = makeNewDatagram(); // data from external function udpSocketSend->write(datagram[Discover]);
udpSocketSend->write(*datagram); readPendingDatagrams();
qWarning() << "sent";
} }
QByteArray* Server::makeNewDatagram() void Socket::readPendingDatagrams()
{ {
QByteArray *datagram = new QByteArray; while (udpSocketGet->waitForReadyRead(1000)) // 1s
*datagram = QByteArray::fromHex("68 64 00 06 71 61"); // global discovery {
// *datagram = QByteArray::fromHex("68 64 00 1e 63 6c ac cf 23 35 f5 8c 20 20 20 20 20 20 8c f5 35 23 cf ac 20 20 20 20 20 20"); // subscribe while (udpSocketGet->hasPendingDatagrams())
// *datagram = QByteArray::fromHex("68 64 00 17 64 63 ac cf 23 35 f5 8c 20 20 20 20 20 20 00 00 00 00 00"); // power off {
// *datagram = QByteArray::fromHex("68 64 00 17 64 63 ac cf 23 35 f5 8c 20 20 20 20 20 20 00 00 00 00 01"); // power on QByteArray datagramGet;
return datagram; datagramGet.resize(udpSocketGet->pendingDatagramSize());
} QHostAddress sender;
quint16 senderPort;
void Server::readPendingDatagrams() udpSocketGet->readDatagram(datagramGet.data(), datagramGet.size(), &sender, &senderPort);
{
qWarning() << "read0";
while (udpSocketGet->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(udpSocketGet->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
udpSocketGet->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort); if (datagramGet != datagram[Discover])
{
qWarning() << "read"; socketIPAddress = sender;
qWarning() << QString(datagram); }
// processTheDatagram(datagram); }
received = true;
} }
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QCoreApplication s20(argc, argv); QCoreApplication s20(argc, argv);
Server server; Socket socket;
while (!server.received) return 0;
{
true;
}
return 0;
} }
#include "s20.moc"