2021-04-24 17:21:31 +00:00
|
|
|
#ifndef NETWORK_CONTROLLER_H
|
|
|
|
#define NETWORK_CONTROLLER_H
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QUdpSocket>
|
|
|
|
#include <QTcpSocket>
|
|
|
|
#include <QThread>
|
2021-05-02 18:02:25 +00:00
|
|
|
#include <QTcpServer>
|
2021-05-03 06:07:04 +00:00
|
|
|
#include <QNetworkDatagram>
|
2021-09-08 15:55:27 +00:00
|
|
|
#include <QMap>
|
2021-04-24 17:21:31 +00:00
|
|
|
|
2021-05-03 05:50:07 +00:00
|
|
|
// this is not a thread-safe class,any interface invoked in multi-thread maybe will cause unkown falut
|
2021-04-29 14:26:45 +00:00
|
|
|
class NetworkController : public QObject
|
2021-04-24 17:21:31 +00:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
2021-04-29 14:26:45 +00:00
|
|
|
public:
|
2021-04-24 17:21:31 +00:00
|
|
|
typedef enum{
|
2021-04-29 14:26:45 +00:00
|
|
|
TYPE_UDP_SERVER = 0,
|
2021-05-01 15:41:40 +00:00
|
|
|
TYPE_TCP_SERVER = 1,
|
|
|
|
TYPE_UDP_CLIENT = 2,
|
|
|
|
TYPE_TCP_CLIENT = 3,
|
2021-05-02 18:02:25 +00:00
|
|
|
TYPE_UNKOWN = 4,
|
2021-05-03 08:49:33 +00:00
|
|
|
}NetworkType;
|
|
|
|
typedef struct{
|
|
|
|
uint32_t len;
|
|
|
|
QString addr;
|
|
|
|
QString port;
|
|
|
|
}RecvResult;
|
2021-04-29 14:26:45 +00:00
|
|
|
|
2021-05-03 08:49:33 +00:00
|
|
|
NetworkType Type();
|
|
|
|
NetworkController(NetworkType type,QString ip,uint16_t port);
|
2021-04-29 14:26:45 +00:00
|
|
|
int SendData(int8_t *data,uint32_t len);
|
2021-05-03 08:49:33 +00:00
|
|
|
RecvResult ReadData(int8_t *data);
|
2021-05-03 05:50:07 +00:00
|
|
|
int Close();
|
2021-04-29 14:26:45 +00:00
|
|
|
~NetworkController();
|
2021-08-29 08:13:50 +00:00
|
|
|
bool State();
|
2021-04-24 17:21:31 +00:00
|
|
|
public slots:
|
|
|
|
void on_ready_read();
|
|
|
|
void on_disconect();
|
2021-05-02 18:02:25 +00:00
|
|
|
void on_server_accept();
|
2021-05-03 05:50:07 +00:00
|
|
|
void on_accept_error(QAbstractSocket::SocketError socketError);
|
2021-08-29 15:50:41 +00:00
|
|
|
private slots:
|
|
|
|
void on_conected();
|
|
|
|
void tcpsock_stateChanged(QAbstractSocket::SocketState);
|
2021-04-24 17:21:31 +00:00
|
|
|
signals:
|
2021-08-29 15:50:41 +00:00
|
|
|
void on_disconnectd();
|
|
|
|
void on_connected();
|
2021-04-24 17:21:31 +00:00
|
|
|
void on_data_recv();
|
|
|
|
void on_conection_ok();
|
|
|
|
void on_conection_close();
|
|
|
|
void on_send_data(QByteArray);
|
|
|
|
private:
|
2021-05-03 08:49:33 +00:00
|
|
|
NetworkType _checkType(NetworkType);
|
2021-05-02 18:02:25 +00:00
|
|
|
|
2021-05-03 08:49:33 +00:00
|
|
|
NetworkType mType;
|
2021-04-24 17:21:31 +00:00
|
|
|
QUdpSocket *mUDP;
|
|
|
|
QTcpSocket *mTcp;
|
2021-05-02 18:02:25 +00:00
|
|
|
QTcpServer *mTcpServer;
|
2021-04-24 17:21:31 +00:00
|
|
|
QIODevice *mCnn;
|
2021-09-08 15:55:27 +00:00
|
|
|
QMap<qintptr,QTcpSocket *> m_clients;
|
2021-04-24 17:21:31 +00:00
|
|
|
QThread mThread;
|
2021-05-03 15:07:13 +00:00
|
|
|
bool mState;
|
2021-04-24 17:21:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // NETWORK_CONTROLLER_H
|