Changed the Network class constructor

Now sockets do not overlap (if you create an object of class Network in some other class).
This commit is contained in:
Denis Davydov 2018-06-21 18:23:36 +03:00
parent 7fa9deb1d4
commit a2fee2fe26
4 changed files with 11 additions and 8 deletions

View File

@ -2,7 +2,7 @@
Handler::Handler() Handler::Handler()
{ {
network = new Network(); network = new Network(false);
connect(network, &Network::json_received, this, &Handler::handle); connect(network, &Network::json_received, this, &Handler::handle);
} }
@ -12,7 +12,7 @@ void Handler::handle(QJsonObject jsonReceived)
QJsonObject jsonSend; QJsonObject jsonSend;
if(jsonReceived["action"] == "createSession" && !jsonReceived.contains("status")) if(jsonReceived["action"] == "createSession" && !jsonReceived.contains("status"))
{ {
jsonSend["peerID"] = QHostAddress("::1").toString(); jsonSend["peerID"] = QHostAddress("fc8f:cc50:70b0:3731:d686:a75e:94f2:f44f").toString();
jsonSend["action"] = "createSession"; jsonSend["action"] = "createSession";
jsonSend["status"] = true; jsonSend["status"] = true;
QString peerReceiver = jsonReceived["peerID"].toString(); QString peerReceiver = jsonReceived["peerID"].toString();

View File

@ -1,10 +1,13 @@
#include "network.hpp" #include "network.hpp"
Network::Network() Network::Network(bool is_server)
{ {
udpSocket = new QUdpSocket(this); udpSocket = new QUdpSocket(this);
if (is_server)
{
udpSocket->bind(QHostAddress::AnyIPv6, 6552); udpSocket->bind(QHostAddress::AnyIPv6, 6552);
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processTheDatagram())); connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processTheDatagram()));
} }
}
void Network::sendDatagram(QJsonObject j, QString s) void Network::sendDatagram(QJsonObject j, QString s)
{ {

View File

@ -11,7 +11,7 @@ class Network : public QObject
private: private:
QUdpSocket* udpSocket; QUdpSocket* udpSocket;
public: public:
Network(); Network(bool is_server = true);
public slots: public slots:
void sendDatagram(QJsonObject j, QString s); void sendDatagram(QJsonObject j, QString s);
signals: signals:

View File

@ -6,7 +6,7 @@ MainWindow::MainWindow(QWidget *parent) :
ui(new Ui::MainWindow) ui(new Ui::MainWindow)
{ {
ui->setupUi(this); ui->setupUi(this);
network = new Network(); network = new Network(true);
handler = new Handler(); handler = new Handler();
} }
@ -18,7 +18,7 @@ MainWindow::~MainWindow()
void MainWindow::on_connectToPeer_clicked() void MainWindow::on_connectToPeer_clicked()
{ {
QJsonObject j; QJsonObject j;
j["peerID"] = QHostAddress("::1").toString(); j["peerID"] = QHostAddress("fc8f:cc50:70b0:3731:d686:a75e:94f2:f44f").toString();
j["action"] = "createSession"; j["action"] = "createSession";
QString s = ui->peerID->text(); QString s = ui->peerID->text();
network->sendDatagram(j, s); network->sendDatagram(j, s);