diff --git a/libraries/libstratosphere/source/htcs/client/htcs_session.cpp b/libraries/libstratosphere/source/htcs/client/htcs_session.cpp new file mode 100644 index 000000000..e3bc5fe1b --- /dev/null +++ b/libraries/libstratosphere/source/htcs/client/htcs_session.cpp @@ -0,0 +1,237 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include "htcs_session.hpp" +#include + +namespace ams::htcs::client { + + namespace { + + struct HtcsObjectAllocatorTag; + using ObjectAllocator = ams::sf::ExpHeapStaticAllocator<16_KB, HtcsObjectAllocatorTag>; + using ObjectFactory = ams::sf::ObjectFactory; + + class StaticAllocatorInitializer { + public: + StaticAllocatorInitializer() { + ObjectAllocator::Initialize(lmem::CreateOption_ThreadSafe); + } + } g_static_allocator_initializer; + + class RemoteSocket { + private: + ::HtcsSocket m_s; + public: + RemoteSocket(::HtcsSocket &s) : m_s(s) { /* ... */ } + ~RemoteSocket() { ::htcsCloseSocket(std::addressof(m_s)); } + public: + Result Accept(sf::Out out_err, sf::Out> out, sf::Out out_address) { AMS_ABORT("Not Implemented"); } + Result Recv(sf::Out out_err, sf::Out out_size, const sf::OutAutoSelectBuffer &buffer, s32 flags) { AMS_ABORT("Not Implemented"); } + Result Send(sf::Out out_err, sf::Out out_size, const sf::InAutoSelectBuffer &buffer, s32 flags) { AMS_ABORT("Not Implemented"); } + Result RecvLargeStart(sf::Out out_task_id, sf::OutCopyHandle out_event, s32 unaligned_size_start, s32 unaligned_size_end, s64 aligned_size, sf::CopyHandle mem_handle, s32 flags) { AMS_ABORT("Not Implemented"); } + Result SendStartOld(sf::Out out_task_id, sf::OutCopyHandle out_event, const sf::InAutoSelectBuffer &buffer, s32 flags) { AMS_ABORT("Not Implemented"); } + Result SendLargeStart(sf::Out out_task_id, sf::OutCopyHandle out_event, const sf::InAutoSelectBuffer &start_buffer, const sf::InAutoSelectBuffer &end_buffer, sf::CopyHandle mem_handle, s64 aligned_size, s32 flags) { AMS_ABORT("Not Implemented"); } + Result ContinueSendOld(sf::Out out_size, sf::Out out_wait, const sf::InAutoSelectBuffer &buffer, u32 task_id) { AMS_ABORT("Not Implemented"); } + + Result Close(sf::Out out_err, sf::Out out_res); + Result Connect(sf::Out out_err, sf::Out out_res, const htcs::SockAddrHtcs &address); + Result Bind(sf::Out out_err, sf::Out out_res, const htcs::SockAddrHtcs &address); + Result Listen(sf::Out out_err, sf::Out out_res, s32 backlog_count); + Result Shutdown(sf::Out out_err, sf::Out out_res, s32 how); + Result Fcntl(sf::Out out_err, sf::Out out_res, s32 command, s32 value); + + Result AcceptStart(sf::Out out_task_id, sf::OutCopyHandle out_event); + Result AcceptResults(sf::Out out_err, sf::Out> out, sf::Out out_address, u32 task_id); + + Result RecvStart(sf::Out out_task_id, sf::OutCopyHandle out_event, s32 mem_size, s32 flags); + Result RecvResults(sf::Out out_err, sf::Out out_size, const sf::OutAutoSelectBuffer &buffer, u32 task_id); + + Result SendStart(sf::Out out_task_id, sf::OutCopyHandle out_event, const sf::InNonSecureAutoSelectBuffer &buffer, s32 flags); + Result SendResults(sf::Out out_err, sf::Out out_size, u32 task_id); + + Result StartSend(sf::Out out_task_id, sf::OutCopyHandle out_event, sf::Out out_max_size, s64 size, s32 flags); + Result ContinueSend(sf::Out out_size, sf::Out out_wait, const sf::InNonSecureAutoSelectBuffer &buffer, u32 task_id); + Result EndSend(sf::Out out_err, sf::Out out_size, u32 task_id); + + Result StartRecv(sf::Out out_task_id, sf::OutCopyHandle out_event, s64 size, s32 flags); + Result EndRecv(sf::Out out_err, sf::Out out_size, const sf::OutAutoSelectBuffer &buffer, u32 task_id); + + Result GetPrimitive(sf::Out out); + }; + static_assert(tma::IsISocket); + + class RemoteManager { + public: + Result Socket(sf::Out out_err, sf::Out out_sock) { AMS_ABORT("Not Implemented"); } + Result Close(sf::Out out_err, sf::Out out_res, s32 desc) { AMS_ABORT("Not Implemented"); } + Result Connect(sf::Out out_err, sf::Out out_res, s32 desc, const htcs::SockAddrHtcs &address) { AMS_ABORT("Not Implemented"); } + Result Bind(sf::Out out_err, sf::Out out_res, s32 desc, const htcs::SockAddrHtcs &address) { AMS_ABORT("Not Implemented"); } + Result Listen(sf::Out out_err, sf::Out out_res, s32 desc, s32 backlog_count) { AMS_ABORT("Not Implemented"); } + Result Accept(sf::Out out_err, sf::Out out_res, sf::Out out_address, s32 desc) { AMS_ABORT("Not Implemented"); } + Result Recv(sf::Out out_err, sf::Out out_size, const sf::OutBuffer &buffer, s32 desc, s32 flags) { AMS_ABORT("Not Implemented"); } + Result Send(sf::Out out_err, sf::Out out_size, s32 desc, const sf::InBuffer &buffer, s32 flags) { AMS_ABORT("Not Implemented"); } + Result Shutdown(sf::Out out_err, sf::Out out_res, s32 desc, s32 how) { AMS_ABORT("Not Implemented"); } + Result Fcntl(sf::Out out_err, sf::Out out_res, s32 desc, s32 command, s32 value) { AMS_ABORT("Not Implemented"); } + Result CreateSocketOld(sf::Out out_err, sf::Out> out) { AMS_ABORT("Not Implemented"); } + + Result GetPeerNameAny(sf::Out out); + Result GetDefaultHostName(sf::Out out); + Result CreateSocket(sf::Out out_err, sf::Out> out, bool enable_disconnection_emulation); + Result RegisterProcessId(const sf::ClientProcessId &client_pid); + Result MonitorManager(const sf::ClientProcessId &client_pid); + Result StartSelect(sf::Out out_task_id, sf::OutCopyHandle out_event, const sf::InMapAliasArray &read_handles, const sf::InMapAliasArray &write_handles, const sf::InMapAliasArray &exception_handles, s64 tv_sec, s64 tv_usec); + Result EndSelect(sf::Out out_err, sf::Out out_count, const sf::OutMapAliasArray &read_handles, const sf::OutMapAliasArray &write_handles, const sf::OutMapAliasArray &exception_handles, u32 task_id); + }; + static_assert(tma::IsIHtcsManager); + + Result RemoteManager::GetPeerNameAny(sf::Out out) { + static_assert(sizeof(htcs::HtcsPeerName) == sizeof(::HtcsPeerName)); + return ::htcsGetPeerNameAny(reinterpret_cast<::HtcsPeerName *>(out.GetPointer())); + } + + Result RemoteManager::GetDefaultHostName(sf::Out out) { + static_assert(sizeof(htcs::HtcsPeerName) == sizeof(::HtcsPeerName)); + return ::htcsGetDefaultHostName(reinterpret_cast<::HtcsPeerName *>(out.GetPointer())); + } + + Result RemoteManager::CreateSocket(sf::Out out_err, sf::Out> out, bool enable_disconnection_emulation) { + ::HtcsSocket libnx_socket; + R_TRY(::htcsCreateSocket(out_err.GetPointer(), std::addressof(libnx_socket), enable_disconnection_emulation)); + + *out = ObjectFactory::CreateSharedEmplaced(libnx_socket); + return ResultSuccess(); + } + + Result RemoteManager::RegisterProcessId(const sf::ClientProcessId &client_pid) { + /* Handled by libnx init. */ + return ResultSuccess(); + } + + Result RemoteManager::MonitorManager(const sf::ClientProcessId &client_pid) { + /* Handled by libnx init. */ + return ResultSuccess(); + } + + Result RemoteManager::StartSelect(sf::Out out_task_id, sf::OutCopyHandle out_event, const sf::InMapAliasArray &read_handles, const sf::InMapAliasArray &write_handles, const sf::InMapAliasArray &exception_handles, s64 tv_sec, s64 tv_usec) { + return ::htcsStartSelect(out_task_id.GetPointer(), out_event.GetHandlePointer(), read_handles.GetPointer(), read_handles.GetSize(), write_handles.GetPointer(), write_handles.GetSize(), exception_handles.GetPointer(), exception_handles.GetSize(), tv_sec, tv_usec); + } + + Result RemoteManager::EndSelect(sf::Out out_err, sf::Out out_count, const sf::OutMapAliasArray &read_handles, const sf::OutMapAliasArray &write_handles, const sf::OutMapAliasArray &exception_handles, u32 task_id) { + return ::htcsEndSelect(out_err.GetPointer(), out_count.GetPointer(), read_handles.GetPointer(), read_handles.GetSize(), write_handles.GetPointer(), write_handles.GetSize(), exception_handles.GetPointer(), exception_handles.GetSize(), task_id); + } + + Result RemoteSocket::Close(sf::Out out_err, sf::Out out_res) { + return ::htcsSocketClose(std::addressof(m_s), out_err.GetPointer(), out_res.GetPointer()); + } + + Result RemoteSocket::Connect(sf::Out out_err, sf::Out out_res, const htcs::SockAddrHtcs &address) { + static_assert(sizeof(htcs::SockAddrHtcs) == sizeof(::SockAddrHtcs)); + return ::htcsSocketConnect(std::addressof(m_s), out_err.GetPointer(), out_res.GetPointer(), reinterpret_cast(std::addressof(address))); + } + + Result RemoteSocket::Bind(sf::Out out_err, sf::Out out_res, const htcs::SockAddrHtcs &address) { + static_assert(sizeof(htcs::SockAddrHtcs) == sizeof(::SockAddrHtcs)); + return ::htcsSocketBind(std::addressof(m_s), out_err.GetPointer(), out_res.GetPointer(), reinterpret_cast(std::addressof(address))); + } + + Result RemoteSocket::Listen(sf::Out out_err, sf::Out out_res, s32 backlog_count) { + return ::htcsSocketListen(std::addressof(m_s), out_err.GetPointer(), out_res.GetPointer(), backlog_count); + } + + Result RemoteSocket::Shutdown(sf::Out out_err, sf::Out out_res, s32 how) { + return ::htcsSocketShutdown(std::addressof(m_s), out_err.GetPointer(), out_res.GetPointer(), how); + } + + Result RemoteSocket::Fcntl(sf::Out out_err, sf::Out out_res, s32 command, s32 value) { + 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()); + } + + Result RemoteSocket::AcceptResults(sf::Out out_err, sf::Out> out, sf::Out out_address, u32 task_id) { + static_assert(sizeof(htcs::SockAddrHtcs) == sizeof(::SockAddrHtcs)); + ::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)); + + *out = ObjectFactory::CreateSharedEmplaced(libnx_socket); + return ResultSuccess(); + } + + Result RemoteSocket::RecvStart(sf::Out out_task_id, sf::OutCopyHandle out_event, s32 mem_size, s32 flags) { + return ::htcsSocketRecvStart(std::addressof(m_s), out_task_id.GetPointer(), out_event.GetHandlePointer(), mem_size, flags); + } + + Result RemoteSocket::RecvResults(sf::Out out_err, sf::Out out_size, const sf::OutAutoSelectBuffer &buffer, u32 task_id) { + return ::htcsSocketRecvResults(std::addressof(m_s), out_err.GetPointer(), out_size.GetPointer(), buffer.GetPointer(), buffer.GetSize(), task_id); + } + + Result RemoteSocket::SendStart(sf::Out out_task_id, sf::OutCopyHandle out_event, const sf::InNonSecureAutoSelectBuffer &buffer, s32 flags) { + return ::htcsSocketSendStart(std::addressof(m_s), out_task_id.GetPointer(), out_event.GetHandlePointer(), buffer.GetPointer(), buffer.GetSize(), flags); + } + + Result RemoteSocket::SendResults(sf::Out out_err, sf::Out out_size, u32 task_id) { + return ::htcsSocketSendResults(std::addressof(m_s), out_err.GetPointer(), out_size.GetPointer(), task_id); + } + + Result RemoteSocket::StartSend(sf::Out out_task_id, sf::OutCopyHandle out_event, sf::Out out_max_size, s64 size, s32 flags) { + return ::htcsSocketStartSend(std::addressof(m_s), out_task_id.GetPointer(), out_event.GetHandlePointer(), out_max_size.GetPointer(), size, flags); + } + + Result RemoteSocket::ContinueSend(sf::Out out_size, sf::Out out_wait, const sf::InNonSecureAutoSelectBuffer &buffer, u32 task_id) { + return ::htcsSocketContinueSend(std::addressof(m_s), out_size.GetPointer(), out_wait.GetPointer(), buffer.GetPointer(), buffer.GetSize(), task_id); + } + + Result RemoteSocket::EndSend(sf::Out out_err, sf::Out out_size, u32 task_id) { + return ::htcsSocketEndSend(std::addressof(m_s), out_err.GetPointer(), out_size.GetPointer(), task_id); + } + + Result RemoteSocket::StartRecv(sf::Out out_task_id, sf::OutCopyHandle out_event, s64 size, s32 flags) { + return ::htcsSocketStartRecv(std::addressof(m_s), out_task_id.GetPointer(), out_event.GetHandlePointer(), size, flags); + } + + Result RemoteSocket::EndRecv(sf::Out out_err, sf::Out out_size, const sf::OutAutoSelectBuffer &buffer, u32 task_id) { + return ::htcsSocketEndRecv(std::addressof(m_s), out_err.GetPointer(), out_size.GetPointer(), buffer.GetPointer(), buffer.GetSize(), task_id); + } + + Result RemoteSocket::GetPrimitive(sf::Out out) { + return ::htcsSocketGetPrimitive(std::addressof(m_s), out.GetPointer()); + } + + } + + void InitializeSessionManager(tma::IHtcsManager **out_manager, tma::IHtcsManager **out_monitor, u32 num_sessions) { + /* Initialize the libnx wrapper. */ + sm::DoWithSession([&] { + R_ABORT_UNLESS(::htcsInitialize(num_sessions)); + }); + + /* Create the output objects. */ + *out_manager = ObjectFactory::CreateSharedEmplaced().Detach(); + + /* Create the output objects. */ + *out_monitor = ObjectFactory::CreateSharedEmplaced().Detach(); + } + + void FinalizeSessionManager() { + /* Exit the libnx wrapper. */ + ::htcsExit(); + } + +} diff --git a/libraries/libstratosphere/source/htcs/client/htcs_session.hpp b/libraries/libstratosphere/source/htcs/client/htcs_session.hpp index bbd088e06..178a5a3fe 100644 --- a/libraries/libstratosphere/source/htcs/client/htcs_session.hpp +++ b/libraries/libstratosphere/source/htcs/client/htcs_session.hpp @@ -18,7 +18,7 @@ namespace ams::htcs::client { - void InitializeSessionManager(tma::IHtcsManager **out_manager, tma::IHtcsManager **out_monitor); + void InitializeSessionManager(tma::IHtcsManager **out_manager, tma::IHtcsManager **out_monitor, u32 num_sessions); void FinalizeSessionManager(); } diff --git a/libraries/libstratosphere/source/htcs/htcs_socket.cpp b/libraries/libstratosphere/source/htcs/htcs_socket.cpp index 3178e99a1..941347bb4 100644 --- a/libraries/libstratosphere/source/htcs/htcs_socket.cpp +++ b/libraries/libstratosphere/source/htcs/htcs_socket.cpp @@ -46,7 +46,7 @@ namespace ams::htcs { AMS_ASSERT(0 < num_sessions && num_sessions <= SessionCountMax); /* Initialize the manager and monitor. */ - client::InitializeSessionManager(std::addressof(g_manager), std::addressof(g_monitor)); + client::InitializeSessionManager(std::addressof(g_manager), std::addressof(g_monitor), num_sessions); /* Register the process. */ const sf::ClientProcessId process_id{0};