2024-01-18 15:41:43 +08:00
|
|
|
#ifndef TCPCONTROLLER_H
|
|
|
|
#define TCPCONTROLLER_H
|
|
|
|
|
2024-01-25 09:57:08 +08:00
|
|
|
#include <QDir>
|
2024-01-18 15:41:43 +08:00
|
|
|
#include <QMap>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QTcpSocket>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
using Routes = QMap<QString, std::function<void(QTcpSocket*)>>;
|
|
|
|
|
|
|
|
class TcpController : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
TcpController(QObject* parent = nullptr);
|
|
|
|
Routes getRoutes();
|
|
|
|
|
|
|
|
private:
|
|
|
|
Routes routes;
|
|
|
|
void setIpaddr(QTcpSocket* socket);
|
2024-01-25 09:57:08 +08:00
|
|
|
void setName(QTcpSocket* socket);
|
|
|
|
void getName(QTcpSocket* socket);
|
2024-01-18 15:41:43 +08:00
|
|
|
void setVideoEnc(QTcpSocket* socket);
|
2024-01-25 09:57:08 +08:00
|
|
|
void getVideoEnc(QTcpSocket* socket);
|
2024-01-18 15:41:43 +08:00
|
|
|
void getFileList(QTcpSocket* socket);
|
|
|
|
void deleteFile(QTcpSocket* socket);
|
2024-01-25 09:57:08 +08:00
|
|
|
|
|
|
|
static QString writeCom(const QString& com);
|
|
|
|
inline QVariantMap parseParams(QTcpSocket* socket);
|
|
|
|
inline QList<QString> listFile(QString path);
|
2024-01-18 15:41:43 +08:00
|
|
|
};
|
|
|
|
|
2024-01-25 09:57:08 +08:00
|
|
|
/**
|
|
|
|
* @brief parse tcp request params
|
|
|
|
* @param socket
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
inline QVariantMap TcpController::parseParams(QTcpSocket* socket)
|
|
|
|
{
|
|
|
|
QVariantMap params;
|
|
|
|
// exmaple:data1=xxx\r\ndata2=xxx\r\n...
|
|
|
|
QStringList data = QString(socket->readAll()).split("\r\n");
|
|
|
|
|
|
|
|
for (int i = 0; i < data.length(); i++) {
|
|
|
|
if (data.at(i).contains("=")) {
|
|
|
|
QStringList pair = data.at(i).split("=");
|
|
|
|
params.insert(pair[0], pair[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief list all .mp4 file in the folder
|
|
|
|
* @param path
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
inline QList<QString> TcpController::listFile(QString path)
|
|
|
|
{
|
|
|
|
QStringList fileList;
|
|
|
|
QDir dir(path);
|
|
|
|
if (!dir.exists()) {
|
|
|
|
qDebug() << "folder " << path << " dont exists";
|
|
|
|
return fileList;
|
|
|
|
}
|
|
|
|
dir.setFilter(QDir::Files);
|
|
|
|
dir.setSorting(QDir::Name);
|
|
|
|
QStringList nameFilters;
|
|
|
|
nameFilters << "*.mp4"
|
|
|
|
<< "*.jpg";
|
|
|
|
dir.setNameFilters(nameFilters);
|
|
|
|
fileList = dir.entryList();
|
|
|
|
return fileList;
|
|
|
|
}
|
|
|
|
|
2024-01-18 15:41:43 +08:00
|
|
|
#endif // TCPCONTROLLER_H
|