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:
yuanyuanxianganddeepseek-flash committed 2026-10-26 15:38:07 +01:00
1 parent 27d40f5678
commit c271911fcb
12 files changed
+729 -11

No files matched your search

+35 -1
View File
@@ -273,6 +273,11 @@ public:
// 该 host 是否有进行中的文件传输会话(MessageHandle 分派守卫)。
bool IsFileTransferPending(uint64_t device_id);
// 该 host 上是否有「MCP 发起的 C2C 传输」正发往 dst_id。用于 COMMAND_SEND_FILE_V2
// 转发分支:MCP 的 C2C 不给源端子连接挂进度对话框(挂上后子连接的后续包会被路由到
// 对话框而不是 MessageHandle,源端最后的 TOKEN_C2C_SEND_RESULT 就会丢)。
bool IsC2CTransferPending(uint64_t src_id, uint64_t dst_id);
// 登记文件传输会话(false = 该 host 已有文件会话或一次性挂起请求)。
bool BeginFileTransferPending(uint64_t device_id, const std::string& tool,
const std::string& localDir, const std::string& remotePath,
@@ -283,6 +288,28 @@ public:
// (无 fmSubCtx/streamSubCtx,ClearFileTransfer 对空指针安全)。
bool BeginFileUpload(uint64_t device_id);
// ===== C2C 文件传输(copy_files_c2c:源客户端 → 目标客户端,服务端只做中继)=====
// 会话键控在「源」设备上:工具线程只等源客户端回传的 TOKEN_C2C_SEND_RESULT,
// 目标端的收发由服务端现有的 COMMAND_SEND_FILE_V2 转发分支完成(不经过 MCP)。
struct C2CResult {
int error = 0; // 0=ok;1002=超时(工具侧);其余为客户端上报的 status
uint32_t status = 0; // C2CSendResultPacket.status(0=全部发出;FEV2_* / 1001)
DWORD win32err = 0; // 源端首个不可访问路径的 win32 码
int32_t workerRc = 0; // FileBatchTransferWorkerV2 返回值
uint32_t fileCount = 0;
uint64_t totalBytes = 0;
};
// 登记 C2C 会话(false = 源设备已有文件会话/挂起请求,或源==目标)。
bool BeginC2CTransfer(uint64_t src_id, uint64_t dst_id, uint64_t transfer_id);
// TOKEN_C2C_SEND_RESULT:源客户端回传发送结果,填会话并唤醒工具线程。
// 无对应会话(迟到的结果、MFC 侧手写包)直接忽略。
void OnC2CResult(uint64_t src_id, const BYTE* buf, ULONG len);
// 工具线程:等源客户端回传结果;true=全部发出(error=0)。调用方随后 ClearFileTransfer(src)。
bool WaitC2CDone(uint64_t src_id, int timeoutMs, C2CResult& out);
// TOKEN_DRIVE_LIST:识别 download_file,存文件管理器子链接并下发 CMD_DOWN_FILES_V2。
// 返回 true=已接管(保持子链接,收尾由 ClearFileTransfer);false=会话已清理(调用方 CancelIO)。
bool OnDownloadDriveList(uint64_t device_id, context* fmSubCtx);
@@ -651,7 +678,14 @@ private:
// ===== P6:文件传输会话(受 m_FileXferMutex 保护;单设备单传输)=====
struct FileTransferSession {
std::string tool; // "download_file" / "upload_file"
std::string tool; // "download_file" / "upload_file" / "copy_files_c2c"
uint64_t dstClientID = 0; // copy_files_c2c:目标客户端(其余工具恒 0)
bool c2cResult = false; // copy_files_c2c:已收到源端结果包
uint32_t c2cStatus = 0; // 结果包 status(0=全部发出)
DWORD c2cWin32 = 0; // 结果包 win32err
int32_t c2cWorkerRc = 0; // 结果包 workerRc
uint32_t c2cFileCount = 0; // 结果包 fileCount
uint64_t c2cBytes = 0; // 结果包 totalBytes
std::string localDir; // download:本机保存目录(ANSI,结尾 '\')
std::string remotePath; // 客户端远程路径(ANSI,发 CMD_DOWN_FILES_V2 用)
bool overwrite = false; // 是否覆盖已存在文件