Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
546404d
feat(http2): complete asynchronous send and read
edubox Oct 26, 2026
952ab52
fix(http2): own async completion state per request
edubox Oct 27, 2026
0417581
test(http2): align async failure fixtures with exception delivery
edubox Oct 27, 2026
13b30cd
fix(http2): order async completion delivery with retirement
edubox Oct 27, 2026
72f2091
test(http2): extend async event waits for slow loopback connects
edubox Oct 27, 2026
b021ca0
test(http2): bind loopback fixtures to the client host
edubox Oct 27, 2026
104ece6
feat(http2): accept the optional async arguments
Oct 28, 2026
0760a87
fix(http2): treat a zero wait timeout as blocking
Oct 28, 2026
57a1b14
test(http2): cover an async send and read without a kernel queue
edubox Oct 28, 2026
cdb5092
fix(http2): follow the 11.00 wait timeout and result contract
edubox Oct 29, 2026
12d8d00
fix(http2): keep a zero wait timeout blocking
edubox Oct 29, 2026
6d00049
fix(http2): report the remaining async wait timeout
edubox Oct 29, 2026
1c06e1a
docs(http2): trim the async wait debt entry
edubox Oct 29, 2026
f364585
fix(http2): leave the zero wait timeout unimplemented
edubox Oct 29, 2026
60b06e5
test(http2): bind the async fixtures to localhost on the first address
edubox Nov 4, 2026
4b14055
Merge remote-tracking branch 'origin/main' into feat/http2-async-comp…
edubox Nov 4, 2026
ce840ea
Merge main into feat/http2-async-completions
oneandonlydean Nov 5, 2026
3a4ca44
Merge branch 'main'
oneandonlydean Nov 5, 2026
19e010d
Merge remote-tracking branch 'origin/main' into feat/http2-async-comp…
edubox Nov 6, 2026
f11bebc
Merge remote-tracking branch 'origin/main' into feat/http2-async-comp…
edubox Nov 6, 2026
7ce9737
Merge remote-tracking branch 'origin/main' into feat/http2-async-comp…
edubox Nov 7, 2026
f40d440
Merge remote-tracking branch 'origin/main' into feat/http2-async-comp…
edubox Nov 8, 2026
1937f3c
docs(libSceHttp2): keep the offline async send caveats
oneandonlydean Nov 8, 2026
cbfd4c4
Merge remote-tracking branch 'origin/main' into feat/http2-async-comp…
edubox Nov 9, 2026
8330f75
docs(libSceHttp2): keep the bundled-module async evidence from #15996
oneandonlydean Nov 9, 2026
ee359d6
fix(libSceHttp2): return a queued completion from a zero-timeout wait
oneandonlydean Nov 9, 2026
4788c23
fix(libSceHttp2): report the measured zero-timeout wait results
edubox Nov 9, 2026
9f95abb
fix(libSceHttp2): report idle and queued async wait results
edubox Nov 9, 2026
b17933d
docs(libSceHttp2): mark the null-timeout idle wait as unmeasured
edubox Nov 9, 2026
0638b71
Merge remote-tracking branch 'origin/main' into feat/http2-async-comp…
edubox Nov 12, 2026
fb0e189
feat(http2): keep the overall deadline off asynchronous sends
edubox Nov 13, 2026
148c3ba
fix(http2): complete asynchronous operations of a deleted request
edubox Nov 13, 2026
ab1f2d5
Merge remote-tracking branch 'origin/main' into feat/http2-async-comp…
edubox Nov 15, 2026
06638cf
Merge remote-tracking branch 'origin/main' into feat/http2-async-comp…
edubox Nov 19, 2026
f2fd1aa
fix(libSceHttp2): refuse an async submit on a deleted request
oneandonlydean Nov 20, 2026
8c98607
fix(libSceHttp2): publish the aborted completions before the transpor…
edubox Nov 21, 2026
003cd60
docs(libSceHttp2): say why a null timeout takes the idle path
edubox Nov 23, 2026
abd6fed
Merge remote-tracking branch 'origin/main' into feat/http2-async-comp…
edubox Nov 23, 2026
301e49b
docs(libSceHttp2): keep main's offline entry and name the wait entry …
edubox Nov 23, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
286 changes: 219 additions & 67 deletions core/libs/prx/libSceHttp2/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,51 @@
#include "prx/libc/include/Api.hpp"
#include "prx/libSceHttp/include/Http.hpp"
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <deque>
#include <exception>
#include <mutex>
#include <memory>
#include <optional>
#include <limits>
#include <stdexcept>
#include <string>
#include <deque>
#include <thread>
#include <type_traits>
#include <unordered_map>
#include <vector>

