2020-07-09 22:49:51 +01:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-07-09 22:49:51 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <mesosphere.hpp>
|
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
|
|
|
void KSession::Initialize(KClientPort *client_port, uintptr_t name) {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
/* Increment reference count. */
|
2020-07-09 23:25:01 +01:00
|
|
|
/* 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. */
|
2020-07-09 22:49:51 +01:00
|
|
|
this->Open();
|
|
|
|
|
|
|
|
/* Create our sub sessions. */
|
2021-10-24 00:04:04 +01:00
|
|
|
KAutoObject::Create<KServerSession>(std::addressof(m_server));
|
|
|
|
KAutoObject::Create<KClientSession>(std::addressof(m_client));
|
2020-07-09 22:49:51 +01:00
|
|
|
|
|
|
|
/* Initialize our sub sessions. */
|
2020-12-18 01:18:47 +00:00
|
|
|
m_server.Initialize(this);
|
|
|
|
m_client.Initialize(this);
|
2020-07-09 22:49:51 +01:00
|
|
|
|
|
|
|
/* Set state and name. */
|
2021-04-07 22:45:38 +01:00
|
|
|
this->SetState(State::Normal);
|
2020-12-18 01:18:47 +00:00
|
|
|
m_name = name;
|
2020-07-09 22:49:51 +01:00
|
|
|
|
|
|
|
/* Set our owner process. */
|
2020-12-18 01:18:47 +00:00
|
|
|
m_process = GetCurrentProcessPointer();
|
|
|
|
m_process->Open();
|
2020-07-09 22:49:51 +01:00
|
|
|
|
|
|
|
/* Set our port. */
|
2020-12-18 01:18:47 +00:00
|
|
|
m_port = client_port;
|
|
|
|
if (m_port != nullptr) {
|
|
|
|
m_port->Open();
|
2020-07-09 22:49:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Mark initialized. */
|
2020-12-18 01:18:47 +00:00
|
|
|
m_initialized = true;
|
2020-07-09 22:49:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void KSession::Finalize() {
|
2020-12-18 01:18:47 +00:00
|
|
|
if (m_port != nullptr) {
|
|
|
|
m_port->OnSessionFinalized();
|
|
|
|
m_port->Close();
|
2020-07-09 22:49:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KSession::OnServerClosed() {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
2021-04-07 22:45:38 +01:00
|
|
|
if (this->GetState() == State::Normal) {
|
|
|
|
this->SetState(State::ServerClosed);
|
2020-12-18 01:18:47 +00:00
|
|
|
m_client.OnServerClosed();
|
2020-07-09 22:49:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KSession::OnClientClosed() {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
2021-04-07 22:45:38 +01:00
|
|
|
if (this->GetState() == State::Normal) {
|
|
|
|
this->SetState(State::ClientClosed);
|
2020-12-18 01:18:47 +00:00
|
|
|
m_server.OnClientClosed();
|
2020-07-09 22:49:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KSession::PostDestroy(uintptr_t arg) {
|
|
|
|
/* Release the session count resource the owner process holds. */
|
|
|
|
KProcess *owner = reinterpret_cast<KProcess *>(arg);
|
|
|
|
owner->ReleaseResource(ams::svc::LimitableResource_SessionCountMax, 1);
|
|
|
|
owner->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|