2021-04-23 17:00:15 -07:00
|
|
|
// Copyright 2021 yuzu emulator team
|
2021-04-13 17:48:37 -07:00
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2021-04-23 17:00:15 -07:00
|
|
|
#include "core/hle/kernel/k_client_port.h"
|
2021-04-13 17:48:37 -07: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 {
|
|
|
|
|
2021-05-08 09:11:36 -07:00
|
|
|
KSession::KSession(KernelCore& kernel_)
|
|
|
|
: KAutoObjectWithSlabHeapAndContainer{kernel_}, server{kernel_}, client{kernel_} {}
|
2021-04-13 17:48:37 -07:00
|
|
|
KSession::~KSession() = default;
|
|
|
|
|
2021-06-04 19:26:48 -07:00
|
|
|
void KSession::Initialize(KClientPort* port_, const std::string& name_,
|
|
|
|
std::shared_ptr<SessionRequestManager> manager_) {
|
2021-04-13 17:48:37 -07: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.
|
|
|
|
Open();
|
|
|
|
|
|
|
|
// Create our sub sessions.
|
|
|
|
KAutoObject::Create(std::addressof(server));
|
|
|
|
KAutoObject::Create(std::addressof(client));
|
|
|
|
|
|
|
|
// Initialize our sub sessions.
|
2021-06-04 19:26:48 -07:00
|
|
|
server.Initialize(this, name_ + ":Server", manager_);
|
2021-04-13 17:48:37 -07:00
|
|
|
client.Initialize(this, name_ + ":Client");
|
|
|
|
|
|
|
|
// Set state and name.
|
|
|
|
SetState(State::Normal);
|
2021-04-23 21:50:04 -07:00
|
|
|
name = name_;
|
2021-04-13 17:48:37 -07:00
|
|
|
|
|
|
|
// Set our owner process.
|
|
|
|
process = kernel.CurrentProcess();
|
|
|
|
process->Open();
|
|
|
|
|
2021-04-23 17:00:15 -07:00
|
|
|
// Set our port.
|
|
|
|
port = port_;
|
|
|
|
if (port != nullptr) {
|
|
|
|
port->Open();
|
|
|
|
}
|
|
|
|
|
2021-04-13 17:48:37 -07:00
|
|
|
// Mark initialized.
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
2021-04-23 17:00:15 -07:00
|
|
|
void KSession::Finalize() {
|
2021-05-01 12:48:41 -07:00
|
|
|
if (port == nullptr) {
|
|
|
|
return;
|
2021-04-23 17:00:15 -07:00
|
|
|
}
|
2021-05-01 12:48:41 -07:00
|
|
|
|
|
|
|
port->OnSessionFinalized();
|
|
|
|
port->Close();
|
2021-04-23 17:00:15 -07:00
|
|
|
}
|
2021-04-13 17:48:37 -07:00
|
|
|
|
|
|
|
void KSession::OnServerClosed() {
|
2021-05-01 12:48:41 -07:00
|
|
|
if (GetState() != State::Normal) {
|
|
|
|
return;
|
2021-04-13 17:48:37 -07:00
|
|
|
}
|
2021-05-01 12:48:41 -07:00
|
|
|
|
|
|
|
SetState(State::ServerClosed);
|
|
|
|
client.OnServerClosed();
|
2021-04-13 17:48:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void KSession::OnClientClosed() {
|
2021-05-01 12:48:41 -07:00
|
|
|
if (GetState() != State::Normal) {
|
|
|
|
return;
|
2021-04-13 17:48:37 -07:00
|
|
|
}
|
2021-05-01 12:48:41 -07:00
|
|
|
|
|
|
|
SetState(State::ClientClosed);
|
|
|
|
server.OnClientClosed();
|
2021-04-13 17:48:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void KSession::PostDestroy(uintptr_t arg) {
|
|
|
|
// Release the session count resource the owner process holds.
|
2021-04-23 22:04:28 -07:00
|
|
|
KProcess* owner = reinterpret_cast<KProcess*>(arg);
|
2021-05-15 22:34:55 -07:00
|
|
|
owner->GetResourceLimit()->Release(LimitableResource::Sessions, 1);
|
2021-04-13 17:48:37 -07:00
|
|
|
owner->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Kernel
|