RecordControlApplication/TcpController.h

60 lines
1.3 KiB
C++
Executable File

#ifndef TCPCONTROLLER_H
#define TCPCONTROLLER_H
#include <QDir>
#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);
void setName(QTcpSocket* socket);
void getName(QTcpSocket* socket);
void setVideoEnc(QTcpSocket* socket);
void getVideoEnc(QTcpSocket* socket);
void getFileList(QTcpSocket* socket);
void deleteFile(QTcpSocket* socket);
void setRecordMode(QTcpSocket* socket);
void setPlaybackMode(QTcpSocket* socket);
void reboot(QTcpSocket* socket);
inline QVariantMap parseParams(QTcpSocket* socket);
};
/**
* @brief 解析tcp请求参数
* @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;
}
#endif // TCPCONTROLLER_H