#include "TcpController.h" #include #include TcpController::TcpController(QObject* parent) : QObject(parent) { // map the requesr url and the handler routes.insert("set_ipaddr", std::bind(&TcpController::setIpaddr, this, std::placeholders::_1)); routes.insert("set_video_enc", std::bind(&TcpController::setVideoEnc, this, std::placeholders::_1)); routes.insert("get_file_list", std::bind(&TcpController::getFileList, this, std::placeholders::_1)); routes.insert("delete_file", std::bind(&TcpController::deleteFile, this, std::placeholders::_1)); } Routes TcpController::getRoutes() { return this->routes; } /** * @brief set box's ip address * @param socket */ void TcpController::setIpaddr(QTcpSocket* socket) { } /** * @brief set the video encode params * @param socket */ void TcpController::setVideoEnc(QTcpSocket* socket) { } /** * @brief get the video file list * @param socket */ void TcpController::getFileList(QTcpSocket* socket) { QDir dir("/root/usb/videos"); if (!dir.exists()) { qDebug() << "dictionary /root/usb/videos dont exists"; return; } dir.setFilter(QDir::Files); dir.setSorting(QDir::Name); QStringList nameFilters; nameFilters << "*.mp4"; dir.setNameFilters(nameFilters); QStringList fileList = dir.entryList(); QString data = fileList.join(","); socket->write(data.toStdString().data()); } /** * @brief download video file * @param socket */ void TcpController::downloadFile(QTcpSocket* socket) { QString url = socket->readLine().trimmed(); QString fileName = url.split("=")[1]; fileName = "/root/usb/videos/" + fileName; qDebug() << fileName; QFile file(fileName); if (!file.open(QIODevice::ReadOnly)) { qDebug() << "read file failed"; return; } int64_t sendSize = 0; int64_t fileSize = file.size(); socket->write(QString("fileSize=%1\r\n").arg(fileSize).toStdString().data()); // loop reading and sending file, 15k bytes once while (sendSize < fileSize) { char buf[1024 * 15] = { 0 }; int len = file.read(buf, sizeof(buf)); len = socket->write(buf, len); sendSize += len; } qDebug() << "send file to " << socket << "end!"; file.close(); } /** * @brief delete video file by file name * @param socket */ void TcpController::deleteFile(QTcpSocket* socket) { QString data = socket->readLine().trimmed(); QString fileName = data.split("=")[1]; }