static constexpr int ERROR_INVALID_ID = static_cast<int>(0x817B1100);
static constexpr int ERROR_NULL_POINTER = static_cast<int>(0x817B1225);
static constexpr int ERROR_NOT_FOUND = static_cast<int>(0x817B1025);
static constexpr int ERROR_INVALID_VALUE = static_cast<int>(0x817B11FE);
static constexpr int ERROR_TIMEOUT = static_cast<int>(0x817B1079);
static constexpr int ERROR_ABORTED = static_cast<int>(0x817B1080);
static constexpr int ERROR_HTTP_INVALID_VALUE = static_cast<int>(0x804311FE);
static constexpr int SSL_ERROR_INVALID_ID = static_cast<int>(0x8095F006);
static constexpr int ERROR_NOT_CONNECTED = static_cast<int>(0x817B1065);
static constexpr int ERROR_UNSUPPORTED_SCHEME = static_cast<int>(0x817B1061);
static constexpr std::uint32_t WEBSOCKET_MIN_PERIOD = 1000000;
static constexpr int ERROR_HTTP2_ASYNC_NOT_SUPPORTED = static_cast<int>(0x80436063);

static std::mutex g_completionsMutex;
static std::unordered_map<int, std::deque<Http2AsyncResult>> g_completions;

enum class Http2Kind : std::uint8_t { Context, Template, Request, CookieBox };

struct Http2AsyncCompletion {
Http2AsyncResult result{};
std::exception_ptr failure;
};

struct Http2AsyncState {
std::mutex mutex;
std::condition_variable changed;
std::deque<Http2AsyncCompletion> pending;
std::size_t inFlight[2] = {0, 0};
std::size_t aborted[2] = {0, 0};
std::size_t InFlight() const { return inFlight[0] + inFlight[1]; }
bool deleted = false;
bool retired = false;
};

