Feature: Add copy_files_c2c to transfer files between clients directly
Adds the 54th MCP tool, copy_files_c2c, which asks the source client to push a set of files straight to another online client. Previously the only way to move files between two clients was to download them to the server machine and upload them again, so the data crossed the wire twice and needed free disk on the server. With C2C the server only relays packets and never parses or stores the payload. Protocol. Two new ids in common/commands.h: COMMAND_C2C_SEND_FILES (174), carrying the destination directory followed by a NUL-separated path list, and TOKEN_C2C_SEND_RESULT (175), a fixed 45-byte result packet sent back over the same sub-connection the transfer used. Clients advertise support with the LOGIN_INFOR capability bit CLIENT_CAP_C2C_PATHS (0x0008), exposed server side as Context::SupportsC2CPaths(). File transfer V2 already knew how to push to dstClientID, so the destination side needed no change at all; existing V2 receive paths keep working, and older clients simply do not advertise the bit and are rejected with a clear error instead of hanging. Client. OnC2CSendFiles expands environment variables, collects directories recursively, opens its own sub-connection (with sub-connection auth), and runs FileBatchTransferWorkerV2 in a detached thread, then reports the result on that same connection. Every early exit now reports a structured failure rather than staying silent. The worker needs the process-level InitFileUpload call that the receive path performs lazily, and a source client may never have opened a file manager or received an upload, so the initialization is factored into EnsureV2TransferInited() and called from both paths. Server. copy_files_c2c mirrors the existing upload tool: gates, id parsing, capability checks, path validation, then a session keyed on the source host that WaitC2CDone polls until the result packet arrives. The C2C relay branch used to attach a CDlgFileSend progress dialog to the source's sub-connection; NotifyProc then routed every later packet on that connection to the dialog, which parsed the result packet as a V1 file chunk and dropped it, so the tool always timed out even though the files had arrived. IsC2CTransferPending() now suppresses dialog creation for MCP-driven transfers only, leaving manual Ctrl+V transfers untouched. Errors map to -32001 for a source-side transfer failure, -32010 for access denied, -32009 for a missing path and -32002 for an offline destination. Both hosts must be Windows: the destination is restricted too, because the source embeds dst_dir in each V2 chunk name as an absolute Windows path and only the Windows receiver applies it literally. A Linux or macOS receiver never gets a target directory here (the clipboard flow sets one with COMMAND_C2C_PREPARE, which this path does not send), so it would strip the drive letter and write under / instead. Supporting that needs a prepare packet carrying an explicit path plus an upgrade of those clients, so for now the tool rejects them with -32005 rather than writing somewhere wrong. Verified end to end on the Debug build: three files including a non-ASCII name, an empty directory and a 3 MB binary arrived with matching SHA-256 in 3.2 s, a non-existent destination directory was created, and the negative cases returned the documented codes. mcp_smoke.ps1 passes 41/41 with two new assertions and 27/27 in -SkipHost mode. Design notes, error table and limitations are in docs/Mcp_FileTransfer_Design.md section 14 and docs/Mcp_Design.md section 15. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
27d40f5678
commit
c271911fcb
12 files changed
+729
-11
No files matched your search
@@ -1373,6 +1373,9 @@ std::string BuildDownloadFile(const Json::Value& id, const Json::Value& args, CM
|
||||
Json::Value BuildUploadFileInputSchema();
|
||||
Json::Value BuildUploadFileOutputSchema();
|
||||
std::string BuildUploadFile(const Json::Value& id, const Json::Value& args, CMy2015RemoteDlg* parent);
|
||||
Json::Value BuildCopyFilesC2CInputSchema();
|
||||
Json::Value BuildCopyFilesC2COutputSchema();
|
||||
std::string BuildCopyFilesC2C(const Json::Value& id, const Json::Value& args, CMy2015RemoteDlg* parent);
|
||||
std::string BuildRemoteClipboard(const Json::Value& id, const Json::Value& args, CMy2015RemoteDlg* parent);
|
||||
|
||||
// ===== P7 前置声明(定义见下方「tools/call 分派」前)=====
|
||||
@@ -1769,6 +1772,18 @@ std::string BuildToolsListResult(const Json::Value& id) {
|
||||
tools.append(tool);
|
||||
}
|
||||
|
||||
// 17b) copy_files_c2c(C2C 文件传输:源主机 → 目标主机,服务端只中继不落盘)
|
||||
// 与 upload_file 同门控:写的是远端目录,需 McpFileTransfer=1 且 McpReadonly=0。
|
||||
// 额外要求源主机是新客户端(CLIENT_CAP_C2C_PATHS),老客户端不认识 COMMAND_C2C_SEND_FILES。
|
||||
if (CMcpServer::Instance().IsFileTransferEnabled() && !CMcpServer::Instance().IsReadonly()) {
|
||||
Json::Value tool(Json::objectValue);
|
||||
tool["name"] = "copy_files_c2c";
|
||||
tool["description"] = u8"在两个在线 Windows 主机之间直接拷贝文件(V2 C2C:源主机自己发、目标主机自己收,服务端只中继,不在主控落盘)。paths 为源主机绝对路径(文件或目录,目录递归、保留相对目录结构);dst_dir 为目标主机保存目录(不存在自动创建)。返回 transfer_id/file_count/total_bytes;receiver_ack 恒为 false —— V2 没有目标端回执,目标端校验失败只记在目标机日志里,需要确证请用 list_files 复核。";
|
||||
tool["inputSchema"] = BuildCopyFilesC2CInputSchema();
|
||||
tool["outputSchema"] = BuildCopyFilesC2COutputSchema();
|
||||
tools.append(tool);
|
||||
}
|
||||
|
||||
// 18) kill_process / operate_window / control_service / power_action(P7:设备管理控制,
|
||||
// 仅 Windows,安全门:McpManage=1 且 McpReadonly=0)
|
||||
if (CMcpServer::Instance().IsManageEnabled() && !CMcpServer::Instance().IsReadonly()) {
|
||||
@@ -5077,6 +5092,250 @@ std::string BuildUploadFile(const Json::Value& id, const Json::Value& args, CMy2
|
||||
return BuildError(id, -32001, msg);
|
||||
}
|
||||
|
||||
// ========== C2C 文件传输:copy_files_c2c ==========
|
||||
|
||||
Json::Value BuildCopyFilesC2CInputSchema() {
|
||||
Json::Value props(Json::objectValue);
|
||||
|
||||
Json::Value src(Json::objectValue);
|
||||
src["type"] = "string";
|
||||
src["description"] = u8"源主机 id(文件从这里发出),取 list_online_hosts / search_hosts 返回的 id";
|
||||
props["src_id"] = src;
|
||||
|
||||
Json::Value dst(Json::objectValue);
|
||||
dst["type"] = "string";
|
||||
dst["description"] = u8"目标主机 id(文件落在这里),同上";
|
||||
props["dst_id"] = dst;
|
||||
|
||||
Json::Value paths(Json::objectValue);
|
||||
paths["type"] = "array";
|
||||
Json::Value pathItems(Json::objectValue);
|
||||
pathItems["type"] = "string";
|
||||
paths["items"] = pathItems;
|
||||
paths["description"] = u8"源主机上的绝对路径列表(文件或目录;目录递归发送,保留相对目录结构)";
|
||||
props["paths"] = paths;
|
||||
|
||||
Json::Value dir(Json::objectValue);
|
||||
dir["type"] = "string";
|
||||
dir["description"] = u8"目标主机保存目录(绝对路径,不存在自动创建),如 D:\\Incoming";
|
||||
props["dst_dir"] = dir;
|
||||
|
||||
Json::Value to(Json::objectValue);
|
||||
to["type"] = "integer";
|
||||
to["description"] = u8"超时毫秒(默认 600000=10 分钟,上限 3600000=1 小时);超时不代表传输失败";
|
||||
props["timeout_ms"] = to;
|
||||
|
||||
Json::Value schema(Json::objectValue);
|
||||
schema["type"] = "object";
|
||||
schema["properties"] = props;
|
||||
Json::Value required(Json::arrayValue);
|
||||
required.append("src_id");
|
||||
required.append("dst_id");
|
||||
required.append("paths");
|
||||
required.append("dst_dir");
|
||||
schema["required"] = required;
|
||||
return schema;
|
||||
}
|
||||
|
||||
Json::Value BuildCopyFilesC2COutputSchema() {
|
||||
Json::Value props(Json::objectValue);
|
||||
Json::Value s1(Json::objectValue); s1["type"] = "string"; props["transfer_id"] = s1;
|
||||
Json::Value s2(Json::objectValue); s2["type"] = "string"; props["src_id"] = s2;
|
||||
Json::Value s3(Json::objectValue); s3["type"] = "string"; props["dst_id"] = s3;
|
||||
Json::Value s4(Json::objectValue); s4["type"] = "string"; props["dst_dir"] = s4;
|
||||
Json::Value s5(Json::objectValue); s5["type"] = "integer"; props["file_count"] = s5;
|
||||
Json::Value s6(Json::objectValue); s6["type"] = "integer"; props["total_bytes"] = s6;
|
||||
Json::Value s7(Json::objectValue); s7["type"] = "boolean"; props["receiver_ack"] = s7;
|
||||
|
||||
Json::Value schema(Json::objectValue);
|
||||
schema["type"] = "object";
|
||||
schema["properties"] = props;
|
||||
return schema;
|
||||
}
|
||||
|
||||
// 取一个十进制主机 id 字符串字段(src_id / dst_id),与 ParseHostIdArg 同规则但不绑定 "id" 键。
|
||||
static bool ParseIdField(const Json::Value& args, const char* key, uint64_t& out, std::string& err) {
|
||||
std::string s = GetStringArg(args, key);
|
||||
if (s.empty()) { err = std::string("Missing required parameter: ") + key; return false; }
|
||||
for (char c : s) {
|
||||
if (c < '0' || c > '9') {
|
||||
err = std::string("Invalid ") + key + ": expected a decimal host id string";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
out = strtoull(s.c_str(), nullptr, 10);
|
||||
if (out == 0) { err = std::string("Invalid ") + key + ": host id must be non-zero"; return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
// tools/call:copy_files_c2c(源主机 → 目标主机直接传文件;服务端只做中继,不落盘)
|
||||
// 报文:COMMAND_C2C_SEND_FILES(服务端 → 源主机主连接)→ 源端自开子连接上行
|
||||
// → 服务端 COMMAND_SEND_FILE_V2 转发分支按 dstClientID 中继到目标主机主连接
|
||||
// → 源端 TOKEN_C2C_SEND_RESULT 回传结果(本线程等的就是它)。
|
||||
std::string BuildCopyFilesC2C(const Json::Value& id, const Json::Value& args, CMy2015RemoteDlg* parent) {
|
||||
CMcpServer& mcp = CMcpServer::Instance();
|
||||
if (!mcp.IsFileTransferEnabled())
|
||||
return BuildError(id, -32006, "File transfer is disabled: requires McpFileTransfer=1");
|
||||
if (mcp.IsReadonly())
|
||||
return BuildError(id, -32006, "copy_files_c2c is disabled: MCP is in read-only mode (set McpReadonly=0 to enable)");
|
||||
|
||||
uint64_t srcId = 0, dstId = 0;
|
||||
std::string err;
|
||||
if (!ParseIdField(args, "src_id", srcId, err)) return BuildError(id, -32602, err);
|
||||
if (!ParseIdField(args, "dst_id", dstId, err)) return BuildError(id, -32602, err);
|
||||
if (srcId == dstId)
|
||||
return BuildError(id, -32602, "src_id and dst_id must be different hosts");
|
||||
|
||||
context* srcCtx = FindMainContext(parent, srcId);
|
||||
if (!srcCtx)
|
||||
return BuildError(id, -32002, "Source host not found or offline: " + std::to_string(srcId));
|
||||
context* dstCtx = FindMainContext(parent, dstId);
|
||||
if (!dstCtx)
|
||||
return BuildError(id, -32002, "Destination host not found or offline: " + std::to_string(dstId));
|
||||
|
||||
// 发送逻辑只在 Windows 客户端实现(client/KernelManager.cpp);目标端只按 V2 收包,无新能力位要求。
|
||||
CString srcType = srcCtx->GetAdditionalData(RES_CLIENT_TYPE);
|
||||
if (srcType == "LNX" || srcType == "MAC")
|
||||
return BuildError(id, -32005, "copy_files_c2c requires a Windows source host");
|
||||
if (!srcCtx->SupportsFileV2())
|
||||
return BuildError(id, -32006, "Source host does not support V2 file transfer");
|
||||
if (!srcCtx->SupportsC2CPaths())
|
||||
return BuildError(id, -32005, "Source host client is too old: upgrade it to a build that supports copy_files_c2c");
|
||||
if (!dstCtx->SupportsFileV2())
|
||||
return BuildError(id, -32006, "Destination host does not support V2 file transfer");
|
||||
// 目标端也必须是 Windows:dst_dir 由源端拼进每个块的 name 字段(绝对 Windows 路径),
|
||||
// 非 Windows 接收端拿不到 COMMAND_C2C_PREPARE 设置的目标目录,会把 C:\x\y 去盘符后
|
||||
// 当绝对路径写(common/FileTransferV2.h:224)——落到 /x/y 并要求 root 权限。
|
||||
CString dstType = dstCtx->GetAdditionalData(RES_CLIENT_TYPE);
|
||||
if (dstType == "LNX" || dstType == "MAC")
|
||||
return BuildError(id, -32005, "copy_files_c2c requires a Windows destination host: "
|
||||
"non-Windows clients cannot honour dst_dir yet");
|
||||
|
||||
std::string dstDirUtf8 = GetStringArg(args, "dst_dir");
|
||||
if (dstDirUtf8.empty())
|
||||
return BuildError(id, -32602, "Missing required parameter: dst_dir");
|
||||
|
||||
const Json::Value& paths = args["paths"];
|
||||
if (!paths.isArray() || paths.size() == 0)
|
||||
return BuildError(id, -32602, "Missing or invalid parameter: paths (non-empty string array)");
|
||||
|
||||
int timeoutMs = 600000;
|
||||
if (args.isMember("timeout_ms")) {
|
||||
int v = 0;
|
||||
if (!GetIntArg(args, "timeout_ms", v) || v <= 0 || v > 3600000)
|
||||
return BuildError(id, -32602, "timeout_ms must be in range 1..3600000");
|
||||
timeoutMs = v;
|
||||
}
|
||||
|
||||
// 目标目录:UTF-8 → 目标端 ANSI(936),去尾斜杠后统一补一个 '\'(作 targetDir 前缀)。
|
||||
std::string dstDirAnsi = ToAnsi(dstDirUtf8, 936);
|
||||
if (dstDirAnsi.empty())
|
||||
return BuildError(id, -32602, "Invalid dst_dir encoding");
|
||||
while (!dstDirAnsi.empty() && (dstDirAnsi.back() == '\\' || dstDirAnsi.back() == '/'))
|
||||
dstDirAnsi.pop_back();
|
||||
if (dstDirAnsi.empty() || HasDotDotSegment(dstDirAnsi))
|
||||
return BuildError(id, -32602, "Invalid dst_dir: " + dstDirUtf8);
|
||||
dstDirAnsi += "\\";
|
||||
|
||||
std::vector<std::string> pathAnsi;
|
||||
for (unsigned int i = 0; i < paths.size(); ++i) {
|
||||
if (!paths[i].isString())
|
||||
return BuildError(id, -32602, "paths must be an array of strings");
|
||||
const std::string u8 = paths[i].asString();
|
||||
std::string ansi = ToAnsi(u8, 936);
|
||||
if (ansi.empty())
|
||||
return BuildError(id, -32602, "Invalid path encoding: " + u8);
|
||||
if (HasDotDotSegment(ansi))
|
||||
return BuildError(id, -32602, "paths must not contain '..' segments: " + u8);
|
||||
pathAnsi.push_back(ansi);
|
||||
}
|
||||
|
||||
const uint64_t transferID = GenerateTransferID();
|
||||
if (!mcp.BeginC2CTransfer(srcId, dstId, transferID))
|
||||
return BuildError(id, -32003, "Device busy: source or destination host already has a transfer in flight");
|
||||
|
||||
// 组包:[C2CSendFilesPacket][dstDir\0][path1\0]...[pathN\0][\0]
|
||||
C2CSendFilesPacket hdr = {};
|
||||
hdr.cmd = (uint8_t)COMMAND_C2C_SEND_FILES;
|
||||
hdr.transferID = transferID;
|
||||
hdr.srcClientID = srcId;
|
||||
hdr.dstClientID = dstId;
|
||||
hdr.flags = 0;
|
||||
strncpy_s(hdr.hash, sizeof(hdr.hash), GetPwdHash().c_str(), _TRUNCATE);
|
||||
strncpy_s(hdr.hmac, sizeof(hdr.hmac), GetHMAC(100).c_str(), _TRUNCATE);
|
||||
|
||||
std::vector<BYTE> pkt;
|
||||
pkt.reserve(sizeof(hdr) + dstDirAnsi.size() + 2);
|
||||
pkt.insert(pkt.end(), (const BYTE*)&hdr, (const BYTE*)&hdr + sizeof(hdr));
|
||||
pkt.insert(pkt.end(), dstDirAnsi.begin(), dstDirAnsi.end());
|
||||
pkt.push_back(0);
|
||||
for (const std::string& p : pathAnsi) {
|
||||
pkt.insert(pkt.end(), p.begin(), p.end());
|
||||
pkt.push_back(0);
|
||||
}
|
||||
pkt.push_back(0);
|
||||
|
||||
if (!srcCtx->Send2Client(pkt.data(), (ULONG)pkt.size())) {
|
||||
mcp.ClearFileTransfer(srcId);
|
||||
return BuildError(id, -32004, "Failed to send command to source host");
|
||||
}
|
||||
|
||||
CMcpServer::C2CResult r;
|
||||
bool ok = mcp.WaitC2CDone(srcId, timeoutMs, r);
|
||||
mcp.ClearFileTransfer(srcId);
|
||||
|
||||
if (!ok) {
|
||||
// 1002 = 工具线程等超时(源端可能仍在发),其余是源端主动回报的失败。
|
||||
if (r.error == 1002)
|
||||
return BuildError(id, -32001, "Timeout waiting for the source host to finish sending (transfer may still be running; verify the destination with list_files)");
|
||||
|
||||
if (r.status == (uint32_t)FEV2_FILE_NOT_FOUND || r.status == (uint32_t)FEV2_ACCESS_DENIED) {
|
||||
const bool denied = (r.win32err == 5) || (r.status == (uint32_t)FEV2_ACCESS_DENIED);
|
||||
std::string msg = denied
|
||||
? "Source path access denied (win32=" + std::to_string(r.win32err) + ")"
|
||||
: "Source path not found or inaccessible (win32=" + std::to_string(r.win32err) + ")";
|
||||
return BuildError(id, denied ? -32010 : -32009, msg);
|
||||
}
|
||||
std::string msg = "C2C transfer failed on the source host (status=" + std::to_string(r.status)
|
||||
+ ", win32=" + std::to_string(r.win32err)
|
||||
+ ", worker_rc=" + std::to_string((int)r.workerRc) + ")";
|
||||
return BuildError(id, -32000, msg);
|
||||
}
|
||||
|
||||
// 审计(不可关闭;只记主机 id、目录与字节数,不落文件名以外的内容)
|
||||
if (parent) {
|
||||
std::string audit = "host " + std::to_string(srcId) + " -> host " + std::to_string(dstId)
|
||||
+ " copy_files_c2c: " + dstDirUtf8 + " (" + std::to_string(r.fileCount)
|
||||
+ " files, " + std::to_string(r.totalBytes) + " bytes)";
|
||||
parent->PostMessageA(WM_SHOWERRORMSG,
|
||||
(WPARAM)new CString(ToAnsi(audit, 936).c_str()),
|
||||
(LPARAM)new CString(_TR("MCP文件传输")));
|
||||
}
|
||||
|
||||
Json::Value result(Json::objectValue);
|
||||
Json::Value sc(Json::objectValue);
|
||||
sc["transfer_id"] = std::to_string(transferID);
|
||||
sc["src_id"] = std::to_string(srcId);
|
||||
sc["dst_id"] = std::to_string(dstId);
|
||||
sc["dst_dir"] = dstDirUtf8;
|
||||
sc["file_count"] = (Json::UInt)r.fileCount;
|
||||
sc["total_bytes"] = (Json::UInt64)r.totalBytes;
|
||||
// V2 没有「目标端已校验」的回执回到服务端:目标端校验失败只记在目标主机日志里。
|
||||
sc["receiver_ack"] = false;
|
||||
result["structuredContent"] = sc;
|
||||
|
||||
Json::Value content(Json::arrayValue);
|
||||
Json::Value item(Json::objectValue);
|
||||
item["type"] = "text";
|
||||
item["text"] = std::string(u8"C2C 发送完成:") + std::to_string(r.fileCount) + u8" 个文件("
|
||||
+ std::to_string(r.totalBytes) + u8" 字节),目标主机保存目录 " + dstDirUtf8
|
||||
+ u8"。目标端无回执:请用 list_files 或 exec_command 在目标主机上复核。";
|
||||
content.append(item);
|
||||
result["content"] = content;
|
||||
result["isError"] = false;
|
||||
return BuildResult(id, result);
|
||||
}
|
||||
|
||||
// ========== P7:设备管理控制 ==========
|
||||
|
||||
// kill_process:主连接 COMMAND_SYSTEM → 系统子连接 TOKEN_PSLIST(就绪)→ COMMAND_KILLPROCESS
|
||||
@@ -6377,6 +6636,7 @@ std::string DispatchSingleHostTool(const Json::Value& id, const std::string& too
|
||||
if (toolName == "remote_clipboard") return BuildRemoteClipboard(id, args, parent);
|
||||
if (toolName == "download_file") return BuildDownloadFile(id, args, parent);
|
||||
if (toolName == "upload_file") return BuildUploadFile(id, args, parent);
|
||||
if (toolName == "copy_files_c2c") return BuildCopyFilesC2C(id, args, parent);
|
||||
if (toolName == "kill_process") return BuildKillProcess(id, args, parent);
|
||||
if (toolName == "operate_window") return BuildOperateWindow(id, args, parent);
|
||||
if (toolName == "control_service") return BuildControlService(id, args, parent);
|
||||
@@ -7728,6 +7988,13 @@ bool CMcpServer::IsFileTransferPending(uint64_t device_id) {
|
||||
return m_FileXferSessions.find(device_id) != m_FileXferSessions.end();
|
||||
}
|
||||
|
||||
bool CMcpServer::IsC2CTransferPending(uint64_t src_id, uint64_t dst_id) {
|
||||
std::lock_guard<std::mutex> lk(m_FileXferMutex);
|
||||
auto it = m_FileXferSessions.find(src_id);
|
||||
return it != m_FileXferSessions.end()
|
||||
&& it->second.tool == "copy_files_c2c" && it->second.dstClientID == dst_id;
|
||||
}
|
||||
|
||||
bool CMcpServer::BeginFileTransferPending(uint64_t device_id, const std::string& tool,
|
||||
const std::string& localDir, const std::string& remotePath,
|
||||
bool overwrite) {
|
||||
@@ -7765,6 +8032,82 @@ bool CMcpServer::BeginFileUpload(uint64_t device_id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// ===== C2C 文件传输(copy_files_c2c)=====
|
||||
// 源客户端收到 COMMAND_C2C_SEND_FILES 后自己开子连接上行,数据包由服务端的
|
||||
// COMMAND_SEND_FILE_V2 转发分支(dstClientID != 0)原样中继到目标客户端主连接,
|
||||
// 服务端不落盘、不解析文件内容;本会话只用来「等源端回传结果 + 单设备单传输互斥」。
|
||||
bool CMcpServer::BeginC2CTransfer(uint64_t src_id, uint64_t dst_id, uint64_t transfer_id) {
|
||||
if (src_id == 0 || src_id == dst_id) return false;
|
||||
{
|
||||
// 与一次性挂起请求互斥(F2):先查对方注册表再查自己的,避免跨锁嵌套死锁。
|
||||
std::lock_guard<std::mutex> lk(m_PendingMutex);
|
||||
if (m_Pending.find(src_id) != m_Pending.end()) return false;
|
||||
if (m_Pending.find(dst_id) != m_Pending.end()) return false;
|
||||
}
|
||||
std::lock_guard<std::mutex> lk(m_FileXferMutex);
|
||||
if (m_FileXferSessions.find(src_id) != m_FileXferSessions.end()) return false; // 单设备单传输
|
||||
if (m_FileXferSessions.find(dst_id) != m_FileXferSessions.end()) return false; // 目标端另有在飞传输
|
||||
FileTransferSession s;
|
||||
s.tool = "copy_files_c2c";
|
||||
s.dstClientID = dst_id;
|
||||
s.transferID = transfer_id;
|
||||
s.startAt = time(nullptr);
|
||||
s.lastDataAt = std::chrono::steady_clock::now();
|
||||
m_FileXferSessions[src_id] = std::move(s);
|
||||
return true;
|
||||
}
|
||||
|
||||
void CMcpServer::OnC2CResult(uint64_t src_id, const BYTE* buf, ULONG len) {
|
||||
if (len < sizeof(C2CSendResultPacket)) return;
|
||||
const C2CSendResultPacket* res = (const C2CSendResultPacket*)buf;
|
||||
std::lock_guard<std::mutex> lk(m_FileXferMutex);
|
||||
auto it = m_FileXferSessions.find(src_id);
|
||||
if (it == m_FileXferSessions.end() || it->second.tool != "copy_files_c2c") return;
|
||||
FileTransferSession& s = it->second;
|
||||
if (s.c2cResult || res->transferID != s.transferID) return; // 首个结果为准,旧传输的迟到包忽略
|
||||
s.c2cResult = true;
|
||||
s.c2cStatus = res->status;
|
||||
s.c2cWin32 = res->win32err;
|
||||
s.c2cWorkerRc = res->workerRc;
|
||||
s.c2cFileCount = res->fileCount;
|
||||
s.c2cBytes = res->totalBytes;
|
||||
s.done = true;
|
||||
s.error = (res->status == (uint32_t)FEV2_OK && res->workerRc == 0)
|
||||
? 0 : (int)(res->status ? res->status : 1001);
|
||||
m_FileXferCv.notify_all();
|
||||
}
|
||||
|
||||
bool CMcpServer::WaitC2CDone(uint64_t src_id, int timeoutMs, C2CResult& out) {
|
||||
std::unique_lock<std::mutex> lk(m_FileXferMutex);
|
||||
auto it = m_FileXferSessions.find(src_id);
|
||||
if (it == m_FileXferSessions.end()) { out.error = 1002; return false; }
|
||||
|
||||
// 只有总超时,没有停滞看门狗:源端全程只在最后回一包结果,中途长时间无包是正常的
|
||||
// (大文件传输),按「无数据即停滞」判会误杀。
|
||||
const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeoutMs);
|
||||
bool signaled = false;
|
||||
while (true) {
|
||||
auto slice = std::chrono::milliseconds(500);
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
if (now + slice > deadline) slice = std::chrono::duration_cast<std::chrono::milliseconds>(deadline - now);
|
||||
if (slice.count() <= 0) break; // 总超时
|
||||
if (m_FileXferCv.wait_for(lk, slice, [&] { return it->second.done; })) {
|
||||
signaled = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
FileTransferSession& s = it->second;
|
||||
out.status = s.c2cStatus;
|
||||
out.win32err = s.c2cWin32;
|
||||
out.workerRc = s.c2cWorkerRc;
|
||||
out.fileCount = s.c2cFileCount;
|
||||
out.totalBytes = s.c2cBytes;
|
||||
if (!signaled && !s.done) { out.error = 1002; return false; } // 超时:传输可能仍在继续
|
||||
out.error = s.error;
|
||||
return s.c2cResult && s.error == 0;
|
||||
}
|
||||
|
||||
bool CMcpServer::OnDownloadDriveList(uint64_t device_id, context* fmSubCtx) {
|
||||
std::string localDir, remotePath;
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user