You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
1012 B
C

3 months ago
#ifndef UDPSOCKETWORKER_H
#define UDPSOCKETWORKER_H
#include <QObject>
#include <QUdpSocket>
#include <QTimer>
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<QString, QTimer *> m_timers; // 存储多个定时器使用ID标识
QMap<QString, QByteArray> m_dataToSend; // 存储每个定时器的发送数据
QMap<QString, QHostAddress> m_targetAddress;
};
#endif // UDPSOCKETWORKER_H