service.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <cstddef>
  6. #include <string>
  7. #include <boost/container/flat_map.hpp>
  8. #include "common/common_types.h"
  9. #include "core/hle/kernel/hle_ipc.h"
  10. #include "core/hle/kernel/object.h"
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////
  12. // Namespace Service
  13. namespace Kernel {
  14. class ClientPort;
  15. class ServerPort;
  16. class ServerSession;
  17. class HLERequestContext;
  18. } // namespace Kernel
  19. namespace FileSys {
  20. class VfsFilesystem;
  21. }
  22. namespace Service {
  23. namespace SM {
  24. class ServiceManager;
  25. }
  26. static const int kMaxPortSize = 8; ///< Maximum size of a port name (8 characters)
  27. /// Arbitrary default number of maximum connections to an HLE service.
  28. static const u32 DefaultMaxSessions = 10;
  29. /**
  30. * This is an non-templated base of ServiceFramework to reduce code bloat and compilation times, it
  31. * is not meant to be used directly.
  32. *
  33. * @see ServiceFramework
  34. */
  35. class ServiceFrameworkBase : public Kernel::SessionRequestHandler {
  36. public:
  37. /// Returns the string identifier used to connect to the service.
  38. std::string GetServiceName() const {
  39. return service_name;
  40. }
  41. /**
  42. * Returns the maximum number of sessions that can be connected to this service at the same
  43. * time.
  44. */
  45. u32 GetMaxSessions() const {
  46. return max_sessions;
  47. }
  48. /// Creates a port pair and registers this service with the given ServiceManager.
  49. void InstallAsService(SM::ServiceManager& service_manager);
  50. /// Creates a port pair and registers it on the kernel's global port registry.
  51. void InstallAsNamedPort();
  52. /// Creates and returns an unregistered port for the service.
  53. Kernel::SharedPtr<Kernel::ClientPort> CreatePort();
  54. void InvokeRequest(Kernel::HLERequestContext& ctx);
  55. ResultCode HandleSyncRequest(Kernel::HLERequestContext& context) override;
  56. protected:
  57. /// Member-function pointer type of SyncRequest handlers.
  58. template <typename Self>
  59. using HandlerFnP = void (Self::*)(Kernel::HLERequestContext&);
  60. private:
  61. template <typename T>
  62. friend class ServiceFramework;
  63. struct FunctionInfoBase {
  64. u32 expected_header;
  65. HandlerFnP<ServiceFrameworkBase> handler_callback;
  66. const char* name;
  67. };
  68. using InvokerFn = void(ServiceFrameworkBase* object, HandlerFnP<ServiceFrameworkBase> member,
  69. Kernel::HLERequestContext& ctx);
  70. ServiceFrameworkBase(const char* service_name, u32 max_sessions, InvokerFn* handler_invoker);
  71. ~ServiceFrameworkBase();
  72. void RegisterHandlersBase(const FunctionInfoBase* functions, std::size_t n);
  73. void ReportUnimplementedFunction(Kernel::HLERequestContext& ctx, const FunctionInfoBase* info);
  74. /// Identifier string used to connect to the service.
  75. std::string service_name;
  76. /// Maximum number of concurrent sessions that this service can handle.
  77. u32 max_sessions;
  78. /**
  79. * Port where incoming connections will be received. Only created when InstallAsService() or
  80. * InstallAsNamedPort() are called.
  81. */
  82. Kernel::SharedPtr<Kernel::ServerPort> port;
  83. /// Function used to safely up-cast pointers to the derived class before invoking a handler.
  84. InvokerFn* handler_invoker;
  85. boost::container::flat_map<u32, FunctionInfoBase> handlers;
  86. };
  87. /**
  88. * Framework for implementing HLE services. Dispatches on the header id of incoming SyncRequests
  89. * based on a table mapping header ids to handler functions. Service implementations should inherit
  90. * from ServiceFramework using the CRTP (`class Foo : public ServiceFramework<Foo> { ... };`) and
  91. * populate it with handlers by calling #RegisterHandlers.
  92. *
  93. * In order to avoid duplicating code in the binary and exposing too many implementation details in
  94. * the header, this class is split into a non-templated base (ServiceFrameworkBase) and a template
  95. * deriving from it (ServiceFramework). The functions in this class will mostly only erase the type
  96. * of the passed in function pointers and then delegate the actual work to the implementation in the
  97. * base class.
  98. */
  99. template <typename Self>
  100. class ServiceFramework : public ServiceFrameworkBase {
  101. protected:
  102. /// Contains information about a request type which is handled by the service.
  103. struct FunctionInfo : FunctionInfoBase {
  104. // TODO(yuriks): This function could be constexpr, but clang is the only compiler that
  105. // doesn't emit an ICE or a wrong diagnostic because of the static_cast.
  106. /**
  107. * Constructs a FunctionInfo for a function.
  108. *
  109. * @param expected_header request header in the command buffer which will trigger dispatch
  110. * to this handler
  111. * @param handler_callback member function in this service which will be called to handle
  112. * the request
  113. * @param name human-friendly name for the request. Used mostly for logging purposes.
  114. */
  115. FunctionInfo(u32 expected_header, HandlerFnP<Self> handler_callback, const char* name)
  116. : FunctionInfoBase{
  117. expected_header,
  118. // Type-erase member function pointer by casting it down to the base class.
  119. static_cast<HandlerFnP<ServiceFrameworkBase>>(handler_callback), name} {}
  120. };
  121. /**
  122. * Initializes the handler with no functions installed.
  123. * @param max_sessions Maximum number of sessions that can be
  124. * connected to this service at the same time.
  125. */
  126. explicit ServiceFramework(const char* service_name, u32 max_sessions = DefaultMaxSessions)
  127. : ServiceFrameworkBase(service_name, max_sessions, Invoker) {}
  128. /// Registers handlers in the service.
  129. template <std::size_t N>
  130. void RegisterHandlers(const FunctionInfo (&functions)[N]) {
  131. RegisterHandlers(functions, N);
  132. }
  133. /**
  134. * Registers handlers in the service. Usually prefer using the other RegisterHandlers
  135. * overload in order to avoid needing to specify the array size.
  136. */
  137. void RegisterHandlers(const FunctionInfo* functions, std::size_t n) {
  138. RegisterHandlersBase(functions, n);
  139. }
  140. private:
  141. /**
  142. * This function is used to allow invocation of pointers to handlers stored in the base class
  143. * without needing to expose the type of this derived class. Pointers-to-member may require a
  144. * fixup when being up or downcast, and thus code that does that needs to know the concrete type
  145. * of the derived class in order to invoke one of it's functions through a pointer.
  146. */
  147. static void Invoker(ServiceFrameworkBase* object, HandlerFnP<ServiceFrameworkBase> member,
  148. Kernel::HLERequestContext& ctx) {
  149. // Cast back up to our original types and call the member function
  150. (static_cast<Self*>(object)->*static_cast<HandlerFnP<Self>>(member))(ctx);
  151. }
  152. };
  153. /// Initialize ServiceManager
  154. void Init(std::shared_ptr<SM::ServiceManager>& sm, FileSys::VfsFilesystem& vfs);
  155. /// Shutdown ServiceManager
  156. void Shutdown();
  157. } // namespace Service