server_session.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 "common/assert.h"
  8. #include "common/common_types.h"
  9. #include "core/hle/kernel/kernel.h"
  10. #include "core/hle/kernel/session.h"
  11. #include "core/hle/kernel/wait_object.h"
  12. #include "core/hle/result.h"
  13. #include "core/memory.h"
  14. namespace Kernel {
  15. class ClientSession;
  16. class ClientPort;
  17. class ServerSession;
  18. class SessionRequestHandler;
  19. class Thread;
  20. /**
  21. * Kernel object representing the server endpoint of an IPC session. Sessions are the basic CTR-OS
  22. * primitive for communication between different processes, and are used to implement service calls
  23. * to the various system services.
  24. *
  25. * To make a service call, the client must write the command header and parameters to the buffer
  26. * located at offset 0x80 of the TLS (Thread-Local Storage) area, then execute a SendSyncRequest
  27. * SVC call with its ClientSession handle. The kernel will read the command header, using it to
  28. * marshall the parameters to the process at the server endpoint of the session.
  29. * After the server replies to the request, the response is marshalled back to the caller's
  30. * TLS buffer and control is transferred back to it.
  31. */
  32. class ServerSession final : public WaitObject {
  33. public:
  34. std::string GetTypeName() const override {
  35. return "ServerSession";
  36. }
  37. static const HandleType HANDLE_TYPE = HandleType::ServerSession;
  38. HandleType GetHandleType() const override {
  39. return HANDLE_TYPE;
  40. }
  41. using SessionPair = std::tuple<SharedPtr<ServerSession>, SharedPtr<ClientSession>>;
  42. /**
  43. * Creates a pair of ServerSession and an associated ClientSession.
  44. * @param name Optional name of the ports.
  45. * @param hle_handler Optional HLE handler for this server session.
  46. * @param client_port Optional The ClientPort that spawned this session.
  47. * @return The created session tuple
  48. */
  49. static SessionPair CreateSessionPair(
  50. const std::string& name = "Unknown",
  51. std::shared_ptr<SessionRequestHandler> hle_handler = nullptr,
  52. SharedPtr<ClientPort> client_port = nullptr);
  53. /**
  54. * Handle a sync request from the emulated application.
  55. * @returns ResultCode from the operation.
  56. */
  57. ResultCode HandleSyncRequest();
  58. bool ShouldWait(Thread* thread) const override;
  59. void Acquire(Thread* thread) override;
  60. std::string name; ///< The name of this session (optional)
  61. bool signaled; ///< Whether there's new data available to this ServerSession
  62. std::shared_ptr<Session> parent; ///< The parent session, which links to the client endpoint.
  63. std::shared_ptr<SessionRequestHandler>
  64. hle_handler; ///< This session's HLE request handler (optional)
  65. private:
  66. ServerSession();
  67. ~ServerSession() override;
  68. /**
  69. * Creates a server session. The server session can have an optional HLE handler,
  70. * which will be invoked to handle the IPC requests that this session receives.
  71. * @param name Optional name of the server session.
  72. * @param hle_handler Optional HLE handler for this server session.
  73. * @return The created server session
  74. */
  75. static ResultVal<SharedPtr<ServerSession>> Create(
  76. std::string name = "Unknown", std::shared_ptr<SessionRequestHandler> hle_handler = nullptr);
  77. };
  78. /**
  79. * Performs command buffer translation for an HLE IPC request.
  80. * The command buffer from the ServerSession thread's TLS is copied into a
  81. * buffer and all descriptors in the buffer are processed.
  82. * TODO(Subv): Implement this function, currently we do not support multiple processes running at
  83. * once, but once that is implemented we'll need to properly translate all descriptors
  84. * in the command buffer.
  85. */
  86. ResultCode TranslateHLERequest(ServerSession* server_session);
  87. }