server_session.h 3.5 KB

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