diff --git a/.gitignore b/.gitignore index c3d3c10..2b022e3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,9 @@ -debug/* -release/* -.qmake.stash -HxUtils.pro.* -Makefile* -*.Debug -*.Release -*.TMP \ No newline at end of file +debug/* +release/* +.qmake.stash +HxUtils.pro.* +Makefile* +*.Debug +*.Release +*.TMP +._* diff --git a/HxBroadcast.cpp b/HxBroadcast.cpp index cbd6817..d9990f9 100644 --- a/HxBroadcast.cpp +++ b/HxBroadcast.cpp @@ -1,17 +1,44 @@ -#include "HxBroadcast.h" - -#include - -int HxBroadcast::port = 0; -QUdpSocket *HxBroadcast::socket = nullptr; - -void HxBroadcast::initialization(int port) -{ - HxBroadcast::port = port; - HxBroadcast::socket = new QUdpSocket(); -} - -void HxBroadcast::publish(QString message) -{ - socket->writeDatagram(message.toUtf8(), QHostAddress::Broadcast, port); -} +#include "HxBroadcast.h" + +#include + +HxBroadcast *broadcast = nullptr; + +HxBroadcast* HxBroadcast::context() { return broadcast; } + +void HxBroadcast::initialization(int port) +{ + broadcast = new HxBroadcast(); + + broadcast->port = port; + broadcast->socket = new QUdpSocket(); + connect(broadcast->socket, &QUdpSocket::readyRead, broadcast, &HxBroadcast::receive_ready_read); + connect(broadcast, &HxBroadcast::publish_event, broadcast, &HxBroadcast::publish_achieve); +} + +void HxBroadcast::publish(QString message) +{ + emit broadcast->publish_event(message); +} + +void HxBroadcast::publish_achieve(QString message) +{ + socket->writeDatagram(message.toUtf8(), QHostAddress::Broadcast, broadcast->port); +} + +void HxBroadcast::receive_ready_read() +{ + /* 拥有等待的数据报 */ + while (socket->hasPendingDatagrams()) + { + QByteArray datagram; + + /* 让datagram的大小为等待处理的数据报的大小,这样才能接收到完整的数据 */ + datagram.resize(socket->pendingDatagramSize()); + + /* 接收数据报,将其存放到datagram中*/ + socket->readDatagram(datagram.data(), datagram.size()); + + emit broadcast->receive_event(QString::fromLocal8Bit(datagram)); + } +} diff --git a/HxBroadcast.h b/HxBroadcast.h index 15478ab..6724206 100644 --- a/HxBroadcast.h +++ b/HxBroadcast.h @@ -1,29 +1,42 @@ -#ifndef HXBROADCAST_H -#define HXBROADCAST_H - -#include -#include - -class HxBroadcast -{ -public: - /** - * @brief 初始化 - * - * @param port 广播端口 - */ - static void initialization(int port); - - /** - * @brief 发布消息 - * - * @param message 消息 - */ - static void publish(QString message); - -private: - static int port; - static QUdpSocket *socket; -}; - -#endif // HXBROADCAST_H +#ifndef HXBROADCAST_H +#define HXBROADCAST_H + +#include +#include + +class HxBroadcast : public QObject +{ + Q_OBJECT +public: + static HxBroadcast* context(); + + /** + * @brief 初始化 + * + * @param port 广播端口 + */ + static void initialization(int port); + + /** + * @brief 发布消息 + * + * @param message 消息 + */ + static void publish(QString message); + +private: + void publish_achieve(QString message); + +signals: + void publish_event(QString message); + void receive_event(QString data); + +private slots: + void receive_ready_read(); + +private: + int port; + QUdpSocket *socket; +}; + +#endif // HXBROADCAST_H