From 016f2ecb25d80392d27fcfc3501e3d2530c83fe3 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sat, 20 Apr 2019 16:17:24 -0700 Subject: [PATCH] libstrat: support arbitrary service object constructors --- include/stratosphere/mitm/mitm_server.hpp | 15 +++++++++++---- include/stratosphere/servers.hpp | 22 +++++++++++----------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/include/stratosphere/mitm/mitm_server.hpp b/include/stratosphere/mitm/mitm_server.hpp index 84bd1424..7224ed5c 100644 --- a/include/stratosphere/mitm/mitm_server.hpp +++ b/include/stratosphere/mitm/mitm_server.hpp @@ -23,7 +23,7 @@ void RegisterMitmServerQueryHandle(Handle query_h, ServiceObjectHolder &&service); -template +template class MitmServer : public IWaitable { static_assert(std::is_base_of::value, "MitM Service Objects must derive from IMitmServiceObject"); private: @@ -76,7 +76,7 @@ class MitmServer : public IWaitable { } /* Create a forward service for this instance. */ - std::shared_ptrforward_service(new Service(), [](Service *s) { + std::shared_ptr forward_service(new Service(), [](Service *s) { /* Custom deleter to ensure service is open as long as necessary. */ serviceClose(s); delete s; @@ -95,14 +95,21 @@ class MitmServer : public IWaitable { smMitMExit(); - this->GetSessionManager()->AddWaitable(new MitmSession(session_h, client_pid, forward_service, std::make_shared(forward_service, client_pid))); + this->GetSessionManager()->AddWaitable(new MitmSession(session_h, client_pid, forward_service, MakeShared(forward_service, client_pid))); return ResultSuccess; } }; template +struct MakeSharedMitmHelper { + static constexpr std::shared_ptr Make(std::shared_ptr forward_srv, u64 client_pid) { + return std::make_shared(forward_srv, client_pid); + } +}; + +template::Make> static void AddMitmServerToManager(SessionManagerBase *manager, const char *srv_name, unsigned int max_sessions) { - auto *srv = new MitmServer(srv_name, max_sessions); + auto *srv = new MitmServer(srv_name, max_sessions); manager->AddWaitable(srv); } diff --git a/include/stratosphere/servers.hpp b/include/stratosphere/servers.hpp index 6da2503c..c37b95b7 100644 --- a/include/stratosphere/servers.hpp +++ b/include/stratosphere/servers.hpp @@ -20,7 +20,7 @@ #include "iwaitable.hpp" #include "ipc.hpp" -template +template class IServer : public IWaitable { static_assert(std::is_base_of::value, "Service Objects must derive from IServiceObject"); protected: @@ -53,33 +53,33 @@ class IServer : public IWaitable { return rc; } - this->GetSessionManager()->AddSession(session_h, std::move(ServiceObjectHolder(std::move(std::make_shared())))); + this->GetSessionManager()->AddSession(session_h, std::move(ServiceObjectHolder(std::move(MakeShared())))); return ResultSuccess; } }; -template -class ServiceServer : public IServer { +template > +class ServiceServer : public IServer { public: - ServiceServer(const char *service_name, unsigned int max_s) : IServer(max_s) { + ServiceServer(const char *service_name, unsigned int max_s) : IServer(max_s) { if (R_FAILED(smRegisterService(&this->port_handle, service_name, false, this->max_sessions))) { /* TODO: Panic. */ } } }; -template -class ExistingPortServer : public IServer { +template > +class ExistingPortServer : public IServer { public: - ExistingPortServer(Handle port_h, unsigned int max_s) : IServer(max_s) { + ExistingPortServer(Handle port_h, unsigned int max_s) : IServer(max_s) { this->port_handle = port_h; } }; -template -class ManagedPortServer : public IServer { +template > +class ManagedPortServer : public IServer { public: - ManagedPortServer(const char *service_name, unsigned int max_s) : IServer(max_s) { + ManagedPortServer(const char *service_name, unsigned int max_s) : IServer(max_s) { if (R_FAILED(svcManageNamedPort(&this->port_handle, service_name, this->max_sessions))) { /* TODO: panic */ }