struct Http2Handle {
Http2Kind kind;
int owner;
Expand All @@ -47,12 +67,97 @@ struct Http2Handle {
std::string method, url;
std::shared_ptr<Http2::Request> transport;
std::shared_ptr<Http2::Capacity> capacity;
std::shared_ptr<Http2AsyncState> async = std::make_shared<Http2AsyncState>();
bool webSocket = false;
};

static std::atomic<int> g_nextHandle{1};
static std::mutex g_handlesMutex;
static std::unordered_map<int, Http2Handle> g_handles;
static std::atomic<GuestSceHttp2Test::AsyncPublishHook> g_asyncPublishHook{nullptr};
static std::atomic<void*> g_asyncPublishHookData{nullptr};

static void PublishAsync(Http2AsyncState& state, const char* function, int req_id,
const Http2AsyncOption* option, int event_type, int result, std::exception_ptr failure) {
Http2AsyncCompletion completion{};
completion.result.event_type = event_type;
completion.result.req_id = req_id;
completion.result.result = result;
completion.failure = std::move(failure);
state.pending.push_back(std::move(completion));
if (const auto hook = g_asyncPublishHook.load(std::memory_order_acquire)) {
hook(g_asyncPublishHookData.load(std::memory_order_acquire));
}
if (option && sceKernelTriggerUserEvent(option->equeue, option->user_event_id, option->user_data) != 0 &&
!state.pending.back().failure) {
try {
NotImplemented_nid_no_patch(function);
} catch (...) {
state.pending.back().failure = std::current_exception();
}
}
state.changed.notify_all();
}

static void CompleteAsync(const std::shared_ptr<Http2AsyncState>& state, const char* function, int req_id,
const Http2AsyncOption* option, int event_type, int result, std::exception_ptr failure) {
std::lock_guard lock(state->mutex);
const auto index = static_cast<std::size_t>(event_type);
if (index < 2 && state->inFlight[index] != 0) --state->inFlight[index];
if (state->deleted || state->retired) return;
PublishAsync(*state, function, req_id, option, event_type, result, std::move(failure));
}

static void CompleteAsyncRead(const std::shared_ptr<Http2AsyncState>& state, const char* function, int req_id,
const Http2AsyncOption* option, void* data, const std::vector<std::uint8_t>& staged, int result, std::exception_ptr failure) {
std::lock_guard lock(state->mutex);
if (state->inFlight[1] != 0) --state->inFlight[1];
if (state->deleted || state->retired) return;
if (result > 0) std::memcpy(data, staged.data(), static_cast<std::size_t>(result));
PublishAsync(*state, function, req_id, option, 1, result, std::move(failure));
}

static void RetireAsync(const std::shared_ptr<Http2AsyncState>& state) {
{
std::lock_guard lock(state->mutex);
state->retired = true;
state->pending.clear();
state->inFlight[0] = state->inFlight[1] = 0;
}
state->changed.notify_all();
}

static void DropAsync(const std::shared_ptr<Http2AsyncState>& state) {
std::lock_guard lock(state->mutex);
state->deleted = true;
state->aborted[0] = state->inFlight[0];
state->aborted[1] = state->inFlight[1];
}

static void AbortAsync(const std::shared_ptr<Http2AsyncState>& state, int req_id) {
std::lock_guard lock(state->mutex);
state->retired = true;
state->pending.clear();
for (std::size_t kind = 0; kind != 2; ++kind) {
for (std::size_t pending = state->aborted[kind]; pending != 0; --pending) {
Http2AsyncCompletion completion{};
completion.result.event_type = static_cast<int>(kind);
completion.result.req_id = req_id;
completion.result.result = ERROR_ABORTED;
state->pending.push_back(std::move(completion));
}
state->aborted[kind] = 0;
state->inFlight[kind] = 0;
}
state->changed.notify_all();
}

namespace GuestSceHttp2Test {
void SetAsyncPublishHookForTests_nid_no_patch(AsyncPublishHook hook, void* data) noexcept {
g_asyncPublishHookData.store(data, std::memory_order_release);
g_asyncPublishHook.store(hook, std::memory_order_release);
}
}

static Http2Handle* FindHandle(int id, Http2Kind kind) {
const auto entry = g_handles.find(id);
Expand All @@ -74,49 +179,12 @@ static void ReleaseOwnedHandles(int owner, bool terminated) {
const int id = child->first;
ReleaseOwnedHandles(id, terminated);
auto& handle = g_handles.at(id);
RetireAsync(handle.async);
if (handle.transport) handle.transport->Stop(terminated);
{
std::lock_guard lock(g_completionsMutex);
g_completions.erase(id);
}
g_handles.erase(id);
}
}

static void PrepareTransport(int id, Http2Handle& handle) {
if (!handle.transport) {
handle.transport = Http2::CreateRequest_nid_no_patch(id, handle.version, handle.sslContext, handle.capacity, handle.settings, handle.method, handle.url);
}
}

static int CompleteAsync(const char* function, int req_id, const void* postData, std::size_t size, const Http2AsyncOption* kqueue_option, const void* option) {
if (std::getenv("APS5_NET_DISCONNECTED") == nullptr) {
NotImplemented_nid_no_patch(function);
return 0;
}
if (option != nullptr) {
NotImplemented_nid_no_patch(function);
return 0;
}
std::lock_guard handlesLock(g_handlesMutex);
auto* handle = FindHandle(req_id, Http2Kind::Request);
if (!handle) return ERROR_INVALID_ID;
PrepareTransport(req_id, *handle);
const int result = handle->transport->Send(postData, size);
std::lock_guard completionsLock(g_completionsMutex);
if (kqueue_option != nullptr) {
if (sceKernelTriggerUserEvent(kqueue_option->equeue, kqueue_option->user_event_id, kqueue_option->user_data) != 0) {
NotImplemented_nid_no_patch(function);
return 0;
}
}
Http2AsyncResult completion{};
completion.req_id = req_id;
completion.result = result;
g_completions[req_id].push_back(completion);
return 0;
}

static bool IsWebSocket(int id) {
std::lock_guard lock(g_handlesMutex);
const Http2Handle* handle = FindHandle(id, Http2Kind::Request);
Expand Down Expand Up @@ -172,12 +240,15 @@ static bool IsConnectionHeader(const char* name) {
return true;
}

static std::shared_ptr<Http2::Request> Transport(int id, bool create) {
static std::shared_ptr<Http2::Request> Transport(int id, bool create, std::shared_ptr<Http2AsyncState>* async = nullptr, bool ignoresTotalTimeout = false) {
std::lock_guard lock(g_handlesMutex);
auto* handle = FindHandle(id, Http2Kind::Request);
if (!handle) return nullptr;
if (create) PrepareTransport(id, *handle);
if (create && !handle->transport) {
handle->transport = Http2::CreateRequest_nid_no_patch(id, handle->version, handle->sslContext, handle->capacity, handle->settings, handle->method, handle->url, ignoresTotalTimeout);
}
if (!handle->transport) NotImplemented_nid_no_patch("sceHttp2: request has not been sent");
if (async) *async = handle->async;
return handle->transport;
}

Expand Down Expand Up @@ -226,15 +297,19 @@ int APS5_VABI sceHttp2CreateTemplate(int lib_http2_ctx_id, const char* user_agen
}

int APS5_VABI sceHttp2DeleteRequest(int req_id) {
std::lock_guard lock(g_handlesMutex);
auto* handle = FindHandle(req_id, Http2Kind::Request);
if (!handle) return ERROR_INVALID_ID;
if (handle->transport) handle->transport->Stop(false);
std::shared_ptr<Http2AsyncState> async;
std::shared_ptr<Http2::Request> transport;
{
std::lock_guard completionsLock(g_completionsMutex);
g_completions.erase(req_id);
std::lock_guard lock(g_handlesMutex);
auto* handle = FindHandle(req_id, Http2Kind::Request);
if (!handle) return ERROR_INVALID_ID;
async = handle->async;
transport = handle->transport;
g_handles.erase(req_id);
}
g_handles.erase(req_id);
DropAsync(async);
AbortAsync(async, req_id);
if (transport) transport->Stop(false);
return 0;
}

Expand Down Expand Up @@ -291,7 +366,30 @@ int APS5_VABI sceHttp2ReadData(int req_id, void* data, size_t size) {
}

int APS5_VABI sceHttp2ReadDataAsync(int req_id, void* data, size_t size, void* kqueue_option, void* option) {
return ERROR_HTTP2_ASYNC_NOT_SUPPORTED;
(void)option;
if (!data && size) return ERROR_NULL_POINTER;
std::shared_ptr<Http2AsyncState> state;
const auto request = Transport(req_id, false, &state);
if (!request) return ERROR_INVALID_ID;
const std::optional<Http2AsyncOption> async = kqueue_option
? std::optional<Http2AsyncOption>(*static_cast<const Http2AsyncOption*>(kqueue_option)) : std::nullopt;
{
std::lock_guard lock(state->mutex);
if (state->deleted || state->retired) return ERROR_INVALID_ID;
++state->inFlight[1];
}
std::thread([request, state, async, data, size, req_id] {
std::vector<std::uint8_t> staged(size);
int result = 0;
std::exception_ptr failure;
try {
result = request->Read(size ? staged.data() : nullptr, size);
} catch (...) {
failure = std::current_exception();
}
CompleteAsyncRead(state, "sceHttp2ReadDataAsync", req_id, async ? &*async : nullptr, data, staged, result, std::move(failure));
}).detach();
return 0;
}

int APS5_VABI sceHttp2SendRequest(int req_id, const void* post_data, size_t size) {
Expand All @@ -300,7 +398,42 @@ int APS5_VABI sceHttp2SendRequest(int req_id, const void* post_data, size_t size
}

int APS5_VABI sceHttp2SendRequestAsync(int req_id, const void* post_data, size_t size, Http2AsyncOption* kqueue_option, void* option) {
return CompleteAsync(__func__, req_id, post_data, size, kqueue_option, option);
(void)option;
if (!post_data && size) return ERROR_NULL_POINTER;
const std::optional<Http2AsyncOption> async = kqueue_option
? std::optional<Http2AsyncOption>(*kqueue_option) : std::nullopt;
std::shared_ptr<Http2AsyncState> state;
if (std::getenv("APS5_NET_DISCONNECTED") != nullptr) {
const auto request = Transport(req_id, true, &state, true);
if (!request) return ERROR_INVALID_ID;
{
std::lock_guard lock(state->mutex);
if (state->deleted || state->retired) return ERROR_INVALID_ID;
++state->inFlight[0];
}
CompleteAsync(state, __func__, req_id, async ? &*async : nullptr, 0, request->Send(post_data, size), {});
return 0;
}
const auto request = Transport(req_id, true, &state, true);
if (!request) return ERROR_INVALID_ID;
std::vector<std::uint8_t> body;
if (size) body.assign(static_cast<const std::uint8_t*>(post_data), static_cast<const std::uint8_t*>(post_data) + size);
{
std::lock_guard lock(state->mutex);
if (state->deleted || state->retired) return ERROR_INVALID_ID;
++state->inFlight[0];
}
std::thread([request, state, async, body = std::move(body), req_id]() mutable {
int result = 0;
std::exception_ptr failure;
try {
result = request->Send(body.empty() ? nullptr : body.data(), body.size());
} catch (...) {
failure = std::current_exception();
}
CompleteAsync(state, "sceHttp2SendRequestAsync", req_id, async ? &*async : nullptr, 0, result, std::move(failure));
}).detach();
return 0;
}

int APS5_VABI sceHttp2SetAuthEnabled(int id, int is_enable) {
Expand Down Expand Up @@ -412,24 +545,43 @@ int APS5_VABI sceHttp2Term(int lib_http2_ctx_id) {
}

int APS5_VABI sceHttp2WaitAsync(int req_id, Http2AsyncResult* result, uint32_t* timeout, void* option) {
(void)timeout;
if (result == nullptr) return ERROR_NULL_POINTER;
if (option != nullptr) return ERROR_INVALID_VALUE;
if (!result) return ERROR_NULL_POINTER;
Http2AsyncResult requested{};
if (!Http2Guest::CopyIn(&requested, result, sizeof(requested))) return ERROR_NULL_POINTER;
if (!Http2Guest::Store(result, requested)) return ERROR_NULL_POINTER;
if (option || requested.reserved) return ERROR_INVALID_VALUE;
std::shared_ptr<Http2AsyncState> state;
{
std::lock_guard lock(g_handlesMutex);
if (FindHandle(req_id, Http2Kind::Request) == nullptr) return ERROR_INVALID_ID;
auto* handle = FindHandle(req_id, Http2Kind::Request);
if (!handle) return ERROR_INVALID_ID;
state = handle->async;
}
if (std::getenv("APS5_NET_DISCONNECTED") != nullptr) {
std::lock_guard lock(g_completionsMutex);
const auto pending = g_completions.find(req_id);
if (pending != g_completions.end() && !pending->second.empty()) {
const Http2AsyncResult completed = pending->second.front();
if (!Http2Guest::Store(result, completed)) return ERROR_NULL_POINTER;
pending->second.pop_front();
return 0;
std::unique_lock lock(state->mutex);
const auto available = [&] { return state->retired || !state->pending.empty(); };
if (!available() && state->InFlight() == 0) return ERROR_NOT_CONNECTED;
if (timeout && *timeout == 0) {
if (!available()) return ERROR_TIMEOUT;
} else if (timeout) {
if (!state->retired && state->pending.empty()) {
const auto deadline = std::chrono::steady_clock::now() + std::chrono::microseconds(*timeout);
const bool ready = state->changed.wait_until(lock, deadline, available);
const auto remaining = std::chrono::duration_cast<std::chrono::microseconds>(deadline - std::chrono::steady_clock::now());
*timeout = remaining.count() > 0 ? static_cast<std::uint32_t>(remaining.count()) : 0;
if (!ready) return ERROR_TIMEOUT;
}
} else {
state->changed.wait(lock, available);
}
NotImplemented_nid_no_patch(__func__);
if (!state->pending.empty()) {
auto completion = std::move(state->pending.front());
state->pending.pop_front();
if (completion.failure) std::rethrow_exception(completion.failure);
if (!Http2Guest::Store(result, completion.result)) return ERROR_NULL_POINTER;
return 0;
}
if (!state->retired) throw std::logic_error("sceHttp2WaitAsync: wait returned without a completion");
return ERROR_INVALID_ID;
}

int APS5_VABI sceHttp2AbortRequest(int req_id) {
Expand Down
Loading
Loading