mirror of
https://github.com/ChronosX88/Influence-cjdns.git
synced 2024-11-23 15:42:18 +00:00
Basic functionality added to the Network class
This commit is contained in:
parent
2c5dca6f9f
commit
0ebdc434a1
36
src/kernel/network.cpp
Normal file
36
src/kernel/network.cpp
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
Network::Network()
|
||||||
|
{
|
||||||
|
udpSocket = new QUdpSocket(this);
|
||||||
|
udpSocket->bind(QHostAddress::AnyIPv6, 6552);
|
||||||
|
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
|
||||||
|
}
|
||||||
|
void Network::readPendingDatagrams()
|
||||||
|
{
|
||||||
|
while (udpSocket->hasPendingDatagrams()) {
|
||||||
|
QNetworkDatagram datagram = udpSocket->receiveDatagram();
|
||||||
|
processTheDatagram(datagram);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void Network::sendDatagram(json *j, QHostAddress ip)
|
||||||
|
{
|
||||||
|
QByteArray baDatagram;
|
||||||
|
QDataStream out(&baDatagram, QIODevice::WriteOnly);
|
||||||
|
QString s = j.dump();
|
||||||
|
out << s;
|
||||||
|
udpSocket->writeDatagram(baDatagram, ip, 6552);
|
||||||
|
}
|
||||||
|
json Network::processTheDatagram()
|
||||||
|
{
|
||||||
|
QByteArray baDatagram;
|
||||||
|
do {
|
||||||
|
baDatagram.resize(udpSocket->pendingDatagramSize ()) ;
|
||||||
|
udpSocket->readDatagram (baDatagram.data(), baDatagram.size()) ;
|
||||||
|
} while (udpSocket->hasPendingDatagrams()) ;
|
||||||
|
|
||||||
|
QString buff;
|
||||||
|
QDataStream in(&baDatagram, QIODevice::Readonly);
|
||||||
|
in >> buff;
|
||||||
|
json j = new json;
|
||||||
|
j = json::parse(buff);
|
||||||
|
return json;
|
||||||
|
}
|
16
src/kernel/network.hpp
Normal file
16
src/kernel/network.hpp
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#include "../lib/json/json.hpp"
|
||||||
|
#include <QtNetwork>
|
||||||
|
|
||||||
|
class Network
|
||||||
|
{
|
||||||
|
using json = nlohmann::json;
|
||||||
|
|
||||||
|
private:
|
||||||
|
QUdpSocket* udpSocket;
|
||||||
|
public:
|
||||||
|
Network();
|
||||||
|
public slots:
|
||||||
|
void sendDatagram();
|
||||||
|
private slots:
|
||||||
|
void readPendingDatagrams();
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user