diff --git a/.gitignore b/.gitignore index 133446b..c3d3c10 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ release/* HxUtils.pro.* Makefile* *.Debug -*.Release \ No newline at end of file +*.Release +*.TMP \ No newline at end of file diff --git a/HxBroadcast.cpp b/HxBroadcast.cpp new file mode 100644 index 0000000..cbd6817 --- /dev/null +++ b/HxBroadcast.cpp @@ -0,0 +1,17 @@ +#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); +} diff --git a/HxBroadcast.h b/HxBroadcast.h new file mode 100644 index 0000000..15478ab --- /dev/null +++ b/HxBroadcast.h @@ -0,0 +1,29 @@ +#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 diff --git a/HxUtils.pro b/HxUtils.pro index 92690c7..eaf7de9 100644 --- a/HxUtils.pro +++ b/HxUtils.pro @@ -29,6 +29,7 @@ DEFINES += QT_DEPRECATED_WARNINGS #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ + HxBroadcast.cpp \ HxDisk.cpp \ HxLog.cpp \ HxProcess.cpp \ @@ -40,6 +41,7 @@ SOURCES += \ HxTrace.cpp HEADERS += \ + HxBroadcast.h \ HxDisk.h \ HxLog.h \ HxProcess.h \