server_session.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 <memory>
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "core/hle/kernel/object.h"
  10. #include "core/hle/kernel/wait_object.h"
  11. #include "core/hle/result.h"
  12. namespace Kernel {
  13. class ClientPort;
  14. class ClientSession;
  15. class HLERequestContext;
  16. class KernelCore;
  17. class ServerSession;
  18. class Session;
  19. class SessionRequestHandler;
  20. class Thread;
  21. /**
  22. * Kernel object representing the server endpoint of an IPC session. Sessions are the basic CTR-OS
  23. * primitive for communication between different processes, and are used to implement service calls
  24. * to the various system services.
  25. *
  26. * To make a service call, the client must write the command header and parameters to the buffer
  27. * located at offset 0x80 of the TLS (Thread-Local Storage) area, then execute a SendSyncRequest
  28. * SVC call with its ClientSession handle. The kernel will read the command header, using it to
  29. * marshall the parameters to the process at the server endpoint of the session.
  30. * After the server replies to the request, the response is marshalled back to the caller's
  31. * TLS buffer and control is transferred back to it.
  32. */
  33. class ServerSession final : public WaitObject {
  34. public:
  35. std::string GetTypeName() const override {
  36. return "ServerSession";
  37. }
  38. std::string GetName() const override {
  39. return name;
  40. }
  41. static constexpr HandleType HANDLE_TYPE = HandleType::ServerSession;
  42. HandleType GetHandleType() const override {
  43. return HANDLE_TYPE;
  44. }
  45. Session* GetParent() {
  46. return parent.get();
  47. }
  48. const Session* GetParent() const {
  49. return parent.get();
  50. }
  51. using SessionPair = std::pair<SharedPtr<ServerSession>, SharedPtr<ClientSession>>;
  52. /**
  53. * Creates a pair of ServerSession and an associated ClientSession.
  54. * @param kernel The kernal instance to create the session pair under.
  55. * @param name Optional name of the ports.
  56. * @param client_port Optional The ClientPort that spawned this session.
  57. * @return The created session tuple
  58. */
  59. static SessionPair CreateSessionPair(KernelCore& kernel, const std::string& name = "Unknown",
  60. SharedPtr<ClientPort> client_port = nullptr);
  61. /**
  62. * Sets the HLE handler for the session. This handler will be called to service IPC requests
  63. * instead of the regular IPC machinery. (The regular IPC machinery is currently not
  64. * implemented.)
  65. */
  66. void SetHleHandler(std::shared_ptr<SessionRequestHandler> hle_handler_) {
  67. hle_handler = std::move(hle_handler_);
  68. }
  69. /**
  70. * Handle a sync request from the emulated application.
  71. * @param thread Thread that initiated the request.
  72. * @returns ResultCode from the operation.
  73. */
  74. ResultCode HandleSyncRequest(SharedPtr<Thread> thread);
  75. bool ShouldWait(const Thread* thread) const override;
  76. void Acquire(Thread* thread) override;
  77. /// Called when a client disconnection occurs.
  78. void ClientDisconnected();
  79. /// Adds a new domain request handler to the collection of request handlers within
  80. /// this ServerSession instance.
  81. void AppendDomainRequestHandler(std::shared_ptr<SessionRequestHandler> handler);
  82. /// Retrieves the total number of domain request handlers that have been
  83. /// appended to this ServerSession instance.
  84. std::size_t NumDomainRequestHandlers() const;
  85. /// Returns true if the session has been converted to a domain, otherwise False
  86. bool IsDomain() const {
  87. return !IsSession();
  88. }
  89. /// Returns true if this session has not been converted to a domain, otherwise false.
  90. bool IsSession() const {
  91. return domain_request_handlers.empty();
  92. }
  93. /// Converts the session to a domain at the end of the current command
  94. void ConvertToDomain() {
  95. convert_to_domain = true;
  96. }
  97. private:
  98. explicit ServerSession(KernelCore& kernel);
  99. ~ServerSession() override;
  100. /**
  101. * Creates a server session. The server session can have an optional HLE handler,
  102. * which will be invoked to handle the IPC requests that this session receives.
  103. * @param kernel The kernel instance to create this server session under.
  104. * @param name Optional name of the server session.
  105. * @return The created server session
  106. */
  107. static ResultVal<SharedPtr<ServerSession>> Create(KernelCore& kernel,
  108. std::string name = "Unknown");
  109. /// Handles a SyncRequest to a domain, forwarding the request to the proper object or closing an
  110. /// object handle.
  111. ResultCode HandleDomainSyncRequest(Kernel::HLERequestContext& context);
  112. /// The parent session, which links to the client endpoint.
  113. std::shared_ptr<Session> parent;
  114. /// This session's HLE request handler (applicable when not a domain)
  115. std::shared_ptr<SessionRequestHandler> hle_handler;
  116. /// This is the list of domain request handlers (after conversion to a domain)
  117. std::vector<std::shared_ptr<SessionRequestHandler>> domain_request_handlers;
  118. /// List of threads that are pending a response after a sync request. This list is processed in
  119. /// a LIFO manner, thus, the last request will be dispatched first.
  120. /// TODO(Subv): Verify if this is indeed processed in LIFO using a hardware test.
  121. std::vector<SharedPtr<Thread>> pending_requesting_threads;
  122. /// Thread whose request is currently being handled. A request is considered "handled" when a
  123. /// response is sent via svcReplyAndReceive.
  124. /// TODO(Subv): Find a better name for this.
  125. SharedPtr<Thread> currently_handling;
  126. /// When set to True, converts the session to a domain at the end of the command
  127. bool convert_to_domain{};
  128. /// The name of this session (optional)
  129. std::string name;
  130. };
  131. } // namespace Kernel