2022-04-23 04:59:50 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2021-04-13 20:48:37 -04:00
|
|
|
|
2021-04-23 20:00:15 -04:00
|
|
|
#include "core/hle/kernel/k_client_port.h"
|
2021-04-13 20:48:37 -04:00
|
|
|
#include "core/hle/kernel/k_client_session.h"
|
|
|
|
#include "core/hle/kernel/k_scoped_resource_reservation.h"
|
|
|
|
#include "core/hle/kernel/k_server_session.h"
|
|
|
|
#include "core/hle/kernel/k_session.h"
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2023-03-07 10:49:41 -05:00
|
|
|
KSession::KSession(KernelCore& kernel)
|
|
|
|
: KAutoObjectWithSlabHeapAndContainer{kernel}, m_server{kernel}, m_client{kernel} {}
|
2021-04-13 20:48:37 -04:00
|
|
|
KSession::~KSession() = default;
|
|
|
|
|
2023-03-06 20:34:25 -05:00
|
|
|
void KSession::Initialize(KClientPort* client_port, uintptr_t name) {
|
2021-04-13 20:48:37 -04:00
|
|
|
// Increment reference count.
|
|
|
|
// Because reference count is one on creation, this will result
|
|
|
|
// in a reference count of two. Thus, when both server and client are closed
|
|
|
|
// this object will be destroyed.
|
2023-03-06 20:34:25 -05:00
|
|
|
this->Open();
|
2021-04-13 20:48:37 -04:00
|
|
|
|
|
|
|
// Create our sub sessions.
|
2023-03-06 20:34:25 -05:00
|
|
|
KAutoObject::Create(std::addressof(m_server));
|
|
|
|
KAutoObject::Create(std::addressof(m_client));
|
2021-04-13 20:48:37 -04:00
|
|
|
|
|
|
|
// Initialize our sub sessions.
|
2023-03-06 20:34:25 -05:00
|
|
|
m_server.Initialize(this);
|
|
|
|
m_client.Initialize(this);
|
2021-04-13 20:48:37 -04:00
|
|
|
|
|
|
|
// Set state and name.
|
2023-03-06 20:34:25 -05:00
|
|
|
this->SetState(State::Normal);
|
|
|
|
m_name = name;
|
2021-04-13 20:48:37 -04:00
|
|
|
|
|
|
|
// Set our owner process.
|
2023-02-13 10:44:41 -05:00
|
|
|
//! FIXME: this is the wrong process!
|
2023-03-07 10:49:41 -05:00
|
|
|
m_process = m_kernel.ApplicationProcess();
|
2023-03-06 20:34:25 -05:00
|
|
|
m_process->Open();
|
2021-04-13 20:48:37 -04:00
|
|
|
|
2021-04-23 20:00:15 -04:00
|
|
|
// Set our port.
|
2023-03-06 20:34:25 -05:00
|
|
|
m_port = client_port;
|
|
|
|
if (m_port != nullptr) {
|
|
|
|
m_port->Open();
|
2021-04-23 20:00:15 -04:00
|
|
|
}
|
|
|
|
|
2021-04-13 20:48:37 -04:00
|
|
|
// Mark initialized.
|
2023-03-06 20:34:25 -05:00
|
|
|
m_initialized = true;
|
2021-04-13 20:48:37 -04:00
|
|
|
}
|
|
|
|
|
2021-04-23 20:00:15 -04:00
|
|
|
void KSession::Finalize() {
|
2023-03-06 20:34:25 -05:00
|
|
|
if (m_port != nullptr) {
|
|
|
|
m_port->OnSessionFinalized();
|
|
|
|
m_port->Close();
|
2021-04-23 20:00:15 -04:00
|
|
|
}
|
|
|
|
}
|
2021-04-13 20:48:37 -04:00
|
|
|
|
|
|
|
void KSession::OnServerClosed() {
|
2023-03-06 20:34:25 -05:00
|
|
|
if (this->GetState() == State::Normal) {
|
|
|
|
this->SetState(State::ServerClosed);
|
|
|
|
m_client.OnServerClosed();
|
2021-04-13 20:48:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KSession::OnClientClosed() {
|
2023-03-06 20:34:25 -05:00
|
|
|
if (this->GetState() == State::Normal) {
|
|
|
|
SetState(State::ClientClosed);
|
|
|
|
m_server.OnClientClosed();
|
2021-04-13 20:48:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KSession::PostDestroy(uintptr_t arg) {
|
|
|
|
// Release the session count resource the owner process holds.
|
2021-04-24 01:04:28 -04:00
|
|
|
KProcess* owner = reinterpret_cast<KProcess*>(arg);
|
2022-11-03 10:22:05 -04:00
|
|
|
owner->GetResourceLimit()->Release(LimitableResource::SessionCountMax, 1);
|
2021-04-13 20:48:37 -04:00
|
|
|
owner->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Kernel
|