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++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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