From 0ec54ed4923f4e1425e959338883ed5ef9f9a018 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sun, 21 Feb 2021 14:31:28 -0800 Subject: [PATCH] htcs: fixes, echo server is now fully functional --- .../sf/cmif/sf_cmif_service_object_holder.hpp | 4 ++-- .../source/htc/server/rpc/htc_rpc_client.hpp | 2 +- .../source/htclow/mux/htclow_mux_task_manager.cpp | 2 +- .../source/htcs/client/htcs_session.cpp | 14 +++++++++++++- .../htcs/client/htcs_virtual_socket_collection.cpp | 5 ++--- .../source/htcs/impl/htcs_manager.cpp | 14 +++++++------- .../source/htcs/impl/htcs_service.cpp | 10 ++++++---- .../sf/hipc/sf_hipc_server_session_manager.cpp | 1 + 8 files changed, 33 insertions(+), 19 deletions(-) diff --git a/libraries/libstratosphere/include/stratosphere/sf/cmif/sf_cmif_service_object_holder.hpp b/libraries/libstratosphere/include/stratosphere/sf/cmif/sf_cmif_service_object_holder.hpp index d8f1c3ea1..cc9340caa 100644 --- a/libraries/libstratosphere/include/stratosphere/sf/cmif/sf_cmif_service_object_holder.hpp +++ b/libraries/libstratosphere/include/stratosphere/sf/cmif/sf_cmif_service_object_holder.hpp @@ -70,11 +70,11 @@ namespace ams::sf::cmif { /* Boolean operators. */ explicit constexpr operator bool() const { - return this->dispatch_meta != nullptr; + return this->srv != nullptr; } constexpr bool operator!() const { - return this->dispatch_meta == nullptr; + return this->srv == nullptr; } /* Getters. */ diff --git a/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_client.hpp b/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_client.hpp index c4715652a..9557cb676 100644 --- a/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_client.hpp +++ b/libraries/libstratosphere/source/htc/server/rpc/htc_rpc_client.hpp @@ -221,7 +221,7 @@ namespace ams::htc::server::rpc { } template requires IsRpcTask - Result VerifyTaskIdWitHandle(u32 task_id, s32 handle) { + Result VerifyTaskIdWithHandle(u32 task_id, s32 handle) { /* Lock ourselves. */ std::scoped_lock lk(m_mutex); diff --git a/libraries/libstratosphere/source/htclow/mux/htclow_mux_task_manager.cpp b/libraries/libstratosphere/source/htclow/mux/htclow_mux_task_manager.cpp index 564271242..4f22cc972 100644 --- a/libraries/libstratosphere/source/htclow/mux/htclow_mux_task_manager.cpp +++ b/libraries/libstratosphere/source/htclow/mux/htclow_mux_task_manager.cpp @@ -119,7 +119,7 @@ namespace ams::htclow::mux { void TaskManager::NotifyReceiveData(impl::ChannelInternalType channel, size_t size) { for (auto i = 0; i < MaxTaskCount; ++i) { - if (m_valid[i] && m_tasks[i].channel == channel && m_tasks[i].size <= size) { + if (m_valid[i] && m_tasks[i].channel == channel && m_tasks[i].type == TaskType_Receive && m_tasks[i].size <= size) { this->CompleteTask(i, EventTrigger_ReceiveData); } } diff --git a/libraries/libstratosphere/source/htcs/client/htcs_session.cpp b/libraries/libstratosphere/source/htcs/client/htcs_session.cpp index e3bc5fe1b..37fc6d749 100644 --- a/libraries/libstratosphere/source/htcs/client/htcs_session.cpp +++ b/libraries/libstratosphere/source/htcs/client/htcs_session.cpp @@ -15,8 +15,13 @@ */ #include #include "htcs_session.hpp" + +extern "C" { + #include +} + namespace ams::htcs::client { namespace { @@ -32,6 +37,10 @@ namespace ams::htcs::client { } } g_static_allocator_initializer; + } + + namespace { + class RemoteSocket { private: ::HtcsSocket m_s; @@ -112,6 +121,8 @@ namespace ams::htcs::client { ::HtcsSocket libnx_socket; R_TRY(::htcsCreateSocket(out_err.GetPointer(), std::addressof(libnx_socket), enable_disconnection_emulation)); + R_SUCCEED_IF(*out_err != 0); + *out = ObjectFactory::CreateSharedEmplaced(libnx_socket); return ResultSuccess(); } @@ -160,7 +171,6 @@ namespace ams::htcs::client { return ::htcsSocketFcntl(std::addressof(m_s), out_err.GetPointer(), out_res.GetPointer(), command, value); } - Result RemoteSocket::AcceptStart(sf::Out out_task_id, sf::OutCopyHandle out_event) { return ::htcsSocketAcceptStart(std::addressof(m_s), out_task_id.GetPointer(), out_event.GetHandlePointer()); } @@ -170,6 +180,8 @@ namespace ams::htcs::client { ::HtcsSocket libnx_socket; R_TRY(::htcsSocketAcceptResults(std::addressof(m_s), out_err.GetPointer(), std::addressof(libnx_socket), reinterpret_cast<::SockAddrHtcs *>(out_address.GetPointer()), task_id)); + R_SUCCEED_IF(*out_err != 0); + *out = ObjectFactory::CreateSharedEmplaced(libnx_socket); return ResultSuccess(); } diff --git a/libraries/libstratosphere/source/htcs/client/htcs_virtual_socket_collection.cpp b/libraries/libstratosphere/source/htcs/client/htcs_virtual_socket_collection.cpp index 0c6794dd7..1ef63f254 100644 --- a/libraries/libstratosphere/source/htcs/client/htcs_virtual_socket_collection.cpp +++ b/libraries/libstratosphere/source/htcs/client/htcs_virtual_socket_collection.cpp @@ -207,7 +207,6 @@ namespace ams::htcs::client { error_code = 0; /* Create the socket. */ - sf::SharedPointer socket(nullptr); return this->CreateSocket(sf::SharedPointer{nullptr}, error_code); } @@ -579,7 +578,7 @@ namespace ams::htcs::client { /* Add the socket to the list. */ if (m_list_count != 0) { - /* Ensure the list remains in sorder order. */ + /* Ensure the list remains in sorted order. */ s32 index; for (index = m_list_count - 1; index >= 0; --index) { if (m_socket_list[index].m_id < id) { @@ -687,7 +686,7 @@ namespace ams::htcs::client { s32 error_code = 0; /* Get socket. */ - sf::SharedPointer res = socket(id); + sf::SharedPointer res = socket(error_code); if (res != nullptr) { /* Assign the new socket. */ s32 index; diff --git a/libraries/libstratosphere/source/htcs/impl/htcs_manager.cpp b/libraries/libstratosphere/source/htcs/impl/htcs_manager.cpp index 0999b284d..f961b4f1d 100644 --- a/libraries/libstratosphere/source/htcs/impl/htcs_manager.cpp +++ b/libraries/libstratosphere/source/htcs/impl/htcs_manager.cpp @@ -45,7 +45,7 @@ namespace ams::htcs::impl { /* Set output. */ if (R_SUCCEEDED(result)) { *out_err = err; - if (out_err == 0) { + if (err == 0) { *out_desc = desc; } else { *out_desc = -1; @@ -77,7 +77,7 @@ namespace ams::htcs::impl { /* Set output. */ if (R_SUCCEEDED(result)) { *out_err = err; - if (out_err == 0) { + if (err == 0) { *out_res = 0; } else { *out_res = -1; @@ -96,7 +96,7 @@ namespace ams::htcs::impl { /* Set output. */ if (R_SUCCEEDED(result)) { *out_err = err; - if (out_err == 0) { + if (err == 0) { *out_res = 0; } else { *out_res = -1; @@ -115,7 +115,7 @@ namespace ams::htcs::impl { /* Set output. */ if (R_SUCCEEDED(result)) { *out_err = err; - if (out_err == 0) { + if (err == 0) { *out_res = 0; } else { *out_res = -1; @@ -135,7 +135,7 @@ namespace ams::htcs::impl { /* Set output. */ if (R_SUCCEEDED(result)) { *out_err = err; - if (out_err == 0) { + if (err == 0) { *out_size = recv_size; } else { *out_size = -1; @@ -155,7 +155,7 @@ namespace ams::htcs::impl { /* Set output. */ if (R_SUCCEEDED(result)) { *out_err = err; - if (out_err == 0) { + if (err == 0) { *out_size = send_size; } else { *out_size = -1; @@ -174,7 +174,7 @@ namespace ams::htcs::impl { /* Set output. */ if (R_SUCCEEDED(result)) { *out_err = err; - if (out_err == 0) { + if (err == 0) { *out_res = 0; } else { *out_res = -1; diff --git a/libraries/libstratosphere/source/htcs/impl/htcs_service.cpp b/libraries/libstratosphere/source/htcs/impl/htcs_service.cpp index 6fd77f87a..1e046d8cc 100644 --- a/libraries/libstratosphere/source/htcs/impl/htcs_service.cpp +++ b/libraries/libstratosphere/source/htcs/impl/htcs_service.cpp @@ -259,7 +259,7 @@ namespace ams::htcs::impl { Result HtcsService::SendSmallContinue(s64 *out_size, const char *buffer, s64 buffer_size, u32 task_id, s32 desc) { /* Verify the task. */ - R_TRY(m_rpc_client->VerifyTaskIdWitHandle(task_id, desc)); + R_TRY(m_rpc_client->VerifyTaskIdWithHandle(task_id, desc)); /* Continue the task. */ R_TRY(m_rpc_client->SendContinue(task_id, buffer, buffer_size)); @@ -293,7 +293,7 @@ namespace ams::htcs::impl { Result HtcsService::SendContinue(s64 *out_size, const char *buffer, s64 buffer_size, u32 task_id, s32 desc) { /* Verify the task. */ - R_TRY(m_rpc_client->VerifyTaskIdWitHandle(task_id, desc)); + R_TRY(m_rpc_client->VerifyTaskIdWithHandle(task_id, desc)); /* Wait for the task to notify. */ m_rpc_client->WaitNotification(task_id); @@ -307,12 +307,14 @@ namespace ams::htcs::impl { R_TRY(m_data_channel_manager->Send(buffer, buffer_size, task_id)); } + /* Set output. */ + *out_size = buffer_size; return ResultSuccess(); } Result HtcsService::SendResults(s32 *out_err, s64 *out_size, u32 task_id, s32 desc) { /* Verify the task. */ - R_TRY(m_rpc_client->VerifyTaskIdWitHandle(task_id, desc)); + R_TRY(m_rpc_client->VerifyTaskIdWithHandle(task_id, desc)); /* Finish the task. */ htcs::SocketError err; @@ -337,7 +339,7 @@ namespace ams::htcs::impl { Result HtcsService::ReceiveResults(s32 *out_err, s64 *out_size, char *buffer, s64 buffer_size, u32 task_id, s32 desc) { /* Verify the task. */ - R_TRY(m_rpc_client->VerifyTaskIdWitHandle(task_id, desc)); + R_TRY(m_rpc_client->VerifyTaskIdWithHandle(task_id, desc)); /* Get the result. */ htcs::SocketError err; diff --git a/libraries/libstratosphere/source/sf/hipc/sf_hipc_server_session_manager.cpp b/libraries/libstratosphere/source/sf/hipc/sf_hipc_server_session_manager.cpp index a07196ef6..0e19a97df 100644 --- a/libraries/libstratosphere/source/sf/hipc/sf_hipc_server_session_manager.cpp +++ b/libraries/libstratosphere/source/sf/hipc/sf_hipc_server_session_manager.cpp @@ -209,6 +209,7 @@ namespace ams::sf::hipc { this->CloseSessionImpl(session); return ResultSuccess(); } + switch (GetCmifCommandType(message)) { case CmifCommandType_Close: {