后台服务协程化

This commit is contained in:
2026-08-18 12:15:41 +08:00
parent 1e1de64199
commit 77f856714d
19 changed files with 164 additions and 25 deletions
+45
View File
@@ -8,6 +8,7 @@
#include <chrono>
#include <cmath>
#include <cstdint>
#include <exception>
#include <optional>
#include <string>
#include <string_view>
@@ -308,6 +309,40 @@ struct Gallery_Plot_Session::Impl {
return std::nullopt;
return Web_Response{Web_Response_Type::Pixels, std::move(*pixels)};
}
void async_handle_frame_request(Web_Response_Completion completion) {
const auto request_started = std::chrono::steady_clock::now();
if (!scene) {
completion({});
return;
}
const bool submitted = scene->async_request_frame(
[this, request_started, completion](Scene_Render_Result render_result, std::exception_ptr exception) mutable {
if (exception) {
completion({{}, std::move(exception)});
return;
}
if (render_result != Scene_Render_Result::none) {
completion({});
return;
}
try {
const auto encode_started = std::chrono::steady_clock::now();
auto pixels = scene->encode_latest_pixels();
const auto encode_finished = std::chrono::steady_clock::now();
if (pixels)
scene->record_pixel_response(request_started, encode_started, encode_finished, pixels->size());
if (!pixels) {
completion({});
return;
}
completion({Web_Response{Web_Response_Type::Pixels, std::move(*pixels)}, {}});
}
catch (...) {
completion({{}, std::current_exception()});
}
});
if (!submitted) completion({});
}
std::optional<Web_Response> handle(const Web_Event& event) {
if (std::holds_alternative<Frame_Request>(event))
return handle_frame_request();
@@ -336,10 +371,20 @@ struct Gallery_Plot_Session::Impl {
},
event);
}
void async_handle(const Web_Event& event, Web_Response_Completion completion) {
if (std::holds_alternative<Frame_Request>(event)) {
async_handle_frame_request(std::move(completion));
return;
}
completion({handle(event), {}});
}
};
Gallery_Plot_Session::Gallery_Plot_Session() : impl_(std::make_unique<Impl>()) {}
Gallery_Plot_Session::~Gallery_Plot_Session() = default;
std::optional<Web_Response> Gallery_Plot_Session::handle(const Web_Event& event) {
return impl_->handle(event);
}
void Gallery_Plot_Session::async_handle(const Web_Event& event, Web_Response_Completion completion) {
impl_->async_handle(event, std::move(completion));
}
} // namespace renderive::web