|
| 1 | +// Compiler for PHP (aka KPHP) |
| 2 | +// Copyright (c) 2025 LLC «V Kontakte» |
| 3 | +// Distributed under the GPL v3 License, see LICENSE.notice.txt |
| 4 | + |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include <cstddef> |
| 8 | +#include <cstdint> |
| 9 | +#include <expected> |
| 10 | +#include <iterator> |
| 11 | +#include <memory> |
| 12 | +#include <optional> |
| 13 | +#include <span> |
| 14 | +#include <string_view> |
| 15 | +#include <utility> |
| 16 | + |
| 17 | +#include "runtime-light/coroutine/task.h" |
| 18 | +#include "runtime-light/stdlib/diagnostics/logs.h" |
| 19 | +#include "runtime-light/stdlib/web-transfer-lib/defs.h" |
| 20 | +#include "runtime-light/stdlib/web-transfer-lib/web-state.h" |
| 21 | +#include "runtime-light/stdlib/web-transfer-lib/web.h" |
| 22 | +#include "runtime-light/tl/tl-core.h" |
| 23 | +#include "runtime-light/tl/tl-functions.h" |
| 24 | +#include "runtime-light/tl/tl-types.h" |
| 25 | + |
| 26 | +namespace kphp::web { |
| 27 | + |
| 28 | +inline auto composite_transfer_open(transfer_backend backend) noexcept -> kphp::coro::task<std::expected<composite_transfer, error>> { |
| 29 | + auto session{WebInstanceState::get().session_get_or_init()}; |
| 30 | + if (!session.has_value()) [[unlikely]] { |
| 31 | + kphp::log::error("failed to start or get session with Web component"); |
| 32 | + } |
| 33 | + |
| 34 | + if ((*session).get() == nullptr) [[unlikely]] { |
| 35 | + kphp::log::error("session with Web components has been closed"); |
| 36 | + } |
| 37 | + |
| 38 | + tl::CompositeWebTransferOpen web_transfer_open{.web_backend = {static_cast<tl::CompositeWebTransferOpen::web_backend_type::underlying_type>(backend)}}; |
| 39 | + tl::storer tls{web_transfer_open.footprint()}; |
| 40 | + web_transfer_open.store(tls); |
| 41 | + |
| 42 | + kphp::stl::vector<std::byte, kphp::memory::script_allocator> resp_buf{}; |
| 43 | + const auto response_buffer_provider{[&resp_buf](size_t size) noexcept -> std::span<std::byte> { |
| 44 | + resp_buf.resize(size); |
| 45 | + return {resp_buf.data(), size}; |
| 46 | + }}; |
| 47 | + |
| 48 | + auto resp{co_await (*session).get()->client.query(tls.view(), std::move(response_buffer_provider))}; |
| 49 | + if (!resp.has_value()) [[unlikely]] { |
| 50 | + kphp::log::error("failed to send request for Composite descriptor creation"); |
| 51 | + } |
| 52 | + |
| 53 | + tl::Either<tl::CompositeWebTransferOpenResultOk, tl::WebError> transfer_open_resp{}; |
| 54 | + tl::fetcher tlf{*resp}; |
| 55 | + if (!transfer_open_resp.fetch(tlf)) [[unlikely]] { |
| 56 | + kphp::log::error("failed to parse response for Composite descriptor creation"); |
| 57 | + } |
| 58 | + |
| 59 | + const auto result{transfer_open_resp.value}; |
| 60 | + if (std::holds_alternative<tl::WebError>(result)) { |
| 61 | + co_return std::unexpected{details::process_error(std::get<1>(std::move(result)))}; |
| 62 | + } |
| 63 | + |
| 64 | + const auto descriptor{std::get<0>(result).descriptor.value}; |
| 65 | + |
| 66 | + auto& composite2config{WebInstanceState::get().composite_transfer2config}; |
| 67 | + kphp::log::assertion(composite2config.contains(descriptor) == false); |
| 68 | + composite2config.emplace(descriptor, composite_transfer_config{}); |
| 69 | + |
| 70 | + co_return std::expected<composite_transfer, error>{descriptor}; |
| 71 | +} |
| 72 | + |
| 73 | +} // namespace kphp::web |
0 commit comments