#ifndef UDPSOCKETWORKER_H #define UDPSOCKETWORKER_H #include #include #include class UdpSocketWorker : public QObject { Q_OBJECT public: explicit UdpSocketWorker(const QHostAddress hostAddr, const int port, QObject *parent = nullptr); ~UdpSocketWorker(); public slots: void sendData(const QByteArray &data, const QHostAddress &address, quint16 port); void processPendingDatagrams(); void startSending(const QString &id, const QByteArray &data, const QHostAddress &address, const quint16 port, int interval = 1000); void stopSending(const QString &id); signals: void dataReceived(const QByteArray &data); private: QUdpSocket *m_udpSocket; QMap m_timers; // 存储多个定时器,使用ID标识 QMap m_dataToSend; // 存储每个定时器的发送数据 QMap m_targetAddress; }; #endif // UDPSOCKETWORKER_H