Feature: share the device transfer gate with MCP file transfers
Preview and MCP must not run file transfers on one device at the same time: MCP's stream interceptor is device-keyed and not tool-aware, so a preview stream arriving while an upload/C2C session exists would be swallowed. Claim the shared DeviceTransferGate in all three MCP entry points (download/read, upload_file, copy_files_c2c) before touching the pending/file-transfer registries, and release it in ClearFileTransfer. C2C claims both src and dst atomically; a failed registry check releases the claim before returning. The gate is acquired before m_PendingMutex and m_FileXferMutex to keep the documented order. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
3b945d1992
commit
4d37d85bac
2 files changed
+96
-34
No files matched your search
@@ -12,7 +12,9 @@
|
||||
#include "McpMachine.h" // 主机管理 / 连接管理工具族
|
||||
#include "McpChat.h" // 远程交谈工具族
|
||||
#include "McpBuildClient.h" // 生成客户端工具族(build_client)
|
||||
#include "FilePreview.h" // DeviceTransferGate:预览与 MCP 文件传输共用设备闸门
|
||||
|
||||
#include <atomic>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
|
||||
@@ -14599,40 +14601,75 @@ bool CMcpServer::IsC2CTransferPending(uint64_t src_id, uint64_t dst_id) {
|
||||
bool CMcpServer::BeginFileTransferPending(uint64_t device_id, const std::string& tool,
|
||||
const std::string& localDir, const std::string& remotePath,
|
||||
bool overwrite, uint64_t maxFileBytes) {
|
||||
// 设备级闸门:预览与 MCP 三个文件传输入口共用;必须在所有注册表锁之前认领。
|
||||
static std::atomic<uint64_t> s_ownerSeq{1};
|
||||
const uint64_t owner = s_ownerSeq.fetch_add(1);
|
||||
if (!DeviceTransferGate::Instance().TryClaim(device_id, owner)) return false;
|
||||
|
||||
// 与一次性挂起请求互斥(F2):先查对方注册表再查自己的,避免跨锁嵌套死锁。
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_PendingMutex);
|
||||
if (m_Pending.find(device_id) != m_Pending.end()) return false;
|
||||
if (m_Pending.find(device_id) != m_Pending.end()) {
|
||||
DeviceTransferGate::Instance().Release(device_id, owner);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool registered = false;
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_FileXferMutex);
|
||||
if (m_FileXferSessions.find(device_id) == m_FileXferSessions.end()) { // 单设备单传输
|
||||
FileTransferSession s;
|
||||
s.tool = tool;
|
||||
s.localDir = localDir;
|
||||
s.remotePath = remotePath;
|
||||
s.overwrite = overwrite;
|
||||
s.maxFileBytes = maxFileBytes;
|
||||
s.startAt = time(nullptr);
|
||||
s.lastDataAt = std::chrono::steady_clock::now();
|
||||
s.gateOwner = owner;
|
||||
m_FileXferSessions[device_id] = std::move(s);
|
||||
registered = true;
|
||||
}
|
||||
}
|
||||
if (!registered) {
|
||||
DeviceTransferGate::Instance().Release(device_id, owner);
|
||||
return false;
|
||||
}
|
||||
std::lock_guard<std::mutex> lk(m_FileXferMutex);
|
||||
if (m_FileXferSessions.find(device_id) != m_FileXferSessions.end()) return false; // 单设备单传输
|
||||
FileTransferSession s;
|
||||
s.tool = tool;
|
||||
s.localDir = localDir;
|
||||
s.remotePath = remotePath;
|
||||
s.overwrite = overwrite;
|
||||
s.maxFileBytes = maxFileBytes;
|
||||
s.startAt = time(nullptr);
|
||||
s.lastDataAt = std::chrono::steady_clock::now();
|
||||
m_FileXferSessions[device_id] = std::move(s);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CMcpServer::BeginFileUpload(uint64_t device_id, uint64_t transfer_id) {
|
||||
static std::atomic<uint64_t> s_ownerSeq{1};
|
||||
const uint64_t owner = s_ownerSeq.fetch_add(1);
|
||||
if (!DeviceTransferGate::Instance().TryClaim(device_id, owner)) return false;
|
||||
|
||||
// 与一次性挂起请求互斥(F2):先查对方注册表再查自己的,避免跨锁嵌套死锁。
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_PendingMutex);
|
||||
if (m_Pending.find(device_id) != m_Pending.end()) return false;
|
||||
if (m_Pending.find(device_id) != m_Pending.end()) {
|
||||
DeviceTransferGate::Instance().Release(device_id, owner);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool registered = false;
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_FileXferMutex);
|
||||
if (m_FileXferSessions.find(device_id) == m_FileXferSessions.end()) { // 单设备单传输
|
||||
FileTransferSession s;
|
||||
s.tool = "upload_file";
|
||||
s.dstClientID = device_id; // 目标端 = 会话宿主,回执按 dstClientID + transferID 匹配
|
||||
s.transferID = transfer_id;
|
||||
s.startAt = time(nullptr);
|
||||
s.lastDataAt = std::chrono::steady_clock::now();
|
||||
s.gateOwner = owner;
|
||||
m_FileXferSessions[device_id] = std::move(s);
|
||||
registered = true;
|
||||
}
|
||||
}
|
||||
if (!registered) {
|
||||
DeviceTransferGate::Instance().Release(device_id, owner);
|
||||
return false;
|
||||
}
|
||||
std::lock_guard<std::mutex> lk(m_FileXferMutex);
|
||||
if (m_FileXferSessions.find(device_id) != m_FileXferSessions.end()) return false; // 单设备单传输
|
||||
FileTransferSession s;
|
||||
s.tool = "upload_file";
|
||||
s.dstClientID = device_id; // 目标端 = 会话宿主,回执按 dstClientID + transferID 匹配
|
||||
s.transferID = transfer_id;
|
||||
s.startAt = time(nullptr);
|
||||
s.lastDataAt = std::chrono::steady_clock::now();
|
||||
m_FileXferSessions[device_id] = std::move(s);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -14642,22 +14679,41 @@ bool CMcpServer::BeginFileUpload(uint64_t device_id, uint64_t transfer_id) {
|
||||
// 服务端不落盘、不解析文件内容;本会话只用来「等源端回传结果 + 单设备单传输互斥」。
|
||||
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;
|
||||
|
||||
// 设备级闸门:C2C 同时占用源和目标两端;必须在所有注册表锁之前认领。
|
||||
static std::atomic<uint64_t> s_ownerSeq{1};
|
||||
const uint64_t owner = s_ownerSeq.fetch_add(1);
|
||||
if (!DeviceTransferGate::Instance().TryClaimPair(src_id, dst_id, owner)) 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;
|
||||
if (m_Pending.find(src_id) != m_Pending.end() ||
|
||||
m_Pending.find(dst_id) != m_Pending.end()) {
|
||||
DeviceTransferGate::Instance().ReleasePair(src_id, dst_id, owner);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool registered = false;
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_FileXferMutex);
|
||||
if (m_FileXferSessions.find(src_id) == m_FileXferSessions.end() && // 单设备单传输
|
||||
m_FileXferSessions.find(dst_id) == m_FileXferSessions.end()) { // 目标端另有在飞传输
|
||||
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();
|
||||
s.gateOwner = owner;
|
||||
m_FileXferSessions[src_id] = std::move(s);
|
||||
registered = true;
|
||||
}
|
||||
}
|
||||
if (!registered) {
|
||||
DeviceTransferGate::Instance().ReleasePair(src_id, dst_id, owner);
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -15028,6 +15084,11 @@ void CMcpServer::ClearFileTransfer(uint64_t device_id) {
|
||||
if (s.streamSubCtx) m_FileXferContextToDevice.erase(s.streamSubCtx);
|
||||
}
|
||||
if (!found) return;
|
||||
// 释放设备闸门(C2C 两端一起放),再关子链接。
|
||||
if (s.tool == "copy_files_c2c")
|
||||
DeviceTransferGate::Instance().ReleasePair(device_id, s.dstClientID, s.gateOwner);
|
||||
else
|
||||
DeviceTransferGate::Instance().Release(device_id, s.gateOwner);
|
||||
// 锁外关闭两条子链接(CancelIO 触发客户端断开,停掉仍在飞的流式数据)。
|
||||
if (s.fmSubCtx) s.fmSubCtx->CancelIO();
|
||||
if (s.streamSubCtx) s.streamSubCtx->CancelIO();
|
||||
|
||||
@@ -911,6 +911,7 @@ private:
|
||||
// ===== P6:文件传输会话(受 m_FileXferMutex 保护;单设备单传输)=====
|
||||
struct FileTransferSession {
|
||||
std::string tool; // "download_file" / "upload_file" / "copy_files_c2c"
|
||||
uint64_t gateOwner = 0; // DeviceTransferGate 认领 owner(收尾释放用)
|
||||
uint64_t dstClientID = 0; // copy_files_c2c:目标客户端(其余工具恒 0)
|
||||
bool c2cResult = false; // copy_files_c2c:已收到源端结果包
|
||||
uint32_t c2cStatus = 0; // 结果包 status(0=全部发出)
|
||||
|
||||
Reference in new issue
Block a user