Skip to content

Commit ad32175

Browse files
MistEOCopilot
andauthored
fix: 更稳健地处理 Windows IPC 临时路径与临时文件清理 (#956)
Co-authored-by: Copilot <[email protected]>
1 parent 15d054f commit ad32175

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

source/AgentCommon/Transceiver.cpp

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
#include <format>
44

5+
#ifdef _WIN32
6+
#include "MaaUtils/SafeWindows.hpp"
7+
#endif
8+
59
#include "MaaUtils/Platform.h"
10+
#include "MaaUtils/StringMisc.hpp"
611
#include "MaaUtils/Uuid.h"
712

813
MAA_AGENT_NS_BEGIN
@@ -29,12 +34,30 @@ bool Transceiver::handle_image_header(const json::value& j)
2934
return true;
3035
}
3136

37+
static std::string temp_directory()
38+
{
39+
auto path = std::filesystem::temp_directory_path();
40+
41+
#ifdef _WIN32
42+
// ZeroMQ IPC 在 Windows 上不支持 Unicode 路径,拉💩
43+
if (GetACP() != CP_UTF8 && std::ranges::any_of(path.native(), [](wchar_t ch) { return ch > 127; })) {
44+
path = MaaNS::path("C:/Temp");
45+
if (!std::filesystem::exists(path)) {
46+
std::filesystem::create_directories(path);
47+
}
48+
}
49+
#endif
50+
51+
return path_to_utf8_string(path);
52+
}
53+
3254
void Transceiver::init_socket(const std::string& identifier, bool bind)
3355
{
34-
static auto kTempDir = std::filesystem::temp_directory_path();
35-
constexpr std::string_view kAddrFormat = "ipc://{}/maafw-agent-{}.sock";
56+
static auto kTempDir = temp_directory();
3657

37-
ipc_addr_ = std::format(kAddrFormat, path_to_utf8_string(kTempDir), identifier);
58+
std::string path = std::format("{}/maafw-agent-{}.sock", kTempDir, identifier);
59+
ipc_addr_ = std::format("ipc://{}", path);
60+
ipc_path_ = MaaNS::path(path);
3861

3962
LogInfo << VAR(ipc_addr_) << VAR(identifier);
4063

@@ -68,6 +91,9 @@ void Transceiver::uninit_socket()
6891

6992
zmq_sock_.close();
7093
zmq_ctx_.close();
94+
95+
std::error_code ec;
96+
std::filesystem::remove(ipc_path_, ec);
7197
}
7298

7399
bool Transceiver::alive()

source/MaaAdbControlUnit/Screencap/EncodeToFile.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ std::optional<cv::Mat> ScreencapEncodeToFileAndPull::screencap()
5555
}
5656

5757
auto image = imread(dst_path);
58-
std::filesystem::remove(dst_path);
58+
std::error_code ec;
59+
std::filesystem::remove(dst_path, ec);
5960

6061
if (image.empty()) {
6162
LogError << "Failed to read image from" << dst_path;

source/MaaFramework/Task/Component/CommandAction.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ TempFileHolder::~TempFileHolder()
2121
continue;
2222
}
2323
LogTrace << "remove" << VAR(p);
24-
std::filesystem::remove(p);
24+
std::error_code ec;
25+
std::filesystem::remove(p, ec);
2526
}
2627
}
2728

source/include/MaaAgent/Transceiver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class Transceiver
8282
zmq::socket_t zmq_sock_;
8383

8484
std::string ipc_addr_;
85+
std::filesystem::path ipc_path_;
8586

8687
std::map<std::string /* uuid */, cv::Mat> recved_images_;
8788

0 commit comments

Comments
 (0)