server_session.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2016 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <tuple>
  5. #include "core/hle/kernel/client_session.h"
  6. #include "core/hle/kernel/server_session.h"
  7. #include "core/hle/kernel/thread.h"
  8. namespace Kernel {
  9. ServerSession::ServerSession() = default;
  10. ServerSession::~ServerSession() {
  11. // This destructor will be called automatically when the last ServerSession handle is closed by the emulated application.
  12. // TODO(Subv): Reduce the ClientPort's connection count,
  13. // if the session is still open, set the connection status to 3 (Closed by server),
  14. }
  15. ResultVal<SharedPtr<ServerSession>> ServerSession::Create(std::string name, std::shared_ptr<Service::SessionRequestHandler> hle_handler) {
  16. SharedPtr<ServerSession> server_session(new ServerSession);
  17. server_session->name = std::move(name);
  18. server_session->signaled = false;
  19. server_session->hle_handler = std::move(hle_handler);
  20. return MakeResult<SharedPtr<ServerSession>>(std::move(server_session));
  21. }
  22. bool ServerSession::ShouldWait() {
  23. return !signaled;
  24. }
  25. void ServerSession::Acquire() {
  26. ASSERT_MSG(!ShouldWait(), "object unavailable!");
  27. signaled = false;
  28. }
  29. ResultCode ServerSession::HandleSyncRequest() {
  30. // The ServerSession received a sync request, this means that there's new data available
  31. // from its ClientSession, so wake up any threads that may be waiting on a svcReplyAndReceive or similar.
  32. // If this ServerSession has an associated HLE handler, forward the request to it.
  33. if (hle_handler != nullptr) {
  34. // Attempt to translate the incoming request's command buffer.
  35. ResultCode result = TranslateHLERequest(this);
  36. if (result.IsError())
  37. return result;
  38. hle_handler->HandleSyncRequest(SharedPtr<ServerSession>(this));
  39. // TODO(Subv): Translate the response command buffer.
  40. }
  41. // If this ServerSession does not have an HLE implementation, just wake up the threads waiting on it.
  42. signaled = true;
  43. WakeupAllWaitingThreads();
  44. return RESULT_SUCCESS;
  45. }
  46. ServerSession::SessionPair ServerSession::CreateSessionPair(const std::string& name,
  47. std::shared_ptr<Service::SessionRequestHandler> hle_handler) {
  48. auto server_session = ServerSession::Create(name + "_Server", std::move(hle_handler)).MoveFrom();
  49. // We keep a non-owning pointer to the ServerSession in the ClientSession because we don't want to prevent the
  50. // ServerSession's destructor from being called when the emulated application closes the last ServerSession handle.
  51. auto client_session = ClientSession::Create(server_session.get(), name + "_Client").MoveFrom();
  52. return std::make_tuple(std::move(server_session), std::move(client_session));
  53. }
  54. ResultCode TranslateHLERequest(ServerSession* server_session) {
  55. // TODO(Subv): Implement this function once multiple concurrent processes are supported.
  56. return RESULT_SUCCESS;
  57. }
  58. }