[feat] implement file transfer module

This commit is contained in:
dijunkun
2025-12-18 18:30:51 +08:00
parent e7bdf42694
commit 5066fcda48
8 changed files with 436 additions and 4 deletions

View File

@@ -207,6 +207,7 @@ int Render::ConnectTo(const std::string& remote_id, const char* password,
}
AddAudioStream(props->peer_, props->audio_label_.c_str());
AddDataStream(props->peer_, props->data_label_.c_str());
AddDataStream(props->peer_, props->file_label_.c_str());
props->connection_status_ = ConnectionStatus::Connecting;

View File

@@ -714,6 +714,7 @@ int Render::CreateConnectionPeer() {
AddAudioStream(peer_, audio_label_.c_str());
AddDataStream(peer_, data_label_.c_str());
AddDataStream(peer_, file_label_.c_str());
return 0;
} else {
return -1;

View File

@@ -45,6 +45,7 @@ class Render {
PeerPtr* peer_ = nullptr;
std::string audio_label_ = "control_audio";
std::string data_label_ = "control_data";
std::string file_label_ = "file";
std::string local_id_ = "";
std::string remote_id_ = "";
bool exit_ = false;
@@ -464,6 +465,7 @@ class Render {
std::string video_secondary_label_ = "secondary_display";
std::string audio_label_ = "audio";
std::string data_label_ = "data";
std::string file_label_ = "file";
Params params_;
SDL_AudioDeviceID input_dev_;
SDL_AudioDeviceID output_dev_;

View File

@@ -1,7 +1,11 @@
#include <chrono>
#include <cmath>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include "device_controller.h"
#include "file_transfer.h"
#include "localization.h"
#include "platform.h"
#include "rd_log.h"
@@ -304,6 +308,12 @@ void Render::OnReceiveDataBufferCb(const char* data, size_t size,
return;
}
// try to parse as file-transfer chunk first
static FileReceiver receiver;
if (receiver.OnData(data, size)) {
return;
}
std::string json_str(data, size);
RemoteAction remote_action;
@@ -485,12 +495,12 @@ void Render::OnConnectionStatusCb(ConnectionStatus status, const char* user_id,
render->need_to_send_host_info_ = true;
render->start_screen_capturer_ = true;
render->start_speaker_capturer_ = true;
// #ifdef CROSSDESK_DEBUG
#ifdef CROSSDESK_DEBUG
render->start_mouse_controller_ = false;
render->start_keyboard_capturer_ = false;
// #else
#else
render->start_mouse_controller_ = true;
// #endif
#endif
if (std::all_of(render->connection_status_.begin(),
render->connection_status_.end(), [](const auto& kv) {
return kv.first.find("web") != std::string::npos;

View File

@@ -1,3 +1,9 @@
#include <filesystem>
#include <fstream>
#include <thread>
#include <vector>
#include "file_transfer.h"
#include "layout.h"
#include "localization.h"
#include "rd_log.h"
@@ -197,6 +203,27 @@ int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& props) {
std::string path = OpenFileDialog(title);
if (!path.empty()) {
LOG_INFO("Selected file: {}", path.c_str());
// Send selected file over file data channel in a background thread.
auto peer = props->peer_;
std::filesystem::path file_path = std::filesystem::path(path);
std::string file_label = file_label_;
std::thread([peer, file_path, file_label]() {
FileSender sender;
int ret = sender.SendFile(
file_path, file_path.filename().string(),
[peer, file_label](const char* buf, size_t sz) -> int {
return SendDataFrame(peer, buf, sz, file_label.c_str(), true);
});
if (ret != 0) {
LOG_ERROR("FileSender::SendFile failed for [{}], ret={}",
file_path.string().c_str(), ret);
} else {
LOG_INFO("File send finished: {}", file_path.string().c_str());
}
}).detach();
}
}