/** * FileTransferV2.h - Unix V2 File Transfer Protocol * * Implements V2 file transfer protocol for Unix clients. * Supports: receive files from server/C2C, send files to server/C2C * * PLATFORM SUPPORT: * - Linux: Supported * - macOS: Supported * - Windows: NOT SUPPORTED (Windows uses different file APIs) */ #pragma once #if defined(_WIN32) || defined(_WIN64) #error "FileTransferV2.h is not supported on Windows." #endif #include "commands.h" #include "file_upload.h" #include "UnixPath.h" #include "../client/IOCPClient.h" #include #include #include #include #include #include #include #include #include "sha256_posix.h" #include #include #include #include #include #include #include class FileTransferV2 { public: // V2 文件接收状态 struct RecvState { uint64_t transferID; uint64_t fileSize; uint64_t receivedBytes; std::string filePath; int fd; RecvState() : transferID(0), fileSize(0), receivedBytes(0), fd(-1) {} ~RecvState() { if (fd >= 0) close(fd); } }; // 编码转换 static std::string gbkToUtf8(const std::string& gbk) { if (gbk.empty()) return gbk; iconv_t cd = iconv_open("UTF-8", "GBK"); if (cd == (iconv_t)-1) return gbk; size_t inLeft = gbk.size(); size_t outLeft = inLeft * 4; std::string output(outLeft, '\0'); char* inPtr = const_cast(gbk.data()); char* outPtr = &output[0]; size_t ret = iconv(cd, &inPtr, &inLeft, &outPtr, &outLeft); iconv_close(cd); if (ret == (size_t)-1) return gbk; output.resize(output.size() - outLeft); return output; } static std::string utf8ToGbk(const std::string& utf8) { if (utf8.empty()) return utf8; iconv_t cd = iconv_open("GBK", "UTF-8"); if (cd == (iconv_t)-1) return utf8; size_t inLeft = utf8.size(); size_t outLeft = inLeft * 2; std::string output(outLeft, '\0'); char* inPtr = const_cast(utf8.data()); char* outPtr = &output[0]; size_t ret = iconv(cd, &inPtr, &inLeft, &outPtr, &outLeft); iconv_close(cd); if (ret == (size_t)-1) return utf8; output.resize(output.size() - outLeft); return output; } // 递归创建目录 static bool MakeDirs(const std::string& path) { std::string dir; size_t lastSlash = path.rfind('/'); if (lastSlash != std::string::npos && lastSlash > 0) { dir = path.substr(0, lastSlash); } else { return true; } std::string current; for (size_t i = 0; i < dir.size(); ++i) { current += dir[i]; if (dir[i] == '/' && current.size() > 1) { mkdir(current.c_str(), 0755); } } if (!current.empty() && current != "/") { mkdir(current.c_str(), 0755); } return true; } // 获取 C2C 目标目录(简化版:使用当前工作目录) static std::string GetTargetDir(uint64_t transferID) { std::lock_guard lock(s_targetDirMtx); auto it = s_targetDirs.find(transferID); if (it != s_targetDirs.end()) { return it->second; } return ""; } // 设置 C2C 目标目录 static void SetTargetDir(uint64_t transferID, const std::string& dir) { std::lock_guard lock(s_targetDirMtx); s_targetDirs[transferID] = dir; Mprintf("[V2] SetTargetDir: transferID=%llu, dir=%s\n", transferID, dir.c_str()); } // 处理 COMMAND_C2C_PREPARE - 在当前目录下创建子目录接收文件 static void HandleC2CPrepare(const uint8_t* data, size_t len, IOCPClient* client) { if (len < sizeof(C2CPreparePacket)) return; const C2CPreparePacket* pkt = (const C2CPreparePacket*)data; // 获取当前工作目录 char cwd[4096] = {}; if (!getcwd(cwd, sizeof(cwd))) { strcpy(cwd, "/tmp"); } // 创建接收子目录: recv_YYYYMM time_t now = time(nullptr); struct tm* t = localtime(&now); char subdir[32]; snprintf(subdir, sizeof(subdir), "recv_%04d%02d", t->tm_year + 1900, t->tm_mon + 1); std::string targetDir = std::string(cwd) + "/" + subdir; mkdir(targetDir.c_str(), 0755); // 已存在则忽略 SetTargetDir(pkt->transferID, targetDir); Mprintf("[V2] C2C Prepare: transferID=%llu, targetDir=%s\n", pkt->transferID, targetDir.c_str()); } // 处理 V2 文件数据包 // 返回: 0=成功, >0=错误码 // reply:主连接(Linux/macOS 传 g_pMainClient)。非空时,C2C 传输的最后一个条目 // 处理完会回一包落盘结果(TOKEN_C2C_RECV_RESULT),服务端据此把 receiver_ack // 变成真实值;传 nullptr 则保持旧行为,不发回执。 static int RecvFileChunkV2(const uint8_t* buf, size_t len, uint64_t myClientID, IOCPClient* reply = nullptr) { // 检查是否是 FILE_COMPLETE_V2 包 if (len >= 1 && buf[0] == COMMAND_FILE_COMPLETE_V2) { return HandleFileComplete(buf, len, myClientID, reply); } if (len < sizeof(FileChunkPacketV2)) { Mprintf("[V2 Recv] Invalid packet length: %zu\n", len); return 2; } const FileChunkPacketV2* pkt = (const FileChunkPacketV2*)buf; if (len < sizeof(FileChunkPacketV2) + pkt->nameLength + pkt->dataLength) { Mprintf("[V2 Recv] Incomplete packet: %zu < %zu\n", len, sizeof(FileChunkPacketV2) + pkt->nameLength + pkt->dataLength); return 3; } // 验证目标 if (pkt->dstClientID != 0 && pkt->dstClientID != myClientID) { Mprintf("[V2 Recv] Target mismatch: dst=%llu, my=%llu\n", pkt->dstClientID, myClientID); return 4; } // 提取文件名(服务端发送的是 GBK 编码) std::string fileNameGbk((const char*)(pkt + 1), pkt->nameLength); std::string fileName = gbkToUtf8(fileNameGbk); // 构建保存路径 std::string savePath; std::string targetDir = GetTargetDir(pkt->transferID); if (!targetDir.empty()) { // C2C 传输:使用预设的目标目录 + 相对路径(保留目录结构) savePath = targetDir; if (!savePath.empty() && savePath.back() != '/') { savePath += '/'; } // 转换 Windows 路径分隔符 std::string relPath = fileName; for (char& c : relPath) { if (c == '\\') c = '/'; } // 移除盘符前缀 (如 C:/)——只认「字母 + 冒号」,否则 /:data 这类合法路径会被改写 if (relPath.size() >= 3 && isalpha((unsigned char)relPath[0]) && relPath[1] == ':' && relPath[2] == '/') { relPath = relPath.substr(3); } else if (relPath.size() >= 2 && isalpha((unsigned char)relPath[0]) && relPath[1] == ':') { relPath = relPath.substr(2); } // 移除开头的 / while (!relPath.empty() && relPath[0] == '/') { relPath = relPath.substr(1); } savePath += relPath; } else { // 服务端传输:用服务端下发的绝对路径。 // 快捷按钮(桌面/下载/用户主页)下发的目标目录是 %USERPROFILE%\..., // 由 WinPathToLinux 展开(见 UnixPath.h),与 Windows 上 // ExpandEnvironmentStringsA 的效果一致。 savePath = WinPathToLinux(fileName); } // 目录处理 if (pkt->flags & FFV2_DIRECTORY) { MakeDirs(savePath + "/dummy"); // MakeDirs 不回报 mkdir 的失败(它还要给服务端下载复用),这里单独确认一次: // 目录没建成也要让回执带上 errno,否则会向服务端谎报成功。 struct stat st; int mkErr = 0; if (stat(savePath.c_str(), &st) != 0) mkErr = errno ? errno : EIO; else if (!S_ISDIR(st.st_mode)) mkErr = EIO; Mprintf("[V2 Recv] Created directory: %s\n", savePath.c_str()); FinishC2CItem(pkt, reply, myClientID, false, true, 0, mkErr); return 0; } // 创建父目录 MakeDirs(savePath); // 获取或创建接收状态 uint64_t stateKey = pkt->transferID ^ ((uint64_t)pkt->fileIndex << 48); int openErr = 0; RecvState* state = GetOrCreateState(stateKey, pkt, savePath, &openErr); if (!state) { Mprintf("[V2 Recv] Failed to create state\n"); FinishC2CItem(pkt, reply, myClientID, true, IsFinalChunk(pkt), 0, openErr ? openErr : EIO); return 5; } // 写入数据 const char* data = (const char*)buf + sizeof(FileChunkPacketV2) + pkt->nameLength; if (lseek(state->fd, pkt->offset, SEEK_SET) == -1) { int e = errno; Mprintf("[V2 Recv] lseek failed: errno=%d\n", e); FinishC2CItem(pkt, reply, myClientID, true, IsFinalChunk(pkt), 0, e); return 6; } size_t written = 0; size_t toWrite = pkt->dataLength; while (written < toWrite) { ssize_t n = write(state->fd, data + written, toWrite - written); if (n <= 0) { if (errno == EINTR) continue; int e = errno; Mprintf("[V2 Recv] write failed: errno=%d\n", e); FinishC2CItem(pkt, reply, myClientID, true, IsFinalChunk(pkt), 0, e); return 7; } written += n; } state->receivedBytes += pkt->dataLength; // 打印进度(每 10% 或最后一块) bool isLast = (pkt->flags & FFV2_LAST_CHUNK) || (state->receivedBytes >= state->fileSize); int progress = state->fileSize > 0 ? (int)(100 * state->receivedBytes / state->fileSize) : 100; static std::map s_lastProgress; if (isLast || progress >= s_lastProgress[stateKey] + 10) { s_lastProgress[stateKey] = progress; Mprintf("[V2 Recv] %s: %llu/%llu (%d%%)\n", savePath.c_str(), state->receivedBytes, state->fileSize, progress); } // 文件完成 if (isLast) { // 落盘长度自检:写到一半失败过、或偏移被改写,都会在这里落成「长度不符」 struct stat st; int szErr = 0; if (fstat(state->fd, &st) != 0) szErr = errno ? errno : EIO; else if ((uint64_t)st.st_size != pkt->fileSize) szErr = EIO; // 记账 + 最后一个条目时回执(要在拿 s_statesMtx 之前做:里面会 Send2Server) FinishC2CItem(pkt, reply, myClientID, true, true, pkt->fileSize, szErr); close(state->fd); state->fd = -1; Mprintf("[V2 Recv] File complete: %s\n", savePath.c_str()); // 清理状态 std::lock_guard lock(s_statesMtx); s_states.erase(stateKey); s_lastProgress.erase(stateKey); } return 0; } // 处理文件完成校验包 static int HandleFileComplete(const uint8_t* buf, size_t len, uint64_t myClientID, IOCPClient* reply = nullptr) { if (len < sizeof(FileCompletePacketV2)) { return 1; } const FileCompletePacketV2* pkt = (const FileCompletePacketV2*)buf; // 验证目标 if (pkt->dstClientID != 0 && pkt->dstClientID != myClientID) { return 0; // 不是给我的,忽略 } Mprintf("[V2] File complete verify: transferID=%llu, fileIndex=%u, size=%llu\n", pkt->transferID, pkt->fileIndex, pkt->fileSize); // 空文件不发任何数据块(worker 只发这个完成包),C2C 回执只能靠它把该条目算作已处理 NoteC2CComplete(pkt, reply, myClientID); // TODO: 实现 SHA-256 校验 // 目前简单返回成功 return 0; } // ============== V2 文件发送 (Linux -> Server) ============== // 生成传输 ID static uint64_t GenerateTransferID() { static std::mutex s_mtx; static uint64_t s_counter = 0; std::lock_guard lock(s_mtx); uint64_t t = (uint64_t)time(nullptr) << 32; return t | (++s_counter); } // 获取公共根目录 (Linux 路径) static std::string GetCommonRoot(const std::vector& files) { if (files.empty()) return "/"; if (files.size() == 1) { size_t lastSlash = files[0].rfind('/'); if (lastSlash != std::string::npos) { return files[0].substr(0, lastSlash + 1); } return "/"; } std::string common = files[0]; for (size_t i = 1; i < files.size(); ++i) { size_t j = 0; while (j < common.size() && j < files[i].size() && common[j] == files[i][j]) { ++j; } common = common.substr(0, j); } // 截断到最后一个 / size_t lastSlash = common.rfind('/'); if (lastSlash != std::string::npos) { return common.substr(0, lastSlash + 1); } return "/"; } // 递归收集目录中的文件和子目录 static void CollectFiles(const std::string& dir, std::vector& files) { // 先添加目录本身(去掉末尾的斜杠) std::string dirEntry = dir; while (!dirEntry.empty() && dirEntry.back() == '/') { dirEntry.pop_back(); } if (!dirEntry.empty()) { files.push_back(dirEntry + "/"); // 以 / 结尾表示目录 } DIR* d = opendir(dir.c_str()); if (!d) return; struct dirent* ent; while ((ent = readdir(d)) != nullptr) { if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue; std::string fullPath = dir; if (fullPath.back() != '/') fullPath += '/'; fullPath += ent->d_name; struct stat st; if (lstat(fullPath.c_str(), &st) != 0) continue; if (S_ISDIR(st.st_mode)) { CollectFiles(fullPath, files); } else if (S_ISREG(st.st_mode)) { files.push_back(fullPath); } } closedir(d); } // Windows 路径转 Linux 路径。服务端对话框的快捷按钮(桌面/下载/用户主页)下发的是 // %USERPROFILE%\Desktop\ 这类 Windows 变量路径,必须先展开(见 UnixPath.h)。 static std::string WinPathToLinux(const std::string& winPath) { return UnixPath::ToUnixPath(winPath); } // errno -> Win32 错误码:服务端 TOKEN_DOWNLOAD_ERROR 按 Win32 码分类 // (McpServer::OnDownloadError: 2/3 -> 路径不存在,5 -> 拒绝访问,其余 -> 一般失败)。 static uint32_t PosixErrnoToWin32(int e) { switch (e) { case ENOENT: return 2; // ERROR_FILE_NOT_FOUND case ENOTDIR: return 3; // ERROR_PATH_NOT_FOUND case EACCES: case EPERM: return 5; // ERROR_ACCESS_DENIED default: return 2; } } // 下载请求无可用文件的结构化回报:[TOKEN_DOWNLOAD_ERROR:1][win32err:4 LE]。 // 走文件管理器子连接(服务端此时还在等第一批 chunk),让它立即失败而不是等停滞看门狗。 // 与 Windows 客户端 CFileManager::SendDownloadError 等价。 static void SendDownloadError(IOCPClient* client, uint32_t win32err) { if (client == nullptr) return; if (win32err == 0) win32err = 2; // ERROR_FILE_NOT_FOUND uint8_t pkt[1 + sizeof(uint32_t)] = { 0 }; pkt[0] = (uint8_t)TOKEN_DOWNLOAD_ERROR; memcpy(pkt + 1, &win32err, sizeof(uint32_t)); client->Send2Server((char*)pkt, (ULONG)sizeof(pkt)); } // 处理 CMD_DOWN_FILES_V2 请求 - 上传文件到服务端 // 格式: [cmd:1][targetDir\0][file1\0][file2\0]...[\0] static void HandleDownFilesV2(const uint8_t* data, size_t len, IOCPClient* client, uint64_t myClientID) { if (len < 2) return; // 解析目标目录 (GBK 编码) const char* p = (const char*)(data + 1); const char* end = (const char*)(data + len); std::string targetDirGbk = p; std::string targetDir = gbkToUtf8(targetDirGbk); p += targetDirGbk.length() + 1; // 解析文件列表 std::vector remotePaths; while (p < end && *p != '\0') { std::string pathGbk = p; remotePaths.push_back(gbkToUtf8(pathGbk)); p += pathGbk.length() + 1; } if (remotePaths.empty()) { Mprintf("[V2 Send] No files to send\n"); SendDownloadError(client, 87); // ERROR_INVALID_PARAMETER(与 Windows 客户端一致) return; } // 收集所有文件 (展开目录) std::vector allFiles; std::vector rootCandidates; int firstErrno = 0; // 首个失败的 errno(全部不存在时回报给服务端) for (const auto& remotePath : remotePaths) { std::string localPath = WinPathToLinux(remotePath); struct stat st; if (stat(localPath.c_str(), &st) != 0) { if (firstErrno == 0) firstErrno = errno ? errno : ENOENT; Mprintf("[V2 Send] File not found: %s (errno=%d)\n", localPath.c_str(), errno); continue; } if (S_ISDIR(st.st_mode)) { // 目录:递归收集 std::string dirPath = localPath; if (dirPath.back() != '/') dirPath += '/'; // 使用父目录参与公共根计算 size_t pos = dirPath.rfind('/', dirPath.length() - 2); std::string parentPath = (pos != std::string::npos) ? dirPath.substr(0, pos + 1) : dirPath; rootCandidates.push_back(parentPath); CollectFiles(dirPath, allFiles); } else { // 文件 rootCandidates.push_back(localPath); allFiles.push_back(localPath); } } if (allFiles.empty()) { Mprintf("[V2 Send] No files found (errno=%d)\n", firstErrno); SendDownloadError(client, PosixErrnoToWin32(firstErrno)); return; } // 计算公共根目录 std::string commonRoot = GetCommonRoot(rootCandidates); Mprintf("[V2 Send] Starting V2 transfer: %zu files, root=%s, target=%s\n", allFiles.size(), commonRoot.c_str(), targetDir.c_str()); // 启动传输线程 std::thread([allFiles, targetDir, commonRoot, client, myClientID]() { SendFilesV2(allFiles, targetDir, commonRoot, client, myClientID); }).detach(); } // V2 文件发送核心函数 static void SendFilesV2(const std::vector& files, const std::string& targetDir, const std::string& commonRoot, IOCPClient* client, uint64_t myClientID) { const size_t CHUNK_SIZE = 60 * 1024; // 60KB per chunk uint64_t transferID = GenerateTransferID(); uint32_t totalFiles = (uint32_t)files.size(); Mprintf("[V2 Send] TransferID=%llu, files=%u\n", transferID, totalFiles); for (uint32_t fileIndex = 0; fileIndex < totalFiles; ++fileIndex) { const std::string& filePath = files[fileIndex]; // 检查是否是目录项(以 / 结尾) bool isDirectory = !filePath.empty() && filePath.back() == '/'; std::string actualPath = isDirectory ? filePath.substr(0, filePath.length() - 1) : filePath; // 计算相对路径 std::string relativePath; if (actualPath.find(commonRoot) == 0) { relativePath = actualPath.substr(commonRoot.length()); } else { size_t lastSlash = actualPath.rfind('/'); relativePath = (lastSlash != std::string::npos) ? actualPath.substr(lastSlash + 1) : actualPath; } // 构建目标文件名 (服务端路径格式: targetDir + relativePath) std::string targetName = targetDir; if (!targetName.empty() && targetName.back() != '\\') { targetName += '\\'; } // 转换为 Windows 风格路径 for (char& c : relativePath) { if (c == '/') c = '\\'; } targetName += relativePath; // 转为 GBK 编码 std::string targetNameGbk = utf8ToGbk(targetName); // 目录项:发送单个包,不包含数据 if (isDirectory) { std::vector buffer(sizeof(FileChunkPacketV2) + targetNameGbk.length()); FileChunkPacketV2* pkt = (FileChunkPacketV2*)buffer.data(); memset(pkt, 0, sizeof(FileChunkPacketV2)); pkt->cmd = COMMAND_SEND_FILE_V2; pkt->transferID = transferID; pkt->srcClientID = myClientID; pkt->dstClientID = 0; // 发送给服务端 pkt->fileIndex = fileIndex; pkt->totalFiles = totalFiles; pkt->fileSize = 0; pkt->offset = 0; pkt->dataLength = 0; pkt->nameLength = targetNameGbk.length(); pkt->flags = FFV2_DIRECTORY | FFV2_LAST_CHUNK; // 写入目录名 memcpy(buffer.data() + sizeof(FileChunkPacketV2), targetNameGbk.c_str(), targetNameGbk.length()); Mprintf("[V2 Send] [%u/%u] DIR: %s -> %s\n", fileIndex + 1, totalFiles, actualPath.c_str(), targetName.c_str()); if (!client->Send2Server((char*)buffer.data(), (ULONG)buffer.size())) { Mprintf("[V2 Send] Send directory failed\n"); } usleep(1000); // 1ms 间隔 continue; } // 获取文件信息 struct stat st; if (stat(actualPath.c_str(), &st) != 0) { Mprintf("[V2 Send] stat failed: %s\n", actualPath.c_str()); continue; } uint64_t fileSize = (uint64_t)st.st_size; // 打开文件 int fd = open(actualPath.c_str(), O_RDONLY); if (fd < 0) { Mprintf("[V2 Send] open failed: %s, errno=%d\n", actualPath.c_str(), errno); continue; } Mprintf("[V2 Send] [%u/%u] %s -> %s (%llu bytes)\n", fileIndex + 1, totalFiles, actualPath.c_str(), targetName.c_str(), fileSize); // 分块发送 uint64_t offset = 0; std::vector buffer; buffer.reserve(sizeof(FileChunkPacketV2) + targetNameGbk.length() + CHUNK_SIZE); Sha256 hasher; // 流式 SHA-256:服务端落盘后会用它校验 bool truncated = false; // 读失败/文件变短 → 不发完成包,避免谎报校验通过 bool sendOk = true; while (offset < fileSize || (offset == 0 && fileSize == 0)) { size_t toRead = std::min(CHUNK_SIZE, (size_t)(fileSize - offset)); bool isLast = (offset + toRead >= fileSize); // 构建包头 buffer.resize(sizeof(FileChunkPacketV2) + targetNameGbk.length() + toRead); FileChunkPacketV2* pkt = (FileChunkPacketV2*)buffer.data(); memset(pkt, 0, sizeof(FileChunkPacketV2)); pkt->cmd = COMMAND_SEND_FILE_V2; pkt->transferID = transferID; pkt->srcClientID = myClientID; pkt->dstClientID = 0; // 发送给服务端 pkt->fileIndex = fileIndex; pkt->totalFiles = totalFiles; pkt->fileSize = fileSize; pkt->offset = offset; pkt->dataLength = toRead; pkt->nameLength = targetNameGbk.length(); pkt->flags = isLast ? FFV2_LAST_CHUNK : FFV2_NONE; // 写入文件名 memcpy(buffer.data() + sizeof(FileChunkPacketV2), targetNameGbk.c_str(), targetNameGbk.length()); // 读取文件数据 if (toRead > 0) { uint8_t* dataPtr = buffer.data() + sizeof(FileChunkPacketV2) + targetNameGbk.length(); ssize_t n = read(fd, dataPtr, toRead); if (n < 0) { Mprintf("[V2 Send] read failed: errno=%d\n", errno); truncated = true; break; } hasher.update(dataPtr, (size_t)n); if ((size_t)n < toRead) { // 文件读取不完整(传输中被截断):调整包大小后作为末块发出, // 但不发完成包 —— 服务端按原 fileSize 收不全,必须让它失败而不是校验通过。 toRead = n; pkt->dataLength = toRead; buffer.resize(sizeof(FileChunkPacketV2) + targetNameGbk.length() + toRead); isLast = true; pkt->flags |= FFV2_LAST_CHUNK; truncated = true; } } // 发送 if (!client->Send2Server((char*)buffer.data(), (ULONG)buffer.size())) { Mprintf("[V2 Send] Send failed\n"); sendOk = false; break; } offset += toRead; // 空文件处理 if (fileSize == 0) break; if (truncated) break; // 已按末块发出,不再续读 // 发送间隔 (避免发送过快) usleep(10000); // 10ms } close(fd); // 完成校验包:服务端凭它判定该文件结束并比对 SHA-256(目录项不发)。 // 与 Windows 客户端 FileBatchTransferWorkerV2 的发送点一致;没有它 MCP // download_file 的会话永远等不到 filesDone 达标。 if (sendOk && !truncated) { FileCompletePacketV2 cpkt; memset(&cpkt, 0, sizeof(cpkt)); cpkt.cmd = COMMAND_FILE_COMPLETE_V2; cpkt.transferID = transferID; cpkt.srcClientID = myClientID; cpkt.dstClientID = 0; // 目标是主控端 cpkt.fileIndex = fileIndex; cpkt.fileSize = fileSize; hasher.final(cpkt.sha256); if (!client->Send2Server((char*)&cpkt, (ULONG)sizeof(cpkt))) { Mprintf("[V2 Send] Send complete packet failed\n"); } } } Mprintf("[V2 Send] Transfer complete: transferID=%llu\n", transferID); } private: static RecvState* GetOrCreateState(uint64_t key, const FileChunkPacketV2* pkt, const std::string& savePath, int* openErrno = nullptr) { std::lock_guard lock(s_statesMtx); auto it = s_states.find(key); if (it != s_states.end()) { return it->second.get(); } // 创建新状态 std::unique_ptr state(new RecvState()); state->transferID = pkt->transferID; state->fileSize = pkt->fileSize; state->filePath = savePath; state->receivedBytes = 0; // 打开文件 state->fd = open(savePath.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644); if (state->fd < 0) { if (openErrno) *openErrno = errno; // errno 会被下面的 Mprintf 冲掉,先存 Mprintf("[V2 Recv] open failed: %s, errno=%d\n", savePath.c_str(), errno); return nullptr; } RecvState* ptr = state.get(); s_states[key] = std::move(state); return ptr; } // ===== C2C 接收回执 ===== // C2C 的目标端不再只是「收包落盘」:本次传输的每个条目都定案后回一包 // C2CSendResultPacket(TOKEN_C2C_RECV_RESULT,与源端的 175 分开,免得两种语义混在一个 token 里), // 把「是否真的写进去了」告诉服务端。Windows 目标端没有这个能力,服务端对它们保持旧语义。 // // 条目定案的三个来源(缺一不可): // 1) 目录项:worker 发的 FFV2_DIRECTORY 包(不带 LAST_CHUNK); // 2) 非空文件:最后一块写盘成功(再 fstat 核对长度); // 3) 空文件:worker 不发任何数据块,只发 COMMAND_FILE_COMPLETE_V2。 // 源端的重发(最多 3 次)和「最后一块 + 完成包」都会重复命中同一 fileIndex,用 done 集合去重。 struct RecvProgress { uint32_t totalFiles = 0; // 从任意数据块/目录包学到(完成包里没有这个字段) std::set doneFiles; // 已定案的文件 fileIndex std::set doneDirs; // 已定案的目录项 fileIndex uint64_t bytes = 0; // 实收文件字节数 bool failed = false; int firstErrno = 0; }; static bool IsFinalChunk(const FileChunkPacketV2* pkt) { return (pkt->flags & FFV2_LAST_CHUNK) != 0; } static uint8_t MapErrnoToFileError(int e) { switch (e) { case EACCES: case EPERM: case EROFS: return FEV2_ACCESS_DENIED; case ENOENT: case ENOTDIR: return FEV2_FILE_NOT_FOUND; case ENOSPC: return FEV2_DISK_FULL; default: return FEV2_WRITE_FAILED; } } // 记录一个条目的处理结果;finalChunk 表示这一包关掉了该条目(文件的最后一块 / 目录项 / 完成包)。 // 所有条目都定案后就发回执。err != 0 表示这一条目失败(errno 原样带给服务端)。 static void FinishC2CItem(const FileChunkPacketV2* pkt, IOCPClient* reply, uint64_t myClientID, bool isFile, bool finalChunk, uint64_t bytes, int err) { if (pkt->dstClientID == 0 || reply == nullptr) return; // 服务端上传/下载、或没有回执通道 C2CSendResultPacket res = {}; { std::lock_guard lk(s_recvProgressMtx); // 传输中途断掉时条目会残留:攒多了整体清一次(残留的都是已废弃的 transferID) if (s_recvProgress.size() > 32) s_recvProgress.clear(); RecvProgress& e = s_recvProgress[pkt->transferID]; if (pkt->totalFiles != 0) e.totalFiles = pkt->totalFiles; // 完成包里没有,沿用学到的值 if (err != 0) { e.failed = true; if (e.firstErrno == 0) e.firstErrno = err; } if (finalChunk) { if (isFile) { if (e.doneFiles.insert(pkt->fileIndex).second && err == 0) e.bytes += bytes; } else { e.doneDirs.insert(pkt->fileIndex); } } const bool finished = e.totalFiles != 0 && e.doneFiles.size() + e.doneDirs.size() >= e.totalFiles; if (!finished) return; res.cmd = (uint8_t)TOKEN_C2C_RECV_RESULT; res.transferID = pkt->transferID; res.dstClientID = myClientID; // 服务端按连接身份 + dstClientID 双重核对 res.status = e.failed ? (uint32_t)MapErrnoToFileError(e.firstErrno) : (uint32_t)FEV2_OK; res.win32err = (uint32_t)e.firstErrno; // 176 包里放的是 errno,不是 win32 码 res.workerRc = 0; // 恒 0:不是源端 worker 返回码 res.fileCount = (uint32_t)e.doneFiles.size(); res.totalBytes = e.bytes; s_recvProgress.erase(pkt->transferID); } // 锁外发送:Send2Server 可能阻塞,不应占着进度表 if (reply->IsConnected() && reply->Send2Server((char*)&res, sizeof(res))) { Mprintf("[V2 Recv] C2C ack: transferID=%llu, files=%u, bytes=%llu, status=%u, errno=%u\n", (unsigned long long)res.transferID, res.fileCount, (unsigned long long)res.totalBytes, res.status, res.win32err); } else { Mprintf("[V2 Recv] C2C ack send failed: transferID=%llu\n", (unsigned long long)res.transferID); } } // COMMAND_FILE_COMPLETE_V2:空文件的唯一「定案」来源(也用来给已定案的非空文件去重) static void NoteC2CComplete(const FileCompletePacketV2* cp, IOCPClient* reply, uint64_t myClientID) { if (cp->dstClientID == 0 || reply == nullptr) return; FileChunkPacketV2 probe = {}; probe.cmd = COMMAND_SEND_FILE_V2; probe.transferID = cp->transferID; probe.dstClientID = cp->dstClientID; probe.fileIndex = cp->fileIndex; probe.totalFiles = 0; // 完成包没有 totalFiles,沿用进度表里学到的 probe.fileSize = cp->fileSize; FinishC2CItem(&probe, reply, myClientID, true, true, 0, 0); // 空文件 bytes=0 } // 接收状态存储 static inline std::map> s_states; static inline std::mutex s_statesMtx; // C2C 目标目录存储 static inline std::map s_targetDirs; static inline std::mutex s_targetDirMtx; // C2C 接收进度(按 transferID;定案发完回执即删) static inline std::map s_recvProgress; static inline std::mutex s_recvProgressMtx; };