htcs: fix magic template argument deduction, do Close/Connect/Bind

This commit is contained in:
Michael Scire 2021-02-18 04:15:23 -08:00 committed by SciresM
parent f71943c03a
commit 0c791f2279
8 changed files with 135 additions and 23 deletions

View File

@ -37,13 +37,13 @@ namespace ams::htc::server::rpc {
using RpcTaskArgumentsType = decltype(RpcTaskFunctionTraits::GetSetArgumentsImpl(&T::SetArguments));
template<typename T> requires IsRpcTask<T>
using RpcTaskResultType = decltype(RpcTaskFunctionTraits::GetGetResultImpl(&T::GetResult));
using RpcTaskResultsType = decltype(RpcTaskFunctionTraits::GetGetResultImpl(&T::GetResult));
template<typename T, typename... Args>
concept IsRpcTaskArgumentsType = IsRpcTask<T> && std::same_as<std::tuple<Args...>, RpcTaskArgumentsType<T>>;
template<typename T, size_t Ix> requires IsRpcTask<T>
using RpcTaskArgumentType = typename std::tuple_element<Ix, RpcTaskArgumentsType<T>>::type;
template<typename T, typename... Args>
concept IsRpcTaskResultType = IsRpcTask<T> && std::same_as<std::tuple<Args...>, RpcTaskResultType<T>>;
template<typename T, size_t Ix> requires IsRpcTask<T>
using RpcTaskResultType = typename std::tuple_element<Ix, RpcTaskResultsType<T>>::type;
class RpcClient {
private:
@ -91,13 +91,9 @@ namespace ams::htc::server::rpc {
Result ReceiveHeader(RpcPacket *header);
Result ReceiveBody(char *dst, size_t size);
Result SendRequest(const char *src, size_t size);
public:
void Wait(u32 task_id) {
os::WaitEvent(m_task_table.Get<Task>(task_id)->GetEvent());
}
template<typename T, typename... Args> requires IsRpcTaskArgumentsType<T, Args...>
Result Begin(u32 *out_task_id, Args... args) {
private:
template<typename T, size_t... Ix> requires IsRpcTask<T>
ALWAYS_INLINE Result BeginImpl(std::index_sequence<Ix...>, u32 *out_task_id, RpcTaskArgumentType<T, Ix>... args) {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
@ -138,8 +134,8 @@ namespace ams::htc::server::rpc {
return ResultSuccess();
}
template<typename T, typename... Args> requires IsRpcTaskResultType<T, Args...>
Result End(u32 task_id, Args... args) {
template<typename T, size_t... Ix> requires IsRpcTask<T>
ALWAYS_INLINE Result EndImpl(std::index_sequence<Ix...>, u32 task_id, RpcTaskResultType<T, Ix>... args) {
/* Lock ourselves. */
std::scoped_lock lk(m_mutex);
@ -173,6 +169,20 @@ namespace ams::htc::server::rpc {
return ResultSuccess();
}
public:
void Wait(u32 task_id) {
os::WaitEvent(m_task_table.Get<Task>(task_id)->GetEvent());
}
template<typename T, typename... Args> requires (IsRpcTask<T> && sizeof...(Args) == std::tuple_size<RpcTaskArgumentsType<T>>::value)
Result Begin(u32 *out_task_id, Args &&... args) {
return this->BeginImpl<T>(std::make_index_sequence<std::tuple_size<RpcTaskArgumentsType<T>>::value>(), out_task_id, std::forward<Args>(args)...);
}
template<typename T, typename... Args> requires (IsRpcTask<T> && sizeof...(Args) == std::tuple_size<RpcTaskResultsType<T>>::value)
Result End(u32 task_id, Args &&... args) {
return this->EndImpl<T>(std::make_index_sequence<std::tuple_size<RpcTaskResultsType<T>>::value>(), task_id, std::forward<Args>(args)...);
}
};
}

View File

@ -58,8 +58,7 @@ namespace ams::htcs::impl {
void HtcsManager::Close(s32 *out_err, s32 *out_res, s32 desc) {
/* Invoke our implementation. */
s32 err;
const Result result = m_impl->DestroySocket(std::addressof(err), desc);
const Result result = m_impl->DestroySocket(desc);
/* Set output. */
*out_err = ConvertResultToErrorCode(result);

View File

@ -50,8 +50,8 @@ namespace ams::htcs::impl {
return m_service.CreateSocket(out_err, out_desc, enable_disconnection_emulation);
}
Result HtcsManagerImpl::DestroySocket(s32 *out_err, s32 desc) {
return m_service.DestroySocket(out_err, desc);
Result HtcsManagerImpl::DestroySocket(s32 desc) {
return m_service.DestroySocket(desc);
}
Result HtcsManagerImpl::Connect(s32 *out_err, s32 desc, const SockAddrHtcs &address) {

View File

@ -43,7 +43,7 @@ namespace ams::htcs::impl {
bool IsServiceAvailable();
public:
Result CreateSocket(s32 *out_err, s32 *out_desc, bool enable_disconnection_emulation);
Result DestroySocket(s32 *out_err, s32 desc);
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);

View File

@ -16,6 +16,7 @@
#include <stratosphere.hpp>
#include "htcs_service.hpp"
#include "rpc/htcs_rpc_tasks.hpp"
#include "htcs_util.hpp"
namespace ams::htcs::impl {
@ -45,4 +46,92 @@ namespace ams::htcs::impl {
return ResultSuccess();
}
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)));
return ResultSuccess();
}
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;
return ResultSuccess();
}
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;
return ResultSuccess();
}
Result HtcsService::Listen(s32 *out_err, s32 desc, s32 backlog_count);
Result HtcsService::Receive(s32 *out_err, s64 *out_size, char *buffer, size_t size, s32 desc, s32 flags);
Result HtcsService::Send(s32 *out_err, s64 *out_size, const char *buffer, size_t size, s32 desc, s32 flags);
Result HtcsService::Shutdown(s32 *out_err, s32 desc, s32 how);
Result HtcsService::Fcntl(s32 *out_err, s32 *out_res, s32 desc, s32 command, s32 value);
Result HtcsService::AcceptStart(u32 *out_task_id, Handle *out_handle, s32 desc);
Result HtcsService::AcceptResults(s32 *out_err, s32 *out_desc, SockAddrHtcs *out_address, u32 task_id, s32 desc);
Result HtcsService::ReceiveSmallStart(u32 *out_task_id, Handle *out_handle, s64 size, s32 desc, s32 flags);
Result HtcsService::ReceiveSmallResults(s32 *out_err, s64 *out_size, char *buffer, s64 buffer_size, u32 task_id, s32 desc);
Result HtcsService::SendSmallStart(u32 *out_task_id, Handle *out_handle, s32 desc, s64 size, s32 flags);
Result HtcsService::SendSmallContinue(s64 *out_size, const char *buffer, s64 buffer_size, u32 task_id, s32 desc);
Result HtcsService::SendSmallResults(s32 *out_err, s64 *out_size, u32 task_id, s32 desc);
Result HtcsService::SendStart(u32 *out_task_id, Handle *out_handle, s32 desc, s64 size, s32 flags);
Result HtcsService::SendContinue(s64 *out_size, const char *buffer, s64 buffer_size, u32 task_id, s32 desc);
Result HtcsService::SendResults(s32 *out_err, s64 *out_size, u32 task_id, s32 desc);
Result HtcsService::ReceiveStart(u32 *out_task_id, Handle *out_handle, s64 size, s32 desc, s32 flags);
Result HtcsService::ReceiveResults(s32 *out_err, s64 *out_size, char *buffer, s64 buffer_size, u32 task_id, s32 desc);
Result HtcsService::SelectStart(u32 *out_task_id, Handle *out_handle, Span<const int> read_handles, Span<const int> write_handles, Span<const int> exception_handles, s64 tv_sec, s64 tv_usec);
Result HtcsService::SelectEnd(s32 *out_err, s32 *out_res, Span<int> read_handles, Span<int> write_handles, Span<int> exception_handles, u32 task_id);
}

View File

@ -34,7 +34,7 @@ namespace ams::htcs::impl {
/* TODO */
public:
Result CreateSocket(s32 *out_err, s32 *out_desc, bool enable_disconnection_emulation);
Result DestroySocket(s32 *out_err, s32 desc);
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);

View File

@ -20,4 +20,17 @@ 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);
}
}

View File

@ -22,8 +22,9 @@ namespace ams::htcs {
R_DEFINE_ERROR_RESULT(InvalidHandle, 9);
R_DEFINE_ERROR_RESULT(InvalidSize, 2014);
R_DEFINE_ERROR_RESULT(Unknown2021, 2021);
R_DEFINE_ERROR_RESULT(Unknown2023, 2023);
R_DEFINE_ERROR_RESULT(InvalidArgument, 2001);
R_DEFINE_ERROR_RESULT(InvalidSize, 2014);
R_DEFINE_ERROR_RESULT(Unknown2021, 2021);
R_DEFINE_ERROR_RESULT(Unknown2023, 2023);
}