k_server_session.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2019 yuzu emulator team
  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 <boost/intrusive/list.hpp>
  10. #include "common/threadsafe_queue.h"
  11. #include "core/hle/kernel/k_synchronization_object.h"
  12. #include "core/hle/kernel/service_thread.h"
  13. #include "core/hle/result.h"
  14. namespace Core::Memory {
  15. class Memory;
  16. }
  17. namespace Core::Timing {
  18. class CoreTiming;
  19. struct EventType;
  20. } // namespace Core::Timing
  21. namespace Kernel {
  22. class HLERequestContext;
  23. class KernelCore;
  24. class KSession;
  25. class SessionRequestHandler;
  26. class KThread;
  27. class KServerSession final : public KSynchronizationObject,
  28. public boost::intrusive::list_base_hook<> {
  29. KERNEL_AUTOOBJECT_TRAITS(KServerSession, KSynchronizationObject);
  30. friend class ServiceThread;
  31. public:
  32. explicit KServerSession(KernelCore& kernel);
  33. virtual ~KServerSession() override;
  34. virtual void Destroy() override;
  35. void Initialize(KSession* parent_, std::string&& name_);
  36. KSession* GetParent() {
  37. return parent;
  38. }
  39. const KSession* GetParent() const {
  40. return parent;
  41. }
  42. virtual bool IsSignaled() const override;
  43. void OnClientClosed();
  44. /**
  45. * Sets the HLE handler for the session. This handler will be called to service IPC requests
  46. * instead of the regular IPC machinery. (The regular IPC machinery is currently not
  47. * implemented.)
  48. */
  49. void SetHleHandler(std::shared_ptr<SessionRequestHandler> hle_handler_) {
  50. hle_handler = std::move(hle_handler_);
  51. }
  52. /**
  53. * Handle a sync request from the emulated application.
  54. *
  55. * @param thread Thread that initiated the request.
  56. * @param memory Memory context to handle the sync request under.
  57. * @param core_timing Core timing context to schedule the request event under.
  58. *
  59. * @returns ResultCode from the operation.
  60. */
  61. ResultCode HandleSyncRequest(KThread* thread, Core::Memory::Memory& memory,
  62. Core::Timing::CoreTiming& core_timing);
  63. /// Adds a new domain request handler to the collection of request handlers within
  64. /// this ServerSession instance.
  65. void AppendDomainRequestHandler(std::shared_ptr<SessionRequestHandler> handler);
  66. /// Retrieves the total number of domain request handlers that have been
  67. /// appended to this ServerSession instance.
  68. std::size_t NumDomainRequestHandlers() const;
  69. /// Returns true if the session has been converted to a domain, otherwise False
  70. bool IsDomain() const {
  71. return !IsSession();
  72. }
  73. /// Returns true if this session has not been converted to a domain, otherwise false.
  74. bool IsSession() const {
  75. return domain_request_handlers.empty();
  76. }
  77. /// Converts the session to a domain at the end of the current command
  78. void ConvertToDomain() {
  79. convert_to_domain = true;
  80. }
  81. private:
  82. /// Queues a sync request from the emulated application.
  83. ResultCode QueueSyncRequest(KThread* thread, Core::Memory::Memory& memory);
  84. /// Completes a sync request from the emulated application.
  85. ResultCode CompleteSyncRequest(HLERequestContext& context);
  86. /// Handles a SyncRequest to a domain, forwarding the request to the proper object or closing an
  87. /// object handle.
  88. ResultCode HandleDomainSyncRequest(Kernel::HLERequestContext& context);
  89. /// This session's HLE request handler (applicable when not a domain)
  90. std::shared_ptr<SessionRequestHandler> hle_handler;
  91. /// This is the list of domain request handlers (after conversion to a domain)
  92. std::vector<std::shared_ptr<SessionRequestHandler>> domain_request_handlers;
  93. /// When set to True, converts the session to a domain at the end of the command
  94. bool convert_to_domain{};
  95. /// Thread to dispatch service requests
  96. std::weak_ptr<ServiceThread> service_thread;
  97. /// KSession that owns this KServerSession
  98. KSession* parent{};
  99. };
  100. } // namespace Kernel