mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-10-13 02:41:16 +00:00
Fix the 3D view stuttering every few seconds while signed in to Orca Cloud
This commit is contained in:
@@ -523,6 +523,7 @@ OrcaCloudServiceAgent::OrcaCloudServiceAgent(std::string log_dir)
|
||||
|
||||
OrcaCloudServiceAgent::~OrcaCloudServiceAgent()
|
||||
{
|
||||
stop_health_check();
|
||||
if (refresh_thread.joinable()) {
|
||||
refresh_thread.join();
|
||||
}
|
||||
@@ -963,20 +964,55 @@ bool OrcaCloudServiceAgent::ensure_token_fresh(const std::string& reason) { retu
|
||||
// ICloudServiceAgent - Server Connectivity
|
||||
// ============================================================================
|
||||
|
||||
int OrcaCloudServiceAgent::connect_server()
|
||||
// /api/v1/health needs no auth, so unlike the data requests this does no token refresh or 401
|
||||
// recovery: refresh_connection() runs it off the UI thread, where a refresh could race a logout, and
|
||||
// it has to stay cancellable.
|
||||
bool OrcaCloudServiceAgent::run_health_check(const std::atomic_bool* cancel)
|
||||
{
|
||||
std::string response;
|
||||
unsigned int http_code = 0;
|
||||
int result = http_get(ORCA_HEALTH_PATH, &response, &http_code);
|
||||
if (cancel && cancel->load())
|
||||
return false;
|
||||
|
||||
bool connected = (result == BAMBU_NETWORK_SUCCESS && http_code >= 200 && http_code < 300);
|
||||
HttpResult res;
|
||||
try {
|
||||
auto http = Http::get(api_base_url + ORCA_HEALTH_PATH);
|
||||
http.tls_verify(true);
|
||||
for (const auto& [key, value] : data_headers())
|
||||
http.header(key, value);
|
||||
if (cancel)
|
||||
http.on_progress([cancel](Http::Progress, bool& cancel_request) { cancel_request = cancel->load(); });
|
||||
http.on_complete([&](std::string body, unsigned status) {
|
||||
res.success = true;
|
||||
res.status = status;
|
||||
res.body = std::move(body);
|
||||
})
|
||||
.on_error([&](std::string body, std::string error, unsigned status) {
|
||||
res.status = status == 0 ? 404 : status; // same mapping as http_get
|
||||
res.body = std::move(body);
|
||||
BOOST_LOG_TRIVIAL(error) << "OrcaCloudServiceAgent: health check failed - " << error;
|
||||
})
|
||||
.timeout_max(30)
|
||||
.perform_sync();
|
||||
} catch (const std::exception& e) {
|
||||
BOOST_LOG_TRIVIAL(error) << "OrcaCloudServiceAgent: health check exception - " << e.what();
|
||||
}
|
||||
// Stopped mid-request: report nothing, the receivers may be going away.
|
||||
if (cancel && cancel->load())
|
||||
return false;
|
||||
|
||||
const bool connected = res.success;
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> lock(state_mutex);
|
||||
is_connected = connected;
|
||||
}
|
||||
if (!connected)
|
||||
invoke_http_error_callback(res.status, res.body);
|
||||
invoke_server_connected_callback(connected ? 0 : -1, res.status);
|
||||
return connected;
|
||||
}
|
||||
|
||||
invoke_server_connected_callback(connected ? 0 : -1, http_code);
|
||||
return connected ? BAMBU_NETWORK_SUCCESS : BAMBU_NETWORK_ERR_CONNECTION_TO_SERVER_FAILED;
|
||||
int OrcaCloudServiceAgent::connect_server()
|
||||
{
|
||||
return run_health_check(nullptr) ? BAMBU_NETWORK_SUCCESS : BAMBU_NETWORK_ERR_CONNECTION_TO_SERVER_FAILED;
|
||||
}
|
||||
|
||||
bool OrcaCloudServiceAgent::is_server_connected()
|
||||
@@ -985,7 +1021,44 @@ bool OrcaCloudServiceAgent::is_server_connected()
|
||||
return is_connected;
|
||||
}
|
||||
|
||||
int OrcaCloudServiceAgent::refresh_connection() { return connect_server(); }
|
||||
// The device manager calls this every 5 s on the UI thread and the GET can take up to its 30 s
|
||||
// timeout, so the check runs on a worker and reports through is_server_connected() and the
|
||||
// callbacks. A call while a check is in flight is folded into it. Called from one thread only (the
|
||||
// UI thread), like stop_health_check().
|
||||
int OrcaCloudServiceAgent::refresh_connection()
|
||||
{
|
||||
if (health_check_stopped.load())
|
||||
return BAMBU_NETWORK_ERR_CANCELED;
|
||||
bool expected = false;
|
||||
if (!health_check_running.compare_exchange_strong(expected, true))
|
||||
return BAMBU_NETWORK_SUCCESS;
|
||||
if (health_check_thread.joinable())
|
||||
health_check_thread.join(); // the previous check is done; only its thread exit is left
|
||||
try {
|
||||
health_check_thread = std::thread([this] {
|
||||
try {
|
||||
run_health_check(&health_check_stopped);
|
||||
} catch (...) {
|
||||
// Callback dispatch (std::function copies, CallAfter) must not terminate the app.
|
||||
BOOST_LOG_TRIVIAL(error) << "OrcaCloudServiceAgent: health check worker exception";
|
||||
}
|
||||
health_check_running.store(false);
|
||||
});
|
||||
} catch (...) {
|
||||
// std::system_error, or std::bad_alloc for the callable: leave the next tick free to retry.
|
||||
health_check_running.store(false);
|
||||
BOOST_LOG_TRIVIAL(error) << "OrcaCloudServiceAgent: cannot start health check";
|
||||
return BAMBU_NETWORK_ERR_CONNECTION_TO_SERVER_FAILED;
|
||||
}
|
||||
return BAMBU_NETWORK_SUCCESS;
|
||||
}
|
||||
|
||||
void OrcaCloudServiceAgent::stop_health_check()
|
||||
{
|
||||
health_check_stopped.store(true);
|
||||
if (health_check_thread.joinable())
|
||||
health_check_thread.join();
|
||||
}
|
||||
|
||||
int OrcaCloudServiceAgent::start_subscribe(std::string module)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user