Revert "hoc-clk: add live vdd2, live boost clock and basic pwm dimming"
This reverts commit 15b7df8ef1.
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
namespace ams::htcs::client {
|
||||
|
||||
void InitializeSessionManager(tma::IHtcsManager **out_manager, tma::IHtcsManager **out_monitor, u32 num_sessions);
|
||||
void FinalizeSessionManager();
|
||||
|
||||
}
|
||||
@@ -1,276 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_session.hpp"
|
||||
|
||||
extern "C" {
|
||||
|
||||
#include <switch/services/htcs.h>
|
||||
|
||||
}
|
||||
|
||||
namespace ams::htcs::client {
|
||||
|
||||
namespace {
|
||||
|
||||
struct HtcsObjectAllocatorTag;
|
||||
using ObjectAllocator = ams::sf::ExpHeapStaticAllocator<16_KB, HtcsObjectAllocatorTag>;
|
||||
using ObjectFactory = ams::sf::ObjectFactory<typename ObjectAllocator::Policy>;
|
||||
|
||||
class StaticAllocatorInitializer {
|
||||
public:
|
||||
StaticAllocatorInitializer() {
|
||||
ObjectAllocator::Initialize(lmem::CreateOption_ThreadSafe);
|
||||
}
|
||||
} g_static_allocator_initializer;
|
||||
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
class RemoteSocket {
|
||||
private:
|
||||
::HtcsSocket m_s;
|
||||
public:
|
||||
RemoteSocket(::HtcsSocket &s) : m_s(s) { /* ... */ }
|
||||
~RemoteSocket() { ::htcsCloseSocket(std::addressof(m_s)); }
|
||||
public:
|
||||
Result Accept(sf::Out<s32> out_err, sf::Out<sf::SharedPointer<tma::ISocket>> out, sf::Out<htcs::SockAddrHtcs> out_address) { AMS_UNUSED(out_err, out, out_address); AMS_ABORT("Not Implemented"); }
|
||||
Result Recv(sf::Out<s32> out_err, sf::Out<s64> out_size, const sf::OutAutoSelectBuffer &buffer, s32 flags) { AMS_UNUSED(out_err, out_size, buffer, flags); AMS_ABORT("Not Implemented"); }
|
||||
Result Send(sf::Out<s32> out_err, sf::Out<s64> out_size, const sf::InAutoSelectBuffer &buffer, s32 flags) { AMS_UNUSED(out_err, out_size, buffer, flags); AMS_ABORT("Not Implemented"); }
|
||||
Result RecvLargeStart(sf::Out<u32> 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_UNUSED(out_task_id, out_event, unaligned_size_start, unaligned_size_end, aligned_size, mem_handle, flags); AMS_ABORT("Not Implemented"); }
|
||||
Result SendStartOld(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, const sf::InAutoSelectBuffer &buffer, s32 flags) { AMS_UNUSED(out_task_id, out_event, buffer, flags); AMS_ABORT("Not Implemented"); }
|
||||
Result SendLargeStart(sf::Out<u32> 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_UNUSED(out_task_id, out_event, start_buffer, end_buffer, mem_handle, aligned_size, flags); AMS_ABORT("Not Implemented"); }
|
||||
Result ContinueSendOld(sf::Out<s64> out_size, sf::Out<bool> out_wait, const sf::InAutoSelectBuffer &buffer, u32 task_id) { AMS_UNUSED(out_size, out_wait, buffer, task_id); AMS_ABORT("Not Implemented"); }
|
||||
|
||||
Result Close(sf::Out<s32> out_err, sf::Out<s32> out_res);
|
||||
Result Connect(sf::Out<s32> out_err, sf::Out<s32> out_res, const htcs::SockAddrHtcs &address);
|
||||
Result Bind(sf::Out<s32> out_err, sf::Out<s32> out_res, const htcs::SockAddrHtcs &address);
|
||||
Result Listen(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 backlog_count);
|
||||
Result Shutdown(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 how);
|
||||
Result Fcntl(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 command, s32 value);
|
||||
|
||||
Result AcceptStart(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event);
|
||||
Result AcceptResults(sf::Out<s32> out_err, sf::Out<sf::SharedPointer<tma::ISocket>> out, sf::Out<htcs::SockAddrHtcs> out_address, u32 task_id);
|
||||
|
||||
Result RecvStart(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, s32 mem_size, s32 flags);
|
||||
Result RecvResults(sf::Out<s32> out_err, sf::Out<s64> out_size, const sf::OutAutoSelectBuffer &buffer, u32 task_id);
|
||||
|
||||
Result SendStart(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, const sf::InNonSecureAutoSelectBuffer &buffer, s32 flags);
|
||||
Result SendResults(sf::Out<s32> out_err, sf::Out<s64> out_size, u32 task_id);
|
||||
|
||||
Result StartSend(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, sf::Out<s64> out_max_size, s64 size, s32 flags);
|
||||
Result ContinueSend(sf::Out<s64> out_size, sf::Out<bool> out_wait, const sf::InNonSecureAutoSelectBuffer &buffer, u32 task_id);
|
||||
Result EndSend(sf::Out<s32> out_err, sf::Out<s64> out_size, u32 task_id);
|
||||
|
||||
Result StartRecv(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, s64 size, s32 flags);
|
||||
Result EndRecv(sf::Out<s32> out_err, sf::Out<s64> out_size, const sf::OutAutoSelectBuffer &buffer, u32 task_id);
|
||||
|
||||
Result GetPrimitive(sf::Out<s32> out);
|
||||
};
|
||||
static_assert(tma::IsISocket<RemoteSocket>);
|
||||
|
||||
class RemoteManager {
|
||||
public:
|
||||
Result Socket(sf::Out<s32> out_err, sf::Out<s32> out_sock) { AMS_UNUSED(out_err, out_sock); AMS_ABORT("Not Implemented"); }
|
||||
Result Close(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 desc) { AMS_UNUSED(out_err, out_res, desc); AMS_ABORT("Not Implemented"); }
|
||||
Result Connect(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 desc, const htcs::SockAddrHtcs &address) { AMS_UNUSED(out_err, out_res, desc, address); AMS_ABORT("Not Implemented"); }
|
||||
Result Bind(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 desc, const htcs::SockAddrHtcs &address) { AMS_UNUSED(out_err, out_res, desc, address); AMS_ABORT("Not Implemented"); }
|
||||
Result Listen(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 desc, s32 backlog_count) { AMS_UNUSED(out_err, out_res, desc, backlog_count); AMS_ABORT("Not Implemented"); }
|
||||
Result Accept(sf::Out<s32> out_err, sf::Out<s32> out_res, sf::Out<htcs::SockAddrHtcs> out_address, s32 desc) { AMS_UNUSED(out_err, out_res, out_address, desc); AMS_ABORT("Not Implemented"); }
|
||||
Result Recv(sf::Out<s32> out_err, sf::Out<s64> out_size, const sf::OutBuffer &buffer, s32 desc, s32 flags) { AMS_UNUSED(out_err, out_size, buffer, desc, flags); AMS_ABORT("Not Implemented"); }
|
||||
Result Send(sf::Out<s32> out_err, sf::Out<s64> out_size, s32 desc, const sf::InBuffer &buffer, s32 flags) { AMS_UNUSED(out_err, out_size, desc, buffer, flags); AMS_ABORT("Not Implemented"); }
|
||||
Result Shutdown(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 desc, s32 how) { AMS_UNUSED(out_err, out_res, desc, how); AMS_ABORT("Not Implemented"); }
|
||||
Result Fcntl(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 desc, s32 command, s32 value) { AMS_UNUSED(out_err, out_res, desc, command, value); AMS_ABORT("Not Implemented"); }
|
||||
Result CreateSocketOld(sf::Out<s32> out_err, sf::Out<sf::SharedPointer<tma::ISocket>> out) { AMS_UNUSED(out_err, out); AMS_ABORT("Not Implemented"); }
|
||||
|
||||
Result GetPeerNameAny(sf::Out<htcs::HtcsPeerName> out);
|
||||
Result GetDefaultHostName(sf::Out<htcs::HtcsPeerName> out);
|
||||
Result CreateSocket(sf::Out<s32> out_err, sf::Out<sf::SharedPointer<tma::ISocket>> out, bool enable_disconnection_emulation);
|
||||
Result RegisterProcessId(const sf::ClientProcessId &client_pid);
|
||||
Result MonitorManager(const sf::ClientProcessId &client_pid);
|
||||
Result StartSelect(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, const sf::InMapAliasArray<s32> &read_handles, const sf::InMapAliasArray<s32> &write_handles, const sf::InMapAliasArray<s32> &exception_handles, s64 tv_sec, s64 tv_usec);
|
||||
Result EndSelect(sf::Out<s32> out_err, sf::Out<s32> out_count, const sf::OutMapAliasArray<s32> &read_handles, const sf::OutMapAliasArray<s32> &write_handles, const sf::OutMapAliasArray<s32> &exception_handles, u32 task_id);
|
||||
};
|
||||
static_assert(tma::IsIHtcsManager<RemoteManager>);
|
||||
|
||||
Result RemoteManager::GetPeerNameAny(sf::Out<htcs::HtcsPeerName> out) {
|
||||
static_assert(sizeof(htcs::HtcsPeerName) == sizeof(::HtcsPeerName));
|
||||
R_RETURN(::htcsGetPeerNameAny(reinterpret_cast<::HtcsPeerName *>(out.GetPointer())));
|
||||
}
|
||||
|
||||
Result RemoteManager::GetDefaultHostName(sf::Out<htcs::HtcsPeerName> out) {
|
||||
static_assert(sizeof(htcs::HtcsPeerName) == sizeof(::HtcsPeerName));
|
||||
R_RETURN(::htcsGetDefaultHostName(reinterpret_cast<::HtcsPeerName *>(out.GetPointer())));
|
||||
}
|
||||
|
||||
Result RemoteManager::CreateSocket(sf::Out<s32> out_err, sf::Out<sf::SharedPointer<tma::ISocket>> out, bool enable_disconnection_emulation) {
|
||||
::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<tma::ISocket, RemoteSocket>(libnx_socket);
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result RemoteManager::RegisterProcessId(const sf::ClientProcessId &client_pid) {
|
||||
/* Handled by libnx init. */
|
||||
AMS_UNUSED(client_pid);
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result RemoteManager::MonitorManager(const sf::ClientProcessId &client_pid) {
|
||||
/* Handled by libnx init. */
|
||||
AMS_UNUSED(client_pid);
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result RemoteManager::StartSelect(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, const sf::InMapAliasArray<s32> &read_handles, const sf::InMapAliasArray<s32> &write_handles, const sf::InMapAliasArray<s32> &exception_handles, s64 tv_sec, s64 tv_usec) {
|
||||
os::NativeHandle event_handle;
|
||||
R_TRY(::htcsStartSelect(out_task_id.GetPointer(), std::addressof(event_handle), read_handles.GetPointer(), read_handles.GetSize(), write_handles.GetPointer(), write_handles.GetSize(), exception_handles.GetPointer(), exception_handles.GetSize(), tv_sec, tv_usec));
|
||||
|
||||
out_event.SetValue(event_handle, true);
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result RemoteManager::EndSelect(sf::Out<s32> out_err, sf::Out<s32> out_count, const sf::OutMapAliasArray<s32> &read_handles, const sf::OutMapAliasArray<s32> &write_handles, const sf::OutMapAliasArray<s32> &exception_handles, u32 task_id) {
|
||||
R_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<s32> out_err, sf::Out<s32> out_res) {
|
||||
R_RETURN(::htcsSocketClose(std::addressof(m_s), out_err.GetPointer(), out_res.GetPointer()));
|
||||
}
|
||||
|
||||
Result RemoteSocket::Connect(sf::Out<s32> out_err, sf::Out<s32> out_res, const htcs::SockAddrHtcs &address) {
|
||||
static_assert(sizeof(htcs::SockAddrHtcs) == sizeof(::HtcsSockAddr));
|
||||
R_RETURN(::htcsSocketConnect(std::addressof(m_s), out_err.GetPointer(), out_res.GetPointer(), reinterpret_cast<const ::HtcsSockAddr *>(std::addressof(address))));
|
||||
}
|
||||
|
||||
Result RemoteSocket::Bind(sf::Out<s32> out_err, sf::Out<s32> out_res, const htcs::SockAddrHtcs &address) {
|
||||
static_assert(sizeof(htcs::SockAddrHtcs) == sizeof(::HtcsSockAddr));
|
||||
R_RETURN(::htcsSocketBind(std::addressof(m_s), out_err.GetPointer(), out_res.GetPointer(), reinterpret_cast<const ::HtcsSockAddr *>(std::addressof(address))));
|
||||
}
|
||||
|
||||
Result RemoteSocket::Listen(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 backlog_count) {
|
||||
R_RETURN(::htcsSocketListen(std::addressof(m_s), out_err.GetPointer(), out_res.GetPointer(), backlog_count));
|
||||
}
|
||||
|
||||
Result RemoteSocket::Shutdown(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 how) {
|
||||
R_RETURN(::htcsSocketShutdown(std::addressof(m_s), out_err.GetPointer(), out_res.GetPointer(), how));
|
||||
}
|
||||
|
||||
Result RemoteSocket::Fcntl(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 command, s32 value) {
|
||||
R_RETURN(::htcsSocketFcntl(std::addressof(m_s), out_err.GetPointer(), out_res.GetPointer(), command, value));
|
||||
}
|
||||
|
||||
Result RemoteSocket::AcceptStart(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event) {
|
||||
os::NativeHandle event_handle;
|
||||
R_TRY(::htcsSocketAcceptStart(std::addressof(m_s), out_task_id.GetPointer(), std::addressof(event_handle)));
|
||||
|
||||
out_event.SetValue(event_handle, true);
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result RemoteSocket::AcceptResults(sf::Out<s32> out_err, sf::Out<sf::SharedPointer<tma::ISocket>> out, sf::Out<htcs::SockAddrHtcs> out_address, u32 task_id) {
|
||||
static_assert(sizeof(htcs::SockAddrHtcs) == sizeof(::HtcsSockAddr));
|
||||
::HtcsSocket libnx_socket;
|
||||
R_TRY(::htcsSocketAcceptResults(std::addressof(m_s), out_err.GetPointer(), std::addressof(libnx_socket), reinterpret_cast<::HtcsSockAddr *>(out_address.GetPointer()), task_id));
|
||||
|
||||
R_SUCCEED_IF(*out_err != 0);
|
||||
|
||||
*out = ObjectFactory::CreateSharedEmplaced<tma::ISocket, RemoteSocket>(libnx_socket);
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result RemoteSocket::RecvStart(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, s32 mem_size, s32 flags) {
|
||||
os::NativeHandle event_handle;
|
||||
R_TRY(::htcsSocketRecvStart(std::addressof(m_s), out_task_id.GetPointer(), std::addressof(event_handle), mem_size, flags));
|
||||
|
||||
out_event.SetValue(event_handle, true);
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result RemoteSocket::RecvResults(sf::Out<s32> out_err, sf::Out<s64> out_size, const sf::OutAutoSelectBuffer &buffer, u32 task_id) {
|
||||
R_RETURN(::htcsSocketRecvResults(std::addressof(m_s), out_err.GetPointer(), out_size.GetPointer(), buffer.GetPointer(), buffer.GetSize(), task_id));
|
||||
}
|
||||
|
||||
Result RemoteSocket::SendStart(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, const sf::InNonSecureAutoSelectBuffer &buffer, s32 flags) {
|
||||
os::NativeHandle event_handle;
|
||||
R_TRY(::htcsSocketSendStart(std::addressof(m_s), out_task_id.GetPointer(), std::addressof(event_handle), buffer.GetPointer(), buffer.GetSize(), flags));
|
||||
|
||||
out_event.SetValue(event_handle, true);
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result RemoteSocket::SendResults(sf::Out<s32> out_err, sf::Out<s64> out_size, u32 task_id) {
|
||||
R_RETURN(::htcsSocketSendResults(std::addressof(m_s), out_err.GetPointer(), out_size.GetPointer(), task_id));
|
||||
}
|
||||
|
||||
Result RemoteSocket::StartSend(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, sf::Out<s64> out_max_size, s64 size, s32 flags) {
|
||||
os::NativeHandle event_handle;
|
||||
R_TRY(::htcsSocketStartSend(std::addressof(m_s), out_task_id.GetPointer(), std::addressof(event_handle), out_max_size.GetPointer(), size, flags));
|
||||
|
||||
out_event.SetValue(event_handle, true);
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result RemoteSocket::ContinueSend(sf::Out<s64> out_size, sf::Out<bool> out_wait, const sf::InNonSecureAutoSelectBuffer &buffer, u32 task_id) {
|
||||
R_RETURN(::htcsSocketContinueSend(std::addressof(m_s), out_size.GetPointer(), out_wait.GetPointer(), buffer.GetPointer(), buffer.GetSize(), task_id));
|
||||
}
|
||||
|
||||
Result RemoteSocket::EndSend(sf::Out<s32> out_err, sf::Out<s64> out_size, u32 task_id) {
|
||||
R_RETURN(::htcsSocketEndSend(std::addressof(m_s), out_err.GetPointer(), out_size.GetPointer(), task_id));
|
||||
}
|
||||
|
||||
Result RemoteSocket::StartRecv(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, s64 size, s32 flags) {
|
||||
os::NativeHandle event_handle;
|
||||
R_TRY(::htcsSocketStartRecv(std::addressof(m_s), out_task_id.GetPointer(), std::addressof(event_handle), size, flags));
|
||||
|
||||
out_event.SetValue(event_handle, true);
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result RemoteSocket::EndRecv(sf::Out<s32> out_err, sf::Out<s64> out_size, const sf::OutAutoSelectBuffer &buffer, u32 task_id) {
|
||||
R_RETURN(::htcsSocketEndRecv(std::addressof(m_s), out_err.GetPointer(), out_size.GetPointer(), buffer.GetPointer(), buffer.GetSize(), task_id));
|
||||
}
|
||||
|
||||
Result RemoteSocket::GetPrimitive(sf::Out<s32> out) {
|
||||
R_RETURN(::htcsSocketGetPrimitive(std::addressof(m_s), out.GetPointer()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void InitializeSessionManager(tma::IHtcsManager **out_manager, tma::IHtcsManager **out_monitor, u32 num_sessions) {
|
||||
/* Ensure we can contact the libnx wrapper. */
|
||||
R_ABORT_UNLESS(sm::Initialize());
|
||||
|
||||
/* Initialize the libnx wrapper. */
|
||||
R_ABORT_UNLESS(::htcsInitialize(num_sessions));
|
||||
|
||||
/* Create the output objects. */
|
||||
*out_manager = ObjectFactory::CreateSharedEmplaced<tma::IHtcsManager, RemoteManager>().Detach();
|
||||
|
||||
/* Create the output objects. */
|
||||
*out_monitor = ObjectFactory::CreateSharedEmplaced<tma::IHtcsManager, RemoteManager>().Detach();
|
||||
}
|
||||
|
||||
void FinalizeSessionManager() {
|
||||
/* Exit the libnx wrapper. */
|
||||
::htcsExit();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_session.hpp"
|
||||
|
||||
namespace ams::htcs::client {
|
||||
|
||||
void InitializeSessionManager(tma::IHtcsManager **out_manager, tma::IHtcsManager **out_monitor, u32 num_sessions) {
|
||||
AMS_UNUSED(out_manager, out_monitor, num_sessions);
|
||||
AMS_ABORT("TODO");
|
||||
}
|
||||
|
||||
void FinalizeSessionManager() {
|
||||
AMS_ABORT("TODO");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,834 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_session.hpp"
|
||||
#include "htcs_virtual_socket_collection.hpp"
|
||||
|
||||
namespace ams::htcs::client {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr inline s32 InvalidSocket = -1;
|
||||
constexpr inline s32 InvalidPrimitive = -1;
|
||||
|
||||
}
|
||||
|
||||
/* Declare client functions. */
|
||||
sf::SharedPointer<tma::ISocket> socket(s32 &last_error);
|
||||
s32 close(sf::SharedPointer<tma::ISocket> socket, s32 &last_error);
|
||||
s32 bind(sf::SharedPointer<tma::ISocket> socket, const htcs::SockAddrHtcs *address, s32 &last_error);
|
||||
s32 listen(sf::SharedPointer<tma::ISocket> socket, s32 backlog_count, s32 &last_error);
|
||||
sf::SharedPointer<tma::ISocket> accept(sf::SharedPointer<tma::ISocket> socket, htcs::SockAddrHtcs *address, s32 &last_error);
|
||||
s32 fcntl(sf::SharedPointer<tma::ISocket> socket, s32 command, s32 value, s32 &last_error);
|
||||
|
||||
s32 shutdown(sf::SharedPointer<tma::ISocket> socket, s32 how, s32 &last_error);
|
||||
|
||||
ssize_t recv(sf::SharedPointer<tma::ISocket> socket, void *buffer, size_t buffer_size, s32 flags, s32 &last_error);
|
||||
ssize_t send(sf::SharedPointer<tma::ISocket> socket, const void *buffer, size_t buffer_size, s32 flags, s32 &last_error);
|
||||
|
||||
s32 connect(sf::SharedPointer<tma::ISocket> socket, const htcs::SockAddrHtcs *address, s32 &last_error);
|
||||
|
||||
s32 select(s32 * const read, s32 &num_read, s32 * const write, s32 &num_write, s32 * const except, s32 &num_except, htcs::TimeVal *timeout, s32 &last_error);
|
||||
|
||||
struct VirtualSocket {
|
||||
s32 m_id;
|
||||
s32 m_primitive;
|
||||
sf::SharedPointer<tma::ISocket> m_socket;
|
||||
bool m_do_bind;
|
||||
htcs::SockAddrHtcs m_address;
|
||||
s32 m_listen_backlog_count;
|
||||
s32 m_fcntl_command;
|
||||
s32 m_fcntl_value;
|
||||
bool m_blocking;
|
||||
|
||||
|
||||
VirtualSocket() {
|
||||
/* Initialize. */
|
||||
this->Init();
|
||||
}
|
||||
|
||||
~VirtualSocket() { /* ... */ }
|
||||
|
||||
void Init() {
|
||||
/* Setup fields. */
|
||||
m_id = InvalidSocket;
|
||||
m_primitive = InvalidPrimitive;
|
||||
m_socket = nullptr;
|
||||
m_blocking = true;
|
||||
m_do_bind = false;
|
||||
|
||||
std::memset(std::addressof(m_address), 0, sizeof(m_address));
|
||||
|
||||
m_listen_backlog_count = -1;
|
||||
|
||||
m_fcntl_command = -1;
|
||||
m_fcntl_value = 0;
|
||||
}
|
||||
|
||||
s32 Bind(const htcs::SockAddrHtcs *address, s32 &last_error) {
|
||||
/* Mark the bind. */
|
||||
m_do_bind = true;
|
||||
|
||||
/* Set our address. */
|
||||
std::memcpy(std::addressof(m_address), address, sizeof(m_address));
|
||||
|
||||
/* Clear the error. */
|
||||
last_error = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
s32 Listen(s32 backlog_count, s32 &last_error) {
|
||||
s32 res = -1;
|
||||
if (m_do_bind) {
|
||||
/* Set backlog count. */
|
||||
m_listen_backlog_count = std::max(backlog_count, 1);
|
||||
|
||||
/* Clear error. */
|
||||
last_error = 0;
|
||||
res = 0;
|
||||
} else {
|
||||
last_error = HTCS_EINVAL;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
s32 Fcntl(s32 command, s32 value, s32 &last_error) {
|
||||
/* Clear error. */
|
||||
s32 res = 0;
|
||||
last_error = 0;
|
||||
|
||||
if (command == HTCS_F_SETFL) {
|
||||
m_fcntl_command = command;
|
||||
m_fcntl_value = value;
|
||||
|
||||
m_blocking = (value & HTCS_O_NONBLOCK) == 0;
|
||||
} else if (command == HTCS_F_GETFL) {
|
||||
res = m_fcntl_value;
|
||||
} else {
|
||||
last_error = HTCS_EINVAL;
|
||||
res = -1;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
s32 SetSocket(sf::SharedPointer<tma::ISocket> socket, s32 &last_error) {
|
||||
s32 res = 0;
|
||||
|
||||
if (m_socket == nullptr && socket != nullptr) {
|
||||
/* Set our socket. */
|
||||
m_socket = socket;
|
||||
|
||||
/* Bind, fcntl, and listen, since those may have been deferred. */
|
||||
if (m_do_bind) {
|
||||
res = bind(m_socket, std::addressof(m_address), last_error);
|
||||
}
|
||||
|
||||
if (res == 0 && m_fcntl_command != -1) {
|
||||
res = fcntl(m_socket, m_fcntl_command, m_fcntl_value, last_error);
|
||||
}
|
||||
|
||||
if (res == 0 && m_listen_backlog_count > 0) {
|
||||
res = listen(m_socket, m_listen_backlog_count, last_error);
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
VirtualSocketCollection::VirtualSocketCollection()
|
||||
: m_socket_list(nullptr),
|
||||
m_list_count(0),
|
||||
m_list_size(0),
|
||||
m_next_id(1),
|
||||
m_mutex()
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
VirtualSocketCollection::~VirtualSocketCollection() {
|
||||
/* Clear ourselves. */
|
||||
this->Clear();
|
||||
|
||||
/* Destroy all sockets in our list. */
|
||||
for (auto i = 0; i < m_list_size; ++i) {
|
||||
std::destroy_at(m_socket_list + i);
|
||||
}
|
||||
|
||||
/* Clear the backing memory for our socket list. */
|
||||
std::memset(m_buffer, 0, sizeof(VirtualSocket) * m_list_size);
|
||||
}
|
||||
|
||||
size_t VirtualSocketCollection::GetWorkingMemorySize(int num_sockets) {
|
||||
AMS_ASSERT(num_sockets < htcs::SocketCountMax);
|
||||
return num_sockets * sizeof(VirtualSocket);
|
||||
}
|
||||
|
||||
void VirtualSocketCollection::Init(void *buffer, size_t buffer_size) {
|
||||
/* Set our buffer. */
|
||||
m_buffer = buffer;
|
||||
m_buffer_size = buffer_size;
|
||||
|
||||
/* Configure our list. */
|
||||
m_list_size = static_cast<s32>(m_buffer_size / sizeof(VirtualSocket));
|
||||
m_list_size = std::min(m_list_size, htcs::SocketCountMax);
|
||||
|
||||
/* Initialize our list. */
|
||||
m_socket_list = static_cast<VirtualSocket *>(m_buffer);
|
||||
for (auto i = 0; i < m_list_size; ++i) {
|
||||
std::construct_at(m_socket_list + i);
|
||||
}
|
||||
}
|
||||
|
||||
void VirtualSocketCollection::Clear() {
|
||||
/* Lock ourselves. */
|
||||
std::scoped_lock lk(m_mutex);
|
||||
|
||||
/* Clear our list. */
|
||||
m_list_count = 0;
|
||||
}
|
||||
|
||||
s32 VirtualSocketCollection::Socket(s32 &error_code) {
|
||||
/* Clear error code. */
|
||||
error_code = 0;
|
||||
|
||||
/* Create the socket. */
|
||||
return this->CreateSocket(sf::SharedPointer<tma::ISocket>{nullptr}, error_code);
|
||||
}
|
||||
|
||||
s32 VirtualSocketCollection::Close(s32 id, s32 &error_code) {
|
||||
/* Clear error code. */
|
||||
error_code = 0;
|
||||
|
||||
/* Prepare to find the socket. */
|
||||
s32 res = 0;
|
||||
sf::SharedPointer<tma::ISocket> socket = nullptr;
|
||||
|
||||
/* Find the socket. */
|
||||
s32 index;
|
||||
{
|
||||
std::scoped_lock lk(m_mutex);
|
||||
|
||||
if (index = this->Find(id, std::addressof(error_code)); index >= 0) {
|
||||
/* Get the socket's object. */
|
||||
VirtualSocket *virt_socket = m_socket_list + index;
|
||||
socket = virt_socket->m_socket;
|
||||
|
||||
/* Move the list. */
|
||||
for (/* ... */; index < m_list_count - 1; ++index) {
|
||||
m_socket_list[index] = m_socket_list[index + 1];
|
||||
}
|
||||
|
||||
/* Clear the now unused last list entry. */
|
||||
m_socket_list[index].Init();
|
||||
|
||||
/* Decrement our list count. */
|
||||
--m_list_count;
|
||||
}
|
||||
}
|
||||
|
||||
/* If we found the socket, close it. */
|
||||
if (socket != nullptr) {
|
||||
close(socket, error_code);
|
||||
|
||||
/* Clear the error code. */
|
||||
res = 0;
|
||||
error_code = 0;
|
||||
}
|
||||
|
||||
return index >= 0 ? res : -1;
|
||||
}
|
||||
|
||||
s32 VirtualSocketCollection::Bind(s32 id, const htcs::SockAddrHtcs *address, s32 &error_code) {
|
||||
/* Setup result/error code. */
|
||||
s32 res = -1;
|
||||
error_code = 0;
|
||||
|
||||
/* Get the socket. */
|
||||
sf::SharedPointer<tma::ISocket> socket = this->GetSocket(id, std::addressof(error_code));
|
||||
|
||||
/* If we found the socket, bind. */
|
||||
if (socket != nullptr) {
|
||||
res = bind(socket, address, error_code);
|
||||
} else if (error_code != HTCS_EBADF) {
|
||||
/* Lock ourselves. */
|
||||
std::scoped_lock lk(m_mutex);
|
||||
|
||||
/* Check if the socket is already bound. */
|
||||
if (const auto index = this->Find(id); index >= 0) {
|
||||
const auto exists = this->HasAddr(address);
|
||||
|
||||
if (m_socket_list[index].m_do_bind) {
|
||||
error_code = HTCS_EINVAL;
|
||||
res = -1;
|
||||
} else if (exists) {
|
||||
error_code = HTCS_EADDRINUSE;
|
||||
res = -1;
|
||||
} else {
|
||||
res = m_socket_list[index].Bind(address, error_code);
|
||||
}
|
||||
} else {
|
||||
error_code = HTCS_EBADF;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
s32 VirtualSocketCollection::Listen(s32 id, s32 backlog_count, s32 &error_code) {
|
||||
/* Setup result/error code. */
|
||||
s32 res = -1;
|
||||
error_code = 0;
|
||||
|
||||
/* Get the socket. */
|
||||
sf::SharedPointer<tma::ISocket> socket = this->GetSocket(id, std::addressof(error_code));
|
||||
|
||||
/* If we found the socket, bind. */
|
||||
if (socket != nullptr) {
|
||||
res = listen(socket, backlog_count, error_code);
|
||||
} else if (error_code != HTCS_EBADF) {
|
||||
/* Lock ourselves. */
|
||||
std::scoped_lock lk(m_mutex);
|
||||
|
||||
/* Try to listen on the virtual socket. */
|
||||
if (const auto index = this->Find(id); index >= 0) {
|
||||
res = m_socket_list[index].Listen(backlog_count, error_code);
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
s32 VirtualSocketCollection::Accept(s32 id, htcs::SockAddrHtcs *address, s32 &error_code) {
|
||||
/* Setup result/error code. */
|
||||
s32 res = -1;
|
||||
error_code = 0;
|
||||
|
||||
/* Declare socket that we're creating. */
|
||||
sf::SharedPointer<tma::ISocket> new_socket = nullptr;
|
||||
|
||||
/* Get the socket. */
|
||||
sf::SharedPointer<tma::ISocket> socket = this->GetSocket(id, std::addressof(error_code));
|
||||
|
||||
/* If we found the socket, bind. */
|
||||
if (socket != nullptr) {
|
||||
if (error_code != HTCS_ENONE) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
new_socket = this->DoAccept(socket, id, address, error_code);
|
||||
} else if (error_code != HTCS_EBADF) {
|
||||
/* Fetch the socket. */
|
||||
socket = this->FetchSocket(id, error_code);
|
||||
|
||||
/* Wait for the socket. */
|
||||
while (socket == nullptr) {
|
||||
/* Determine whether we should block/listen. */
|
||||
bool block_until_done = true;
|
||||
bool listened = false;
|
||||
|
||||
s32 index;
|
||||
{
|
||||
std::scoped_lock lk(m_mutex);
|
||||
if (index = this->Find(id, std::addressof(error_code)); index >= 0) {
|
||||
block_until_done = m_socket_list[index].m_blocking;
|
||||
listened = m_socket_list[index].m_listen_backlog_count > 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Check that the socket exists. */
|
||||
if (index < 0) {
|
||||
error_code = HTCS_EINTR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Check that the socket has been listened. */
|
||||
if (!listened) {
|
||||
error_code = HTCS_EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Check that we should block. */
|
||||
if (!block_until_done) {
|
||||
error_code = HTCS_EWOULDBLOCK;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Wait before trying again. */
|
||||
os::SleepThread(TimeSpan::FromMilliSeconds(500));
|
||||
|
||||
/* Fetch the potentially updated socket. */
|
||||
socket = this->FetchSocket(id, error_code);
|
||||
}
|
||||
|
||||
/* Check that we haven't errored. */
|
||||
if (error_code != HTCS_ENONE) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Do the accept. */
|
||||
new_socket = this->DoAccept(socket, id, address, error_code);
|
||||
}
|
||||
|
||||
/* If we have a new socket, register it. */
|
||||
if (new_socket != 0) {
|
||||
res = this->CreateSocket(new_socket, error_code);
|
||||
if (res < 0) {
|
||||
s32 tmp_error_code;
|
||||
close(new_socket, tmp_error_code);
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
s32 VirtualSocketCollection::Fcntl(s32 id, s32 command, s32 value, s32 &error_code) {
|
||||
/* Setup result/error code. */
|
||||
s32 res = -1;
|
||||
error_code = 0;
|
||||
|
||||
/* Get the socket. */
|
||||
sf::SharedPointer<tma::ISocket> socket = this->GetSocket(id, std::addressof(error_code));
|
||||
|
||||
/* If we found the socket, bind. */
|
||||
if (socket != nullptr) {
|
||||
res = fcntl(socket, command, value, error_code);
|
||||
} else if (error_code != HTCS_EBADF) {
|
||||
/* Lock ourselves. */
|
||||
std::scoped_lock lk(m_mutex);
|
||||
|
||||
/* Try to listen on the virtual socket. */
|
||||
if (const auto index = this->Find(id); index >= 0) {
|
||||
res = m_socket_list[index].Fcntl(command, value, error_code);
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
s32 VirtualSocketCollection::Shutdown(s32 id, s32 how, s32 &error_code) {
|
||||
/* Setup result/error code. */
|
||||
s32 res = -1;
|
||||
error_code = 0;
|
||||
|
||||
/* Get the socket. */
|
||||
sf::SharedPointer<tma::ISocket> socket = this->GetSocket(id, std::addressof(error_code));
|
||||
|
||||
/* If we found the socket, bind. */
|
||||
if (socket != nullptr) {
|
||||
res = shutdown(socket, how, error_code);
|
||||
} else if (error_code != HTCS_EBADF) {
|
||||
error_code = HTCS_ENOTCONN;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
ssize_t VirtualSocketCollection::Recv(s32 id, void *buffer, size_t buffer_size, s32 flags, s32 &error_code) {
|
||||
/* Setup result/error code. */
|
||||
ssize_t res = -1;
|
||||
error_code = 0;
|
||||
|
||||
/* Fetch the socket. */
|
||||
sf::SharedPointer<tma::ISocket> socket = this->FetchSocket(id, error_code);
|
||||
|
||||
/* If we found the socket, bind. */
|
||||
if (socket != nullptr) {
|
||||
if (error_code != HTCS_ENONE) {
|
||||
return -1;
|
||||
}
|
||||
res = recv(socket, buffer, buffer_size, flags, error_code);
|
||||
} else if (error_code != HTCS_EBADF) {
|
||||
error_code = HTCS_ENOTCONN;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
ssize_t VirtualSocketCollection::Send(s32 id, const void *buffer, size_t buffer_size, s32 flags, s32 &error_code) {
|
||||
/* Setup result/error code. */
|
||||
ssize_t res = -1;
|
||||
error_code = 0;
|
||||
|
||||
/* Fetch the socket. */
|
||||
sf::SharedPointer<tma::ISocket> socket = this->FetchSocket(id, error_code);
|
||||
|
||||
/* If we found the socket, bind. */
|
||||
if (socket != nullptr) {
|
||||
if (error_code != HTCS_ENONE) {
|
||||
return -1;
|
||||
}
|
||||
res = send(socket, buffer, buffer_size, flags, error_code);
|
||||
} else if (error_code != HTCS_EBADF) {
|
||||
error_code = HTCS_ENOTCONN;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
s32 VirtualSocketCollection::Connect(s32 id, const htcs::SockAddrHtcs *address, s32 &error_code) {
|
||||
/* Setup result/error code. */
|
||||
s32 res = -1;
|
||||
error_code = 0;
|
||||
|
||||
/* Fetch the socket. */
|
||||
sf::SharedPointer<tma::ISocket> socket = this->FetchSocket(id, error_code);
|
||||
|
||||
/* If we found the socket, bind. */
|
||||
if (socket != nullptr) {
|
||||
if (error_code != HTCS_ENONE) {
|
||||
return -1;
|
||||
}
|
||||
res = connect(socket, address, error_code);
|
||||
} else if (error_code != HTCS_EBADF) {
|
||||
error_code = HTCS_EADDRNOTAVAIL;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
s32 VirtualSocketCollection::Select(htcs::FdSet *read, htcs::FdSet *write, htcs::FdSet *except, htcs::TimeVal *timeout, s32 &error_code) {
|
||||
/* Setup result/error code. */
|
||||
s32 res = -1;
|
||||
s32 tmp_error_code = 0;
|
||||
|
||||
/* Declare buffers. */
|
||||
s32 read_primitives[SocketCountMax];
|
||||
s32 write_primitives[SocketCountMax];
|
||||
s32 except_primitives[SocketCountMax];
|
||||
|
||||
/* Get reads. */
|
||||
s32 num_read = this->GetSockets(read_primitives, read, tmp_error_code);
|
||||
if (tmp_error_code != HTCS_ENONE) {
|
||||
error_code = tmp_error_code;
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Get writes. */
|
||||
s32 num_write = this->GetSockets(write_primitives, write, tmp_error_code);
|
||||
if (tmp_error_code != HTCS_ENONE) {
|
||||
error_code = tmp_error_code;
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Get excepts. */
|
||||
s32 num_except = this->GetSockets(except_primitives, except, tmp_error_code);
|
||||
if (tmp_error_code != HTCS_ENONE) {
|
||||
error_code = tmp_error_code;
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Perform the select. */
|
||||
if (num_read + num_write + num_except > 0) {
|
||||
res = select(read_primitives, num_read, write_primitives, num_write, except_primitives, num_except, timeout, error_code);
|
||||
|
||||
/* Set the socket primitives. */
|
||||
this->SetSockets(read, read_primitives, num_read);
|
||||
this->SetSockets(write, write_primitives, num_write);
|
||||
this->SetSockets(except, except_primitives, num_except);
|
||||
} else {
|
||||
error_code = HTCS_EINVAL;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
s32 VirtualSocketCollection::CreateId() {
|
||||
/* Lock ourselves. */
|
||||
std::scoped_lock lk(m_mutex);
|
||||
|
||||
/* Get a free id. */
|
||||
s32 res = 0;
|
||||
do {
|
||||
res = m_next_id++;
|
||||
if (m_next_id <= 0) {
|
||||
m_next_id = 1;
|
||||
}
|
||||
} while (this->Find(res) >= 0);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
s32 VirtualSocketCollection::Add(sf::SharedPointer<tma::ISocket> socket) {
|
||||
/* Check that the socket isn't null. */
|
||||
if (socket == nullptr) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Create the socket. */
|
||||
s32 error_code;
|
||||
return this->CreateSocket(socket, error_code);
|
||||
}
|
||||
|
||||
void VirtualSocketCollection::Insert(s32 id, sf::SharedPointer<tma::ISocket> socket) {
|
||||
/* Lock ourselves. */
|
||||
std::scoped_lock lk(m_mutex);
|
||||
|
||||
/* Add the socket to the list. */
|
||||
if (m_list_count != 0) {
|
||||
/* 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) {
|
||||
break;
|
||||
}
|
||||
m_socket_list[index + 1] = m_socket_list[index];
|
||||
}
|
||||
|
||||
/* Set the socket in the list. */
|
||||
m_socket_list[index + 1].m_id = id;
|
||||
m_socket_list[index + 1].m_socket = socket;
|
||||
} else {
|
||||
/* Set the socket in the list. */
|
||||
m_socket_list[0].m_id = id;
|
||||
m_socket_list[0].m_socket = socket;
|
||||
}
|
||||
|
||||
/* Increment our count. */
|
||||
++m_list_count;
|
||||
}
|
||||
|
||||
void VirtualSocketCollection::SetSize(s32 size) {
|
||||
AMS_UNUSED(size);
|
||||
}
|
||||
|
||||
s32 VirtualSocketCollection::Find(s32 id, s32 *error_code) {
|
||||
/* Perform a binary search to find the socket. */
|
||||
if (m_list_count > 0) {
|
||||
s32 left = 0;
|
||||
s32 right = m_list_count - 1;
|
||||
while (left <= right) {
|
||||
const s32 mid = (left + right) / 2;
|
||||
if (m_socket_list[mid].m_id == id) {
|
||||
return mid;
|
||||
} else if (m_socket_list[mid].m_id > id) {
|
||||
right = mid - 1;
|
||||
} else /* if (m_socket_list[mid].m_id < id) */ {
|
||||
left = mid + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* We failed to find the socket. */
|
||||
if (error_code != nullptr) {
|
||||
*error_code = HTCS_EBADF;
|
||||
}
|
||||
|
||||
return InvalidSocket;
|
||||
}
|
||||
|
||||
s32 VirtualSocketCollection::FindByPrimitive(s32 primitive) {
|
||||
/* Find a socket with the desired primitive. */
|
||||
for (auto i = 0; i < m_list_size; ++i) {
|
||||
if (m_socket_list[i].m_primitive == primitive) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return InvalidPrimitive;
|
||||
}
|
||||
|
||||
bool VirtualSocketCollection::HasAddr(const htcs::SockAddrHtcs *address) {
|
||||
/* Try to find a matching socket. */
|
||||
for (auto i = 0; i < m_list_count; ++i) {
|
||||
if (m_socket_list[i].m_address.family == address->family &&
|
||||
std::strcmp(m_socket_list[i].m_address.peer_name.name, address->peer_name.name) == 0 &&
|
||||
std::strcmp(m_socket_list[i].m_address.port_name.name, address->port_name.name) == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
sf::SharedPointer<tma::ISocket> VirtualSocketCollection::GetSocket(s32 id, s32 *error_code) {
|
||||
sf::SharedPointer<tma::ISocket> res = nullptr;
|
||||
|
||||
/* Get the socket. */
|
||||
{
|
||||
std::scoped_lock lk(m_mutex);
|
||||
|
||||
if (const auto index = this->Find(id, error_code); index >= 0) {
|
||||
res = m_socket_list[index].m_socket;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
sf::SharedPointer<tma::ISocket> VirtualSocketCollection::FetchSocket(s32 id, s32 &error_code) {
|
||||
/* Clear the error code. */
|
||||
error_code = 0;
|
||||
|
||||
/* Get the socket. */
|
||||
auto socket = this->GetSocket(id, std::addressof(error_code));
|
||||
if (socket == nullptr && error_code == HTCS_ENONE) {
|
||||
socket = this->RealizeSocket(id);
|
||||
}
|
||||
|
||||
return socket;
|
||||
}
|
||||
|
||||
sf::SharedPointer<tma::ISocket> VirtualSocketCollection::RealizeSocket(s32 id) {
|
||||
/* Clear the error code. */
|
||||
s32 error_code = 0;
|
||||
|
||||
/* Get socket. */
|
||||
sf::SharedPointer<tma::ISocket> res = socket(error_code);
|
||||
if (res != nullptr) {
|
||||
/* Assign the new socket. */
|
||||
s32 index;
|
||||
{
|
||||
std::scoped_lock lk(m_mutex);
|
||||
|
||||
index = this->Find(id, std::addressof(error_code));
|
||||
if (index >= 0) {
|
||||
m_socket_list[index].SetSocket(res, error_code);
|
||||
}
|
||||
}
|
||||
|
||||
/* If the socket was deleted, close it. */
|
||||
if (index < 0) {
|
||||
s32 temp_error = 0;
|
||||
close(res, temp_error);
|
||||
res = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
sf::SharedPointer<tma::ISocket> VirtualSocketCollection::DoAccept(sf::SharedPointer<tma::ISocket> socket, s32 id, htcs::SockAddrHtcs *address, s32 &error_code) {
|
||||
/* Clear the error code. */
|
||||
error_code = 0;
|
||||
|
||||
/* Try to accept. */
|
||||
sf::SharedPointer<tma::ISocket> new_socket = accept(socket, address, error_code);
|
||||
if (error_code == HTCS_ENETDOWN) {
|
||||
new_socket = accept(socket, address, error_code);
|
||||
|
||||
std::scoped_lock lk(m_mutex);
|
||||
if (const auto index = this->Find(id, std::addressof(error_code)); index >= 0) {
|
||||
m_socket_list[index].m_socket = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
return new_socket;
|
||||
}
|
||||
|
||||
s32 VirtualSocketCollection::GetSockets(s32 * const out_primitives, htcs::FdSet *set, s32 &error_code) {
|
||||
/* Clear the error code. */
|
||||
error_code = 0;
|
||||
s32 count = 0;
|
||||
|
||||
/* Walk the fdset. */
|
||||
if (set != nullptr) {
|
||||
for (auto i = 0; i < FdSetSize; ++i) {
|
||||
/* If the set no longer has fds, we're done. */
|
||||
if (set->fds[i] == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
/* Find the fd's primitive. */
|
||||
s32 primitive = InvalidPrimitive;
|
||||
s32 index;
|
||||
{
|
||||
std::scoped_lock lk(m_mutex);
|
||||
if (index = this->Find(set->fds[i], std::addressof(error_code)); index >= 0) {
|
||||
/* Get the primitive, if necessary. */
|
||||
if (m_socket_list[index].m_primitive == InvalidPrimitive && m_socket_list[index].m_socket != nullptr) {
|
||||
m_socket_list[index].m_socket->GetPrimitive(std::addressof(m_socket_list[index].m_primitive));
|
||||
}
|
||||
|
||||
primitive = m_socket_list[index].m_primitive;
|
||||
}
|
||||
}
|
||||
|
||||
/* Check that an error didn't occur. */
|
||||
if (error_code != HTCS_ENONE) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* If the primitive is invalid, try to realize the socket. */
|
||||
if (primitive == InvalidPrimitive) {
|
||||
if (this->RealizeSocket(set->fds[i]) != nullptr) {
|
||||
std::scoped_lock lk(m_mutex);
|
||||
|
||||
/* Get the primitive. */
|
||||
if (index = this->Find(set->fds[i], std::addressof(error_code)); index >= 0) {
|
||||
m_socket_list[index].m_socket->GetPrimitive(std::addressof(m_socket_list[index].m_primitive));
|
||||
|
||||
primitive = m_socket_list[index].m_primitive;
|
||||
}
|
||||
}
|
||||
|
||||
/* Check that an error didn't occur. */
|
||||
if (error_code != HTCS_ENONE) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Set the output primitive. */
|
||||
if (primitive != InvalidPrimitive) {
|
||||
out_primitives[count++] = primitive;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
void VirtualSocketCollection::SetSockets(htcs::FdSet *set, s32 * const primitives, s32 count) {
|
||||
if (set != nullptr) {
|
||||
/* Clear the set. */
|
||||
FdSetZero(set);
|
||||
|
||||
/* Copy the fds. */
|
||||
for (auto i = 0; i < count; ++i) {
|
||||
std::scoped_lock lk(m_mutex);
|
||||
|
||||
if (const auto index = this->FindByPrimitive(primitives[i]); index >= 0) {
|
||||
set->fds[i] = m_socket_list[index].m_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s32 VirtualSocketCollection::CreateSocket(sf::SharedPointer<tma::ISocket> socket, s32 &error_code) {
|
||||
/* Clear the error code. */
|
||||
error_code = 0;
|
||||
s32 id = InvalidSocket;
|
||||
|
||||
/* Check that we can add to the list. */
|
||||
if (m_list_count < m_list_size) {
|
||||
/* Create a new id. */
|
||||
id = this->CreateId();
|
||||
|
||||
/* Insert the socket into the list. */
|
||||
this->Insert(id, socket);
|
||||
} else {
|
||||
if (socket != nullptr) {
|
||||
s32 tmp_error_code;
|
||||
close(socket, tmp_error_code);
|
||||
}
|
||||
|
||||
error_code = HTCS_EMFILE;
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
namespace ams::htcs::client {
|
||||
|
||||
struct VirtualSocket;
|
||||
|
||||
class VirtualSocketCollection {
|
||||
private:
|
||||
void *m_buffer;
|
||||
size_t m_buffer_size;
|
||||
VirtualSocket *m_socket_list;
|
||||
s32 m_list_count;
|
||||
s32 m_list_size;
|
||||
s32 m_next_id;
|
||||
os::SdkMutex m_mutex;
|
||||
public:
|
||||
static size_t GetWorkingMemorySize(int num_sockets);
|
||||
public:
|
||||
explicit VirtualSocketCollection();
|
||||
~VirtualSocketCollection();
|
||||
public:
|
||||
void Init(void *buffer, size_t buffer_size);
|
||||
void Clear();
|
||||
|
||||
s32 Socket(s32 &error_code);
|
||||
s32 Close(s32 id, s32 &error_code);
|
||||
s32 Bind(s32 id, const htcs::SockAddrHtcs *address, s32 &error_code);
|
||||
s32 Listen(s32 id, s32 backlog_count, s32 &error_code);
|
||||
s32 Accept(s32 id, htcs::SockAddrHtcs *address, s32 &error_code);
|
||||
s32 Fcntl(s32 id, s32 command, s32 value, s32 &error_code);
|
||||
|
||||
s32 Shutdown(s32 id, s32 how, s32 &error_code);
|
||||
|
||||
ssize_t Recv(s32 id, void *buffer, size_t buffer_size, s32 flags, s32 &error_code);
|
||||
ssize_t Send(s32 id, const void *buffer, size_t buffer_size, s32 flags, s32 &error_code);
|
||||
|
||||
s32 Connect(s32 id, const htcs::SockAddrHtcs *address, s32 &error_code);
|
||||
|
||||
s32 Select(htcs::FdSet *read, htcs::FdSet *write, htcs::FdSet *except, htcs::TimeVal *timeout, s32 &error_code);
|
||||
private:
|
||||
s32 CreateId();
|
||||
|
||||
s32 Add(sf::SharedPointer<tma::ISocket> socket);
|
||||
|
||||
void Insert(s32 id, sf::SharedPointer<tma::ISocket> socket);
|
||||
void SetSize(s32 size);
|
||||
|
||||
s32 Find(s32 id, s32 *error_code = nullptr);
|
||||
s32 FindByPrimitive(s32 primitive);
|
||||
|
||||
bool HasAddr(const htcs::SockAddrHtcs *address);
|
||||
|
||||
sf::SharedPointer<tma::ISocket> GetSocket(s32 id, s32 *error_code = nullptr);
|
||||
sf::SharedPointer<tma::ISocket> FetchSocket(s32 id, s32 &error_code);
|
||||
sf::SharedPointer<tma::ISocket> RealizeSocket(s32 id);
|
||||
|
||||
sf::SharedPointer<tma::ISocket> DoAccept(sf::SharedPointer<tma::ISocket> socket, s32 id, htcs::SockAddrHtcs *address, s32 &error_code);
|
||||
|
||||
s32 GetSockets(s32 * const out_primitives, htcs::FdSet *set, s32 &error_code);
|
||||
void SetSockets(htcs::FdSet *set, s32 * const primitives, s32 count);
|
||||
|
||||
s32 CreateSocket(sf::SharedPointer<tma::ISocket> socket, s32 &error_code);
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,765 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "client/htcs_session.hpp"
|
||||
#include "client/htcs_virtual_socket_collection.hpp"
|
||||
|
||||
namespace ams::htcs {
|
||||
|
||||
namespace {
|
||||
|
||||
constinit bool g_initialized = false;
|
||||
constinit bool g_enable_disconnection_emulation = false;
|
||||
|
||||
constinit AllocateFunction g_allocate_function = nullptr;
|
||||
constinit DeallocateFunction g_deallocate_function = nullptr;
|
||||
|
||||
constinit void *g_buffer = nullptr;
|
||||
constinit size_t g_buffer_size = 0;
|
||||
|
||||
constinit os::TlsSlot g_tls_slot = {};
|
||||
|
||||
constinit tma::IHtcsManager *g_manager = nullptr;
|
||||
constinit tma::IHtcsManager *g_monitor = nullptr;
|
||||
|
||||
constinit client::VirtualSocketCollection *g_sockets = nullptr;
|
||||
|
||||
void SetLastError(uintptr_t error_code) {
|
||||
os::SetTlsValue(g_tls_slot, error_code);
|
||||
}
|
||||
|
||||
void InitializeImpl(void *buffer, size_t buffer_size, int num_sessions) {
|
||||
/* Check the session count. */
|
||||
AMS_ASSERT(0 < num_sessions && num_sessions <= SessionCountMax);
|
||||
|
||||
/* Initialize the manager and monitor. */
|
||||
client::InitializeSessionManager(std::addressof(g_manager), std::addressof(g_monitor), num_sessions);
|
||||
|
||||
/* Register the process. */
|
||||
const sf::ClientProcessId process_id{0};
|
||||
R_ABORT_UNLESS(g_manager->RegisterProcessId(process_id));
|
||||
R_ABORT_UNLESS(g_monitor->MonitorManager(process_id));
|
||||
|
||||
/* Allocate a tls slot for our last error. */
|
||||
os::SdkAllocateTlsSlot(std::addressof(g_tls_slot), nullptr);
|
||||
|
||||
/* Setup the virtual socket collection. */
|
||||
AMS_ASSERT(buffer != nullptr);
|
||||
g_sockets = reinterpret_cast<client::VirtualSocketCollection *>(buffer);
|
||||
std::construct_at(g_sockets);
|
||||
g_sockets->Init(static_cast<u8 *>(buffer) + sizeof(*g_sockets), buffer_size - sizeof(*g_sockets));
|
||||
|
||||
/* Mark initialized. */
|
||||
g_initialized = true;
|
||||
}
|
||||
|
||||
void InitializeImpl(AllocateFunction allocate, DeallocateFunction deallocate, int num_sessions, int num_sockets) {
|
||||
/* Check the session count. */
|
||||
AMS_ASSERT(0 < num_sessions && num_sessions <= SessionCountMax);
|
||||
|
||||
/* Set the allocation functions. */
|
||||
g_allocate_function = allocate;
|
||||
g_deallocate_function = deallocate;
|
||||
|
||||
/* Allocate a buffer. */
|
||||
g_buffer_size = sizeof(client::VirtualSocketCollection) + client::VirtualSocketCollection::GetWorkingMemorySize(num_sockets);
|
||||
g_buffer = g_allocate_function(g_buffer_size);
|
||||
|
||||
/* Initialize. */
|
||||
InitializeImpl(g_buffer, g_buffer_size, num_sessions);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool IsInitialized() {
|
||||
return g_initialized;
|
||||
}
|
||||
|
||||
size_t GetWorkingMemorySize(int num_sockets) {
|
||||
AMS_ASSERT(num_sockets <= SocketCountMax);
|
||||
return sizeof(client::VirtualSocketCollection) + client::VirtualSocketCollection::GetWorkingMemorySize(num_sockets);
|
||||
}
|
||||
|
||||
void Initialize(AllocateFunction allocate, DeallocateFunction deallocate, int num_sessions) {
|
||||
/* Check that we're not already initialized. */
|
||||
AMS_ASSERT(!IsInitialized());
|
||||
|
||||
/* Configure disconnection emulation. */
|
||||
g_enable_disconnection_emulation = true;
|
||||
|
||||
/* Initialize. */
|
||||
InitializeImpl(allocate, deallocate, num_sessions, htcs::SocketCountMax);
|
||||
}
|
||||
|
||||
void Initialize(void *buffer, size_t buffer_size) {
|
||||
/* Check that we're not already initialized. */
|
||||
AMS_ASSERT(!IsInitialized());
|
||||
|
||||
/* Configure disconnection emulation. */
|
||||
g_enable_disconnection_emulation = true;
|
||||
|
||||
/* Initialize. */
|
||||
InitializeImpl(buffer, buffer_size, htcs::SessionCountMax);
|
||||
}
|
||||
|
||||
void InitializeForDisableDisconnectionEmulation(AllocateFunction allocate, DeallocateFunction deallocate, int num_sessions) {
|
||||
/* Check that we're not already initialized. */
|
||||
AMS_ASSERT(!IsInitialized());
|
||||
|
||||
/* Configure disconnection emulation. */
|
||||
g_enable_disconnection_emulation = false;
|
||||
|
||||
/* Initialize. */
|
||||
InitializeImpl(allocate, deallocate, num_sessions, htcs::SocketCountMax);
|
||||
}
|
||||
|
||||
void InitializeForDisableDisconnectionEmulation(void *buffer, size_t buffer_size) {
|
||||
/* Check that we're not already initialized. */
|
||||
AMS_ASSERT(!IsInitialized());
|
||||
|
||||
/* Configure disconnection emulation. */
|
||||
g_enable_disconnection_emulation = false;
|
||||
|
||||
/* Initialize. */
|
||||
InitializeImpl(buffer, buffer_size, htcs::SessionCountMax);
|
||||
}
|
||||
|
||||
void InitializeForSystem(void *buffer, size_t buffer_size, int num_sessions) {
|
||||
/* Check that we're not already initialized. */
|
||||
AMS_ASSERT(!IsInitialized());
|
||||
|
||||
/* Configure disconnection emulation. */
|
||||
g_enable_disconnection_emulation = true;
|
||||
|
||||
/* Initialize. */
|
||||
InitializeImpl(buffer, buffer_size, num_sessions);
|
||||
}
|
||||
|
||||
void Finalize() {
|
||||
/* Check that we're initialized. */
|
||||
AMS_ASSERT(IsInitialized());
|
||||
|
||||
/* Set not initialized. */
|
||||
g_initialized = false;
|
||||
|
||||
/* Destroy the virtual socket collection. */
|
||||
std::destroy_at(g_sockets);
|
||||
g_sockets = nullptr;
|
||||
|
||||
/* Free the buffer, if we have one. */
|
||||
if (g_buffer != nullptr) {
|
||||
g_deallocate_function(g_buffer, g_buffer_size);
|
||||
g_buffer = nullptr;
|
||||
g_buffer_size = 0;
|
||||
}
|
||||
|
||||
/* Free the tls slot. */
|
||||
os::FreeTlsSlot(g_tls_slot);
|
||||
|
||||
/* Release the manager objects. */
|
||||
sf::ReleaseSharedObject(g_manager);
|
||||
sf::ReleaseSharedObject(g_monitor);
|
||||
g_manager = nullptr;
|
||||
g_monitor = nullptr;
|
||||
|
||||
/* Finalize the htcs client sessions. */
|
||||
client::FinalizeSessionManager();
|
||||
}
|
||||
|
||||
const HtcsPeerName GetPeerNameAny() {
|
||||
/* Check that we have a manager. */
|
||||
AMS_ASSERT(g_manager != nullptr);
|
||||
|
||||
/* Get name. */
|
||||
HtcsPeerName name;
|
||||
g_manager->GetPeerNameAny(std::addressof(name));
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
const HtcsPeerName GetDefaultHostName() {
|
||||
/* Check that we have a manager. */
|
||||
AMS_ASSERT(g_manager != nullptr);
|
||||
|
||||
/* Get name. */
|
||||
HtcsPeerName name;
|
||||
g_manager->GetDefaultHostName(std::addressof(name));
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
s32 GetLastError() {
|
||||
/* Check that we have a manager. */
|
||||
AMS_ASSERT(g_manager != nullptr);
|
||||
|
||||
return static_cast<s32>(os::GetTlsValue(g_tls_slot));
|
||||
}
|
||||
|
||||
s32 Socket() {
|
||||
/* Check that we have a manager. */
|
||||
AMS_ASSERT(g_manager != nullptr);
|
||||
|
||||
/* Check that we have a socket collection. */
|
||||
AMS_ASSERT(g_sockets != nullptr);
|
||||
|
||||
/* Perform the operation. */
|
||||
s32 error_code = 0;
|
||||
const s32 ret = g_sockets->Socket(error_code);
|
||||
if (ret < 0) {
|
||||
SetLastError(static_cast<uintptr_t>(error_code));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
s32 Close(s32 desc) {
|
||||
/* Check that we have a manager. */
|
||||
AMS_ASSERT(g_manager != nullptr);
|
||||
|
||||
/* Check that we have a socket collection. */
|
||||
AMS_ASSERT(g_sockets != nullptr);
|
||||
|
||||
/* Perform the operation. */
|
||||
s32 error_code = 0;
|
||||
const s32 ret = g_sockets->Close(desc, error_code);
|
||||
if (ret < 0) {
|
||||
SetLastError(static_cast<uintptr_t>(error_code));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
s32 Connect(s32 desc, const SockAddrHtcs *address) {
|
||||
/* Check that we have a manager. */
|
||||
AMS_ASSERT(g_manager != nullptr);
|
||||
|
||||
/* Check that we have a socket collection. */
|
||||
AMS_ASSERT(g_sockets != nullptr);
|
||||
|
||||
/* Check that the address family is correct. */
|
||||
AMS_ASSERT(address->family == HTCS_AF_HTCS);
|
||||
|
||||
/* Perform the operation. */
|
||||
s32 error_code = 0;
|
||||
const s32 ret = g_sockets->Connect(desc, address, error_code);
|
||||
if (ret < 0) {
|
||||
SetLastError(static_cast<uintptr_t>(error_code));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
s32 Bind(s32 desc, const SockAddrHtcs *address) {
|
||||
/* Check that we have a manager. */
|
||||
AMS_ASSERT(g_manager != nullptr);
|
||||
|
||||
/* Check that we have a socket collection. */
|
||||
AMS_ASSERT(g_sockets != nullptr);
|
||||
|
||||
/* Check that the address family is correct. */
|
||||
AMS_ASSERT(address->family == HTCS_AF_HTCS);
|
||||
|
||||
/* Perform the operation. */
|
||||
s32 error_code = 0;
|
||||
const s32 ret = g_sockets->Bind(desc, address, error_code);
|
||||
if (ret < 0) {
|
||||
SetLastError(static_cast<uintptr_t>(error_code));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
s32 Listen(s32 desc, s32 backlog_count) {
|
||||
/* Check that we have a manager. */
|
||||
AMS_ASSERT(g_manager != nullptr);
|
||||
|
||||
/* Check that we have a socket collection. */
|
||||
AMS_ASSERT(g_sockets != nullptr);
|
||||
|
||||
/* Perform the operation. */
|
||||
s32 error_code = 0;
|
||||
const s32 ret = g_sockets->Listen(desc, backlog_count, error_code);
|
||||
if (ret < 0) {
|
||||
SetLastError(static_cast<uintptr_t>(error_code));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
s32 Accept(s32 desc, SockAddrHtcs *address) {
|
||||
/* Check that we have a manager. */
|
||||
AMS_ASSERT(g_manager != nullptr);
|
||||
|
||||
/* Check that we have a socket collection. */
|
||||
AMS_ASSERT(g_sockets != nullptr);
|
||||
|
||||
/* Ensure we have an address. */
|
||||
SockAddrHtcs tmp;
|
||||
if (address == nullptr) {
|
||||
address = std::addressof(tmp);
|
||||
}
|
||||
|
||||
/* Perform the operation. */
|
||||
s32 error_code = 0;
|
||||
const s32 ret = g_sockets->Accept(desc, address, error_code);
|
||||
if (ret < 0) {
|
||||
SetLastError(static_cast<uintptr_t>(error_code));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
s32 Shutdown(s32 desc, s32 how) {
|
||||
/* Check that we have a manager. */
|
||||
AMS_ASSERT(g_manager != nullptr);
|
||||
|
||||
/* Check that we have a socket collection. */
|
||||
AMS_ASSERT(g_sockets != nullptr);
|
||||
|
||||
/* Perform the operation. */
|
||||
s32 error_code = 0;
|
||||
const s32 ret = g_sockets->Shutdown(desc, how, error_code);
|
||||
if (ret < 0) {
|
||||
SetLastError(static_cast<uintptr_t>(error_code));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
s32 Fcntl(s32 desc, s32 command, s32 value) {
|
||||
/* Check that we have a manager. */
|
||||
AMS_ASSERT(g_manager != nullptr);
|
||||
|
||||
/* Check that we have a socket collection. */
|
||||
AMS_ASSERT(g_sockets != nullptr);
|
||||
|
||||
/* Perform the operation. */
|
||||
s32 error_code = 0;
|
||||
const s32 ret = g_sockets->Fcntl(desc, command, value, error_code);
|
||||
if (ret < 0) {
|
||||
SetLastError(static_cast<uintptr_t>(error_code));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
s32 Select(s32 count, FdSet *read, FdSet *write, FdSet *exception, TimeVal *timeout) {
|
||||
AMS_UNUSED(count);
|
||||
|
||||
/* Check that we have a manager. */
|
||||
AMS_ASSERT(g_manager != nullptr);
|
||||
|
||||
/* Check that we have a socket collection. */
|
||||
AMS_ASSERT(g_sockets != nullptr);
|
||||
|
||||
/* Check that we have some form of input. */
|
||||
if (read == nullptr && write == nullptr && exception == nullptr) {
|
||||
SetLastError(static_cast<uintptr_t>(HTCS_EINVAL));
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Check that the timeout is valid. */
|
||||
if (timeout != nullptr && (timeout->tv_sec < 0 || timeout->tv_usec < 0)) {
|
||||
SetLastError(static_cast<uintptr_t>(HTCS_EINVAL));
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Perform the operation. */
|
||||
s32 error_code = 0;
|
||||
const s32 ret = g_sockets->Select(read, write, exception, timeout, error_code);
|
||||
if (ret < 0) {
|
||||
SetLastError(static_cast<uintptr_t>(error_code));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t Recv(s32 desc, void *buffer, size_t buffer_size, s32 flags) {
|
||||
/* Check that we have a manager. */
|
||||
AMS_ASSERT(g_manager != nullptr);
|
||||
|
||||
/* Check that we have a socket collection. */
|
||||
AMS_ASSERT(g_sockets != nullptr);
|
||||
|
||||
/* Perform the operation. */
|
||||
s32 error_code = 0;
|
||||
const ssize_t ret = g_sockets->Recv(desc, buffer, buffer_size, flags, error_code);
|
||||
if (ret < 0) {
|
||||
SetLastError(static_cast<uintptr_t>(error_code));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t Send(s32 desc, const void *buffer, size_t buffer_size, s32 flags) {
|
||||
/* Check that we have a manager. */
|
||||
AMS_ASSERT(g_manager != nullptr);
|
||||
|
||||
/* Check that we have a socket collection. */
|
||||
AMS_ASSERT(g_sockets != nullptr);
|
||||
|
||||
/* Perform the operation. */
|
||||
s32 error_code = 0;
|
||||
const ssize_t ret = g_sockets->Send(desc, buffer, buffer_size, flags, error_code);
|
||||
if (ret < 0) {
|
||||
SetLastError(static_cast<uintptr_t>(error_code));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void FdSetZero(FdSet *set) {
|
||||
AMS_ASSERT(set != nullptr);
|
||||
|
||||
std::memset(set, 0, sizeof(*set));
|
||||
}
|
||||
|
||||
void FdSetSet(s32 fd, FdSet *set) {
|
||||
AMS_ASSERT(set != nullptr);
|
||||
|
||||
for (auto i = 0; i < FdSetSize; ++i) {
|
||||
if (set->fds[i] == 0) {
|
||||
set->fds[i] = fd;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void FdSetClr(s32 fd, FdSet *set) {
|
||||
AMS_ASSERT(set != nullptr);
|
||||
|
||||
for (auto i = 0; i < FdSetSize; ++i) {
|
||||
if (set->fds[i] == fd) {
|
||||
std::memcpy(set->fds + i, set->fds + i + 1, (FdSetSize - (i + 1)) * sizeof(fd));
|
||||
set->fds[FdSetSize - 1] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool FdSetIsSet(s32 fd, const FdSet *set) {
|
||||
AMS_ASSERT(set != nullptr);
|
||||
|
||||
for (auto i = 0; i < FdSetSize; ++i) {
|
||||
if (set->fds[i] == fd) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
namespace client {
|
||||
|
||||
sf::SharedPointer<tma::ISocket> socket(s32 &last_error) {
|
||||
sf::SharedPointer<tma::ISocket> socket = nullptr;
|
||||
R_ABORT_UNLESS(g_manager->CreateSocket(std::addressof(last_error), std::addressof(socket), g_enable_disconnection_emulation));
|
||||
return socket;
|
||||
}
|
||||
|
||||
s32 close(sf::SharedPointer<tma::ISocket> socket, s32 &last_error) {
|
||||
s32 res;
|
||||
socket->Close(std::addressof(last_error), std::addressof(res));
|
||||
return res;
|
||||
}
|
||||
|
||||
s32 bind(sf::SharedPointer<tma::ISocket> socket, const htcs::SockAddrHtcs *address, s32 &last_error) {
|
||||
/* Create null-terminated address. */
|
||||
htcs::SockAddrHtcs null_terminated_address;
|
||||
null_terminated_address.family = address->family;
|
||||
util::Strlcpy(null_terminated_address.peer_name.name, address->peer_name.name, PeerNameBufferLength);
|
||||
util::Strlcpy(null_terminated_address.port_name.name, address->port_name.name, PortNameBufferLength);
|
||||
|
||||
s32 res;
|
||||
socket->Bind(std::addressof(last_error), std::addressof(res), null_terminated_address);
|
||||
return res;
|
||||
}
|
||||
|
||||
s32 listen(sf::SharedPointer<tma::ISocket> socket, s32 backlog_count, s32 &last_error) {
|
||||
s32 res;
|
||||
socket->Listen(std::addressof(last_error), std::addressof(res), backlog_count);
|
||||
return res;
|
||||
}
|
||||
|
||||
sf::SharedPointer<tma::ISocket> accept(sf::SharedPointer<tma::ISocket> socket, htcs::SockAddrHtcs *address, s32 &last_error) {
|
||||
/* Begin the accept. */
|
||||
sf::SharedPointer<tma::ISocket> res = nullptr;
|
||||
u32 task_id = 0;
|
||||
sf::NativeHandle event_handle;
|
||||
if (R_SUCCEEDED(socket->AcceptStart(std::addressof(task_id), std::addressof(event_handle)))) {
|
||||
/* Create system event. */
|
||||
os::SystemEventType event;
|
||||
os::AttachReadableHandleToSystemEvent(std::addressof(event), event_handle.GetOsHandle(), event_handle.IsManaged(), os::EventClearMode_ManualClear);
|
||||
event_handle.Detach();
|
||||
|
||||
/* When we're done, clean up the event. */
|
||||
ON_SCOPE_EXIT { os::DestroySystemEvent(std::addressof(event)); };
|
||||
|
||||
/* Wait for the accept to finish. */
|
||||
os::WaitSystemEvent(std::addressof(event));
|
||||
|
||||
/* End the accept. */
|
||||
socket->AcceptResults(std::addressof(last_error), std::addressof(res), address, task_id);
|
||||
} else {
|
||||
/* Set error. */
|
||||
last_error = HTCS_EINTR;
|
||||
}
|
||||
|
||||
/* Sleep, if an error occurred. */
|
||||
if (last_error != HTCS_ENONE) {
|
||||
os::SleepThread(TimeSpan::FromMilliSeconds(1));
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
s32 fcntl(sf::SharedPointer<tma::ISocket> socket, s32 command, s32 value, s32 &last_error) {
|
||||
s32 res;
|
||||
socket->Fcntl(std::addressof(last_error), std::addressof(res), command, value);
|
||||
return res;
|
||||
}
|
||||
|
||||
s32 shutdown(sf::SharedPointer<tma::ISocket> socket, s32 how, s32 &last_error) {
|
||||
s32 res;
|
||||
socket->Shutdown(std::addressof(last_error), std::addressof(res), how);
|
||||
return res;
|
||||
}
|
||||
|
||||
s32 connect(sf::SharedPointer<tma::ISocket> socket, const htcs::SockAddrHtcs *address, s32 &last_error) {
|
||||
/* Create null-terminated address. */
|
||||
htcs::SockAddrHtcs null_terminated_address;
|
||||
null_terminated_address.family = address->family;
|
||||
util::Strlcpy(null_terminated_address.peer_name.name, address->peer_name.name, PeerNameBufferLength);
|
||||
util::Strlcpy(null_terminated_address.port_name.name, address->port_name.name, PortNameBufferLength);
|
||||
|
||||
s32 res;
|
||||
socket->Connect(std::addressof(last_error), std::addressof(res), null_terminated_address);
|
||||
return res;
|
||||
}
|
||||
|
||||
s32 select(s32 * const read, s32 &num_read, s32 * const write, s32 &num_write, s32 * const except, s32 &num_except, htcs::TimeVal *timeout, s32 &last_error) {
|
||||
/* Determine the timeout values. */
|
||||
s64 tv_sec = -1;
|
||||
s64 tv_usec = -1;
|
||||
if (timeout != nullptr) {
|
||||
tv_sec = timeout->tv_sec;
|
||||
tv_usec = timeout->tv_usec;
|
||||
}
|
||||
|
||||
using InArray = sf::InMapAliasArray<s32>;
|
||||
using OutArray = sf::OutMapAliasArray<s32>;
|
||||
|
||||
/* Begin the select. */
|
||||
s32 res = -1;
|
||||
u32 task_id = 0;
|
||||
sf::NativeHandle event_handle;
|
||||
if (R_SUCCEEDED(g_manager->StartSelect(std::addressof(task_id), std::addressof(event_handle), InArray(read, num_read), InArray(write, num_write), InArray(except, num_except), tv_sec, tv_usec))) {
|
||||
/* Create system event. */
|
||||
os::SystemEventType event;
|
||||
os::AttachReadableHandleToSystemEvent(std::addressof(event), event_handle.GetOsHandle(), event_handle.IsManaged(), os::EventClearMode_ManualClear);
|
||||
event_handle.Detach();
|
||||
|
||||
/* When we're done, clean up the event. */
|
||||
ON_SCOPE_EXIT { os::DestroySystemEvent(std::addressof(event)); };
|
||||
|
||||
/* Wait for the select to finish. */
|
||||
os::WaitSystemEvent(std::addressof(event));
|
||||
|
||||
/* End the select. */
|
||||
g_manager->EndSelect(std::addressof(last_error), std::addressof(res), OutArray(read, num_read), OutArray(write, num_write), OutArray(except, num_except), task_id);
|
||||
} else {
|
||||
/* Set error. */
|
||||
last_error = HTCS_EINTR;
|
||||
os::SleepThread(TimeSpan::FromMilliSeconds(1));
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr size_t MaximumBufferSizeForSmallTransfer = 0xDFE0;
|
||||
|
||||
ssize_t recvLarge(sf::SharedPointer<tma::ISocket> socket, void *buffer, size_t buffer_size, s32 flags, s32 &last_error) {
|
||||
/* Setup. */
|
||||
s64 res = -1;
|
||||
last_error = HTCS_EINTR;
|
||||
|
||||
/* Start the receive. */
|
||||
u32 task_id = 0;
|
||||
sf::NativeHandle event_handle;
|
||||
if (R_SUCCEEDED(socket->StartRecv(std::addressof(task_id), std::addressof(event_handle), static_cast<s64>(buffer_size), flags))) {
|
||||
/* Create system event. */
|
||||
os::SystemEventType event;
|
||||
os::AttachReadableHandleToSystemEvent(std::addressof(event), event_handle.GetOsHandle(), event_handle.IsManaged(), os::EventClearMode_ManualClear);
|
||||
event_handle.Detach();
|
||||
|
||||
/* When we're done, clean up the event. */
|
||||
ON_SCOPE_EXIT { os::DestroySystemEvent(std::addressof(event)); };
|
||||
|
||||
/* Wait for the receive to finish. */
|
||||
os::WaitSystemEvent(std::addressof(event));
|
||||
|
||||
/* End the receive. */
|
||||
socket->EndRecv(std::addressof(last_error), std::addressof(res), sf::OutAutoSelectBuffer(buffer, buffer_size), task_id);
|
||||
} else {
|
||||
/* Set error. */
|
||||
last_error = HTCS_EINTR;
|
||||
os::SleepThread(TimeSpan::FromMilliSeconds(1));
|
||||
}
|
||||
|
||||
return static_cast<ssize_t>(res);
|
||||
}
|
||||
|
||||
ssize_t sendLarge(sf::SharedPointer<tma::ISocket> socket, const void *buffer, size_t buffer_size, s32 flags, s32 &last_error) {
|
||||
/* Setup. */
|
||||
s64 res = -1;
|
||||
last_error = HTCS_EINTR;
|
||||
|
||||
/* Start the send. */
|
||||
u32 task_id = 0;
|
||||
s64 max_size = 0;
|
||||
sf::NativeHandle event_handle;
|
||||
if (R_SUCCEEDED(socket->StartSend(std::addressof(task_id), std::addressof(event_handle), std::addressof(max_size), static_cast<s64>(buffer_size), flags))) {
|
||||
/* Create system event. */
|
||||
os::SystemEventType event;
|
||||
os::AttachReadableHandleToSystemEvent(std::addressof(event), event_handle.GetOsHandle(), event_handle.IsManaged(), os::EventClearMode_ManualClear);
|
||||
event_handle.Detach();
|
||||
|
||||
/* When we're done, clean up the event. */
|
||||
ON_SCOPE_EXIT { os::DestroySystemEvent(std::addressof(event)); };
|
||||
|
||||
/* Send all the data. */
|
||||
bool done = false;
|
||||
size_t sent = 0;
|
||||
while (sent < buffer_size) {
|
||||
/* Determine how much to send, this iteration. */
|
||||
const u8 *cur = static_cast<const u8 *>(buffer) + sent;
|
||||
const s64 cur_size = std::min(max_size, static_cast<s64>(buffer_size - sent));
|
||||
|
||||
/* Continue sending data. */
|
||||
s64 cur_sent = 0;
|
||||
bool wait = false;
|
||||
const Result result = socket->ContinueSend(std::addressof(cur_sent), std::addressof(wait), sf::InNonSecureAutoSelectBuffer(cur, cur_size), task_id);
|
||||
if (cur_sent <= 0 || R_FAILED(result)) {
|
||||
done = true;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Wait if we should. */
|
||||
if (wait) {
|
||||
os::WaitSystemEvent(std::addressof(event));
|
||||
os::ClearSystemEvent(std::addressof(event));
|
||||
}
|
||||
|
||||
/* Advance. */
|
||||
sent += cur_sent;
|
||||
}
|
||||
|
||||
/* Wait for the send to finish. */
|
||||
if (!done) {
|
||||
os::WaitSystemEvent(std::addressof(event));
|
||||
}
|
||||
|
||||
/* End the send. */
|
||||
socket->EndSend(std::addressof(last_error), std::addressof(res), task_id);
|
||||
} else {
|
||||
/* Set error. */
|
||||
last_error = HTCS_EINTR;
|
||||
os::SleepThread(TimeSpan::FromMilliSeconds(1));
|
||||
}
|
||||
|
||||
return static_cast<ssize_t>(res);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ssize_t recv(sf::SharedPointer<tma::ISocket> socket, void *buffer, size_t buffer_size, s32 flags, s32 &last_error) {
|
||||
/* Determine how much to receive. */
|
||||
size_t recv_size = buffer_size;
|
||||
|
||||
if ((flags & HTCS_MSG_WAITALL) == 0) {
|
||||
recv_size = std::min(MaximumBufferSizeForSmallTransfer, buffer_size);
|
||||
}
|
||||
|
||||
/* Perform a large receive, if we have to. */
|
||||
if (recv_size > MaximumBufferSizeForSmallTransfer) {
|
||||
return recvLarge(socket, buffer, recv_size, flags, last_error);
|
||||
}
|
||||
|
||||
/* Start the receive. */
|
||||
s64 res = -1;
|
||||
u32 task_id = 0;
|
||||
sf::NativeHandle event_handle;
|
||||
if (R_SUCCEEDED(socket->RecvStart(std::addressof(task_id), std::addressof(event_handle), static_cast<s32>(recv_size), flags))) {
|
||||
/* Create system event. */
|
||||
os::SystemEventType event;
|
||||
os::AttachReadableHandleToSystemEvent(std::addressof(event), event_handle.GetOsHandle(), event_handle.IsManaged(), os::EventClearMode_ManualClear);
|
||||
event_handle.Detach();
|
||||
|
||||
/* When we're done, clean up the event. */
|
||||
ON_SCOPE_EXIT { os::DestroySystemEvent(std::addressof(event)); };
|
||||
|
||||
/* Wait for the receive to finish. */
|
||||
os::WaitSystemEvent(std::addressof(event));
|
||||
|
||||
/* End the receive. */
|
||||
socket->RecvResults(std::addressof(last_error), std::addressof(res), sf::OutAutoSelectBuffer(buffer, recv_size), task_id);
|
||||
} else {
|
||||
/* Set error. */
|
||||
last_error = HTCS_EINTR;
|
||||
os::SleepThread(TimeSpan::FromMilliSeconds(1));
|
||||
}
|
||||
|
||||
return static_cast<ssize_t>(res);
|
||||
}
|
||||
|
||||
ssize_t send(sf::SharedPointer<tma::ISocket> socket, const void *buffer, size_t buffer_size, s32 flags, s32 &last_error) {
|
||||
/* Perform a large send, if we have to. */
|
||||
if (buffer_size > MaximumBufferSizeForSmallTransfer) {
|
||||
return sendLarge(socket, buffer, buffer_size, flags, last_error);
|
||||
}
|
||||
|
||||
/* Start the send. */
|
||||
s64 res = -1;
|
||||
u32 task_id = 0;
|
||||
sf::NativeHandle event_handle;
|
||||
if (R_SUCCEEDED(socket->SendStart(std::addressof(task_id), std::addressof(event_handle), sf::InNonSecureAutoSelectBuffer(buffer, buffer_size), flags))) {
|
||||
/* Create system event. */
|
||||
os::SystemEventType event;
|
||||
os::AttachReadableHandleToSystemEvent(std::addressof(event), event_handle.GetOsHandle(), event_handle.IsManaged(), os::EventClearMode_ManualClear);
|
||||
event_handle.Detach();
|
||||
|
||||
/* When we're done, clean up the event. */
|
||||
ON_SCOPE_EXIT { os::DestroySystemEvent(std::addressof(event)); };
|
||||
|
||||
/* Wait for the send to finish. */
|
||||
os::WaitSystemEvent(std::addressof(event));
|
||||
|
||||
/* End the send. */
|
||||
socket->SendResults(std::addressof(last_error), std::addressof(res), task_id);
|
||||
} else {
|
||||
/* Set error. */
|
||||
last_error = HTCS_EINTR;
|
||||
os::SleepThread(TimeSpan::FromMilliSeconds(1));
|
||||
}
|
||||
|
||||
return static_cast<ssize_t>(res);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_impl.hpp"
|
||||
|
||||
namespace ams::htcs::impl {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr const htcs::HtcsPeerName PeerNameAny = {""};
|
||||
constexpr const htcs::HtcsPeerName DefaultHostName = {""};
|
||||
|
||||
}
|
||||
|
||||
const htcs::HtcsPeerName GetPeerNameAny() { return PeerNameAny; }
|
||||
const htcs::HtcsPeerName GetDefaultHostName() { return DefaultHostName; }
|
||||
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
namespace ams::htcs::impl {
|
||||
|
||||
const htcs::HtcsPeerName GetPeerNameAny();
|
||||
const htcs::HtcsPeerName GetDefaultHostName();
|
||||
|
||||
}
|
||||
@@ -1,398 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_manager.hpp"
|
||||
#include "htcs_manager_impl.hpp"
|
||||
#include "htcs_util.hpp"
|
||||
|
||||
namespace ams::htcs::impl {
|
||||
|
||||
HtcsManager::HtcsManager(mem::StandardAllocator *allocator, htclow::HtclowManager *htclow_manager) : m_allocator(allocator), m_impl(static_cast<HtcsManagerImpl *>(allocator->Allocate(sizeof(HtcsManagerImpl), alignof(HtcsManagerImpl)))) {
|
||||
std::construct_at(m_impl, m_allocator, htclow_manager);
|
||||
}
|
||||
|
||||
HtcsManager::~HtcsManager() {
|
||||
std::destroy_at(m_impl);
|
||||
m_allocator->Free(m_impl);
|
||||
}
|
||||
|
||||
os::EventType *HtcsManager::GetServiceAvailabilityEvent() {
|
||||
return m_impl->GetServiceAvailabilityEvent();
|
||||
}
|
||||
|
||||
bool HtcsManager::IsServiceAvailable() {
|
||||
return m_impl->IsServiceAvailable();
|
||||
}
|
||||
|
||||
void HtcsManager::Socket(s32 *out_err, s32 *out_desc, bool enable_disconnection_emulation) {
|
||||
/* Invoke our implementation. */
|
||||
s32 err = -1, desc = -1;
|
||||
const Result result = m_impl->CreateSocket(std::addressof(err), std::addressof(desc), enable_disconnection_emulation);
|
||||
|
||||
/* Set output. */
|
||||
if (R_SUCCEEDED(result)) {
|
||||
*out_err = err;
|
||||
if (err == 0) {
|
||||
*out_desc = desc;
|
||||
} else {
|
||||
*out_desc = -1;
|
||||
}
|
||||
} else {
|
||||
*out_err = ConvertResultToErrorCode(result);
|
||||
*out_desc = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void HtcsManager::Close(s32 *out_err, s32 *out_res, s32 desc) {
|
||||
/* Invoke our implementation. */
|
||||
const Result result = m_impl->DestroySocket(desc);
|
||||
|
||||
/* Set output. */
|
||||
*out_err = ConvertResultToErrorCode(result);
|
||||
if (R_SUCCEEDED(result)) {
|
||||
*out_res = 0;
|
||||
} else {
|
||||
*out_res = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void HtcsManager::Connect(s32 *out_err, s32 *out_res, const SockAddrHtcs &address, s32 desc) {
|
||||
/* Invoke our implementation. */
|
||||
s32 err = -1;
|
||||
const Result result = m_impl->Connect(std::addressof(err), desc, address);
|
||||
|
||||
/* Set output. */
|
||||
if (R_SUCCEEDED(result)) {
|
||||
*out_err = err;
|
||||
if (err == 0) {
|
||||
*out_res = 0;
|
||||
} else {
|
||||
*out_res = -1;
|
||||
}
|
||||
} else {
|
||||
*out_err = ConvertResultToErrorCode(result);
|
||||
*out_res = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void HtcsManager::Bind(s32 *out_err, s32 *out_res, const SockAddrHtcs &address, s32 desc) {
|
||||
/* Invoke our implementation. */
|
||||
s32 err = -1;
|
||||
const Result result = m_impl->Bind(std::addressof(err), desc, address);
|
||||
|
||||
/* Set output. */
|
||||
if (R_SUCCEEDED(result)) {
|
||||
*out_err = err;
|
||||
if (err == 0) {
|
||||
*out_res = 0;
|
||||
} else {
|
||||
*out_res = -1;
|
||||
}
|
||||
} else {
|
||||
*out_err = ConvertResultToErrorCode(result);
|
||||
*out_res = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void HtcsManager::Listen(s32 *out_err, s32 *out_res, s32 backlog_count, s32 desc) {
|
||||
/* Invoke our implementation. */
|
||||
s32 err = -1;
|
||||
const Result result = m_impl->Listen(std::addressof(err), desc, backlog_count);
|
||||
|
||||
/* Set output. */
|
||||
if (R_SUCCEEDED(result)) {
|
||||
*out_err = err;
|
||||
if (err == 0) {
|
||||
*out_res = 0;
|
||||
} else {
|
||||
*out_res = -1;
|
||||
}
|
||||
} else {
|
||||
*out_err = ConvertResultToErrorCode(result);
|
||||
*out_res = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void HtcsManager::Recv(s32 *out_err, s64 *out_size, char *buffer, size_t size, s32 flags, s32 desc) {
|
||||
/* Invoke our implementation. */
|
||||
s32 err = -1;
|
||||
s64 recv_size = -1;
|
||||
const Result result = m_impl->Receive(std::addressof(err), std::addressof(recv_size), buffer, size, desc, flags);
|
||||
|
||||
/* Set output. */
|
||||
if (R_SUCCEEDED(result)) {
|
||||
*out_err = err;
|
||||
if (err == 0) {
|
||||
*out_size = recv_size;
|
||||
} else {
|
||||
*out_size = -1;
|
||||
}
|
||||
} else {
|
||||
*out_err = ConvertResultToErrorCode(result);
|
||||
*out_size = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void HtcsManager::Send(s32 *out_err, s64 *out_size, const char *buffer, size_t size, s32 flags, s32 desc) {
|
||||
/* Invoke our implementation. */
|
||||
s32 err = -1;
|
||||
s64 send_size = -1;
|
||||
const Result result = m_impl->Send(std::addressof(err), std::addressof(send_size), buffer, size, desc, flags);
|
||||
|
||||
/* Set output. */
|
||||
if (R_SUCCEEDED(result)) {
|
||||
*out_err = err;
|
||||
if (err == 0) {
|
||||
*out_size = send_size;
|
||||
} else {
|
||||
*out_size = -1;
|
||||
}
|
||||
} else {
|
||||
*out_err = ConvertResultToErrorCode(result);
|
||||
*out_size = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void HtcsManager::Shutdown(s32 *out_err, s32 *out_res, s32 how, s32 desc) {
|
||||
/* Invoke our implementation. */
|
||||
s32 err = -1;
|
||||
const Result result = m_impl->Shutdown(std::addressof(err), desc, how);
|
||||
|
||||
/* Set output. */
|
||||
if (R_SUCCEEDED(result)) {
|
||||
*out_err = err;
|
||||
if (err == 0) {
|
||||
*out_res = 0;
|
||||
} else {
|
||||
*out_res = -1;
|
||||
}
|
||||
} else {
|
||||
if (htcs::ResultInvalidHandle::Includes(result)) {
|
||||
*out_err = HTCS_ENOTCONN;
|
||||
} else {
|
||||
*out_err = ConvertResultToErrorCode(result);
|
||||
}
|
||||
*out_res = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void HtcsManager::Fcntl(s32 *out_err, s32 *out_res, s32 command, s32 value, s32 desc) {
|
||||
/* Invoke our implementation. */
|
||||
s32 err = -1, res = -1;
|
||||
const Result result = m_impl->Fcntl(std::addressof(err), std::addressof(res), desc, command, value);
|
||||
|
||||
/* Set output. */
|
||||
if (R_SUCCEEDED(result)) {
|
||||
*out_err = err;
|
||||
*out_res = res;
|
||||
} else {
|
||||
*out_err = ConvertResultToErrorCode(result);
|
||||
*out_res = -1;
|
||||
}
|
||||
}
|
||||
|
||||
Result HtcsManager::AcceptStart(u32 *out_task_id, os::NativeHandle *out_handle, s32 desc) {
|
||||
R_RETURN(m_impl->AcceptStart(out_task_id, out_handle, desc));
|
||||
}
|
||||
|
||||
void HtcsManager::AcceptResults(s32 *out_err, s32 *out_desc, SockAddrHtcs *out_address, u32 task_id, s32 desc) {
|
||||
/* Invoke our implementation. */
|
||||
s32 err = -1;
|
||||
const Result result = m_impl->AcceptResults(std::addressof(err), out_desc, out_address, task_id, desc);
|
||||
|
||||
/* Set output. */
|
||||
if (R_SUCCEEDED(result)) {
|
||||
*out_err = err;
|
||||
} else {
|
||||
if (htc::ResultCancelled::Includes(result)) {
|
||||
*out_err = HTCS_ENETDOWN;
|
||||
} else if (htc::ResultTaskQueueNotAvailable::Includes(result)) {
|
||||
*out_err = HTCS_EINTR;
|
||||
} else {
|
||||
*out_err = ConvertResultToErrorCode(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Result HtcsManager::RecvStart(u32 *out_task_id, os::NativeHandle *out_handle, s64 size, s32 desc, s32 flags) {
|
||||
R_RETURN(m_impl->RecvStart(out_task_id, out_handle, size, desc, flags));
|
||||
}
|
||||
|
||||
void HtcsManager::RecvResults(s32 *out_err, s64 *out_size, char *buffer, s64 buffer_size, u32 task_id, s32 desc) {
|
||||
/* Invoke our implementation. */
|
||||
s32 err = -1;
|
||||
s64 size = -1;
|
||||
const Result result = m_impl->RecvResults(std::addressof(err), std::addressof(size), buffer, buffer_size, task_id, desc);
|
||||
|
||||
/* Set output. */
|
||||
if (R_SUCCEEDED(result)) {
|
||||
*out_err = err;
|
||||
if (err == 0) {
|
||||
*out_size = size;
|
||||
} else {
|
||||
*out_size = -1;
|
||||
}
|
||||
} else {
|
||||
if (htc::ResultTaskQueueNotAvailable::Includes(result)) {
|
||||
*out_err = HTCS_EINTR;
|
||||
} else {
|
||||
*out_err = ConvertResultToErrorCode(result);
|
||||
}
|
||||
*out_size = -1;
|
||||
}
|
||||
}
|
||||
|
||||
Result HtcsManager::SendStart(u32 *out_task_id, os::NativeHandle *out_handle, const char *buffer, s64 size, s32 desc, s32 flags) {
|
||||
R_RETURN(m_impl->SendStart(out_task_id, out_handle, buffer, size, desc, flags));
|
||||
}
|
||||
|
||||
Result HtcsManager::SendLargeStart(u32 *out_task_id, os::NativeHandle *out_handle, const char **buffers, const s64 *sizes, s32 count, s32 desc, s32 flags) {
|
||||
R_RETURN(m_impl->SendLargeStart(out_task_id, out_handle, buffers, sizes, count, desc, flags));
|
||||
}
|
||||
|
||||
void HtcsManager::SendResults(s32 *out_err, s64 *out_size, u32 task_id, s32 desc) {
|
||||
/* Invoke our implementation. */
|
||||
s32 err = -1;
|
||||
s64 size = -1;
|
||||
const Result result = m_impl->SendResults(std::addressof(err), std::addressof(size), task_id, desc);
|
||||
|
||||
/* Set output. */
|
||||
if (R_SUCCEEDED(result)) {
|
||||
*out_err = err;
|
||||
if (err == 0) {
|
||||
*out_size = size;
|
||||
} else {
|
||||
*out_size = -1;
|
||||
}
|
||||
} else {
|
||||
if (htc::ResultTaskQueueNotAvailable::Includes(result)) {
|
||||
*out_err = HTCS_EINTR;
|
||||
} else {
|
||||
*out_err = ConvertResultToErrorCode(result);
|
||||
}
|
||||
*out_size = -1;
|
||||
}
|
||||
}
|
||||
|
||||
Result HtcsManager::StartSend(u32 *out_task_id, os::NativeHandle *out_handle, s32 desc, s64 size, s32 flags) {
|
||||
R_RETURN(m_impl->StartSend(out_task_id, out_handle, desc, size, flags));
|
||||
}
|
||||
|
||||
Result HtcsManager::ContinueSend(s64 *out_size, const char *buffer, s64 buffer_size, u32 task_id, s32 desc) {
|
||||
/* Invoke our implementation. */
|
||||
s64 size = -1;
|
||||
R_TRY_CATCH(m_impl->ContinueSend(std::addressof(size), buffer, buffer_size, task_id, desc)) {
|
||||
R_CONVERT(htclow::ResultInvalidChannelState, tma::ResultUnknown())
|
||||
R_CONVERT(htc::ResultTaskCancelled, tma::ResultUnknown())
|
||||
} R_END_TRY_CATCH;
|
||||
|
||||
/* Set output. */
|
||||
*out_size = size;
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
void HtcsManager::EndSend(s32 *out_err, s64 *out_size, u32 task_id, s32 desc) {
|
||||
/* Invoke our implementation. */
|
||||
s32 err = -1;
|
||||
s64 size = -1;
|
||||
const Result result = m_impl->EndSend(std::addressof(err), std::addressof(size), task_id, desc);
|
||||
|
||||
/* Set output. */
|
||||
if (R_SUCCEEDED(result)) {
|
||||
*out_err = err;
|
||||
if (err == 0) {
|
||||
*out_size = size;
|
||||
} else {
|
||||
*out_size = -1;
|
||||
}
|
||||
} else {
|
||||
if (htc::ResultTaskQueueNotAvailable::Includes(result)) {
|
||||
*out_err = HTCS_EINTR;
|
||||
} else {
|
||||
*out_err = ConvertResultToErrorCode(result);
|
||||
}
|
||||
*out_size = -1;
|
||||
}
|
||||
}
|
||||
|
||||
Result HtcsManager::StartRecv(u32 *out_task_id, os::NativeHandle *out_handle, s64 size, s32 desc, s32 flags) {
|
||||
R_RETURN(m_impl->StartRecv(out_task_id, out_handle, size, desc, flags));
|
||||
}
|
||||
|
||||
void HtcsManager::EndRecv(s32 *out_err, s64 *out_size, char *buffer, s64 buffer_size, u32 task_id, s32 desc) {
|
||||
/* Invoke our implementation. */
|
||||
s32 err = -1;
|
||||
s64 size = -1;
|
||||
const Result result = m_impl->EndRecv(std::addressof(err), std::addressof(size), buffer, buffer_size, task_id, desc);
|
||||
|
||||
/* Set output. */
|
||||
if (R_SUCCEEDED(result)) {
|
||||
*out_err = err;
|
||||
if (err == 0) {
|
||||
*out_size = size;
|
||||
} else {
|
||||
*out_size = -1;
|
||||
}
|
||||
} else {
|
||||
if (htc::ResultCancelled::Includes(result) || htc::ResultTaskQueueNotAvailable::Includes(result)) {
|
||||
*out_err = 0;
|
||||
} else {
|
||||
*out_err = ConvertResultToErrorCode(result);
|
||||
}
|
||||
*out_size = -1;
|
||||
}
|
||||
}
|
||||
|
||||
Result HtcsManager::StartSelect(u32 *out_task_id, os::NativeHandle *out_handle, Span<const int> read_handles, Span<const int> write_handles, Span<const int> exception_handles, s64 tv_sec, s64 tv_usec) {
|
||||
/* Invoke our implementation. */
|
||||
R_TRY_CATCH(m_impl->StartSelect(out_task_id, out_handle, read_handles, write_handles, exception_handles, tv_sec, tv_usec)) {
|
||||
R_CONVERT(htc::ResultTaskCancelled, tma::ResultUnknown())
|
||||
} R_END_TRY_CATCH;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result HtcsManager::EndSelect(s32 *out_err, s32 *out_count, Span<int> read_handles, Span<int> write_handles, Span<int> exception_handles, u32 task_id) {
|
||||
/* Invoke our implementation. */
|
||||
s32 err = -1;
|
||||
bool empty = false;
|
||||
const Result result = m_impl->EndSelect(std::addressof(err), std::addressof(empty), read_handles, write_handles, exception_handles, task_id);
|
||||
|
||||
/* Set output. */
|
||||
if (R_SUCCEEDED(result) && !empty) {
|
||||
*out_err = err;
|
||||
if (err == 0) {
|
||||
const auto num_read = std::count_if(read_handles.begin(), read_handles.end(), [](int handle) { return handle != 0; });
|
||||
const auto num_write = std::count_if(write_handles.begin(), write_handles.end(), [](int handle) { return handle != 0; });
|
||||
const auto num_exception = std::count_if(exception_handles.begin(), exception_handles.end(), [](int handle) { return handle != 0; });
|
||||
*out_count = num_read + num_write + num_exception;
|
||||
} else {
|
||||
*out_count = -1;
|
||||
}
|
||||
} else {
|
||||
if (R_SUCCEEDED(result)) {
|
||||
*out_err = 0;
|
||||
*out_count = 0;
|
||||
} else {
|
||||
*out_err = ConvertResultToErrorCode(err);
|
||||
*out_count = -1;
|
||||
}
|
||||
}
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <stratosphere.hpp>
|
||||
#include "../../htclow/htclow_manager.hpp"
|
||||
|
||||
namespace ams::htcs::impl {
|
||||
|
||||
class HtcsManagerImpl;
|
||||
|
||||
class HtcsManager {
|
||||
private:
|
||||
mem::StandardAllocator *m_allocator;
|
||||
HtcsManagerImpl *m_impl;
|
||||
public:
|
||||
HtcsManager(mem::StandardAllocator *allocator, htclow::HtclowManager *htclow_manager);
|
||||
~HtcsManager();
|
||||
public:
|
||||
os::EventType *GetServiceAvailabilityEvent();
|
||||
|
||||
bool IsServiceAvailable();
|
||||
public:
|
||||
void Socket(s32 *out_err, s32 *out_desc, bool enable_disconnection_emulation);
|
||||
void Close(s32 *out_err, s32 *out_res, s32 desc);
|
||||
void Connect(s32 *out_err, s32 *out_res, const SockAddrHtcs &address, s32 desc);
|
||||
void Bind(s32 *out_err, s32 *out_res, const SockAddrHtcs &address, s32 desc);
|
||||
void Listen(s32 *out_err, s32 *out_res, s32 backlog_count, s32 desc);
|
||||
void Recv(s32 *out_err, s64 *out_size, char *buffer, size_t size, s32 flags, s32 desc);
|
||||
void Send(s32 *out_err, s64 *out_size, const char *buffer, size_t size, s32 flags, s32 desc);
|
||||
void Shutdown(s32 *out_err, s32 *out_res, s32 how, s32 desc);
|
||||
void Fcntl(s32 *out_err, s32 *out_res, s32 command, s32 value, s32 desc);
|
||||
|
||||
Result AcceptStart(u32 *out_task_id, os::NativeHandle *out_handle, s32 desc);
|
||||
void AcceptResults(s32 *out_err, s32 *out_desc, SockAddrHtcs *out_address, u32 task_id, s32 desc);
|
||||
|
||||
Result RecvStart(u32 *out_task_id, os::NativeHandle *out_handle, s64 size, s32 desc, s32 flags);
|
||||
void RecvResults(s32 *out_err, s64 *out_size, char *buffer, s64 buffer_size, u32 task_id, s32 desc);
|
||||
|
||||
Result SendStart(u32 *out_task_id, os::NativeHandle *out_handle, const char *buffer, s64 size, s32 desc, s32 flags);
|
||||
Result SendLargeStart(u32 *out_task_id, os::NativeHandle *out_handle, const char **buffers, const s64 *sizes, s32 count, s32 desc, s32 flags);
|
||||
void SendResults(s32 *out_err, s64 *out_size, u32 task_id, s32 desc);
|
||||
|
||||
Result StartSend(u32 *out_task_id, os::NativeHandle *out_handle, s32 desc, s64 size, s32 flags);
|
||||
Result ContinueSend(s64 *out_size, const char *buffer, s64 buffer_size, u32 task_id, s32 desc);
|
||||
void EndSend(s32 *out_err, s64 *out_size, u32 task_id, s32 desc);
|
||||
|
||||
Result StartRecv(u32 *out_task_id, os::NativeHandle *out_handle, s64 size, s32 desc, s32 flags);
|
||||
void EndRecv(s32 *out_err, s64 *out_size, char *buffer, s64 buffer_size, u32 task_id, s32 desc);
|
||||
|
||||
Result StartSelect(u32 *out_task_id, os::NativeHandle *out_handle, Span<const int> read_handles, Span<const int> write_handles, Span<const int> exception_handles, s64 tv_sec, s64 tv_usec);
|
||||
Result EndSelect(s32 *out_err, s32 *out_count, Span<int> read_handles, Span<int> write_handles, Span<int> exception_handles, u32 task_id);
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_manager.hpp"
|
||||
|
||||
namespace ams::htcs::impl::HtcsManagerHolder {
|
||||
|
||||
namespace {
|
||||
|
||||
constinit os::SdkMutex g_holder_mutex;
|
||||
constinit int g_holder_reference_count = 0;
|
||||
|
||||
mem::StandardAllocator g_allocator;
|
||||
|
||||
constinit HtcsManager *g_manager = nullptr;
|
||||
|
||||
alignas(os::MemoryPageSize) u8 g_heap_buffer[416_KB];
|
||||
|
||||
}
|
||||
|
||||
void AddReference() {
|
||||
std::scoped_lock lk(g_holder_mutex);
|
||||
|
||||
if ((g_holder_reference_count++) == 0) {
|
||||
/* Add reference to the htclow manager. */
|
||||
htclow::HtclowManagerHolder::AddReference();
|
||||
|
||||
/* Initialize the allocator for the manager. */
|
||||
g_allocator.Initialize(g_heap_buffer, sizeof(g_heap_buffer));
|
||||
|
||||
/* Allocate the manager. */
|
||||
g_manager = static_cast<HtcsManager *>(g_allocator.Allocate(sizeof(HtcsManager), alignof(HtcsManager)));
|
||||
|
||||
/* Construct the manager. */
|
||||
std::construct_at(g_manager, std::addressof(g_allocator), htclow::HtclowManagerHolder::GetHtclowManager());
|
||||
}
|
||||
|
||||
AMS_ASSERT(g_holder_reference_count > 0);
|
||||
}
|
||||
|
||||
void Release() {
|
||||
std::scoped_lock lk(g_holder_mutex);
|
||||
|
||||
AMS_ASSERT(g_holder_reference_count > 0);
|
||||
|
||||
if ((--g_holder_reference_count) == 0) {
|
||||
/* Destroy the manager. */
|
||||
std::destroy_at(g_manager);
|
||||
g_allocator.Free(g_manager);
|
||||
g_manager = nullptr;
|
||||
|
||||
/* Finalize the allocator. */
|
||||
g_allocator.Finalize();
|
||||
|
||||
/* Release reference to the htclow manager. */
|
||||
htclow::HtclowManagerHolder::Release();
|
||||
}
|
||||
}
|
||||
|
||||
HtcsManager *GetHtcsManager() {
|
||||
std::scoped_lock lk(g_holder_mutex);
|
||||
|
||||
return g_manager;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,189 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_manager.hpp"
|
||||
#include "htcs_manager_impl.hpp"
|
||||
|
||||
namespace ams::htcs::impl {
|
||||
|
||||
HtcsManagerImpl::HtcsManagerImpl(mem::StandardAllocator *allocator, htclow::HtclowManager *htclow_manager)
|
||||
: m_allocator(allocator),
|
||||
m_driver(htclow_manager, htclow::ModuleId::Htcs),
|
||||
m_driver_manager(std::addressof(m_driver)),
|
||||
m_rpc_client(m_allocator, std::addressof(m_driver), HtcsClientChannelId),
|
||||
m_data_channel_manager(std::addressof(m_rpc_client), htclow_manager),
|
||||
m_service(m_allocator, m_driver_manager.GetDriver(), std::addressof(m_rpc_client), std::addressof(m_data_channel_manager)),
|
||||
m_monitor(m_allocator, m_driver_manager.GetDriver(), std::addressof(m_rpc_client), std::addressof(m_service))
|
||||
{
|
||||
/* Start the monitor. */
|
||||
m_monitor.Start();
|
||||
}
|
||||
|
||||
HtcsManagerImpl::~HtcsManagerImpl() {
|
||||
/* Cancel our monitor. */
|
||||
m_monitor.Cancel();
|
||||
m_monitor.Wait();
|
||||
}
|
||||
|
||||
os::EventType *HtcsManagerImpl::GetServiceAvailabilityEvent() {
|
||||
return m_monitor.GetServiceAvailabilityEvent();
|
||||
}
|
||||
|
||||
bool HtcsManagerImpl::IsServiceAvailable() {
|
||||
return m_monitor.IsServiceAvailable();
|
||||
}
|
||||
|
||||
Result HtcsManagerImpl::CreateSocket(s32 *out_err, s32 *out_desc, bool enable_disconnection_emulation) {
|
||||
R_RETURN(m_service.CreateSocket(out_err, out_desc, enable_disconnection_emulation));
|
||||
}
|
||||
|
||||
Result HtcsManagerImpl::DestroySocket(s32 desc) {
|
||||
R_RETURN(m_service.DestroySocket(desc));
|
||||
}
|
||||
|
||||
Result HtcsManagerImpl::Connect(s32 *out_err, s32 desc, const SockAddrHtcs &address) {
|
||||
R_RETURN(m_service.Connect(out_err, desc, address));
|
||||
}
|
||||
|
||||
Result HtcsManagerImpl::Bind(s32 *out_err, s32 desc, const SockAddrHtcs &address) {
|
||||
R_RETURN(m_service.Bind(out_err, desc, address));
|
||||
}
|
||||
|
||||
Result HtcsManagerImpl::Listen(s32 *out_err, s32 desc, s32 backlog_count) {
|
||||
R_RETURN(m_service.Listen(out_err, desc, backlog_count));
|
||||
}
|
||||
|
||||
Result HtcsManagerImpl::Receive(s32 *out_err, s64 *out_size, char *buffer, size_t size, s32 desc, s32 flags) {
|
||||
R_RETURN(m_service.Receive(out_err, out_size, buffer, size, desc, flags));
|
||||
}
|
||||
|
||||
Result HtcsManagerImpl::Send(s32 *out_err, s64 *out_size, const char *buffer, size_t size, s32 desc, s32 flags) {
|
||||
R_RETURN(m_service.Send(out_err, out_size, buffer, size, desc, flags));
|
||||
}
|
||||
|
||||
Result HtcsManagerImpl::Shutdown(s32 *out_err, s32 desc, s32 how) {
|
||||
R_RETURN(m_service.Shutdown(out_err, desc, how));
|
||||
}
|
||||
|
||||
Result HtcsManagerImpl::Fcntl(s32 *out_err, s32 *out_res, s32 desc, s32 command, s32 value) {
|
||||
R_RETURN(m_service.Fcntl(out_err, out_res, desc, command, value));
|
||||
}
|
||||
|
||||
Result HtcsManagerImpl::AcceptStart(u32 *out_task_id, os::NativeHandle *out_handle, s32 desc) {
|
||||
R_RETURN(m_service.AcceptStart(out_task_id, out_handle, desc));
|
||||
}
|
||||
|
||||
Result HtcsManagerImpl::AcceptResults(s32 *out_err, s32 *out_desc, SockAddrHtcs *out_address, u32 task_id, s32 desc) {
|
||||
R_RETURN(m_service.AcceptResults(out_err, out_desc, out_address, task_id, desc));
|
||||
}
|
||||
|
||||
Result HtcsManagerImpl::RecvStart(u32 *out_task_id, os::NativeHandle *out_handle, s64 size, s32 desc, s32 flags) {
|
||||
R_RETURN(m_service.ReceiveSmallStart(out_task_id, out_handle, size, desc, flags));
|
||||
}
|
||||
|
||||
Result HtcsManagerImpl::RecvResults(s32 *out_err, s64 *out_size, char *buffer, s64 buffer_size, u32 task_id, s32 desc) {
|
||||
R_RETURN(m_service.ReceiveSmallResults(out_err, out_size, buffer, buffer_size, task_id, desc));
|
||||
}
|
||||
|
||||
Result HtcsManagerImpl::SendStart(u32 *out_task_id, os::NativeHandle *out_handle, const char *buffer, s64 size, s32 desc, s32 flags) {
|
||||
/* Start the send. */
|
||||
u32 task_id{};
|
||||
os::NativeHandle handle;
|
||||
R_TRY(m_service.SendSmallStart(std::addressof(task_id), std::addressof(handle), desc, size, flags));
|
||||
|
||||
/* Continue the send. */
|
||||
s64 continue_size;
|
||||
const Result result = m_service.SendSmallContinue(std::addressof(continue_size), buffer, size, task_id, desc);
|
||||
if (R_SUCCEEDED(result) || htcs::ResultCompleted::Includes(result) || htc::ResultTaskQueueNotAvailable::Includes(result)) {
|
||||
*out_task_id = task_id;
|
||||
*out_handle = handle;
|
||||
|
||||
R_SUCCEED();
|
||||
} else {
|
||||
os::SystemEventType event;
|
||||
os::AttachReadableHandleToSystemEvent(std::addressof(event), handle, true, os::EventClearMode_ManualClear);
|
||||
|
||||
s32 err;
|
||||
s64 rsize;
|
||||
m_service.SendSmallResults(std::addressof(err), std::addressof(rsize), task_id, desc);
|
||||
|
||||
os::DestroySystemEvent(std::addressof(event));
|
||||
|
||||
R_RETURN(result);
|
||||
}
|
||||
}
|
||||
|
||||
Result HtcsManagerImpl::SendLargeStart(u32 *out_task_id, os::NativeHandle *out_handle, const char **buffers, const s64 *sizes, s32 count, s32 desc, s32 flags) {
|
||||
/* NOTE: Nintendo aborts here, too. */
|
||||
AMS_UNUSED(out_task_id, out_handle, buffers, sizes, count, desc, flags);
|
||||
AMS_ABORT("HtcsManagerImpl::SendLargeStart is not implemented");
|
||||
}
|
||||
|
||||
Result HtcsManagerImpl::SendResults(s32 *out_err, s64 *out_size, u32 task_id, s32 desc) {
|
||||
R_RETURN(m_service.SendSmallResults(out_err, out_size, task_id, desc));
|
||||
}
|
||||
|
||||
Result HtcsManagerImpl::StartSend(u32 *out_task_id, os::NativeHandle *out_handle, s32 desc, s64 size, s32 flags) {
|
||||
R_RETURN(m_service.SendStart(out_task_id, out_handle, desc, size, flags));
|
||||
}
|
||||
|
||||
Result HtcsManagerImpl::ContinueSend(s64 *out_size, const char *buffer, s64 buffer_size, u32 task_id, s32 desc) {
|
||||
R_RETURN(m_service.SendContinue(out_size, buffer, buffer_size, task_id, desc));
|
||||
}
|
||||
|
||||
Result HtcsManagerImpl::EndSend(s32 *out_err, s64 *out_size, u32 task_id, s32 desc) {
|
||||
R_RETURN(m_service.SendResults(out_err, out_size, task_id, desc));
|
||||
}
|
||||
|
||||
Result HtcsManagerImpl::StartRecv(u32 *out_task_id, os::NativeHandle *out_handle, s64 size, s32 desc, s32 flags) {
|
||||
R_RETURN(m_service.ReceiveStart(out_task_id, out_handle, size, desc, flags));
|
||||
}
|
||||
|
||||
Result HtcsManagerImpl::EndRecv(s32 *out_err, s64 *out_size, char *buffer, s64 buffer_size, u32 task_id, s32 desc) {
|
||||
R_RETURN(m_service.ReceiveResults(out_err, out_size, buffer, buffer_size, task_id, desc));
|
||||
}
|
||||
|
||||
Result HtcsManagerImpl::StartSelect(u32 *out_task_id, os::NativeHandle *out_handle, Span<const int> read_handles, Span<const int> write_handles, Span<const int> exception_handles, s64 tv_sec, s64 tv_usec) {
|
||||
/* Start the select. */
|
||||
u32 task_id{};
|
||||
os::NativeHandle handle = os::InvalidNativeHandle;
|
||||
const Result result = m_service.SelectStart(std::addressof(task_id), std::addressof(handle), read_handles, write_handles, exception_handles, tv_sec, tv_usec);
|
||||
|
||||
/* Ensure our state ends up clean. */
|
||||
if (htcs::ResultCancelled::Includes(result)) {
|
||||
s32 err;
|
||||
bool empty;
|
||||
m_service.SelectEnd(std::addressof(err), std::addressof(empty), Span<int>{}, Span<int>{}, Span<int>{}, task_id);
|
||||
|
||||
if (handle != os::InvalidNativeHandle) {
|
||||
os::SystemEventType event;
|
||||
os::AttachReadableHandleToSystemEvent(std::addressof(event), handle, true, os::EventClearMode_ManualClear);
|
||||
|
||||
os::DestroySystemEvent(std::addressof(event));
|
||||
}
|
||||
} else if (R_SUCCEEDED(result)) {
|
||||
*out_task_id = task_id;
|
||||
*out_handle = handle;
|
||||
}
|
||||
|
||||
R_RETURN(result);
|
||||
}
|
||||
|
||||
Result HtcsManagerImpl::EndSelect(s32 *out_err, bool *out_empty, Span<int> read_handles, Span<int> write_handles, Span<int> exception_handles, u32 task_id) {
|
||||
R_RETURN(m_service.SelectEnd(out_err, out_empty, read_handles, write_handles, exception_handles, task_id));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <stratosphere.hpp>
|
||||
#include "../../htclow/htclow_manager.hpp"
|
||||
#include "../../htc/server/driver/htc_htclow_driver.hpp"
|
||||
#include "../../htc/server/driver/htc_driver_manager.hpp"
|
||||
#include "../../htc/server/rpc/htc_rpc_client.hpp"
|
||||
#include "rpc/htcs_data_channel_manager.hpp"
|
||||
#include "htcs_service.hpp"
|
||||
#include "htcs_monitor.hpp"
|
||||
|
||||
namespace ams::htcs::impl {
|
||||
|
||||
class HtcsManagerImpl {
|
||||
private:
|
||||
mem::StandardAllocator *m_allocator;
|
||||
htc::server::driver::HtclowDriver m_driver;
|
||||
htc::server::driver::DriverManager m_driver_manager;
|
||||
htc::server::rpc::RpcClient m_rpc_client;
|
||||
rpc::DataChannelManager m_data_channel_manager;
|
||||
HtcsService m_service;
|
||||
HtcsMonitor m_monitor;
|
||||
public:
|
||||
HtcsManagerImpl(mem::StandardAllocator *allocator, htclow::HtclowManager *htclow_manager);
|
||||
~HtcsManagerImpl();
|
||||
public:
|
||||
os::EventType *GetServiceAvailabilityEvent();
|
||||
|
||||
bool IsServiceAvailable();
|
||||
public:
|
||||
Result CreateSocket(s32 *out_err, s32 *out_desc, bool enable_disconnection_emulation);
|
||||
Result DestroySocket(s32 desc);
|
||||
Result Connect(s32 *out_err, s32 desc, const SockAddrHtcs &address);
|
||||
Result Bind(s32 *out_err, s32 desc, const SockAddrHtcs &address);
|
||||
Result Listen(s32 *out_err, s32 desc, s32 backlog_count);
|
||||
Result Receive(s32 *out_err, s64 *out_size, char *buffer, size_t size, s32 desc, s32 flags);
|
||||
Result Send(s32 *out_err, s64 *out_size, const char *buffer, size_t size, s32 desc, s32 flags);
|
||||
Result Shutdown(s32 *out_err, s32 desc, s32 how);
|
||||
Result Fcntl(s32 *out_err, s32 *out_res, s32 desc, s32 command, s32 value);
|
||||
|
||||
Result AcceptStart(u32 *out_task_id, os::NativeHandle *out_handle, s32 desc);
|
||||
Result AcceptResults(s32 *out_err, s32 *out_desc, SockAddrHtcs *out_address, u32 task_id, s32 desc);
|
||||
|
||||
Result RecvStart(u32 *out_task_id, os::NativeHandle *out_handle, s64 size, s32 desc, s32 flags);
|
||||
Result RecvResults(s32 *out_err, s64 *out_size, char *buffer, s64 buffer_size, u32 task_id, s32 desc);
|
||||
|
||||
Result SendStart(u32 *out_task_id, os::NativeHandle *out_handle, const char *buffer, s64 size, s32 desc, s32 flags);
|
||||
Result SendLargeStart(u32 *out_task_id, os::NativeHandle *out_handle, const char **buffers, const s64 *sizes, s32 count, s32 desc, s32 flags);
|
||||
Result SendResults(s32 *out_err, s64 *out_size, u32 task_id, s32 desc);
|
||||
|
||||
Result StartSend(u32 *out_task_id, os::NativeHandle *out_handle, s32 desc, s64 size, s32 flags);
|
||||
Result ContinueSend(s64 *out_size, const char *buffer, s64 buffer_size, u32 task_id, s32 desc);
|
||||
Result EndSend(s32 *out_err, s64 *out_size, u32 task_id, s32 desc);
|
||||
|
||||
Result StartRecv(u32 *out_task_id, os::NativeHandle *out_handle, s64 size, s32 desc, s32 flags);
|
||||
Result EndRecv(s32 *out_err, s64 *out_size, char *buffer, s64 buffer_size, u32 task_id, s32 desc);
|
||||
|
||||
Result StartSelect(u32 *out_task_id, os::NativeHandle *out_handle, Span<const int> read_handles, Span<const int> write_handles, Span<const int> exception_handles, s64 tv_sec, s64 tv_usec);
|
||||
Result EndSelect(s32 *out_err, bool *out_empty, Span<int> read_handles, Span<int> write_handles, Span<int> exception_handles, u32 task_id);
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,106 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_manager.hpp"
|
||||
#include "htcs_manager_impl.hpp"
|
||||
|
||||
namespace ams::htcs::impl {
|
||||
|
||||
HtcsMonitor::HtcsMonitor(mem::StandardAllocator *allocator, htc::server::driver::IDriver *drv, htc::server::rpc::RpcClient *rc, HtcsService *srv)
|
||||
: m_allocator(allocator),
|
||||
m_driver(drv),
|
||||
m_rpc_client(rc),
|
||||
m_service(srv),
|
||||
m_monitor_thread_stack(m_allocator->Allocate(os::MemoryPageSize, os::ThreadStackAlignment)),
|
||||
m_mutex(),
|
||||
m_cancel_event(os::EventClearMode_ManualClear),
|
||||
m_service_availability_event(os::EventClearMode_ManualClear),
|
||||
m_cancelled(false),
|
||||
m_is_service_available(false)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
HtcsMonitor::~HtcsMonitor() {
|
||||
/* Free thread stack. */
|
||||
m_allocator->Free(m_monitor_thread_stack);
|
||||
}
|
||||
|
||||
void HtcsMonitor::Start() {
|
||||
/* Create the monitor thread. */
|
||||
R_ABORT_UNLESS(os::CreateThread(std::addressof(m_monitor_thread), ThreadEntry, this, m_monitor_thread_stack, os::MemoryPageSize, AMS_GET_SYSTEM_THREAD_PRIORITY(htc, HtcsMonitor)));
|
||||
|
||||
/* Set thread name. */
|
||||
os::SetThreadNamePointer(std::addressof(m_monitor_thread), AMS_GET_SYSTEM_THREAD_NAME(htc, HtcsMonitor));
|
||||
|
||||
/* Start the monitor thread. */
|
||||
os::StartThread(std::addressof(m_monitor_thread));
|
||||
}
|
||||
|
||||
void HtcsMonitor::Cancel() {
|
||||
/* Cancel, and signal. */
|
||||
m_cancelled = true;
|
||||
m_cancel_event.Signal();
|
||||
}
|
||||
|
||||
void HtcsMonitor::Wait() {
|
||||
/* Wait for the thread. */
|
||||
os::WaitThread(std::addressof(m_monitor_thread));
|
||||
os::DestroyThread(std::addressof(m_monitor_thread));
|
||||
}
|
||||
|
||||
void HtcsMonitor::ThreadBody() {
|
||||
/* Loop so long as we're not cancelled. */
|
||||
while (!m_cancelled) {
|
||||
/* Open the rpc client. */
|
||||
m_rpc_client->Open();
|
||||
|
||||
/* Ensure we close, if something goes wrong. */
|
||||
auto client_guard = SCOPE_GUARD { m_rpc_client->Close(); };
|
||||
|
||||
/* Wait for the rpc server. */
|
||||
if (m_rpc_client->WaitAny(htclow::ChannelState_Connectable, m_cancel_event.GetBase()) != 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
/* Start the rpc client. */
|
||||
const Result start_result = m_rpc_client->Start();
|
||||
if (R_FAILED(start_result)) {
|
||||
/* DEBUG */
|
||||
R_ABORT_UNLESS(start_result);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* We're available! */
|
||||
this->SetServiceAvailability(true);
|
||||
client_guard.Cancel();
|
||||
|
||||
/* We're available, so we want to cleanup when we're done. */
|
||||
ON_SCOPE_EXIT {
|
||||
m_rpc_client->Close();
|
||||
m_rpc_client->Cancel();
|
||||
m_rpc_client->Wait();
|
||||
this->SetServiceAvailability(false);
|
||||
};
|
||||
|
||||
/* Wait to become disconnected. */
|
||||
if (m_rpc_client->WaitAny(htclow::ChannelState_Disconnected, m_cancel_event.GetBase()) != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <stratosphere.hpp>
|
||||
#include "../../htc/server/driver/htc_i_driver.hpp"
|
||||
#include "../../htc/server/rpc/htc_rpc_client.hpp"
|
||||
#include "htcs_service.hpp"
|
||||
|
||||
namespace ams::htcs::impl {
|
||||
|
||||
class HtcsMonitor {
|
||||
private:
|
||||
mem::StandardAllocator *m_allocator;
|
||||
htc::server::driver::IDriver *m_driver;
|
||||
htc::server::rpc::RpcClient *m_rpc_client;
|
||||
HtcsService *m_service;
|
||||
void *m_monitor_thread_stack;
|
||||
os::ThreadType m_monitor_thread;
|
||||
os::SdkMutex m_mutex;
|
||||
os::Event m_cancel_event;
|
||||
os::Event m_service_availability_event;
|
||||
bool m_cancelled;
|
||||
bool m_is_service_available;
|
||||
private:
|
||||
static void ThreadEntry(void *arg) {
|
||||
static_cast<HtcsMonitor *>(arg)->ThreadBody();
|
||||
}
|
||||
|
||||
void ThreadBody();
|
||||
public:
|
||||
HtcsMonitor(mem::StandardAllocator *allocator, htc::server::driver::IDriver *drv, htc::server::rpc::RpcClient *rc, HtcsService *srv);
|
||||
~HtcsMonitor();
|
||||
public:
|
||||
void Start();
|
||||
void Cancel();
|
||||
void Wait();
|
||||
|
||||
os::EventType *GetServiceAvailabilityEvent() { return m_service_availability_event.GetBase(); }
|
||||
|
||||
bool IsServiceAvailable() {
|
||||
/* Lock ourselves. */
|
||||
std::scoped_lock lk(m_mutex);
|
||||
|
||||
/* Get availability. */
|
||||
return m_is_service_available;
|
||||
}
|
||||
private:
|
||||
void SetServiceAvailability(bool available) {
|
||||
/* Lock ourselves. */
|
||||
std::scoped_lock lk(m_mutex);
|
||||
|
||||
/* Set availability. */
|
||||
m_is_service_available = available;
|
||||
|
||||
/* Signal availability change. */
|
||||
m_service_availability_event.Signal();
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,413 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_service.hpp"
|
||||
#include "rpc/htcs_rpc_tasks.hpp"
|
||||
#include "htcs_util.hpp"
|
||||
|
||||
namespace ams::htcs::impl {
|
||||
|
||||
void HtcsService::WaitTask(u32 task_id) {
|
||||
return m_rpc_client->Wait(task_id);
|
||||
}
|
||||
|
||||
Result HtcsService::CreateSocket(s32 *out_err, s32 *out_desc, bool enable_disconnection_emulation) {
|
||||
/* Set disconnection emulation enabled. */
|
||||
m_driver->SetDisconnectionEmulationEnabled(enable_disconnection_emulation);
|
||||
|
||||
/* Begin the task. */
|
||||
u32 task_id{};
|
||||
R_TRY(m_rpc_client->Begin<rpc::SocketTask>(std::addressof(task_id)));
|
||||
|
||||
/* Wait for the task to complete. */
|
||||
this->WaitTask(task_id);
|
||||
|
||||
/* Finish the task. */
|
||||
htcs::SocketError err;
|
||||
s32 desc;
|
||||
R_TRY(m_rpc_client->End<rpc::SocketTask>(task_id, std::addressof(err), std::addressof(desc)));
|
||||
|
||||
/* Set output. */
|
||||
*out_err = err;
|
||||
*out_desc = desc;
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result HtcsService::DestroySocket(s32 desc) {
|
||||
/* Begin the task. */
|
||||
u32 task_id{};
|
||||
R_TRY(m_rpc_client->Begin<rpc::CloseTask>(std::addressof(task_id), desc));
|
||||
|
||||
/* Wait for the task to complete. */
|
||||
this->WaitTask(task_id);
|
||||
|
||||
/* Cancel the socket. */
|
||||
m_rpc_client->CancelBySocket(desc);
|
||||
|
||||
/* Finish the task. */
|
||||
htcs::SocketError err;
|
||||
R_TRY(m_rpc_client->End<rpc::CloseTask>(task_id, std::addressof(err)));
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result HtcsService::Connect(s32 *out_err, s32 desc, const SockAddrHtcs &address) {
|
||||
/* Validate the address. */
|
||||
R_UNLESS(address.family == 0, htcs::ResultInvalidArgument());
|
||||
R_UNLESS(IsValidName(address.peer_name), htcs::ResultInvalidArgument());
|
||||
R_UNLESS(IsValidName(address.port_name), htcs::ResultInvalidArgument());
|
||||
|
||||
/* Begin the task. */
|
||||
u32 task_id{};
|
||||
R_TRY(m_rpc_client->Begin<rpc::ConnectTask>(std::addressof(task_id), desc, address.peer_name, address.port_name));
|
||||
|
||||
/* Wait for the task to complete. */
|
||||
this->WaitTask(task_id);
|
||||
|
||||
/* Finish the task. */
|
||||
htcs::SocketError err;
|
||||
R_TRY(m_rpc_client->End<rpc::ConnectTask>(task_id, std::addressof(err)));
|
||||
|
||||
/* Set output. */
|
||||
*out_err = err;
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result HtcsService::Bind(s32 *out_err, s32 desc, const SockAddrHtcs &address) {
|
||||
/* Validate the address. */
|
||||
R_UNLESS(address.family == 0, htcs::ResultInvalidArgument());
|
||||
R_UNLESS(IsValidName(address.peer_name), htcs::ResultInvalidArgument());
|
||||
R_UNLESS(IsValidName(address.port_name), htcs::ResultInvalidArgument());
|
||||
|
||||
/* Begin the task. */
|
||||
u32 task_id{};
|
||||
R_TRY(m_rpc_client->Begin<rpc::BindTask>(std::addressof(task_id), desc, address.peer_name, address.port_name));
|
||||
|
||||
/* Wait for the task to complete. */
|
||||
this->WaitTask(task_id);
|
||||
|
||||
/* Finish the task. */
|
||||
htcs::SocketError err;
|
||||
R_TRY(m_rpc_client->End<rpc::BindTask>(task_id, std::addressof(err)));
|
||||
|
||||
/* Set output. */
|
||||
*out_err = err;
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result HtcsService::Listen(s32 *out_err, s32 desc, s32 backlog_count) {
|
||||
/* Begin the task. */
|
||||
u32 task_id{};
|
||||
R_TRY(m_rpc_client->Begin<rpc::ListenTask>(std::addressof(task_id), desc, backlog_count));
|
||||
|
||||
/* Wait for the task to complete. */
|
||||
this->WaitTask(task_id);
|
||||
|
||||
/* Finish the task. */
|
||||
htcs::SocketError err;
|
||||
R_TRY(m_rpc_client->End<rpc::ListenTask>(task_id, std::addressof(err)));
|
||||
|
||||
/* Set output. */
|
||||
*out_err = err;
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result HtcsService::Receive(s32 *out_err, s64 *out_size, char *buffer, s64 size, s32 desc, s32 flags) {
|
||||
/* Begin the task. */
|
||||
u32 task_id{};
|
||||
R_TRY(m_rpc_client->Begin<rpc::ReceiveTask>(std::addressof(task_id), desc, size, static_cast<htcs::MessageFlag>(flags)));
|
||||
|
||||
/* Wait for the task to complete. */
|
||||
this->WaitTask(task_id);
|
||||
|
||||
/* Finish the task. */
|
||||
R_TRY(this->ReceiveResults(out_err, out_size, buffer, size, task_id, desc));
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result HtcsService::Send(s32 *out_err, s64 *out_size, const char *buffer, s64 size, s32 desc, s32 flags) {
|
||||
/* Begin the task. */
|
||||
u32 task_id{};
|
||||
R_TRY(m_rpc_client->Begin<rpc::SendTask>(std::addressof(task_id), desc, size, static_cast<htcs::MessageFlag>(flags)));
|
||||
|
||||
/* Send the data. */
|
||||
s64 cont_size;
|
||||
const Result result = this->SendContinue(std::addressof(cont_size), buffer, size, task_id, desc);
|
||||
if (R_FAILED(result)) {
|
||||
R_RETURN(this->SendResults(out_err, out_size, task_id, desc));
|
||||
}
|
||||
|
||||
/* Wait for the task to complete. */
|
||||
this->WaitTask(task_id);
|
||||
|
||||
/* Finish the task. */
|
||||
R_TRY(this->SendResults(out_err, out_size, task_id, desc));
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result HtcsService::Shutdown(s32 *out_err, s32 desc, s32 how) {
|
||||
/* Begin the task. */
|
||||
u32 task_id{};
|
||||
R_TRY(m_rpc_client->Begin<rpc::ShutdownTask>(std::addressof(task_id), desc, static_cast<htcs::ShutdownType>(how)));
|
||||
|
||||
/* Wait for the task to complete. */
|
||||
this->WaitTask(task_id);
|
||||
|
||||
/* Finish the task. */
|
||||
htcs::SocketError err;
|
||||
R_TRY(m_rpc_client->End<rpc::ShutdownTask>(task_id, std::addressof(err)));
|
||||
|
||||
/* Set output. */
|
||||
*out_err = err;
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result HtcsService::Fcntl(s32 *out_err, s32 *out_res, s32 desc, s32 command, s32 value) {
|
||||
/* Begin the task. */
|
||||
u32 task_id{};
|
||||
R_TRY(m_rpc_client->Begin<rpc::FcntlTask>(std::addressof(task_id), desc, command, value));
|
||||
|
||||
/* Wait for the task to complete. */
|
||||
this->WaitTask(task_id);
|
||||
|
||||
/* Finish the task. */
|
||||
htcs::SocketError err;
|
||||
s32 res;
|
||||
R_TRY(m_rpc_client->End<rpc::FcntlTask>(task_id, std::addressof(err), std::addressof(res)));
|
||||
|
||||
/* Set output. */
|
||||
*out_err = err;
|
||||
*out_res = res;
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result HtcsService::AcceptStart(u32 *out_task_id, os::NativeHandle *out_handle, s32 desc) {
|
||||
/* Begin the task. */
|
||||
u32 task_id{};
|
||||
R_TRY(m_rpc_client->Begin<rpc::AcceptTask>(std::addressof(task_id), desc));
|
||||
|
||||
/* Detach the task. */
|
||||
*out_task_id = task_id;
|
||||
*out_handle = m_rpc_client->DetachReadableHandle(task_id);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result HtcsService::AcceptResults(s32 *out_err, s32 *out_desc, SockAddrHtcs *out_address, u32 task_id, s32 desc) {
|
||||
AMS_UNUSED(out_address);
|
||||
|
||||
/* Finish the task. */
|
||||
htcs::SocketError err;
|
||||
s32 ret_desc;
|
||||
R_TRY(m_rpc_client->End<rpc::AcceptTask>(task_id, std::addressof(err), std::addressof(ret_desc), desc));
|
||||
|
||||
/* Set output. */
|
||||
*out_err = err;
|
||||
*out_desc = ret_desc;
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result HtcsService::ReceiveSmallStart(u32 *out_task_id, os::NativeHandle *out_handle, s64 size, s32 desc, s32 flags) {
|
||||
/* Begin the task. */
|
||||
u32 task_id{};
|
||||
R_TRY(m_rpc_client->Begin<rpc::ReceiveSmallTask>(std::addressof(task_id), desc, size, static_cast<htcs::MessageFlag>(flags)));
|
||||
|
||||
/* Detach the task. */
|
||||
*out_task_id = task_id;
|
||||
*out_handle = m_rpc_client->DetachReadableHandle(task_id);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result HtcsService::ReceiveSmallResults(s32 *out_err, s64 *out_size, char *buffer, s64 buffer_size, u32 task_id, s32 desc) {
|
||||
AMS_UNUSED(desc);
|
||||
|
||||
/* Continue the task. */
|
||||
m_rpc_client->ReceiveContinue<rpc::ReceiveSmallTask>(task_id, buffer, buffer_size);
|
||||
|
||||
/* Finish the task. */
|
||||
htcs::SocketError err;
|
||||
R_TRY(m_rpc_client->End<rpc::ReceiveSmallTask>(task_id, std::addressof(err), out_size));
|
||||
|
||||
/* Set output. */
|
||||
*out_err = err;
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result HtcsService::SendSmallStart(u32 *out_task_id, os::NativeHandle *out_handle, s32 desc, s64 size, s32 flags) {
|
||||
/* Begin the task. */
|
||||
u32 task_id{};
|
||||
R_TRY(m_rpc_client->Begin<rpc::SendSmallTask>(std::addressof(task_id), desc, size, static_cast<htcs::MessageFlag>(flags)));
|
||||
|
||||
/* Detach the task. */
|
||||
*out_task_id = task_id;
|
||||
*out_handle = m_rpc_client->DetachReadableHandle(task_id);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
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->VerifyTaskIdWithHandle<rpc::SendSmallTask>(task_id, desc));
|
||||
|
||||
/* Continue the task. */
|
||||
R_TRY(m_rpc_client->SendContinue<rpc::SendSmallTask>(task_id, buffer, buffer_size));
|
||||
|
||||
/* Set output. */
|
||||
*out_size = buffer_size;
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result HtcsService::SendSmallResults(s32 *out_err, s64 *out_size, u32 task_id, s32 desc) {
|
||||
AMS_UNUSED(desc);
|
||||
|
||||
/* Finish the task. */
|
||||
htcs::SocketError err;
|
||||
R_TRY(m_rpc_client->End<rpc::SendSmallTask>(task_id, std::addressof(err), out_size));
|
||||
|
||||
/* Set output. */
|
||||
*out_err = err;
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result HtcsService::SendStart(u32 *out_task_id, os::NativeHandle *out_handle, s32 desc, s64 size, s32 flags) {
|
||||
/* Begin the task. */
|
||||
u32 task_id{};
|
||||
R_TRY(m_rpc_client->Begin<rpc::SendTask>(std::addressof(task_id), desc, size, static_cast<htcs::MessageFlag>(flags)));
|
||||
|
||||
/* Detach the task. */
|
||||
*out_task_id = task_id;
|
||||
*out_handle = m_rpc_client->DetachReadableHandle(task_id);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
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->VerifyTaskIdWithHandle<rpc::SendTask>(task_id, desc));
|
||||
|
||||
/* Wait for the task to notify. */
|
||||
m_rpc_client->WaitNotification<rpc::SendTask>(task_id);
|
||||
|
||||
/* Check the task status. */
|
||||
R_UNLESS(!m_rpc_client->IsCompleted<rpc::SendTask>(task_id), htcs::ResultCompleted());
|
||||
R_UNLESS(!m_rpc_client->IsCancelled<rpc::SendTask>(task_id), htcs::ResultCancelled());
|
||||
|
||||
/* Send the data. */
|
||||
if (buffer_size > 0) {
|
||||
R_TRY(m_data_channel_manager->Send(buffer, buffer_size, task_id));
|
||||
}
|
||||
|
||||
/* Set output. */
|
||||
*out_size = buffer_size;
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result HtcsService::SendResults(s32 *out_err, s64 *out_size, u32 task_id, s32 desc) {
|
||||
/* Verify the task. */
|
||||
R_TRY(m_rpc_client->VerifyTaskIdWithHandle<rpc::SendTask>(task_id, desc));
|
||||
|
||||
/* Finish the task. */
|
||||
htcs::SocketError err;
|
||||
R_TRY(m_rpc_client->End<rpc::SendTask>(task_id, std::addressof(err), out_size));
|
||||
|
||||
/* Set output. */
|
||||
*out_err = err;
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result HtcsService::ReceiveStart(u32 *out_task_id, os::NativeHandle *out_handle, s64 size, s32 desc, s32 flags) {
|
||||
/* Begin the task. */
|
||||
u32 task_id{};
|
||||
R_TRY(m_rpc_client->Begin<rpc::ReceiveTask>(std::addressof(task_id), desc, size, static_cast<htcs::MessageFlag>(flags)));
|
||||
|
||||
/* Detach the task. */
|
||||
*out_task_id = task_id;
|
||||
*out_handle = m_rpc_client->DetachReadableHandle(task_id);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
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->VerifyTaskIdWithHandle<rpc::ReceiveTask>(task_id, desc));
|
||||
|
||||
/* Get the result. */
|
||||
htcs::SocketError err{};
|
||||
s64 recv_size{};
|
||||
const Result result = m_rpc_client->GetResult<rpc::ReceiveTask>(task_id, std::addressof(err), std::addressof(recv_size));
|
||||
if (R_FAILED(result) || err != HTCS_ENONE) {
|
||||
/* Finish the task. */
|
||||
R_TRY(m_rpc_client->End<rpc::ReceiveTask>(task_id, std::addressof(err), out_size));
|
||||
|
||||
/* Set output. */
|
||||
*out_err = err;
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
/* Check the size. */
|
||||
R_UNLESS(recv_size <= buffer_size, htcs::ResultInvalidArgument());
|
||||
|
||||
/* Perform remaining processing. */
|
||||
if (recv_size > 0) {
|
||||
/* Receive data. */
|
||||
const Result recv_result = m_data_channel_manager->Receive(buffer, recv_size, task_id);
|
||||
|
||||
/* Finish the task. */
|
||||
R_TRY(m_rpc_client->End<rpc::ReceiveTask>(task_id, std::addressof(err), out_size));
|
||||
|
||||
/* Check that our receive succeeded. */
|
||||
R_TRY(recv_result);
|
||||
} else {
|
||||
/* Finish the task. */
|
||||
R_TRY(m_rpc_client->End<rpc::ReceiveTask>(task_id, std::addressof(err), out_size));
|
||||
}
|
||||
|
||||
/* Set output. */
|
||||
*out_err = err;
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result HtcsService::SelectStart(u32 *out_task_id, os::NativeHandle *out_handle, Span<const int> read_handles, Span<const int> write_handles, Span<const int> exception_handles, s64 tv_sec, s64 tv_usec) {
|
||||
/* Begin the task. */
|
||||
u32 task_id{};
|
||||
R_TRY(m_rpc_client->Begin<rpc::SelectTask>(std::addressof(task_id), read_handles, write_handles, exception_handles, tv_sec, tv_usec));
|
||||
|
||||
/* Detach the task. */
|
||||
*out_task_id = task_id;
|
||||
*out_handle = m_rpc_client->DetachReadableHandle(task_id);
|
||||
|
||||
/* Check that the task isn't cancelled. */
|
||||
R_UNLESS(!m_rpc_client->IsCancelled<rpc::SelectTask>(task_id), htcs::ResultCancelled());
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result HtcsService::SelectEnd(s32 *out_err, bool *out_empty, Span<int> read_handles, Span<int> write_handles, Span<int> exception_handles, u32 task_id) {
|
||||
/* Finish the task. */
|
||||
htcs::SocketError err;
|
||||
bool empty;
|
||||
R_TRY(m_rpc_client->End<rpc::SelectTask>(task_id, std::addressof(err), std::addressof(empty), read_handles, write_handles, exception_handles));
|
||||
|
||||
/* Set output. */
|
||||
*out_err = err;
|
||||
*out_empty = empty;
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <stratosphere.hpp>
|
||||
#include "../../htc/server/driver/htc_i_driver.hpp"
|
||||
#include "../../htc/server/rpc/htc_rpc_client.hpp"
|
||||
#include "rpc/htcs_data_channel_manager.hpp"
|
||||
|
||||
namespace ams::htcs::impl {
|
||||
|
||||
class HtcsService {
|
||||
private:
|
||||
mem::StandardAllocator *m_allocator;
|
||||
htc::server::driver::IDriver *m_driver;
|
||||
htc::server::rpc::RpcClient *m_rpc_client;
|
||||
rpc::DataChannelManager *m_data_channel_manager;
|
||||
public:
|
||||
HtcsService(mem::StandardAllocator *allocator, htc::server::driver::IDriver *drv, htc::server::rpc::RpcClient *rc, rpc::DataChannelManager *dcm)
|
||||
: m_allocator(allocator), m_driver(drv), m_rpc_client(rc), m_data_channel_manager(dcm) { /* ... */ }
|
||||
public:
|
||||
Result CreateSocket(s32 *out_err, s32 *out_desc, bool enable_disconnection_emulation);
|
||||
Result DestroySocket(s32 desc);
|
||||
Result Connect(s32 *out_err, s32 desc, const SockAddrHtcs &address);
|
||||
Result Bind(s32 *out_err, s32 desc, const SockAddrHtcs &address);
|
||||
Result Listen(s32 *out_err, s32 desc, s32 backlog_count);
|
||||
Result Receive(s32 *out_err, s64 *out_size, char *buffer, s64 size, s32 desc, s32 flags);
|
||||
Result Send(s32 *out_err, s64 *out_size, const char *buffer, s64 size, s32 desc, s32 flags);
|
||||
Result Shutdown(s32 *out_err, s32 desc, s32 how);
|
||||
Result Fcntl(s32 *out_err, s32 *out_res, s32 desc, s32 command, s32 value);
|
||||
|
||||
Result AcceptStart(u32 *out_task_id, os::NativeHandle *out_handle, s32 desc);
|
||||
Result AcceptResults(s32 *out_err, s32 *out_desc, SockAddrHtcs *out_address, u32 task_id, s32 desc);
|
||||
|
||||
Result ReceiveSmallStart(u32 *out_task_id, os::NativeHandle *out_handle, s64 size, s32 desc, s32 flags);
|
||||
Result ReceiveSmallResults(s32 *out_err, s64 *out_size, char *buffer, s64 buffer_size, u32 task_id, s32 desc);
|
||||
|
||||
Result SendSmallStart(u32 *out_task_id, os::NativeHandle *out_handle, s32 desc, s64 size, s32 flags);
|
||||
Result SendSmallContinue(s64 *out_size, const char *buffer, s64 buffer_size, u32 task_id, s32 desc);
|
||||
Result SendSmallResults(s32 *out_err, s64 *out_size, u32 task_id, s32 desc);
|
||||
|
||||
Result SendStart(u32 *out_task_id, os::NativeHandle *out_handle, s32 desc, s64 size, s32 flags);
|
||||
Result SendContinue(s64 *out_size, const char *buffer, s64 buffer_size, u32 task_id, s32 desc);
|
||||
Result SendResults(s32 *out_err, s64 *out_size, u32 task_id, s32 desc);
|
||||
|
||||
Result ReceiveStart(u32 *out_task_id, os::NativeHandle *out_handle, s64 size, s32 desc, s32 flags);
|
||||
Result ReceiveResults(s32 *out_err, s64 *out_size, char *buffer, s64 buffer_size, u32 task_id, s32 desc);
|
||||
|
||||
Result SelectStart(u32 *out_task_id, os::NativeHandle *out_handle, Span<const int> read_handles, Span<const int> write_handles, Span<const int> exception_handles, s64 tv_sec, s64 tv_usec);
|
||||
Result SelectEnd(s32 *out_err, bool *out_empty, Span<int> read_handles, Span<int> write_handles, Span<int> exception_handles, u32 task_id);
|
||||
private:
|
||||
void WaitTask(u32 task_id);
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_util.hpp"
|
||||
|
||||
namespace ams::htcs::impl {
|
||||
|
||||
s32 ConvertResultToErrorCode(const Result result) {
|
||||
/* Convert success. */
|
||||
if (R_SUCCEEDED(result)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
R_TRY_CATCH(result) {
|
||||
R_CATCH(htclow::ResultNonBlockingReceiveFailed) { return HTCS_EWOULDBLOCK; }
|
||||
R_CATCH(htcs::ResultInvalidHandle) { return HTCS_EBADF; }
|
||||
R_CATCH(htc::ResultUnknown2001) { return HTCS_EINVAL; }
|
||||
R_CATCH(htc::ResultUnknown2101) { return HTCS_EMFILE; }
|
||||
R_CATCH(htc::ResultTaskCancelled) { return HTCS_EINTR; }
|
||||
R_CATCH(htc::ResultInvalidTaskId) { return HTCS_EINTR; }
|
||||
R_CATCH(htc::ResultCancelled) { return HTCS_EINTR; }
|
||||
R_CATCH(htc::ResultTaskQueueNotAvailable) { return HTCS_ENETDOWN; }
|
||||
R_CATCH(htclow::ResultConnectionFailure) { return HTCS_ENETDOWN; }
|
||||
R_CATCH(htclow::ResultChannelNotExist) { return HTCS_ENOTCONN; }
|
||||
R_CATCH_ALL() { return HTCS_EUNKNOWN; }
|
||||
} R_END_TRY_CATCH_WITH_ABORT_UNLESS;
|
||||
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
namespace ams::htcs::impl {
|
||||
|
||||
s32 ConvertResultToErrorCode(const Result result);
|
||||
|
||||
constexpr bool IsValidName(const char *name) {
|
||||
static_assert(PeerNameBufferLength == PortNameBufferLength);
|
||||
return util::Strnlen(name, PeerNameBufferLength) < PeerNameBufferLength;
|
||||
}
|
||||
|
||||
constexpr bool IsValidName(const HtcsPeerName &name) {
|
||||
return IsValidName(name.name);
|
||||
}
|
||||
|
||||
constexpr bool IsValidName(const HtcsPortName &name) {
|
||||
return IsValidName(name.name);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_rpc_tasks.hpp"
|
||||
#include "htcs_data_channel_manager.hpp"
|
||||
#include "../../../htclow/htclow_channel.hpp"
|
||||
|
||||
namespace ams::htcs::impl::rpc {
|
||||
|
||||
Result DataChannelManager::Receive(void *buffer, s64 buffer_size, u32 task_id) {
|
||||
/* Check that the buffer size is allowable. */
|
||||
R_UNLESS(util::IsIntValueRepresentable<size_t>(buffer_size), htcs::ResultInvalidSize());
|
||||
|
||||
/* Create an htclow channel. */
|
||||
htclow::Channel channel(m_htclow_manager);
|
||||
|
||||
/* Open the channel. */
|
||||
R_ABORT_UNLESS(channel.Open(std::addressof(m_module), GetReceiveDataChannelId(task_id)));
|
||||
|
||||
/* Ensure that we close the channel, when we're done. */
|
||||
ON_SCOPE_EXIT { channel.Close(); };
|
||||
|
||||
/* Set the channel config. */
|
||||
constexpr htclow::ChannelConfig BulkReceiveConfig = {
|
||||
.flow_control_enabled = false,
|
||||
.handshake_enabled = false,
|
||||
.max_packet_size = 0x3E000,
|
||||
};
|
||||
channel.SetConfig(BulkReceiveConfig);
|
||||
|
||||
/* Set the receive buffer. */
|
||||
channel.SetReceiveBuffer(buffer, buffer_size);
|
||||
|
||||
/* Connect the channel. */
|
||||
R_TRY(channel.Connect());
|
||||
|
||||
/* Ensure that we clean up when we're done. */
|
||||
ON_SCOPE_EXIT { channel.Shutdown(); };
|
||||
|
||||
/* Notify the receive task. */
|
||||
R_TRY(m_rpc_client->Notify<ReceiveTask>(task_id));
|
||||
|
||||
/* Wait to receive the data. */
|
||||
R_TRY(channel.WaitReceive(buffer_size));
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result DataChannelManager::Send(const void *buffer, s64 buffer_size, u32 task_id) {
|
||||
/* Check that the buffer size is allowable. */
|
||||
R_UNLESS(util::IsIntValueRepresentable<size_t>(buffer_size), htcs::ResultInvalidSize());
|
||||
|
||||
/* Create an htclow channel. */
|
||||
htclow::Channel channel(m_htclow_manager);
|
||||
|
||||
/* Open the channel. */
|
||||
R_ABORT_UNLESS(channel.Open(std::addressof(m_module), GetSendDataChannelId(task_id)));
|
||||
|
||||
/* Ensure that we close the channel, when we're done. */
|
||||
ON_SCOPE_EXIT { channel.Close(); };
|
||||
|
||||
/* Set the channel config. */
|
||||
constexpr htclow::ChannelConfig BulkSendConfig = {
|
||||
.flow_control_enabled = false,
|
||||
.handshake_enabled = false,
|
||||
.max_packet_size = 0x3E000,
|
||||
};
|
||||
channel.SetConfig(BulkSendConfig);
|
||||
|
||||
/* Set the send buffer. */
|
||||
channel.SetSendBufferWithData(buffer, buffer_size);
|
||||
|
||||
/* Connect the channel. */
|
||||
R_TRY(channel.Connect());
|
||||
|
||||
/* Ensure that we clean up when we're done. */
|
||||
ON_SCOPE_EXIT { channel.Shutdown(); };
|
||||
|
||||
/* Wait to send the data. */
|
||||
R_TRY(channel.Flush());
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <stratosphere.hpp>
|
||||
#include "../../../htclow/htclow_manager.hpp"
|
||||
#include "../../../htc/server/rpc/htc_rpc_client.hpp"
|
||||
|
||||
namespace ams::htcs::impl::rpc {
|
||||
|
||||
class DataChannelManager {
|
||||
private:
|
||||
htc::server::rpc::RpcClient* m_rpc_client;
|
||||
htclow::HtclowManager *m_htclow_manager;
|
||||
htclow::Module m_module;
|
||||
public:
|
||||
DataChannelManager(htc::server::rpc::RpcClient *client, htclow::HtclowManager *htclow_manager) : m_rpc_client(client), m_htclow_manager(htclow_manager), m_module(htclow::ModuleId::Htcs) { /* ... */ }
|
||||
public:
|
||||
Result Receive(void *buffer, s64 buffer_size, u32 task_id);
|
||||
Result Send(const void *buffer, s64 buffer_size, u32 task_id);
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,89 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_rpc_tasks.hpp"
|
||||
|
||||
namespace ams::htcs::impl::rpc {
|
||||
|
||||
Result AcceptTask::SetArguments(s32 server_handle) {
|
||||
/* Check that we're valid. */
|
||||
R_UNLESS(this->IsValid(), htcs::ResultInvalidTask());
|
||||
|
||||
/* Set our arguments. */
|
||||
m_server_handle = server_handle;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
void AcceptTask::Complete(htcs::SocketError err, s32 desc) {
|
||||
/* Set our results. */
|
||||
m_err = err;
|
||||
m_desc = desc;
|
||||
|
||||
/* Complete. */
|
||||
HtcsSignalingTask::Complete();
|
||||
}
|
||||
|
||||
Result AcceptTask::GetResult(htcs::SocketError *out_err, s32 *out_desc, s32 server_handle) const {
|
||||
/* Check the server handle. */
|
||||
R_UNLESS(m_server_handle == server_handle, htcs::ResultInvalidServerHandle());
|
||||
|
||||
/* Sanity check our state. */
|
||||
AMS_ASSERT(this->GetTaskState() == htc::server::rpc::RpcTaskState::Completed);
|
||||
|
||||
/* Set the output. */
|
||||
*out_err = m_err;
|
||||
*out_desc = m_desc;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result AcceptTask::ProcessResponse(const char *data, size_t size) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Convert the input to a packet. */
|
||||
auto *packet = reinterpret_cast<const HtcsRpcPacket *>(data);
|
||||
|
||||
/* Complete the task. */
|
||||
this->Complete(static_cast<htcs::SocketError>(packet->params[0]), packet->params[1]);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result AcceptTask::CreateRequest(size_t *out, char *data, size_t size, u32 task_id) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Create the packet. */
|
||||
auto *packet = reinterpret_cast<HtcsRpcPacket *>(data);
|
||||
*packet = {
|
||||
.protocol = HtcsProtocol,
|
||||
.version = this->GetVersion(),
|
||||
.category = HtcsPacketCategory::Request,
|
||||
.type = HtcsPacketType::Accept,
|
||||
.body_size = 0,
|
||||
.task_id = task_id,
|
||||
.params = {
|
||||
m_server_handle,
|
||||
},
|
||||
};
|
||||
|
||||
/* Set the output size. */
|
||||
*out = sizeof(*packet);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_rpc_tasks.hpp"
|
||||
|
||||
namespace ams::htcs::impl::rpc {
|
||||
|
||||
Result BindTask::SetArguments(s32 handle, const HtcsPeerName &peer_name, const HtcsPortName &port_name) {
|
||||
/* Set our arguments. */
|
||||
m_handle = handle;
|
||||
m_peer_name = peer_name;
|
||||
m_port_name = port_name;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
void BindTask::Complete(htcs::SocketError err) {
|
||||
/* Set our results. */
|
||||
m_err = err;
|
||||
|
||||
/* Complete. */
|
||||
HtcsTask::Complete();
|
||||
}
|
||||
|
||||
Result BindTask::GetResult(htcs::SocketError *out_err) const {
|
||||
/* Sanity check our state. */
|
||||
AMS_ASSERT(this->GetTaskState() == htc::server::rpc::RpcTaskState::Completed);
|
||||
|
||||
/* Set the output. */
|
||||
*out_err = m_err;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result BindTask::ProcessResponse(const char *data, size_t size) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Convert the input to a packet. */
|
||||
auto *packet = reinterpret_cast<const HtcsRpcPacket *>(data);
|
||||
|
||||
/* Complete the task. */
|
||||
this->Complete(static_cast<htcs::SocketError>(packet->params[0]));
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result BindTask::CreateRequest(size_t *out, char *data, size_t size, u32 task_id) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Create the packet. */
|
||||
auto *packet = reinterpret_cast<HtcsRpcPacket *>(data);
|
||||
*packet = {
|
||||
.protocol = HtcsProtocol,
|
||||
.version = this->GetVersion(),
|
||||
.category = HtcsPacketCategory::Request,
|
||||
.type = HtcsPacketType::Bind,
|
||||
.body_size = sizeof(m_peer_name) + sizeof(m_port_name),
|
||||
.task_id = task_id,
|
||||
.params = {
|
||||
m_handle,
|
||||
},
|
||||
};
|
||||
std::memcpy(packet->data + 0, std::addressof(m_peer_name), sizeof(m_peer_name));
|
||||
std::memcpy(packet->data + sizeof(m_peer_name), std::addressof(m_port_name), sizeof(m_port_name));
|
||||
|
||||
/* Set the output size. */
|
||||
*out = sizeof(*packet) + sizeof(m_peer_name) + sizeof(m_port_name);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_rpc_tasks.hpp"
|
||||
|
||||
namespace ams::htcs::impl::rpc {
|
||||
|
||||
Result CloseTask::SetArguments(s32 handle) {
|
||||
/* Set our arguments. */
|
||||
m_handle = handle;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
void CloseTask::Complete(htcs::SocketError err) {
|
||||
/* Set our results. */
|
||||
m_err = err;
|
||||
|
||||
/* Complete. */
|
||||
HtcsTask::Complete();
|
||||
}
|
||||
|
||||
Result CloseTask::GetResult(htcs::SocketError *out_err) const {
|
||||
/* Sanity check our state. */
|
||||
AMS_ASSERT(this->GetTaskState() == htc::server::rpc::RpcTaskState::Completed);
|
||||
|
||||
/* Set the output. */
|
||||
*out_err = m_err;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result CloseTask::ProcessResponse(const char *data, size_t size) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Convert the input to a packet. */
|
||||
auto *packet = reinterpret_cast<const HtcsRpcPacket *>(data);
|
||||
|
||||
/* Complete the task. */
|
||||
this->Complete(static_cast<htcs::SocketError>(packet->params[0]));
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result CloseTask::CreateRequest(size_t *out, char *data, size_t size, u32 task_id) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Create the packet. */
|
||||
auto *packet = reinterpret_cast<HtcsRpcPacket *>(data);
|
||||
*packet = {
|
||||
.protocol = HtcsProtocol,
|
||||
.version = this->GetVersion(),
|
||||
.category = HtcsPacketCategory::Request,
|
||||
.type = HtcsPacketType::Close,
|
||||
.body_size = 0,
|
||||
.task_id = task_id,
|
||||
.params = {
|
||||
m_handle,
|
||||
},
|
||||
};
|
||||
|
||||
/* Set the output size. */
|
||||
*out = sizeof(*packet);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_rpc_tasks.hpp"
|
||||
|
||||
namespace ams::htcs::impl::rpc {
|
||||
|
||||
Result ConnectTask::SetArguments(s32 handle, const HtcsPeerName &peer_name, const HtcsPortName &port_name) {
|
||||
/* Set our arguments. */
|
||||
m_handle = handle;
|
||||
m_peer_name = peer_name;
|
||||
m_port_name = port_name;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
void ConnectTask::Complete(htcs::SocketError err) {
|
||||
/* Set our results. */
|
||||
m_err = err;
|
||||
|
||||
/* Complete. */
|
||||
HtcsTask::Complete();
|
||||
}
|
||||
|
||||
Result ConnectTask::GetResult(htcs::SocketError *out_err) const {
|
||||
/* Sanity check our state. */
|
||||
AMS_ASSERT(this->GetTaskState() == htc::server::rpc::RpcTaskState::Completed);
|
||||
|
||||
/* Set the output. */
|
||||
*out_err = m_err;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result ConnectTask::ProcessResponse(const char *data, size_t size) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Convert the input to a packet. */
|
||||
auto *packet = reinterpret_cast<const HtcsRpcPacket *>(data);
|
||||
|
||||
/* Complete the task. */
|
||||
this->Complete(static_cast<htcs::SocketError>(packet->params[0]));
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result ConnectTask::CreateRequest(size_t *out, char *data, size_t size, u32 task_id) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Create the packet. */
|
||||
auto *packet = reinterpret_cast<HtcsRpcPacket *>(data);
|
||||
*packet = {
|
||||
.protocol = HtcsProtocol,
|
||||
.version = this->GetVersion(),
|
||||
.category = HtcsPacketCategory::Request,
|
||||
.type = HtcsPacketType::Connect,
|
||||
.body_size = sizeof(m_peer_name) + sizeof(m_port_name),
|
||||
.task_id = task_id,
|
||||
.params = {
|
||||
m_handle,
|
||||
},
|
||||
};
|
||||
std::memcpy(packet->data + 0, std::addressof(m_peer_name), sizeof(m_peer_name));
|
||||
std::memcpy(packet->data + sizeof(m_peer_name), std::addressof(m_port_name), sizeof(m_port_name));
|
||||
|
||||
/* Set the output size. */
|
||||
*out = sizeof(*packet) + sizeof(m_peer_name) + sizeof(m_port_name);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_rpc_tasks.hpp"
|
||||
|
||||
namespace ams::htcs::impl::rpc {
|
||||
|
||||
Result FcntlTask::SetArguments(s32 handle, s32 command, s32 value) {
|
||||
/* Set our arguments. */
|
||||
m_handle = handle;
|
||||
m_command = command;
|
||||
m_value = value;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
void FcntlTask::Complete(htcs::SocketError err, s32 res) {
|
||||
/* Set our results. */
|
||||
m_err = err;
|
||||
m_res = res;
|
||||
|
||||
/* Complete. */
|
||||
HtcsTask::Complete();
|
||||
}
|
||||
|
||||
Result FcntlTask::GetResult(htcs::SocketError *out_err, s32 *out_res) const {
|
||||
/* Sanity check our state. */
|
||||
AMS_ASSERT(this->GetTaskState() == htc::server::rpc::RpcTaskState::Completed);
|
||||
|
||||
/* Set the output. */
|
||||
*out_err = m_err;
|
||||
*out_res = m_res;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result FcntlTask::ProcessResponse(const char *data, size_t size) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Convert the input to a packet. */
|
||||
auto *packet = reinterpret_cast<const HtcsRpcPacket *>(data);
|
||||
|
||||
/* Complete the task. */
|
||||
this->Complete(static_cast<htcs::SocketError>(packet->params[0]), packet->params[1]);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result FcntlTask::CreateRequest(size_t *out, char *data, size_t size, u32 task_id) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Create the packet. */
|
||||
auto *packet = reinterpret_cast<HtcsRpcPacket *>(data);
|
||||
*packet = {
|
||||
.protocol = HtcsProtocol,
|
||||
.version = this->GetVersion(),
|
||||
.category = HtcsPacketCategory::Request,
|
||||
.type = HtcsPacketType::Fcntl,
|
||||
.body_size = 0,
|
||||
.task_id = task_id,
|
||||
.params = {
|
||||
m_handle,
|
||||
m_command,
|
||||
m_value,
|
||||
},
|
||||
};
|
||||
|
||||
/* Set the output size. */
|
||||
*out = sizeof(*packet);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_rpc_tasks.hpp"
|
||||
|
||||
namespace ams::htcs::impl::rpc {
|
||||
|
||||
Result ListenTask::SetArguments(s32 handle, s32 backlog) {
|
||||
/* Set our arguments. */
|
||||
m_handle = handle;
|
||||
m_backlog = backlog;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
void ListenTask::Complete(htcs::SocketError err) {
|
||||
/* Set our results. */
|
||||
m_err = err;
|
||||
|
||||
/* Complete. */
|
||||
HtcsTask::Complete();
|
||||
}
|
||||
|
||||
Result ListenTask::GetResult(htcs::SocketError *out_err) const {
|
||||
/* Sanity check our state. */
|
||||
AMS_ASSERT(this->GetTaskState() == htc::server::rpc::RpcTaskState::Completed);
|
||||
|
||||
/* Set the output. */
|
||||
*out_err = m_err;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result ListenTask::ProcessResponse(const char *data, size_t size) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Convert the input to a packet. */
|
||||
auto *packet = reinterpret_cast<const HtcsRpcPacket *>(data);
|
||||
|
||||
/* Complete the task. */
|
||||
this->Complete(static_cast<htcs::SocketError>(packet->params[0]));
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result ListenTask::CreateRequest(size_t *out, char *data, size_t size, u32 task_id) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Create the packet. */
|
||||
auto *packet = reinterpret_cast<HtcsRpcPacket *>(data);
|
||||
*packet = {
|
||||
.protocol = HtcsProtocol,
|
||||
.version = this->GetVersion(),
|
||||
.category = HtcsPacketCategory::Request,
|
||||
.type = HtcsPacketType::Listen,
|
||||
.body_size = 0,
|
||||
.task_id = task_id,
|
||||
.params = {
|
||||
m_handle,
|
||||
m_backlog,
|
||||
},
|
||||
};
|
||||
|
||||
/* Set the output size. */
|
||||
*out = sizeof(*packet);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_rpc_tasks.hpp"
|
||||
|
||||
namespace ams::htcs::impl::rpc {
|
||||
|
||||
Result ReceiveSmallTask::SetArguments(s32 handle, s64 size, htcs::MessageFlag flags) {
|
||||
/* Check that we're valid. */
|
||||
R_UNLESS(this->IsValid(), htcs::ResultInvalidTask());
|
||||
|
||||
/* Set our arguments. */
|
||||
m_handle = handle;
|
||||
m_size = size;
|
||||
m_flags = flags;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
void ReceiveSmallTask::Complete(htcs::SocketError err, s64 size) {
|
||||
/* Set our results. */
|
||||
m_err = err;
|
||||
m_result_size = size;
|
||||
|
||||
/* Complete. */
|
||||
HtcsSignalingTask::Complete();
|
||||
}
|
||||
|
||||
Result ReceiveSmallTask::GetResult(htcs::SocketError *out_err, s64 *out_size) const {
|
||||
/* Sanity check our state. */
|
||||
AMS_ASSERT(this->GetTaskState() == htc::server::rpc::RpcTaskState::Completed);
|
||||
|
||||
/* Set the output. */
|
||||
*out_err = m_err;
|
||||
*out_size = m_result_size;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result ReceiveSmallTask::ProcessResponse(const char *data, size_t size) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Convert the input to a packet. */
|
||||
auto *packet = reinterpret_cast<const HtcsRpcPacket *>(data);
|
||||
|
||||
/* Copy the data to our buffer. */
|
||||
std::memcpy(m_buffer, packet->data, packet->body_size);
|
||||
|
||||
/* Complete the task. */
|
||||
this->Complete(static_cast<htcs::SocketError>(packet->params[0]), packet->body_size);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result ReceiveSmallTask::CreateRequest(size_t *out, char *data, size_t size, u32 task_id) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Create the packet. */
|
||||
auto *packet = reinterpret_cast<HtcsRpcPacket *>(data);
|
||||
*packet = {
|
||||
.protocol = HtcsProtocol,
|
||||
.version = this->GetVersion(),
|
||||
.category = HtcsPacketCategory::Request,
|
||||
.type = HtcsPacketType::Receive,
|
||||
.body_size = 0,
|
||||
.task_id = task_id,
|
||||
.params = {
|
||||
m_handle,
|
||||
m_size,
|
||||
static_cast<s64>(m_flags),
|
||||
},
|
||||
};
|
||||
|
||||
/* Set the output size. */
|
||||
*out = sizeof(*packet);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
bool ReceiveSmallTask::IsReceiveBufferRequired() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,114 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_rpc_tasks.hpp"
|
||||
|
||||
namespace ams::htcs::impl::rpc {
|
||||
|
||||
Result ReceiveTask::SetArguments(s32 handle, s64 size, htcs::MessageFlag flags) {
|
||||
/* Check that we're valid. */
|
||||
R_UNLESS(this->IsValid(), htcs::ResultInvalidTask());
|
||||
|
||||
/* Set our arguments. */
|
||||
m_handle = handle;
|
||||
m_size = size;
|
||||
m_flags = flags;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
void ReceiveTask::Complete(htcs::SocketError err, s64 size) {
|
||||
/* Set our results. */
|
||||
m_err = err;
|
||||
m_result_size = size;
|
||||
|
||||
/* Complete. */
|
||||
HtcsSignalingTask::Complete();
|
||||
}
|
||||
|
||||
Result ReceiveTask::GetResult(htcs::SocketError *out_err, s64 *out_size) const {
|
||||
/* Sanity check our state. */
|
||||
AMS_ASSERT(this->GetTaskState() == htc::server::rpc::RpcTaskState::Completed);
|
||||
|
||||
/* Set the output. */
|
||||
*out_err = m_err;
|
||||
*out_size = m_result_size;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result ReceiveTask::ProcessResponse(const char *data, size_t size) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Convert the input to a packet. */
|
||||
auto *packet = reinterpret_cast<const HtcsRpcPacket *>(data);
|
||||
|
||||
/* Complete the task. */
|
||||
this->Complete(static_cast<htcs::SocketError>(packet->params[0]), packet->params[1]);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result ReceiveTask::CreateRequest(size_t *out, char *data, size_t size, u32 task_id) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Create the packet. */
|
||||
auto *packet = reinterpret_cast<HtcsRpcPacket *>(data);
|
||||
*packet = {
|
||||
.protocol = HtcsProtocol,
|
||||
.version = this->GetVersion(),
|
||||
.category = HtcsPacketCategory::Request,
|
||||
.type = HtcsPacketType::ReceiveLarge,
|
||||
.body_size = 0,
|
||||
.task_id = task_id,
|
||||
.params = {
|
||||
m_handle,
|
||||
m_size,
|
||||
static_cast<s64>(m_flags),
|
||||
GetReceiveDataChannelId(task_id),
|
||||
},
|
||||
};
|
||||
|
||||
/* Set the output size. */
|
||||
*out = sizeof(*packet);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result ReceiveTask::CreateNotification(size_t *out, char *data, size_t size, u32 task_id) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Create the packet. */
|
||||
auto *packet = reinterpret_cast<HtcsRpcPacket *>(data);
|
||||
*packet = {
|
||||
.protocol = HtcsProtocol,
|
||||
.version = this->GetVersion(),
|
||||
.category = HtcsPacketCategory::Notification,
|
||||
.type = HtcsPacketType::ReceiveLarge,
|
||||
.body_size = 0,
|
||||
.task_id = task_id,
|
||||
.params = {
|
||||
/* ... */
|
||||
},
|
||||
};
|
||||
|
||||
/* Set the output size. */
|
||||
*out = sizeof(*packet);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,155 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_rpc_tasks.hpp"
|
||||
|
||||
namespace ams::htcs::impl::rpc {
|
||||
|
||||
Result SelectTask::SetArguments(Span<const int> read_handles, Span<const int> write_handles, Span<const int> exception_handles, s64 tv_sec, s64 tv_usec) {
|
||||
/* Check that we're valid. */
|
||||
R_UNLESS(this->IsValid(), htcs::ResultInvalidTask());
|
||||
|
||||
/* Sanity check the spans. */
|
||||
AMS_ASSERT(read_handles.size() < static_cast<size_t>(SocketCountMax));
|
||||
AMS_ASSERT(write_handles.size() < static_cast<size_t>(SocketCountMax));
|
||||
AMS_ASSERT(exception_handles.size() < static_cast<size_t>(SocketCountMax));
|
||||
|
||||
/* Set our arguments. */
|
||||
m_read_handle_count = static_cast<s32>(read_handles.size());
|
||||
m_write_handle_count = static_cast<s32>(write_handles.size());
|
||||
m_exception_handle_count = static_cast<s32>(exception_handles.size());
|
||||
m_tv_sec = tv_sec;
|
||||
m_tv_usec = tv_usec;
|
||||
|
||||
/* Copy the handles. */
|
||||
std::memcpy(m_handles, read_handles.data(), read_handles.size_bytes());
|
||||
std::memcpy(m_handles + m_read_handle_count, write_handles.data(), write_handles.size_bytes());
|
||||
std::memcpy(m_handles + m_read_handle_count + m_write_handle_count, exception_handles.data(), exception_handles.size_bytes());
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
void SelectTask::Complete(htcs::SocketError err, s32 read_handle_count, s32 write_handle_count, s32 exception_handle_count, const void *body, s64 body_size) {
|
||||
/* Sanity check the handle counts. */
|
||||
const auto handle_count = read_handle_count + write_handle_count + exception_handle_count;
|
||||
AMS_ASSERT(0 <= read_handle_count && read_handle_count < SocketCountMax);
|
||||
AMS_ASSERT(0 <= write_handle_count && write_handle_count < SocketCountMax);
|
||||
AMS_ASSERT(0 <= exception_handle_count && exception_handle_count < SocketCountMax);
|
||||
AMS_ASSERT(handle_count * static_cast<s64>(sizeof(s32)) == body_size);
|
||||
AMS_UNUSED(handle_count, body_size);
|
||||
|
||||
/* Set our results. */
|
||||
m_err = err;
|
||||
m_out_read_handle_count = read_handle_count;
|
||||
m_out_write_handle_count = write_handle_count;
|
||||
m_out_exception_handle_count = exception_handle_count;
|
||||
|
||||
/* Copy the handles. */
|
||||
std::memcpy(m_out_handles, static_cast<const s32 *>(body), sizeof(s32) * read_handle_count);
|
||||
std::memcpy(m_out_handles + read_handle_count, static_cast<const s32 *>(body) + read_handle_count, sizeof(s32) * write_handle_count);
|
||||
std::memcpy(m_out_handles + read_handle_count + write_handle_count, static_cast<const s32 *>(body) + read_handle_count + write_handle_count, sizeof(s32) * exception_handle_count);
|
||||
|
||||
/* Complete. */
|
||||
HtcsSignalingTask::Complete();
|
||||
}
|
||||
|
||||
Result SelectTask::GetResult(htcs::SocketError *out_err, bool *out_empty, Span<int> read_handles, Span<int> write_handles, Span<int> exception_handles) const {
|
||||
/* Set the output error. */
|
||||
*out_err = m_err;
|
||||
|
||||
/* Set the output empty value. */
|
||||
const bool empty = m_err == HTCS_ENONE && m_out_read_handle_count == 0 && m_out_write_handle_count == 0 && m_out_exception_handle_count == 0;
|
||||
*out_empty = empty;
|
||||
|
||||
/* Clear the output spans. */
|
||||
std::fill(read_handles.begin(), read_handles.end(), 0);
|
||||
std::fill(write_handles.begin(), write_handles.end(), 0);
|
||||
std::fill(exception_handles.begin(), exception_handles.end(), 0);
|
||||
|
||||
/* Copy the handles. */
|
||||
if (m_err == HTCS_ENONE && !empty) {
|
||||
const s32 * const out_read_start = m_out_handles;
|
||||
const s32 * const out_read_end = out_read_start + m_out_read_handle_count;
|
||||
const s32 * const out_write_start = out_read_end;
|
||||
const s32 * const out_write_end = out_write_start + m_out_write_handle_count;
|
||||
const s32 * const out_exception_start = out_write_end;
|
||||
const s32 * const out_exception_end = out_exception_start + m_out_exception_handle_count;
|
||||
std::copy(out_read_start, out_read_end, read_handles.begin());
|
||||
std::copy(out_write_start, out_write_end, write_handles.begin());
|
||||
std::copy(out_exception_start, out_exception_end, exception_handles.begin());
|
||||
} else {
|
||||
const s32 * const read_start = m_handles;
|
||||
const s32 * const read_end = read_start + m_read_handle_count;
|
||||
const s32 * const write_start = read_end;
|
||||
const s32 * const write_end = write_start + m_write_handle_count;
|
||||
const s32 * const exception_start = write_end;
|
||||
const s32 * const exception_end = exception_start + m_exception_handle_count;
|
||||
std::copy(read_start, read_end, read_handles.begin());
|
||||
std::copy(write_start, write_end, write_handles.begin());
|
||||
std::copy(exception_start, exception_end, exception_handles.begin());
|
||||
}
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SelectTask::ProcessResponse(const char *data, size_t size) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Convert the input to a packet. */
|
||||
auto *packet = reinterpret_cast<const HtcsRpcPacket *>(data);
|
||||
|
||||
/* Complete the task. */
|
||||
this->Complete(static_cast<htcs::SocketError>(packet->params[0]), packet->params[1], packet->params[2], packet->params[3], packet->data, size - sizeof(*packet));
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SelectTask::CreateRequest(size_t *out, char *data, size_t size, u32 task_id) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Determine the body size. */
|
||||
const auto handle_count = m_read_handle_count + m_write_handle_count + m_exception_handle_count;
|
||||
const s64 body_size = static_cast<s64>(handle_count * sizeof(s32));
|
||||
AMS_ASSERT(sizeof(HtcsRpcPacket) + body_size <= size);
|
||||
|
||||
/* Create the packet. */
|
||||
auto *packet = reinterpret_cast<HtcsRpcPacket *>(data);
|
||||
*packet = {
|
||||
.protocol = HtcsProtocol,
|
||||
.version = this->GetVersion(),
|
||||
.category = HtcsPacketCategory::Request,
|
||||
.type = HtcsPacketType::Select,
|
||||
.body_size = body_size,
|
||||
.task_id = task_id,
|
||||
.params = {
|
||||
m_read_handle_count,
|
||||
m_write_handle_count,
|
||||
m_exception_handle_count,
|
||||
m_tv_sec,
|
||||
m_tv_usec,
|
||||
},
|
||||
};
|
||||
|
||||
/* Set the packet body. */
|
||||
std::memcpy(packet->data, m_handles, body_size);
|
||||
|
||||
/* Set the output size. */
|
||||
*out = sizeof(*packet) + body_size;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,137 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_rpc_tasks.hpp"
|
||||
|
||||
namespace ams::htcs::impl::rpc {
|
||||
|
||||
void SendSmallTask::SetBuffer(const void *buffer, s64 buffer_size) {
|
||||
/* Sanity check the buffer size. */
|
||||
AMS_ASSERT(0 <= buffer_size && buffer_size <= static_cast<s64>(sizeof(m_buffer)));
|
||||
|
||||
/* Set our buffer. */
|
||||
if (buffer_size > 0) {
|
||||
std::memcpy(m_buffer, buffer, buffer_size);
|
||||
}
|
||||
m_buffer_size = buffer_size;
|
||||
}
|
||||
|
||||
void SendSmallTask::NotifyDataChannelReady() {
|
||||
/* Notify. */
|
||||
this->Notify();
|
||||
|
||||
/* Signal our ready event. */
|
||||
m_ready_event.Signal();
|
||||
}
|
||||
|
||||
void SendSmallTask::WaitNotification() {
|
||||
/* Wait on our ready event. */
|
||||
m_ready_event.Wait();
|
||||
}
|
||||
|
||||
Result SendSmallTask::SetArguments(s32 handle, s64 size, htcs::MessageFlag flags) {
|
||||
/* Check that we're valid. */
|
||||
R_UNLESS(this->IsValid(), htcs::ResultInvalidTask());
|
||||
|
||||
/* Set our arguments. */
|
||||
m_handle = handle;
|
||||
m_size = size;
|
||||
m_flags = flags;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
void SendSmallTask::Complete(htcs::SocketError err, s64 size) {
|
||||
/* Set our results. */
|
||||
m_err = err;
|
||||
m_result_size = size;
|
||||
|
||||
/* Signal our ready event. */
|
||||
m_ready_event.Signal();
|
||||
|
||||
/* Complete. */
|
||||
HtcsSignalingTask::Complete();
|
||||
}
|
||||
|
||||
Result SendSmallTask::GetResult(htcs::SocketError *out_err, s64 *out_size) const {
|
||||
/* Sanity check our state. */
|
||||
AMS_ASSERT(this->GetTaskState() == htc::server::rpc::RpcTaskState::Completed);
|
||||
|
||||
/* Set the output. */
|
||||
*out_err = m_err;
|
||||
*out_size = m_result_size;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
void SendSmallTask::Cancel(htc::server::rpc::RpcTaskCancelReason reason) {
|
||||
/* Cancel the task. */
|
||||
HtcsSignalingTask::Cancel(reason);
|
||||
|
||||
/* Signal our ready event. */
|
||||
m_ready_event.Signal();
|
||||
}
|
||||
|
||||
Result SendSmallTask::ProcessResponse(const char *data, size_t size) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Convert the input to a packet. */
|
||||
auto *packet = reinterpret_cast<const HtcsRpcPacket *>(data);
|
||||
|
||||
/* Complete the task. */
|
||||
this->Complete(static_cast<htcs::SocketError>(packet->params[0]), this->GetSize());
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SendSmallTask::CreateRequest(size_t *out, char *data, size_t size, u32 task_id) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Sanity check our size. */
|
||||
AMS_ASSERT(sizeof(HtcsRpcPacket) + this->GetBufferSize() <= size);
|
||||
|
||||
/* Create the packet. */
|
||||
auto *packet = reinterpret_cast<HtcsRpcPacket *>(data);
|
||||
*packet = {
|
||||
.protocol = HtcsProtocol,
|
||||
.version = this->GetVersion(),
|
||||
.category = HtcsPacketCategory::Request,
|
||||
.type = HtcsPacketType::Send,
|
||||
.body_size = this->GetSize(),
|
||||
.task_id = task_id,
|
||||
.params = {
|
||||
m_handle,
|
||||
m_size,
|
||||
static_cast<s64>(m_flags),
|
||||
},
|
||||
};
|
||||
|
||||
/* Set the body. */
|
||||
if (this->GetSize() > 0) {
|
||||
std::memcpy(packet->data, this->GetBuffer(), this->GetSize());
|
||||
}
|
||||
|
||||
/* Set the output size. */
|
||||
*out = sizeof(*packet) + this->GetSize();
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
bool SendSmallTask::IsSendBufferRequired() {
|
||||
return this->GetSize() > 0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,151 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_rpc_tasks.hpp"
|
||||
|
||||
namespace ams::htcs::impl::rpc {
|
||||
|
||||
void SendTask::SetBuffer(const void *buffer, s64 buffer_size) {
|
||||
/* Set our buffer. */
|
||||
m_buffer = buffer;
|
||||
m_buffer_size = buffer_size;
|
||||
}
|
||||
|
||||
void SendTask::NotifyDataChannelReady() {
|
||||
/* Notify. */
|
||||
this->Notify();
|
||||
|
||||
/* Signal our ready event. */
|
||||
m_ready_event.Signal();
|
||||
}
|
||||
|
||||
void SendTask::WaitNotification() {
|
||||
/* Wait on our ready event. */
|
||||
m_ready_event.Wait();
|
||||
}
|
||||
|
||||
Result SendTask::SetArguments(s32 handle, s64 size, htcs::MessageFlag flags) {
|
||||
/* Check that we're valid. */
|
||||
R_UNLESS(this->IsValid(), htcs::ResultInvalidTask());
|
||||
|
||||
/* Set our arguments. */
|
||||
m_handle = handle;
|
||||
m_size = size;
|
||||
m_flags = flags;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
void SendTask::Complete(htcs::SocketError err, s64 size) {
|
||||
/* Set our results. */
|
||||
m_err = err;
|
||||
m_result_size = size;
|
||||
|
||||
/* Signal our ready event. */
|
||||
m_ready_event.Signal();
|
||||
|
||||
/* Complete. */
|
||||
HtcsSignalingTask::Complete();
|
||||
}
|
||||
|
||||
Result SendTask::GetResult(htcs::SocketError *out_err, s64 *out_size) const {
|
||||
/* Sanity check our state. */
|
||||
AMS_ASSERT(this->GetTaskState() == htc::server::rpc::RpcTaskState::Completed);
|
||||
|
||||
/* Set the output. */
|
||||
*out_err = m_err;
|
||||
*out_size = m_result_size;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
void SendTask::Cancel(htc::server::rpc::RpcTaskCancelReason reason) {
|
||||
/* Cancel the task. */
|
||||
HtcsSignalingTask::Cancel(reason);
|
||||
|
||||
/* Signal our ready event. */
|
||||
m_ready_event.Signal();
|
||||
}
|
||||
|
||||
Result SendTask::ProcessResponse(const char *data, size_t size) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Convert the input to a packet. */
|
||||
auto *packet = reinterpret_cast<const HtcsRpcPacket *>(data);
|
||||
|
||||
/* Complete the task. */
|
||||
this->Complete(static_cast<htcs::SocketError>(packet->params[0]), packet->params[1]);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SendTask::CreateRequest(size_t *out, char *data, size_t size, u32 task_id) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Create the packet. */
|
||||
auto *packet = reinterpret_cast<HtcsRpcPacket *>(data);
|
||||
*packet = {
|
||||
.protocol = HtcsProtocol,
|
||||
.version = this->GetVersion(),
|
||||
.category = HtcsPacketCategory::Request,
|
||||
.type = HtcsPacketType::SendLarge,
|
||||
.body_size = 0,
|
||||
.task_id = task_id,
|
||||
.params = {
|
||||
m_handle,
|
||||
m_size,
|
||||
static_cast<s64>(m_flags),
|
||||
GetSendDataChannelId(task_id),
|
||||
},
|
||||
};
|
||||
|
||||
/* Set the output size. */
|
||||
*out = sizeof(*packet);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SendTask::ProcessNotification(const char *data, size_t size) {
|
||||
AMS_UNUSED(data, size);
|
||||
|
||||
this->NotifyDataChannelReady();
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SendTask::CreateNotification(size_t *out, char *data, size_t size, u32 task_id) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Create the packet. */
|
||||
auto *packet = reinterpret_cast<HtcsRpcPacket *>(data);
|
||||
*packet = {
|
||||
.protocol = HtcsProtocol,
|
||||
.version = this->GetVersion(),
|
||||
.category = HtcsPacketCategory::Notification,
|
||||
.type = HtcsPacketType::SendLarge,
|
||||
.body_size = 0,
|
||||
.task_id = task_id,
|
||||
.params = {
|
||||
/* ... */
|
||||
},
|
||||
};
|
||||
|
||||
/* Set the output size. */
|
||||
*out = sizeof(*packet);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_rpc_tasks.hpp"
|
||||
|
||||
namespace ams::htcs::impl::rpc {
|
||||
|
||||
Result ShutdownTask::SetArguments(s32 handle, ShutdownType how) {
|
||||
/* Set our arguments. */
|
||||
m_handle = handle;
|
||||
m_how = how;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
void ShutdownTask::Complete(htcs::SocketError err) {
|
||||
/* Set our results. */
|
||||
m_err = err;
|
||||
|
||||
/* Complete. */
|
||||
HtcsTask::Complete();
|
||||
}
|
||||
|
||||
Result ShutdownTask::GetResult(htcs::SocketError *out_err) const {
|
||||
/* Sanity check our state. */
|
||||
AMS_ASSERT(this->GetTaskState() == htc::server::rpc::RpcTaskState::Completed);
|
||||
|
||||
/* Set the output. */
|
||||
*out_err = m_err;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result ShutdownTask::ProcessResponse(const char *data, size_t size) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Convert the input to a packet. */
|
||||
auto *packet = reinterpret_cast<const HtcsRpcPacket *>(data);
|
||||
|
||||
/* Complete the task. */
|
||||
this->Complete(static_cast<htcs::SocketError>(packet->params[0]));
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result ShutdownTask::CreateRequest(size_t *out, char *data, size_t size, u32 task_id) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Create the packet. */
|
||||
auto *packet = reinterpret_cast<HtcsRpcPacket *>(data);
|
||||
*packet = {
|
||||
.protocol = HtcsProtocol,
|
||||
.version = this->GetVersion(),
|
||||
.category = HtcsPacketCategory::Request,
|
||||
.type = HtcsPacketType::Shutdown,
|
||||
.body_size = 0,
|
||||
.task_id = task_id,
|
||||
.params = {
|
||||
m_handle,
|
||||
static_cast<s64>(m_how),
|
||||
},
|
||||
};
|
||||
|
||||
/* Set the output size. */
|
||||
*out = sizeof(*packet);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_rpc_tasks.hpp"
|
||||
|
||||
namespace ams::htcs::impl::rpc {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr int MaxEventCount = 0x22;
|
||||
|
||||
constinit os::SdkMutex g_event_count_mutex;
|
||||
constinit int g_event_count = 0;
|
||||
|
||||
}
|
||||
|
||||
HtcsSignalingTask::HtcsSignalingTask(HtcsTaskType type) : HtcsTask(type), m_is_valid(false) {
|
||||
/* Acquire the exclusive right to create an event. */
|
||||
std::scoped_lock lk(g_event_count_mutex);
|
||||
|
||||
/* Create an event. */
|
||||
if (AMS_LIKELY(g_event_count < MaxEventCount)) {
|
||||
/* Make the event. */
|
||||
R_ABORT_UNLESS(os::CreateSystemEvent(std::addressof(m_system_event), os::EventClearMode_ManualClear, true));
|
||||
|
||||
/* Increment the event count. */
|
||||
++g_event_count;
|
||||
|
||||
/* Mark ourselves as valid. */
|
||||
m_is_valid = true;
|
||||
}
|
||||
}
|
||||
|
||||
HtcsSignalingTask::~HtcsSignalingTask() {
|
||||
/* If we have an event, we need to destroy it. */
|
||||
if (AMS_LIKELY(m_is_valid)) {
|
||||
/* Acquire exclusive access to the event count. */
|
||||
std::scoped_lock lk(g_event_count_mutex);
|
||||
|
||||
/* Destroy our event. */
|
||||
os::DestroySystemEvent(std::addressof(m_system_event));
|
||||
|
||||
/* Decrement the event count. */
|
||||
if ((--g_event_count) < 0) {
|
||||
g_event_count = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_rpc_tasks.hpp"
|
||||
|
||||
namespace ams::htcs::impl::rpc {
|
||||
|
||||
namespace {
|
||||
|
||||
constinit s16 g_protocol_version = HtcsMaxVersion;
|
||||
|
||||
}
|
||||
|
||||
HtcsTask::HtcsTask(HtcsTaskType type) : m_task_type(type), m_version(g_protocol_version) {
|
||||
/* ... */
|
||||
}
|
||||
|
||||
Result SocketTask::SetArguments() {
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
void SocketTask::Complete(htcs::SocketError err, s32 desc) {
|
||||
/* Set our results. */
|
||||
m_err = err;
|
||||
m_desc = desc;
|
||||
|
||||
/* Complete. */
|
||||
HtcsTask::Complete();
|
||||
}
|
||||
|
||||
Result SocketTask::GetResult(htcs::SocketError *out_err, s32 *out_desc) const {
|
||||
/* Sanity check our state. */
|
||||
AMS_ASSERT(this->GetTaskState() == htc::server::rpc::RpcTaskState::Completed);
|
||||
|
||||
/* Set the output. */
|
||||
*out_err = m_err;
|
||||
*out_desc = m_desc;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SocketTask::ProcessResponse(const char *data, size_t size) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Convert the input to a packet. */
|
||||
auto *packet = reinterpret_cast<const HtcsRpcPacket *>(data);
|
||||
|
||||
/* Update the global protocol version. */
|
||||
g_protocol_version = std::min(g_protocol_version, packet->version);
|
||||
|
||||
/* Complete the task. */
|
||||
this->Complete(static_cast<htcs::SocketError>(packet->params[0]), packet->params[1]);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SocketTask::CreateRequest(size_t *out, char *data, size_t size, u32 task_id) {
|
||||
AMS_UNUSED(size);
|
||||
|
||||
/* Create the packet. */
|
||||
auto *packet = reinterpret_cast<HtcsRpcPacket *>(data);
|
||||
*packet = {
|
||||
.protocol = HtcsProtocol,
|
||||
.version = 3,
|
||||
.category = HtcsPacketCategory::Request,
|
||||
.type = HtcsPacketType::Socket,
|
||||
.body_size = 0,
|
||||
.task_id = task_id,
|
||||
.params = {
|
||||
/* ... */
|
||||
},
|
||||
};
|
||||
|
||||
/* Set the output size. */
|
||||
*out = sizeof(*packet);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,473 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <stratosphere.hpp>
|
||||
#include "../../../htc/server/rpc/htc_rpc_tasks.hpp"
|
||||
|
||||
namespace ams::htcs::impl::rpc {
|
||||
|
||||
enum class HtcsTaskType {
|
||||
Receive = 0,
|
||||
Send = 1,
|
||||
Shutdown = 2,
|
||||
Close = 3,
|
||||
Connect = 4,
|
||||
Listen = 5,
|
||||
Accept = 6,
|
||||
Socket = 7,
|
||||
Bind = 8,
|
||||
Fcntl = 9,
|
||||
ReceiveSmall = 10,
|
||||
SendSmall = 11,
|
||||
Select = 12,
|
||||
};
|
||||
|
||||
constexpr inline s16 HtcsProtocol = 5;
|
||||
constexpr inline const s16 HtcsMaxVersion = 4;
|
||||
|
||||
enum class HtcsPacketCategory : s16 {
|
||||
Request = 0,
|
||||
Response = 1,
|
||||
Notification = 2,
|
||||
};
|
||||
|
||||
enum class HtcsPacketType : s16 {
|
||||
Receive = 32,
|
||||
Send = 33,
|
||||
Shutdown = 34,
|
||||
Close = 35,
|
||||
Connect = 36,
|
||||
Listen = 37,
|
||||
Accept = 38,
|
||||
Socket = 39,
|
||||
Bind = 40,
|
||||
Fcntl = 41,
|
||||
ReceiveLarge = 42,
|
||||
SendLarge = 43,
|
||||
Select = 44,
|
||||
};
|
||||
|
||||
struct HtcsRpcPacket {
|
||||
s16 protocol;
|
||||
s16 version;
|
||||
HtcsPacketCategory category;
|
||||
HtcsPacketType type;
|
||||
s64 body_size;
|
||||
u32 task_id{};
|
||||
s64 params[5];
|
||||
char data[];
|
||||
};
|
||||
static_assert(sizeof(HtcsRpcPacket) == 0x40);
|
||||
|
||||
constexpr inline u16 ReceiveDataChannelIdBegin = htc::server::rpc::MaxRpcCount;
|
||||
constexpr inline u16 ReceiveDataChannelIdEnd = ReceiveDataChannelIdBegin + htc::server::rpc::MaxRpcCount;
|
||||
static_assert(ReceiveDataChannelIdEnd - ReceiveDataChannelIdBegin == htc::server::rpc::MaxRpcCount);
|
||||
|
||||
constexpr inline u16 SendDataChannelIdBegin = ReceiveDataChannelIdEnd;
|
||||
constexpr inline u16 SendDataChannelIdEnd = SendDataChannelIdBegin + htc::server::rpc::MaxRpcCount;
|
||||
static_assert(SendDataChannelIdEnd - SendDataChannelIdBegin == htc::server::rpc::MaxRpcCount);
|
||||
|
||||
constexpr inline u16 GetReceiveDataChannelId(u32 task_id) {
|
||||
const u16 channel_id = task_id + ReceiveDataChannelIdBegin;
|
||||
AMS_ASSERT(ReceiveDataChannelIdBegin <= channel_id && channel_id < ReceiveDataChannelIdEnd);
|
||||
|
||||
return channel_id;
|
||||
}
|
||||
|
||||
constexpr inline u16 GetSendDataChannelId(u32 task_id) {
|
||||
const u16 channel_id = task_id + SendDataChannelIdBegin;
|
||||
AMS_ASSERT(SendDataChannelIdBegin <= channel_id && channel_id < SendDataChannelIdEnd);
|
||||
|
||||
return channel_id;
|
||||
}
|
||||
|
||||
class HtcsTask : public htc::server::rpc::Task {
|
||||
private:
|
||||
HtcsTaskType m_task_type;
|
||||
s16 m_version;
|
||||
public:
|
||||
HtcsTask(HtcsTaskType type); /* Defined in socket_task.cpp, for namespacing reasons. */
|
||||
|
||||
HtcsTaskType GetTaskType() const { return m_task_type; }
|
||||
s16 GetVersion() const { return m_version; }
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
concept IsHtcsTask = std::derived_from<T, HtcsTask>;
|
||||
|
||||
class HtcsSignalingTask : public HtcsTask {
|
||||
private:
|
||||
os::SystemEventType m_system_event;
|
||||
bool m_is_valid;
|
||||
public:
|
||||
HtcsSignalingTask(HtcsTaskType type);
|
||||
virtual ~HtcsSignalingTask();
|
||||
|
||||
bool IsValid() const { return m_is_valid; }
|
||||
|
||||
void Complete() {
|
||||
os::SignalSystemEvent(std::addressof(m_system_event));
|
||||
HtcsTask::Complete();
|
||||
}
|
||||
public:
|
||||
virtual void Cancel(htc::server::rpc::RpcTaskCancelReason reason) override {
|
||||
HtcsTask::Cancel(reason);
|
||||
os::SignalSystemEvent(std::addressof(m_system_event));
|
||||
}
|
||||
|
||||
virtual os::SystemEventType *GetSystemEvent() override { return std::addressof(m_system_event); }
|
||||
};
|
||||
|
||||
class ReceiveTask : public HtcsSignalingTask {
|
||||
public:
|
||||
static constexpr inline HtcsTaskType TaskType = HtcsTaskType::Receive;
|
||||
private:
|
||||
s32 m_handle;
|
||||
s64 m_size;
|
||||
htcs::MessageFlag m_flags;
|
||||
void *m_buffer;
|
||||
s64 m_buffer_size;
|
||||
htcs::SocketError m_err;
|
||||
s64 m_result_size;
|
||||
public:
|
||||
ReceiveTask() : HtcsSignalingTask(TaskType) { /* ... */ }
|
||||
|
||||
s32 GetHandle() const { return m_handle; }
|
||||
s64 GetSize() const { return m_size; }
|
||||
htcs::MessageFlag GetFlags() const { return m_flags; }
|
||||
void *GetBuffer() const { return m_buffer; }
|
||||
s64 GetBufferSize() const { return m_buffer_size; }
|
||||
|
||||
s64 GetResultSize() const {
|
||||
AMS_ASSERT(this->GetTaskState() == htc::server::rpc::RpcTaskState::Completed);
|
||||
return m_result_size;
|
||||
}
|
||||
public:
|
||||
Result SetArguments(s32 handle, s64 size, htcs::MessageFlag flags);
|
||||
void Complete(htcs::SocketError err, s64 size);
|
||||
Result GetResult(htcs::SocketError *out_err, s64 *out_size) const;
|
||||
public:
|
||||
virtual Result ProcessResponse(const char *data, size_t size) override;
|
||||
virtual Result CreateRequest(size_t *out, char *data, size_t size, u32 task_id) override;
|
||||
virtual Result CreateNotification(size_t *out, char *data, size_t size, u32 task_id) override;
|
||||
};
|
||||
|
||||
class SendTask : public HtcsSignalingTask {
|
||||
public:
|
||||
static constexpr inline HtcsTaskType TaskType = HtcsTaskType::Send;
|
||||
private:
|
||||
os::Event m_ready_event;
|
||||
s32 m_handle;
|
||||
s64 m_size;
|
||||
htcs::MessageFlag m_flags;
|
||||
const void *m_buffer;
|
||||
s64 m_buffer_size;
|
||||
htcs::SocketError m_err;
|
||||
s64 m_result_size;
|
||||
public:
|
||||
SendTask() : HtcsSignalingTask(TaskType), m_ready_event(os::EventClearMode_ManualClear) { /* ... */ }
|
||||
|
||||
s32 GetHandle() const { return m_handle; }
|
||||
s64 GetSize() const { return m_size; }
|
||||
htcs::MessageFlag GetFlags() const { return m_flags; }
|
||||
const void *GetBuffer() const { return m_buffer; }
|
||||
s64 GetBufferSize() const { return m_buffer_size; }
|
||||
|
||||
void SetBuffer(const void *buffer, s64 buffer_size);
|
||||
void NotifyDataChannelReady();
|
||||
void WaitNotification();
|
||||
public:
|
||||
Result SetArguments(s32 handle, s64 size, htcs::MessageFlag flags);
|
||||
void Complete(htcs::SocketError err, s64 size);
|
||||
Result GetResult(htcs::SocketError *out_err, s64 *out_size) const;
|
||||
public:
|
||||
virtual void Cancel(htc::server::rpc::RpcTaskCancelReason reason) override;
|
||||
virtual Result ProcessResponse(const char *data, size_t size) override;
|
||||
virtual Result CreateRequest(size_t *out, char *data, size_t size, u32 task_id) override;
|
||||
virtual Result ProcessNotification(const char *data, size_t size) override;
|
||||
virtual Result CreateNotification(size_t *out, char *data, size_t size, u32 task_id) override;
|
||||
};
|
||||
|
||||
class ShutdownTask : public HtcsTask {
|
||||
public:
|
||||
static constexpr inline HtcsTaskType TaskType = HtcsTaskType::Shutdown;
|
||||
private:
|
||||
s32 m_handle;
|
||||
ShutdownType m_how;
|
||||
htcs::SocketError m_err;
|
||||
public:
|
||||
ShutdownTask() : HtcsTask(TaskType) { /* ... */ }
|
||||
|
||||
s32 GetHandle() const { return m_handle; }
|
||||
ShutdownType GetHow() const { return m_how; }
|
||||
public:
|
||||
Result SetArguments(s32 handle, ShutdownType how);
|
||||
void Complete(htcs::SocketError err);
|
||||
Result GetResult(htcs::SocketError *out_err) const;
|
||||
public:
|
||||
virtual Result ProcessResponse(const char *data, size_t size) override;
|
||||
virtual Result CreateRequest(size_t *out, char *data, size_t size, u32 task_id) override;
|
||||
};
|
||||
|
||||
class CloseTask : public HtcsTask {
|
||||
public:
|
||||
static constexpr inline HtcsTaskType TaskType = HtcsTaskType::Close;
|
||||
private:
|
||||
s32 m_handle;
|
||||
htcs::SocketError m_err;
|
||||
public:
|
||||
CloseTask() : HtcsTask(TaskType) { /* ... */ }
|
||||
|
||||
s32 GetHandle() const { return m_handle; }
|
||||
public:
|
||||
Result SetArguments(s32 handle);
|
||||
void Complete(htcs::SocketError err);
|
||||
Result GetResult(htcs::SocketError *out_err) const;
|
||||
public:
|
||||
virtual Result ProcessResponse(const char *data, size_t size) override;
|
||||
virtual Result CreateRequest(size_t *out, char *data, size_t size, u32 task_id) override;
|
||||
};
|
||||
|
||||
class ConnectTask : public HtcsTask {
|
||||
public:
|
||||
static constexpr inline HtcsTaskType TaskType = HtcsTaskType::Connect;
|
||||
private:
|
||||
s32 m_handle;
|
||||
HtcsPeerName m_peer_name;
|
||||
HtcsPortName m_port_name;
|
||||
htcs::SocketError m_err;
|
||||
public:
|
||||
ConnectTask() : HtcsTask(TaskType) { /* ... */ }
|
||||
|
||||
s32 GetHandle() const { return m_handle; }
|
||||
const HtcsPeerName &GetPeerName() const { return m_peer_name; }
|
||||
const HtcsPortName &GetPortName() const { return m_port_name; }
|
||||
public:
|
||||
Result SetArguments(s32 handle, const HtcsPeerName &peer_name, const HtcsPortName &port_name);
|
||||
void Complete(htcs::SocketError err);
|
||||
Result GetResult(htcs::SocketError *out_err) const;
|
||||
public:
|
||||
virtual Result ProcessResponse(const char *data, size_t size) override;
|
||||
virtual Result CreateRequest(size_t *out, char *data, size_t size, u32 task_id) override;
|
||||
};
|
||||
|
||||
class ListenTask : public HtcsTask {
|
||||
public:
|
||||
static constexpr inline HtcsTaskType TaskType = HtcsTaskType::Listen;
|
||||
private:
|
||||
s32 m_handle;
|
||||
s32 m_backlog;
|
||||
htcs::SocketError m_err;
|
||||
public:
|
||||
ListenTask() : HtcsTask(TaskType) { /* ... */ }
|
||||
|
||||
s32 GetHandle() const { return m_handle; }
|
||||
s32 GetBacklog() const { return m_backlog; }
|
||||
public:
|
||||
Result SetArguments(s32 handle, s32 backlog);
|
||||
void Complete(htcs::SocketError err);
|
||||
Result GetResult(htcs::SocketError *out_err) const;
|
||||
public:
|
||||
virtual Result ProcessResponse(const char *data, size_t size) override;
|
||||
virtual Result CreateRequest(size_t *out, char *data, size_t size, u32 task_id) override;
|
||||
};
|
||||
|
||||
class AcceptTask : public HtcsSignalingTask {
|
||||
public:
|
||||
static constexpr inline HtcsTaskType TaskType = HtcsTaskType::Accept;
|
||||
private:
|
||||
s32 m_server_handle;
|
||||
htcs::SocketError m_err;
|
||||
s32 m_desc;
|
||||
public:
|
||||
AcceptTask() : HtcsSignalingTask(TaskType) { /* ... */ }
|
||||
|
||||
s32 GetServerHandle() const { return m_server_handle; }
|
||||
public:
|
||||
Result SetArguments(s32 server_handle);
|
||||
void Complete(htcs::SocketError err, s32 desc);
|
||||
Result GetResult(htcs::SocketError *out_err, s32 *out_desc, s32 server_handle) const;
|
||||
public:
|
||||
virtual Result ProcessResponse(const char *data, size_t size) override;
|
||||
virtual Result CreateRequest(size_t *out, char *data, size_t size, u32 task_id) override;
|
||||
};
|
||||
|
||||
class SocketTask : public HtcsTask {
|
||||
public:
|
||||
static constexpr inline HtcsTaskType TaskType = HtcsTaskType::Socket;
|
||||
private:
|
||||
htcs::SocketError m_err;
|
||||
s32 m_desc;
|
||||
public:
|
||||
SocketTask() : HtcsTask(TaskType) { /* ... */ }
|
||||
public:
|
||||
Result SetArguments();
|
||||
void Complete(htcs::SocketError err, s32 desc);
|
||||
Result GetResult(htcs::SocketError *out_err, s32 *out_desc) const;
|
||||
public:
|
||||
virtual Result ProcessResponse(const char *data, size_t size) override;
|
||||
virtual Result CreateRequest(size_t *out, char *data, size_t size, u32 task_id) override;
|
||||
};
|
||||
|
||||
class BindTask : public HtcsTask {
|
||||
public:
|
||||
static constexpr inline HtcsTaskType TaskType = HtcsTaskType::Bind;
|
||||
private:
|
||||
s32 m_handle;
|
||||
HtcsPeerName m_peer_name;
|
||||
HtcsPortName m_port_name;
|
||||
htcs::SocketError m_err;
|
||||
public:
|
||||
BindTask() : HtcsTask(TaskType) { /* ... */ }
|
||||
|
||||
s32 GetHandle() const { return m_handle; }
|
||||
const HtcsPeerName &GetPeerName() const { return m_peer_name; }
|
||||
const HtcsPortName &GetPortName() const { return m_port_name; }
|
||||
public:
|
||||
Result SetArguments(s32 handle, const HtcsPeerName &peer_name, const HtcsPortName &port_name);
|
||||
void Complete(htcs::SocketError err);
|
||||
Result GetResult(htcs::SocketError *out_err) const;
|
||||
public:
|
||||
virtual Result ProcessResponse(const char *data, size_t size) override;
|
||||
virtual Result CreateRequest(size_t *out, char *data, size_t size, u32 task_id) override;
|
||||
};
|
||||
|
||||
class FcntlTask : public HtcsTask {
|
||||
public:
|
||||
static constexpr inline HtcsTaskType TaskType = HtcsTaskType::Fcntl;
|
||||
private:
|
||||
s32 m_handle;
|
||||
s32 m_command;
|
||||
s32 m_value;
|
||||
htcs::SocketError m_err;
|
||||
s32 m_res;
|
||||
public:
|
||||
FcntlTask() : HtcsTask(TaskType) { /* ... */ }
|
||||
|
||||
s32 GetHandle() const { return m_handle; }
|
||||
s32 GetCommand() const { return m_command; }
|
||||
s32 GetValue() const { return m_value; }
|
||||
public:
|
||||
Result SetArguments(s32 handle, s32 command, s32 value);
|
||||
void Complete(htcs::SocketError err, s32 res);
|
||||
Result GetResult(htcs::SocketError *out_err, s32 *out_res) const;
|
||||
public:
|
||||
virtual Result ProcessResponse(const char *data, size_t size) override;
|
||||
virtual Result CreateRequest(size_t *out, char *data, size_t size, u32 task_id) override;
|
||||
};
|
||||
|
||||
class ReceiveSmallTask : public HtcsSignalingTask {
|
||||
public:
|
||||
static constexpr inline HtcsTaskType TaskType = HtcsTaskType::ReceiveSmall;
|
||||
private:
|
||||
s32 m_handle;
|
||||
s64 m_size;
|
||||
htcs::MessageFlag m_flags;
|
||||
char m_buffer[0xE000];
|
||||
htcs::SocketError m_err;
|
||||
s64 m_result_size;
|
||||
public:
|
||||
ReceiveSmallTask() : HtcsSignalingTask(TaskType) { /* ... */ }
|
||||
|
||||
s32 GetHandle() const { return m_handle; }
|
||||
s64 GetSize() const { return m_size; }
|
||||
htcs::MessageFlag GetFlags() const { return m_flags; }
|
||||
void *GetBuffer() { return m_buffer; }
|
||||
s64 GetBufferSize() const { return static_cast<s64>(sizeof(m_buffer)); }
|
||||
|
||||
s64 GetResultSize() const {
|
||||
AMS_ASSERT(this->GetTaskState() == htc::server::rpc::RpcTaskState::Completed);
|
||||
return m_result_size;
|
||||
}
|
||||
public:
|
||||
Result SetArguments(s32 handle, s64 size, htcs::MessageFlag flags);
|
||||
void Complete(htcs::SocketError err, s64 size);
|
||||
Result GetResult(htcs::SocketError *out_err, s64 *out_size) const;
|
||||
public:
|
||||
virtual Result ProcessResponse(const char *data, size_t size) override;
|
||||
virtual Result CreateRequest(size_t *out, char *data, size_t size, u32 task_id) override;
|
||||
virtual bool IsReceiveBufferRequired() override;
|
||||
};
|
||||
|
||||
class SendSmallTask : public HtcsSignalingTask {
|
||||
public:
|
||||
static constexpr inline HtcsTaskType TaskType = HtcsTaskType::SendSmall;
|
||||
private:
|
||||
os::Event m_ready_event;
|
||||
s32 m_handle;
|
||||
s64 m_size;
|
||||
htcs::MessageFlag m_flags;
|
||||
char m_buffer[0xE000];
|
||||
s64 m_buffer_size;
|
||||
htcs::SocketError m_err;
|
||||
s64 m_result_size;
|
||||
public:
|
||||
SendSmallTask() : HtcsSignalingTask(TaskType), m_ready_event(os::EventClearMode_ManualClear) { /* ... */ }
|
||||
|
||||
s32 GetHandle() const { return m_handle; }
|
||||
s64 GetSize() const { return m_size; }
|
||||
htcs::MessageFlag GetFlags() const { return m_flags; }
|
||||
void *GetBuffer() { return m_buffer; }
|
||||
s64 GetBufferSize() const { return m_buffer_size; }
|
||||
|
||||
void SetBuffer(const void *buffer, s64 buffer_size);
|
||||
void NotifyDataChannelReady();
|
||||
void WaitNotification();
|
||||
public:
|
||||
Result SetArguments(s32 handle, s64 size, htcs::MessageFlag flags);
|
||||
void Complete(htcs::SocketError err, s64 size);
|
||||
Result GetResult(htcs::SocketError *out_err, s64 *out_size) const;
|
||||
public:
|
||||
virtual void Cancel(htc::server::rpc::RpcTaskCancelReason reason) override;
|
||||
virtual Result ProcessResponse(const char *data, size_t size) override;
|
||||
virtual Result CreateRequest(size_t *out, char *data, size_t size, u32 task_id) override;
|
||||
virtual bool IsSendBufferRequired() override;
|
||||
};
|
||||
|
||||
class SelectTask : public HtcsSignalingTask {
|
||||
public:
|
||||
static constexpr inline HtcsTaskType TaskType = HtcsTaskType::Select;
|
||||
private:
|
||||
s32 m_handles[SocketCountMax * 3];
|
||||
s32 m_read_handle_count;
|
||||
s32 m_write_handle_count;
|
||||
s32 m_exception_handle_count;
|
||||
s64 m_tv_sec;
|
||||
s64 m_tv_usec;
|
||||
htcs::SocketError m_err;
|
||||
s32 m_out_handles[SocketCountMax * 3];
|
||||
s32 m_out_read_handle_count;
|
||||
s32 m_out_write_handle_count;
|
||||
s32 m_out_exception_handle_count;
|
||||
public:
|
||||
SelectTask() : HtcsSignalingTask(TaskType) { /* ... */ }
|
||||
|
||||
const s32 *GetHandles() const { return m_handles; }
|
||||
s32 GetReadHandleCount() const { return m_read_handle_count; }
|
||||
s32 GetWriteHandleCount() const { return m_write_handle_count; }
|
||||
s32 GetExceptionHandleCount() const { return m_exception_handle_count; }
|
||||
s64 GetTimeoutSeconds() const { return m_tv_sec; }
|
||||
s64 GetTimeoutMicroSeconds() const { return m_tv_usec; }
|
||||
public:
|
||||
Result SetArguments(Span<const int> read_handles, Span<const int> write_handles, Span<const int> exception_handles, s64 tv_sec, s64 tv_usec);
|
||||
void Complete(htcs::SocketError err, s32 read_handle_count, s32 write_handle_count, s32 exception_handle_count, const void *body, s64 body_size);
|
||||
Result GetResult(htcs::SocketError *out_err, bool *out_empty, Span<int> read_handles, Span<int> write_handles, Span<int> exception_handles) const;
|
||||
public:
|
||||
virtual Result ProcessResponse(const char *data, size_t size) override;
|
||||
virtual Result CreateRequest(size_t *out, char *data, size_t size, u32 task_id) override;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_manager_service_object.hpp"
|
||||
|
||||
namespace ams::htcs::server {
|
||||
|
||||
namespace {
|
||||
|
||||
static constexpr inline size_t NumServers = 1;
|
||||
static constexpr inline size_t MaxSessions = 63;
|
||||
static constexpr inline sm::ServiceName ServiceName = sm::ServiceName::Encode("htcs");
|
||||
|
||||
struct ServerOptions {
|
||||
static constexpr size_t PointerBufferSize = 0x80;
|
||||
static constexpr size_t MaxDomains = 0x10;
|
||||
static constexpr size_t MaxDomainObjects = 100;
|
||||
static constexpr bool CanDeferInvokeRequest = false;
|
||||
static constexpr bool CanManageMitmServers = false;
|
||||
};
|
||||
|
||||
using ServerManager = sf::hipc::ServerManager<NumServers, ServerOptions, MaxSessions>;
|
||||
|
||||
/* Service object. */
|
||||
ServerManager g_server_manager;
|
||||
|
||||
/* Service object. */
|
||||
constinit sf::UnmanagedServiceObject<tma::IHtcsManager, ManagerServiceObject> g_htcs_service_object;
|
||||
|
||||
}
|
||||
|
||||
void Initialize() {
|
||||
/* Add a reference to the htcs manager. */
|
||||
htcs::impl::HtcsManagerHolder::AddReference();
|
||||
}
|
||||
|
||||
void RegisterHipcServer() {
|
||||
/* Register the service. */
|
||||
R_ABORT_UNLESS(g_server_manager.RegisterObjectForServer(g_htcs_service_object.GetShared(), ServiceName, MaxSessions));
|
||||
}
|
||||
|
||||
void LoopHipcServer() {
|
||||
/* Loop, servicing services. */
|
||||
g_server_manager.LoopProcess();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_manager_service_object.hpp"
|
||||
#include "htcs_socket_service_object.hpp"
|
||||
#include "htcs_service_object_allocator.hpp"
|
||||
#include "../impl/htcs_manager.hpp"
|
||||
#include "../impl/htcs_impl.hpp"
|
||||
|
||||
namespace ams::htcs::server {
|
||||
|
||||
namespace {
|
||||
|
||||
class StaticAllocatorInitializer {
|
||||
public:
|
||||
StaticAllocatorInitializer() {
|
||||
ServiceObjectAllocator::Initialize(lmem::CreateOption_ThreadSafe);
|
||||
}
|
||||
} g_static_allocator_initializer;
|
||||
|
||||
}
|
||||
|
||||
Result ManagerServiceObject::GetPeerNameAny(sf::Out<htcs::HtcsPeerName> out) {
|
||||
*out = impl::GetPeerNameAny();
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result ManagerServiceObject::GetDefaultHostName(sf::Out<htcs::HtcsPeerName> out) {
|
||||
*out = impl::GetDefaultHostName();
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result ManagerServiceObject::CreateSocketOld(sf::Out<s32> out_err, sf::Out<sf::SharedPointer<tma::ISocket>> out) {
|
||||
R_RETURN(this->CreateSocket(out_err, out, false));
|
||||
}
|
||||
|
||||
Result ManagerServiceObject::CreateSocket(sf::Out<s32> out_err, sf::Out<sf::SharedPointer<tma::ISocket>> out, bool enable_disconnection_emulation) {
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* Create a new socket. */
|
||||
s32 desc;
|
||||
manager->Socket(out_err.GetPointer(), std::addressof(desc), enable_disconnection_emulation);
|
||||
|
||||
/* If an error occurred, we're done. */
|
||||
R_SUCCEED_IF(*out_err != 0);
|
||||
|
||||
/* Create a new socket object. */
|
||||
*out = ServiceObjectFactory::CreateSharedEmplaced<tma::ISocket, SocketServiceObject>(this, desc);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result ManagerServiceObject::RegisterProcessId(const sf::ClientProcessId &client_pid) {
|
||||
/* NOTE: Nintendo does nothing here. */
|
||||
AMS_UNUSED(client_pid);
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result ManagerServiceObject::MonitorManager(const sf::ClientProcessId &client_pid) {
|
||||
/* NOTE: Nintendo does nothing here. */
|
||||
AMS_UNUSED(client_pid);
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result ManagerServiceObject::StartSelect(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, const sf::InMapAliasArray<s32> &read_handles, const sf::InMapAliasArray<s32> &write_handles, const sf::InMapAliasArray<s32> &exception_handles, s64 tv_sec, s64 tv_usec) {
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* Start the select. */
|
||||
os::NativeHandle event_handle;
|
||||
R_TRY(manager->StartSelect(out_task_id.GetPointer(), std::addressof(event_handle), read_handles.ToSpan(), write_handles.ToSpan(), exception_handles.ToSpan(), tv_sec, tv_usec));
|
||||
|
||||
/* Set the output event handle. */
|
||||
out_event.SetValue(event_handle, true);
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result ManagerServiceObject::EndSelect(sf::Out<s32> out_err, sf::Out<s32> out_count, const sf::OutMapAliasArray<s32> &read_handles, const sf::OutMapAliasArray<s32> &write_handles, const sf::OutMapAliasArray<s32> &exception_handles, u32 task_id) {
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* End the select. */
|
||||
R_RETURN(manager->EndSelect(out_err.GetPointer(), out_count.GetPointer(), read_handles.ToSpan(), write_handles.ToSpan(), exception_handles.ToSpan(), task_id));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
namespace ams::htcs::server {
|
||||
|
||||
class ManagerServiceObject : public sf::ISharedObject {
|
||||
public:
|
||||
Result Socket(sf::Out<s32> out_err, sf::Out<s32> out_sock);
|
||||
Result Close(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 desc);
|
||||
Result Connect(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 desc, const htcs::SockAddrHtcs &address);
|
||||
Result Bind(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 desc, const htcs::SockAddrHtcs &address);
|
||||
Result Listen(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 desc, s32 backlog_count);
|
||||
Result Accept(sf::Out<s32> out_err, sf::Out<s32> out_res, sf::Out<htcs::SockAddrHtcs> out_address, s32 desc);
|
||||
Result Recv(sf::Out<s32> out_err, sf::Out<s64> out_size, const sf::OutBuffer &buffer, s32 desc, s32 flags);
|
||||
Result Send(sf::Out<s32> out_err, sf::Out<s64> out_size, s32 desc, const sf::InBuffer &buffer, s32 flags);
|
||||
Result Shutdown(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 desc, s32 how);
|
||||
Result Fcntl(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 desc, s32 command, s32 value);
|
||||
Result GetPeerNameAny(sf::Out<htcs::HtcsPeerName> out);
|
||||
Result GetDefaultHostName(sf::Out<htcs::HtcsPeerName> out);
|
||||
Result CreateSocketOld(sf::Out<s32> out_err, sf::Out<sf::SharedPointer<tma::ISocket>> out);
|
||||
Result CreateSocket(sf::Out<s32> out_err, sf::Out<sf::SharedPointer<tma::ISocket>> out, bool enable_disconnection_emulation);
|
||||
Result RegisterProcessId(const sf::ClientProcessId &client_pid);
|
||||
Result MonitorManager(const sf::ClientProcessId &client_pid);
|
||||
Result StartSelect(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, const sf::InMapAliasArray<s32> &read_handles, const sf::InMapAliasArray<s32> &write_handles, const sf::InMapAliasArray<s32> &exception_handles, s64 tv_sec, s64 tv_usec);
|
||||
Result EndSelect(sf::Out<s32> out_err, sf::Out<s32> out_count, const sf::OutMapAliasArray<s32> &read_handles, const sf::OutMapAliasArray<s32> &write_handles, const sf::OutMapAliasArray<s32> &exception_handles, u32 task_id);
|
||||
};
|
||||
static_assert(tma::IsIHtcsManager<ManagerServiceObject>);
|
||||
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_manager_service_object.hpp"
|
||||
#include "htcs_socket_service_object.hpp"
|
||||
|
||||
namespace ams::htcs::server {
|
||||
|
||||
#define AMS_HTCS_MANAGER_DEPRECATED_API(...) ({ AMS_UNUSED(__VA_ARGS__); AMS_ABORT("Deprecated IHtcsManager API %s was called.\n", AMS_CURRENT_FUNCTION_NAME); })
|
||||
|
||||
Result ManagerServiceObject::Socket(sf::Out<s32> out_err, sf::Out<s32> out_sock) {
|
||||
/* NOTE: This is a deprecated API, and Nintendo aborts when it is called. */
|
||||
AMS_HTCS_MANAGER_DEPRECATED_API(out_err, out_sock);
|
||||
}
|
||||
|
||||
Result ManagerServiceObject::Close(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 desc) {
|
||||
/* NOTE: This is a deprecated API, and Nintendo aborts when it is called. */
|
||||
AMS_HTCS_MANAGER_DEPRECATED_API(out_err, out_res, desc);
|
||||
}
|
||||
|
||||
Result ManagerServiceObject::Connect(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 desc, const htcs::SockAddrHtcs &address) {
|
||||
/* NOTE: This is a deprecated API, and Nintendo aborts when it is called. */
|
||||
AMS_HTCS_MANAGER_DEPRECATED_API(out_err, out_res, desc, address);
|
||||
}
|
||||
|
||||
Result ManagerServiceObject::Bind(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 desc, const htcs::SockAddrHtcs &address) {
|
||||
/* NOTE: This is a deprecated API, and Nintendo aborts when it is called. */
|
||||
AMS_HTCS_MANAGER_DEPRECATED_API(out_err, out_res, desc, address);
|
||||
}
|
||||
|
||||
Result ManagerServiceObject::Listen(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 desc, s32 backlog_count) {
|
||||
/* NOTE: This is a deprecated API, and Nintendo aborts when it is called. */
|
||||
AMS_HTCS_MANAGER_DEPRECATED_API(out_err, out_res, desc, backlog_count);
|
||||
}
|
||||
|
||||
Result ManagerServiceObject::Accept(sf::Out<s32> out_err, sf::Out<s32> out_res, sf::Out<htcs::SockAddrHtcs> out_address, s32 desc) {
|
||||
/* NOTE: This is a deprecated API, and Nintendo aborts when it is called. */
|
||||
AMS_HTCS_MANAGER_DEPRECATED_API(out_err, out_res, out_address, desc);
|
||||
}
|
||||
|
||||
Result ManagerServiceObject::Recv(sf::Out<s32> out_err, sf::Out<s64> out_size, const sf::OutBuffer &buffer, s32 desc, s32 flags) {
|
||||
/* NOTE: This is a deprecated API, and Nintendo aborts when it is called. */
|
||||
AMS_HTCS_MANAGER_DEPRECATED_API(out_err, out_size, buffer, desc, flags);
|
||||
}
|
||||
|
||||
Result ManagerServiceObject::Send(sf::Out<s32> out_err, sf::Out<s64> out_size, s32 desc, const sf::InBuffer &buffer, s32 flags) {
|
||||
/* NOTE: This is a deprecated API, and Nintendo aborts when it is called. */
|
||||
AMS_HTCS_MANAGER_DEPRECATED_API(out_err, out_size, desc, buffer, flags);
|
||||
}
|
||||
|
||||
Result ManagerServiceObject::Shutdown(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 desc, s32 how) {
|
||||
/* NOTE: This is a deprecated API, and Nintendo aborts when it is called. */
|
||||
AMS_HTCS_MANAGER_DEPRECATED_API(out_err, out_res, desc, how);
|
||||
}
|
||||
|
||||
Result ManagerServiceObject::Fcntl(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 desc, s32 command, s32 value) {
|
||||
/* NOTE: This is a deprecated API, and Nintendo aborts when it is called. */
|
||||
AMS_HTCS_MANAGER_DEPRECATED_API(out_err, out_res, desc, command, value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
namespace ams::htcs::server {
|
||||
|
||||
struct ServiceObjectAllocatorTag;
|
||||
using ServiceObjectAllocator = ams::sf::ExpHeapStaticAllocator<32_KB, ServiceObjectAllocatorTag>;
|
||||
using ServiceObjectFactory = ams::sf::ObjectFactory<typename ServiceObjectAllocator::Policy>;
|
||||
|
||||
}
|
||||
@@ -1,333 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_socket_service_object.hpp"
|
||||
#include "htcs_service_object_allocator.hpp"
|
||||
#include "../impl/htcs_manager.hpp"
|
||||
|
||||
namespace ams::htcs::server {
|
||||
|
||||
SocketServiceObject::SocketServiceObject(ManagerServiceObject *manager, s32 desc) : m_manager(manager, true), m_desc(desc) {
|
||||
/* ... */
|
||||
}
|
||||
|
||||
SocketServiceObject::~SocketServiceObject() {
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* Close the underlying socket. */
|
||||
s32 dummy_err, dummy_res;
|
||||
manager->Close(std::addressof(dummy_err), std::addressof(dummy_res), m_desc);
|
||||
}
|
||||
|
||||
Result SocketServiceObject::Close(sf::Out<s32> out_err, sf::Out<s32> out_res) {
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* Close the underlying socket. */
|
||||
manager->Close(out_err.GetPointer(), out_res.GetPointer(), m_desc);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SocketServiceObject::Connect(sf::Out<s32> out_err, sf::Out<s32> out_res, const htcs::SockAddrHtcs &address) {
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* Perform the connect. */
|
||||
manager->Connect(out_err.GetPointer(), out_res.GetPointer(), address, m_desc);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SocketServiceObject::Bind(sf::Out<s32> out_err, sf::Out<s32> out_res, const htcs::SockAddrHtcs &address) {
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* Perform the bind. */
|
||||
manager->Bind(out_err.GetPointer(), out_res.GetPointer(), address, m_desc);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SocketServiceObject::Listen(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 backlog_count) {
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* Perform the listen. */
|
||||
manager->Listen(out_err.GetPointer(), out_res.GetPointer(), backlog_count, m_desc);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SocketServiceObject::Recv(sf::Out<s32> out_err, sf::Out<s64> out_size, const sf::OutAutoSelectBuffer &buffer, s32 flags) {
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* Perform the recv. */
|
||||
manager->Recv(out_err.GetPointer(), out_size.GetPointer(), reinterpret_cast<char *>(buffer.GetPointer()), buffer.GetSize(), flags, m_desc);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SocketServiceObject::Send(sf::Out<s32> out_err, sf::Out<s64> out_size, const sf::InAutoSelectBuffer &buffer, s32 flags) {
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* Perform the send. */
|
||||
manager->Send(out_err.GetPointer(), out_size.GetPointer(), reinterpret_cast<const char *>(buffer.GetPointer()), buffer.GetSize(), flags, m_desc);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SocketServiceObject::Shutdown(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 how) {
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* Perform the shutdown. */
|
||||
manager->Shutdown(out_err.GetPointer(), out_res.GetPointer(), how, m_desc);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SocketServiceObject::Fcntl(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 command, s32 value) {
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* Perform the fcntl. */
|
||||
manager->Fcntl(out_err.GetPointer(), out_res.GetPointer(), command, value, m_desc);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SocketServiceObject::AcceptStart(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event) {
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* Start the accept. */
|
||||
os::NativeHandle event_handle;
|
||||
R_TRY(manager->AcceptStart(out_task_id.GetPointer(), std::addressof(event_handle), m_desc));
|
||||
|
||||
/* Set the output event handle. */
|
||||
out_event.SetValue(event_handle, true);
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SocketServiceObject::AcceptResults(sf::Out<s32> out_err, sf::Out<sf::SharedPointer<tma::ISocket>> out, sf::Out<htcs::SockAddrHtcs> out_address, u32 task_id) {
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* Get the accept results. */
|
||||
s32 desc = -1;
|
||||
manager->AcceptResults(out_err.GetPointer(), std::addressof(desc), out_address.GetPointer(), task_id, m_desc);
|
||||
|
||||
/* If an error occurred, we're done. */
|
||||
R_SUCCEED_IF(*out_err != 0);
|
||||
|
||||
/* Create a new socket object. */
|
||||
*out = ServiceObjectFactory::CreateSharedEmplaced<tma::ISocket, SocketServiceObject>(m_manager.Get(), desc);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SocketServiceObject::RecvStart(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, s32 mem_size, s32 flags) {
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* Start the recv. */
|
||||
os::NativeHandle event_handle;
|
||||
R_TRY(manager->RecvStart(out_task_id.GetPointer(), std::addressof(event_handle), mem_size, m_desc, flags));
|
||||
|
||||
/* Set the output event handle. */
|
||||
out_event.SetValue(event_handle, true);
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SocketServiceObject::RecvResults(sf::Out<s32> out_err, sf::Out<s64> out_size, const sf::OutAutoSelectBuffer &buffer, u32 task_id) {
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* Get the recv results. */
|
||||
manager->RecvResults(out_err.GetPointer(), out_size.GetPointer(), reinterpret_cast<char *>(buffer.GetPointer()), buffer.GetSize(), task_id, m_desc);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SocketServiceObject::RecvLargeStart(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, s32 unaligned_size_start, s32 unaligned_size_end, s64 aligned_size, sf::CopyHandle &&mem_handle, s32 flags) {
|
||||
/* Check that the transfer memory size is okay. */
|
||||
R_UNLESS(util::IsIntValueRepresentable<size_t>(aligned_size), htcs::ResultInvalidSize());
|
||||
|
||||
/* Attach the transfer memory. */
|
||||
os::TransferMemoryType tmem;
|
||||
os::AttachTransferMemory(std::addressof(tmem), static_cast<size_t>(aligned_size), mem_handle.GetOsHandle(), mem_handle.IsManaged());
|
||||
mem_handle.Detach();
|
||||
ON_SCOPE_EXIT { os::DestroyTransferMemory(std::addressof(tmem)); };
|
||||
|
||||
/* Map the transfer memory. */
|
||||
void *address;
|
||||
R_TRY(os::MapTransferMemory(std::addressof(address), std::addressof(tmem), os::MemoryPermission_None));
|
||||
ON_SCOPE_EXIT { os::UnmapTransferMemory(std::addressof(tmem)); };
|
||||
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* Start the large receive. */
|
||||
os::NativeHandle event_handle;
|
||||
R_TRY(manager->RecvStart(out_task_id.GetPointer(), std::addressof(event_handle), unaligned_size_start + aligned_size + unaligned_size_end, m_desc, flags));
|
||||
|
||||
/* Set the output event handle. */
|
||||
out_event.SetValue(event_handle, true);
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SocketServiceObject::SendStartOld(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, const sf::InAutoSelectBuffer &buffer, s32 flags) {
|
||||
R_RETURN(this->SendStart(out_task_id, out_event, sf::InNonSecureAutoSelectBuffer(buffer.GetPointer(), buffer.GetSize()), flags));
|
||||
}
|
||||
|
||||
Result SocketServiceObject::SendLargeStart(sf::Out<u32> 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) {
|
||||
/* Check that the sizes are okay. */
|
||||
R_UNLESS(util::IsIntValueRepresentable<s64>(start_buffer.GetSize()), htcs::ResultInvalidSize());
|
||||
R_UNLESS(util::IsIntValueRepresentable<s64>(end_buffer.GetSize()), htcs::ResultInvalidSize());
|
||||
R_UNLESS(util::IsIntValueRepresentable<size_t>(aligned_size), htcs::ResultInvalidSize());
|
||||
|
||||
/* Attach the transfer memory. */
|
||||
os::TransferMemoryType tmem;
|
||||
os::AttachTransferMemory(std::addressof(tmem), static_cast<size_t>(aligned_size), mem_handle.GetOsHandle(), mem_handle.IsManaged());
|
||||
mem_handle.Detach();
|
||||
ON_SCOPE_EXIT { os::DestroyTransferMemory(std::addressof(tmem)); };
|
||||
|
||||
/* Map the transfer memory. */
|
||||
void *address;
|
||||
R_TRY(os::MapTransferMemory(std::addressof(address), std::addressof(tmem), os::MemoryPermission_None));
|
||||
ON_SCOPE_EXIT { os::UnmapTransferMemory(std::addressof(tmem)); };
|
||||
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* Start the large send. */
|
||||
constexpr auto NumBuffers = 3;
|
||||
const char *pointers[NumBuffers] = { reinterpret_cast<const char *>(start_buffer.GetPointer()), static_cast<const char *>(address), reinterpret_cast<const char *>(end_buffer.GetPointer()) };
|
||||
s64 sizes[NumBuffers] = { static_cast<s64>(start_buffer.GetSize()), aligned_size, static_cast<s64>(end_buffer.GetSize()) };
|
||||
|
||||
os::NativeHandle event_handle;
|
||||
R_TRY(manager->SendLargeStart(out_task_id.GetPointer(), std::addressof(event_handle), pointers, sizes, NumBuffers, m_desc, flags));
|
||||
|
||||
/* Set the output event handle. */
|
||||
out_event.SetValue(event_handle, true);
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SocketServiceObject::SendResults(sf::Out<s32> out_err, sf::Out<s64> out_size, u32 task_id) {
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* Get the send results. */
|
||||
manager->SendResults(out_err.GetPointer(), out_size.GetPointer(), task_id, m_desc);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SocketServiceObject::StartSend(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, sf::Out<s64> out_max_size, s64 size, s32 flags) {
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* Start the send. */
|
||||
os::NativeHandle event_handle;
|
||||
R_TRY(manager->StartSend(out_task_id.GetPointer(), std::addressof(event_handle), m_desc, size, flags));
|
||||
|
||||
/* Set the output max size to the size. */
|
||||
*out_max_size = size;
|
||||
|
||||
/* Set the output event handle. */
|
||||
out_event.SetValue(event_handle, true);
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SocketServiceObject::ContinueSendOld(sf::Out<s64> out_size, sf::Out<bool> out_wait, const sf::InAutoSelectBuffer &buffer, u32 task_id) {
|
||||
R_RETURN(this->ContinueSend(out_size, out_wait, sf::InNonSecureAutoSelectBuffer(buffer.GetPointer(), buffer.GetSize()), task_id));
|
||||
}
|
||||
|
||||
Result SocketServiceObject::EndSend(sf::Out<s32> out_err, sf::Out<s64> out_size, u32 task_id) {
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* End the send. */
|
||||
manager->EndSend(out_err.GetPointer(), out_size.GetPointer(), task_id, m_desc);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SocketServiceObject::StartRecv(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, s64 size, s32 flags) {
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* Start the recv. */
|
||||
os::NativeHandle event_handle;
|
||||
R_TRY(manager->StartRecv(out_task_id.GetPointer(), std::addressof(event_handle), size, m_desc, flags));
|
||||
|
||||
/* Set the output event handle. */
|
||||
out_event.SetValue(event_handle, true);
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SocketServiceObject::EndRecv(sf::Out<s32> out_err, sf::Out<s64> out_size, const sf::OutAutoSelectBuffer &buffer, u32 task_id) {
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* End the recv. */
|
||||
manager->EndRecv(out_err.GetPointer(), out_size.GetPointer(), reinterpret_cast<char *>(buffer.GetPointer()), buffer.GetSize(), task_id, m_desc);
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SocketServiceObject::SendStart(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, const sf::InNonSecureAutoSelectBuffer &buffer, s32 flags) {
|
||||
/* Check that the sizes are okay. */
|
||||
R_UNLESS(util::IsIntValueRepresentable<s64>(buffer.GetSize()), htcs::ResultInvalidSize());
|
||||
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* Start the send. */
|
||||
os::NativeHandle event_handle;
|
||||
R_TRY(manager->SendStart(out_task_id.GetPointer(), std::addressof(event_handle), reinterpret_cast<const char *>(buffer.GetPointer()), buffer.GetSize(), m_desc, flags));
|
||||
|
||||
/* Set the output event handle. */
|
||||
out_event.SetValue(event_handle, true);
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SocketServiceObject::ContinueSend(sf::Out<s64> out_size, sf::Out<bool> out_wait, const sf::InNonSecureAutoSelectBuffer &buffer, u32 task_id) {
|
||||
/* Check that the sizes are okay. */
|
||||
R_UNLESS(util::IsIntValueRepresentable<s64>(buffer.GetSize()), htcs::ResultInvalidSize());
|
||||
|
||||
/* Get the htcs manager. */
|
||||
auto *manager = impl::HtcsManagerHolder::GetHtcsManager();
|
||||
|
||||
/* Continue the send. */
|
||||
R_TRY(manager->ContinueSend(out_size.GetPointer(), reinterpret_cast<const char *>(buffer.GetPointer()), buffer.GetSize(), task_id, m_desc));
|
||||
|
||||
/* We aren't doing a waiting send. */
|
||||
*out_wait = false;
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result SocketServiceObject::GetPrimitive(sf::Out<s32> out) {
|
||||
/* Get our descriptor. */
|
||||
*out = m_desc;
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_manager_service_object.hpp"
|
||||
|
||||
namespace ams::htcs::server {
|
||||
|
||||
class SocketServiceObject {
|
||||
private:
|
||||
sf::SharedPointer<ManagerServiceObject> m_manager;
|
||||
s32 m_desc;
|
||||
public:
|
||||
SocketServiceObject(ManagerServiceObject *manager, s32 desc);
|
||||
~SocketServiceObject();
|
||||
public:
|
||||
Result Close(sf::Out<s32> out_err, sf::Out<s32> out_res);
|
||||
Result Connect(sf::Out<s32> out_err, sf::Out<s32> out_res, const htcs::SockAddrHtcs &address);
|
||||
Result Bind(sf::Out<s32> out_err, sf::Out<s32> out_res, const htcs::SockAddrHtcs &address);
|
||||
Result Listen(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 backlog_count);
|
||||
Result Accept(sf::Out<s32> out_err, sf::Out<sf::SharedPointer<tma::ISocket>> out, sf::Out<htcs::SockAddrHtcs> out_address);
|
||||
Result Recv(sf::Out<s32> out_err, sf::Out<s64> out_size, const sf::OutAutoSelectBuffer &buffer, s32 flags);
|
||||
Result Send(sf::Out<s32> out_err, sf::Out<s64> out_size, const sf::InAutoSelectBuffer &buffer, s32 flags);
|
||||
Result Shutdown(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 how);
|
||||
Result Fcntl(sf::Out<s32> out_err, sf::Out<s32> out_res, s32 command, s32 value);
|
||||
Result AcceptStart(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event);
|
||||
Result AcceptResults(sf::Out<s32> out_err, sf::Out<sf::SharedPointer<tma::ISocket>> out, sf::Out<htcs::SockAddrHtcs> out_address, u32 task_id);
|
||||
Result RecvStart(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, s32 mem_size, s32 flags);
|
||||
Result RecvResults(sf::Out<s32> out_err, sf::Out<s64> out_size, const sf::OutAutoSelectBuffer &buffer, u32 task_id);
|
||||
Result RecvLargeStart(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, s32 unaligned_size_start, s32 unaligned_size_end, s64 aligned_size, sf::CopyHandle &&mem_handle, s32 flags);
|
||||
Result SendStartOld(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, const sf::InAutoSelectBuffer &buffer, s32 flags);
|
||||
Result SendLargeStart(sf::Out<u32> 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);
|
||||
Result SendResults(sf::Out<s32> out_err, sf::Out<s64> out_size, u32 task_id);
|
||||
Result StartSend(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, sf::Out<s64> out_max_size, s64 size, s32 flags);
|
||||
Result ContinueSendOld(sf::Out<s64> out_size, sf::Out<bool> out_wait, const sf::InAutoSelectBuffer &buffer, u32 task_id);
|
||||
Result EndSend(sf::Out<s32> out_err, sf::Out<s64> out_size, u32 task_id);
|
||||
Result StartRecv(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, s64 size, s32 flags);
|
||||
Result EndRecv(sf::Out<s32> out_err, sf::Out<s64> out_size, const sf::OutAutoSelectBuffer &buffer, u32 task_id);
|
||||
Result SendStart(sf::Out<u32> out_task_id, sf::OutCopyHandle out_event, const sf::InNonSecureAutoSelectBuffer &buffer, s32 flags);
|
||||
Result ContinueSend(sf::Out<s64> out_size, sf::Out<bool> out_wait, const sf::InNonSecureAutoSelectBuffer &buffer, u32 task_id);
|
||||
Result GetPrimitive(sf::Out<s32> out);
|
||||
};
|
||||
static_assert(tma::IsISocket<SocketServiceObject>);
|
||||
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "htcs_manager_service_object.hpp"
|
||||
#include "htcs_socket_service_object.hpp"
|
||||
|
||||
namespace ams::htcs::server {
|
||||
|
||||
#define AMS_HTCS_MANAGER_DEPRECATED_API(...) ({ AMS_UNUSED(__VA_ARGS__); AMS_ABORT("Deprecated IHtcsManager API %s was called.\n", AMS_CURRENT_FUNCTION_NAME); })
|
||||
|
||||
Result SocketServiceObject::Accept(sf::Out<s32> out_err, sf::Out<sf::SharedPointer<tma::ISocket>> out, sf::Out<htcs::SockAddrHtcs> out_address) {
|
||||
/* NOTE: This is a deprecated API, and Nintendo aborts when it is called. */
|
||||
AMS_HTCS_MANAGER_DEPRECATED_API(out_err, out, out_address);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user