service.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2014 Citra Emulator Project
  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 <unordered_map>
  8. #include <boost/container/flat_map.hpp>
  9. #include "common/common_types.h"
  10. #include "core/hle/ipc.h"
  11. #include "core/hle/kernel/client_port.h"
  12. #include "core/hle/kernel/thread.h"
  13. #include "core/hle/result.h"
  14. #include "core/memory.h"
  15. namespace Kernel {
  16. class ServerSession;
  17. }
  18. ////////////////////////////////////////////////////////////////////////////////////////////////////
  19. // Namespace Service
  20. namespace Service {
  21. static const int kMaxPortSize = 8; ///< Maximum size of a port name (8 characters)
  22. /// Arbitrary default number of maximum connections to an HLE service.
  23. static const u32 DefaultMaxSessions = 10;
  24. /**
  25. * Interface implemented by HLE Session handlers.
  26. * This can be provided to a ServerSession in order to hook into several relevant events
  27. * (such as a new connection or a SyncRequest) so they can be implemented in the emulator.
  28. */
  29. class SessionRequestHandler {
  30. public:
  31. /**
  32. * Handles a sync request from the emulated application.
  33. * @param server_session The ServerSession that was triggered for this sync request,
  34. * it should be used to differentiate which client (As in ClientSession) we're answering to.
  35. * TODO(Subv): Use a wrapper structure to hold all the information relevant to
  36. * this request (ServerSession, Originator thread, Translated command buffer, etc).
  37. * @returns ResultCode the result code of the translate operation.
  38. */
  39. virtual void HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) = 0;
  40. /**
  41. * Signals that a client has just connected to this HLE handler and keeps the
  42. * associated ServerSession alive for the duration of the connection.
  43. * @param server_session Owning pointer to the ServerSession associated with the connection.
  44. */
  45. void ClientConnected(Kernel::SharedPtr<Kernel::ServerSession> server_session);
  46. /**
  47. * Signals that a client has just disconnected from this HLE handler and releases the
  48. * associated ServerSession.
  49. * @param server_session ServerSession associated with the connection.
  50. */
  51. void ClientDisconnected(Kernel::SharedPtr<Kernel::ServerSession> server_session);
  52. protected:
  53. /// List of sessions that are connected to this handler.
  54. /// A ServerSession whose server endpoint is an HLE implementation is kept alive by this list
  55. // for the duration of the connection.
  56. std::vector<Kernel::SharedPtr<Kernel::ServerSession>> connected_sessions;
  57. };
  58. /**
  59. * Framework for implementing HLE service handlers which dispatch incoming SyncRequests based on a
  60. * table mapping header ids to handler functions.
  61. */
  62. class Interface : public SessionRequestHandler {
  63. public:
  64. /**
  65. * Creates an HLE interface with the specified max sessions.
  66. * @param max_sessions Maximum number of sessions that can be
  67. * connected to this service at the same time.
  68. */
  69. Interface(u32 max_sessions = DefaultMaxSessions);
  70. virtual ~Interface();
  71. std::string GetName() const {
  72. return GetPortName();
  73. }
  74. virtual void SetVersion(u32 raw_version) {
  75. version.raw = raw_version;
  76. }
  77. /**
  78. * Gets the maximum allowed number of sessions that can be connected to this service
  79. * at the same time.
  80. * @returns The maximum number of connections allowed.
  81. */
  82. u32 GetMaxSessions() const {
  83. return max_sessions;
  84. }
  85. typedef void (*Function)(Interface*);
  86. struct FunctionInfo {
  87. u32 id;
  88. Function func;
  89. const char* name;
  90. };
  91. /**
  92. * Gets the string name used by CTROS for a service
  93. * @return Port name of service
  94. */
  95. virtual std::string GetPortName() const {
  96. return "[UNKNOWN SERVICE PORT]";
  97. }
  98. protected:
  99. void HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) override;
  100. /**
  101. * Registers the functions in the service
  102. */
  103. template <size_t N>
  104. inline void Register(const FunctionInfo (&functions)[N]) {
  105. Register(functions, N);
  106. }
  107. void Register(const FunctionInfo* functions, size_t n);
  108. union {
  109. u32 raw;
  110. BitField<0, 8, u32> major;
  111. BitField<8, 8, u32> minor;
  112. BitField<16, 8, u32> build;
  113. BitField<24, 8, u32> revision;
  114. } version = {};
  115. private:
  116. u32 max_sessions; ///< Maximum number of concurrent sessions that this service can handle.
  117. boost::container::flat_map<u32, FunctionInfo> m_functions;
  118. };
  119. /// Initialize ServiceManager
  120. void Init();
  121. /// Shutdown ServiceManager
  122. void Shutdown();
  123. /// Map of named ports managed by the kernel, which can be retrieved using the ConnectToPort SVC.
  124. extern std::unordered_map<std::string, Kernel::SharedPtr<Kernel::ClientPort>> g_kernel_named_ports;
  125. /// Map of services registered with the "srv:" service, retrieved using GetServiceHandle.
  126. extern std::unordered_map<std::string, Kernel::SharedPtr<Kernel::ClientPort>> g_srv_services;
  127. /// Adds a service to the services table
  128. void AddService(Interface* interface_);
  129. } // namespace