增加文件传输功能

This commit is contained in:
huanglinhuan
2025-12-22 14:49:47 +08:00
parent 065251f727
commit e3db1b57a0
13 changed files with 1014 additions and 11 deletions

View File

@@ -2,6 +2,7 @@
#include "ScreenCapture.h"
#include "IddBridge.h"
#include "VideoEncoder.h"
#include "TcpServer.h"
#include <iostream>
#include <sstream>
#include <vector>
@@ -18,6 +19,8 @@ int main(int argc, char* argv[]) {
int outputIndex = -1;
bool iddProducer = false;
int producerOutputIndex = -1;
std::string fileToSend = "";
std::string folderToSend = "";
if (argc > 1) ipStr = argv[1];
if (argc > 2) port = std::stoi(argv[2]);
@@ -36,6 +39,10 @@ int main(int argc, char* argv[]) {
try {
producerOutputIndex = std::stoi(arg.substr(std::string("--producer-output=").size()));
} catch (...) {}
} else if (arg.rfind("--send-file=", 0) == 0) {
fileToSend = arg.substr(std::string("--send-file=").size());
} else if (arg.rfind("--send-folder=", 0) == 0) {
folderToSend = arg.substr(std::string("--send-folder=").size());
}
}
@@ -69,6 +76,39 @@ int main(int argc, char* argv[]) {
}
}
// Start TCP Server
TcpServer tcpServer;
if (tcpServer.Start(8889)) {
// Wait for client to connect if we need to send a file immediately
if (!fileToSend.empty()) {
std::cout << "Waiting for client to connect to send file: " << fileToSend << "..." << std::endl;
// Simple polling wait (in a real app, use events/condition variables)
// But TcpServer::SendFile checks if client is connected.
// We'll try to send in a loop or thread.
std::thread([&tcpServer, fileToSend]() {
// Wait for up to 30 seconds for a client
for (int i = 0; i < 300; ++i) {
if (tcpServer.SendFile(fileToSend)) {
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}).detach();
} else if (!folderToSend.empty()) {
std::cout << "Waiting for client to connect to send folder: " << folderToSend << "..." << std::endl;
std::thread([&tcpServer, folderToSend]() {
for (int i = 0; i < 300; ++i) {
if (tcpServer.SendFolder(folderToSend)) {
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}).detach();
}
} else {
std::cerr << "Failed to start TCP Server on port 8889" << std::endl;
}
// Debug: Open file to save H.264 stream if filename is provided
std::ofstream outFile;
if (!outputFileName.empty()) {