Merge pull request #3284 from Subv/session_data

HLE/Services: Allow specifying a SessionData template parameter to ServiceFramework.
This commit is contained in:
Sebastian Valle 2017-12-16 09:14:34 -05:00 committed by GitHub
commit 125f5d1e68
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 103 additions and 88 deletions

View file

@ -2,26 +2,32 @@
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <algorithm>
#include <vector> #include <vector>
#include <boost/range/algorithm_ext/erase.hpp>
#include "common/assert.h" #include "common/assert.h"
#include "common/common_types.h" #include "common/common_types.h"
#include "core/hle/kernel/handle_table.h" #include "core/hle/kernel/handle_table.h"
#include "core/hle/kernel/hle_ipc.h" #include "core/hle/kernel/hle_ipc.h"
#include "core/hle/kernel/kernel.h" #include "core/hle/kernel/kernel.h"
#include "core/hle/kernel/process.h" #include "core/hle/kernel/process.h"
#include "core/hle/kernel/server_session.h"
namespace Kernel { namespace Kernel {
SessionRequestHandler::SessionInfo::SessionInfo(SharedPtr<ServerSession> session,
std::unique_ptr<SessionDataBase> data)
: session(std::move(session)), data(std::move(data)) {}
void SessionRequestHandler::ClientConnected(SharedPtr<ServerSession> server_session) { void SessionRequestHandler::ClientConnected(SharedPtr<ServerSession> server_session) {
server_session->SetHleHandler(shared_from_this()); server_session->SetHleHandler(shared_from_this());
connected_sessions.push_back(server_session); connected_sessions.emplace_back(std::move(server_session), MakeSessionData());
} }
void SessionRequestHandler::ClientDisconnected(SharedPtr<ServerSession> server_session) { void SessionRequestHandler::ClientDisconnected(SharedPtr<ServerSession> server_session) {
server_session->SetHleHandler(nullptr); server_session->SetHleHandler(nullptr);
boost::range::remove_erase(connected_sessions, server_session); connected_sessions.erase(
std::remove_if(connected_sessions.begin(), connected_sessions.end(),
[&](const SessionInfo& info) { return info.session == server_session; }),
connected_sessions.end());
} }
HLERequestContext::HLERequestContext(SharedPtr<ServerSession> session) HLERequestContext::HLERequestContext(SharedPtr<ServerSession> session)

View file

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <algorithm>
#include <array> #include <array>
#include <memory> #include <memory>
#include <vector> #include <vector>
@ -54,13 +55,36 @@ public:
* associated ServerSession. * associated ServerSession.
* @param server_session ServerSession associated with the connection. * @param server_session ServerSession associated with the connection.
*/ */
virtual void ClientDisconnected(SharedPtr<ServerSession> server_session); void ClientDisconnected(SharedPtr<ServerSession> server_session);
/// Empty placeholder structure for services with no per-session data. The session data classes
/// in each service must inherit from this.
struct SessionDataBase {};
protected: protected:
/// List of sessions that are connected to this handler. /// Creates the storage for the session data of the service.
/// A ServerSession whose server endpoint is an HLE implementation is kept alive by this list virtual std::unique_ptr<SessionDataBase> MakeSessionData() const = 0;
// for the duration of the connection.
std::vector<SharedPtr<ServerSession>> connected_sessions; /// Returns the session data associated with the server session.
template <typename T>
T* GetSessionData(SharedPtr<ServerSession> session) {
static_assert(std::is_base_of<SessionDataBase, T>(),
"T is not a subclass of SessionDataBase");
auto itr = std::find_if(connected_sessions.begin(), connected_sessions.end(),
[&](const SessionInfo& info) { return info.session == session; });
ASSERT(itr != connected_sessions.end());
return static_cast<T*>(itr->data.get());
}
struct SessionInfo {
SessionInfo(SharedPtr<ServerSession> session, std::unique_ptr<SessionDataBase> data);
SharedPtr<ServerSession> session;
std::unique_ptr<SessionDataBase> data;
};
/// List of sessions that are connected to this handler. A ServerSession whose server endpoint
/// is an HLE implementation is kept alive by this list for the duration of the connection.
std::vector<SessionInfo> connected_sessions;
}; };
class MappedBuffer { class MappedBuffer {

View file

@ -92,15 +92,15 @@ void File::Read(Kernel::HLERequestContext& ctx) {
LOG_TRACE(Service_FS, "Read %s: offset=0x%" PRIx64 " length=0x%08X", GetName().c_str(), offset, LOG_TRACE(Service_FS, "Read %s: offset=0x%" PRIx64 " length=0x%08X", GetName().c_str(), offset,
length); length);
const SessionSlot& file = GetSessionSlot(ctx.Session()); const FileSessionSlot* file = GetSessionData(ctx.Session());
if (file.subfile && length > file.size) { if (file->subfile && length > file->size) {
LOG_WARNING(Service_FS, "Trying to read beyond the subfile size, truncating"); LOG_WARNING(Service_FS, "Trying to read beyond the subfile size, truncating");
length = file.size; length = file->size;
} }
// This file session might have a specific offset from where to start reading, apply it. // This file session might have a specific offset from where to start reading, apply it.
offset += file.offset; offset += file->offset;
if (offset + length > backend->GetSize()) { if (offset + length > backend->GetSize()) {
LOG_ERROR(Service_FS, "Reading from out of bounds offset=0x%" PRIx64 LOG_ERROR(Service_FS, "Reading from out of bounds offset=0x%" PRIx64
@ -134,10 +134,10 @@ void File::Write(Kernel::HLERequestContext& ctx) {
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2); IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
const SessionSlot& file = GetSessionSlot(ctx.Session()); const FileSessionSlot* file = GetSessionData(ctx.Session());
// Subfiles can not be written to // Subfiles can not be written to
if (file.subfile) { if (file->subfile) {
rb.Push(FileSys::ERROR_UNSUPPORTED_OPEN_FLAGS); rb.Push(FileSys::ERROR_UNSUPPORTED_OPEN_FLAGS);
rb.Push<u32>(0); rb.Push<u32>(0);
rb.PushMappedBuffer(buffer); rb.PushMappedBuffer(buffer);
@ -160,28 +160,28 @@ void File::Write(Kernel::HLERequestContext& ctx) {
void File::GetSize(Kernel::HLERequestContext& ctx) { void File::GetSize(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx, 0x0804, 0, 0); IPC::RequestParser rp(ctx, 0x0804, 0, 0);
const SessionSlot& file = GetSessionSlot(ctx.Session()); const FileSessionSlot* file = GetSessionData(ctx.Session());
IPC::RequestBuilder rb = rp.MakeBuilder(3, 0); IPC::RequestBuilder rb = rp.MakeBuilder(3, 0);
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
rb.Push<u64>(file.size); rb.Push<u64>(file->size);
} }
void File::SetSize(Kernel::HLERequestContext& ctx) { void File::SetSize(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx, 0x0805, 2, 0); IPC::RequestParser rp(ctx, 0x0805, 2, 0);
u64 size = rp.Pop<u64>(); u64 size = rp.Pop<u64>();
SessionSlot& file = GetSessionSlot(ctx.Session()); FileSessionSlot* file = GetSessionData(ctx.Session());
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
// SetSize can not be called on subfiles. // SetSize can not be called on subfiles.
if (file.subfile) { if (file->subfile) {
rb.Push(FileSys::ERROR_UNSUPPORTED_OPEN_FLAGS); rb.Push(FileSys::ERROR_UNSUPPORTED_OPEN_FLAGS);
return; return;
} }
file.size = size; file->size = size;
backend->SetSize(size); backend->SetSize(size);
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
} }
@ -190,9 +190,9 @@ void File::Close(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx, 0x0808, 0, 0); IPC::RequestParser rp(ctx, 0x0808, 0, 0);
// TODO(Subv): Only close the backend if this client is the only one left. // TODO(Subv): Only close the backend if this client is the only one left.
if (session_slots.size() > 1) if (connected_sessions.size() > 1)
LOG_WARNING(Service_FS, "Closing File backend but %zu clients still connected", LOG_WARNING(Service_FS, "Closing File backend but %zu clients still connected",
session_slots.size()); connected_sessions.size());
backend->Close(); backend->Close();
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
@ -204,10 +204,10 @@ void File::Flush(Kernel::HLERequestContext& ctx) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
const SessionSlot& file = GetSessionSlot(ctx.Session()); const FileSessionSlot* file = GetSessionData(ctx.Session());
// Subfiles can not be flushed. // Subfiles can not be flushed.
if (file.subfile) { if (file->subfile) {
rb.Push(FileSys::ERROR_UNSUPPORTED_OPEN_FLAGS); rb.Push(FileSys::ERROR_UNSUPPORTED_OPEN_FLAGS);
return; return;
} }
@ -219,8 +219,8 @@ void File::Flush(Kernel::HLERequestContext& ctx) {
void File::SetPriority(Kernel::HLERequestContext& ctx) { void File::SetPriority(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx, 0x080A, 1, 0); IPC::RequestParser rp(ctx, 0x080A, 1, 0);
SessionSlot& file = GetSessionSlot(ctx.Session()); FileSessionSlot* file = GetSessionData(ctx.Session());
file.priority = rp.Pop<u32>(); file->priority = rp.Pop<u32>();
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
@ -228,11 +228,11 @@ void File::SetPriority(Kernel::HLERequestContext& ctx) {
void File::GetPriority(Kernel::HLERequestContext& ctx) { void File::GetPriority(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx, 0x080B, 0, 0); IPC::RequestParser rp(ctx, 0x080B, 0, 0);
const SessionSlot& file = GetSessionSlot(ctx.Session()); const FileSessionSlot* file = GetSessionData(ctx.Session());
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0); IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
rb.Push(file.priority); rb.Push(file->priority);
} }
void File::OpenLinkFile(Kernel::HLERequestContext& ctx) { void File::OpenLinkFile(Kernel::HLERequestContext& ctx) {
@ -246,16 +246,13 @@ void File::OpenLinkFile(Kernel::HLERequestContext& ctx) {
auto server = std::get<SharedPtr<ServerSession>>(sessions); auto server = std::get<SharedPtr<ServerSession>>(sessions);
ClientConnected(server); ClientConnected(server);
const SessionSlot& original_file = GetSessionSlot(ctx.Session()); FileSessionSlot* slot = GetSessionData(server);
const FileSessionSlot* original_file = GetSessionData(ctx.Session());
SessionSlot slot{}; slot->priority = original_file->priority;
slot.priority = original_file.priority; slot->offset = 0;
slot.offset = 0; slot->size = backend->GetSize();
slot.size = backend->GetSize(); slot->subfile = false;
slot.session = server;
slot.subfile = false;
session_slots.emplace_back(std::move(slot));
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
rb.PushMoveObjects(std::get<SharedPtr<ClientSession>>(sessions)); rb.PushMoveObjects(std::get<SharedPtr<ClientSession>>(sessions));
@ -268,9 +265,9 @@ void File::OpenSubFile(Kernel::HLERequestContext& ctx) {
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2); IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
const SessionSlot& original_file = GetSessionSlot(ctx.Session()); const FileSessionSlot* original_file = GetSessionData(ctx.Session());
if (original_file.subfile) { if (original_file->subfile) {
// OpenSubFile can not be called on a file which is already as subfile // OpenSubFile can not be called on a file which is already as subfile
rb.Push(FileSys::ERROR_UNSUPPORTED_OPEN_FLAGS); rb.Push(FileSys::ERROR_UNSUPPORTED_OPEN_FLAGS);
return; return;
@ -285,7 +282,7 @@ void File::OpenSubFile(Kernel::HLERequestContext& ctx) {
// TODO(Subv): Check for overflow and return ERR_WRITE_BEYOND_END // TODO(Subv): Check for overflow and return ERR_WRITE_BEYOND_END
if (end > original_file.size) { if (end > original_file->size) {
rb.Push(FileSys::ERR_WRITE_BEYOND_END); rb.Push(FileSys::ERR_WRITE_BEYOND_END);
return; return;
} }
@ -297,47 +294,26 @@ void File::OpenSubFile(Kernel::HLERequestContext& ctx) {
auto server = std::get<SharedPtr<ServerSession>>(sessions); auto server = std::get<SharedPtr<ServerSession>>(sessions);
ClientConnected(server); ClientConnected(server);
SessionSlot slot{}; FileSessionSlot* slot = GetSessionData(server);
slot.priority = original_file.priority; slot->priority = original_file->priority;
slot.offset = offset; slot->offset = offset;
slot.size = size; slot->size = size;
slot.session = server; slot->subfile = true;
slot.subfile = true;
session_slots.emplace_back(std::move(slot));
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
rb.PushMoveObjects(std::get<SharedPtr<ClientSession>>(sessions)); rb.PushMoveObjects(std::get<SharedPtr<ClientSession>>(sessions));
} }
File::SessionSlot& File::GetSessionSlot(Kernel::SharedPtr<Kernel::ServerSession> session) {
auto itr = std::find_if(session_slots.begin(), session_slots.end(),
[&](const SessionSlot& slot) { return slot.session == session; });
ASSERT(itr != session_slots.end());
return *itr;
}
void File::ClientDisconnected(Kernel::SharedPtr<Kernel::ServerSession> server_session) {
session_slots.erase(
std::remove_if(session_slots.begin(), session_slots.end(),
[&](const SessionSlot& slot) { return slot.session == server_session; }),
session_slots.end());
SessionRequestHandler::ClientDisconnected(server_session);
}
Kernel::SharedPtr<Kernel::ClientSession> File::Connect() { Kernel::SharedPtr<Kernel::ClientSession> File::Connect() {
auto sessions = Kernel::ServerSession::CreateSessionPair(GetName()); auto sessions = Kernel::ServerSession::CreateSessionPair(GetName());
auto server = std::get<Kernel::SharedPtr<Kernel::ServerSession>>(sessions); auto server = std::get<Kernel::SharedPtr<Kernel::ServerSession>>(sessions);
ClientConnected(server); ClientConnected(server);
SessionSlot slot{}; FileSessionSlot* slot = GetSessionData(server);
slot.priority = 0; slot->priority = 0;
slot.offset = 0; slot->offset = 0;
slot.size = backend->GetSize(); slot->size = backend->GetSize();
slot.session = server; slot->subfile = false;
slot.subfile = false;
session_slots.emplace_back(std::move(slot));
return std::get<Kernel::SharedPtr<Kernel::ClientSession>>(sessions); return std::get<Kernel::SharedPtr<Kernel::ClientSession>>(sessions);
} }

View file

@ -50,9 +50,16 @@ enum class MediaType : u32 { NAND = 0, SDMC = 1, GameCard = 2 };
typedef u64 ArchiveHandle; typedef u64 ArchiveHandle;
struct FileSessionSlot : public Kernel::SessionRequestHandler::SessionDataBase {
u32 priority; ///< Priority of the file. TODO(Subv): Find out what this means
u64 offset; ///< Offset that this session will start reading from.
u64 size; ///< Max size of the file that this session is allowed to access
bool subfile; ///< Whether this file was opened via OpenSubFile or not.
};
// TODO: File is not a real service, but it can still utilize ServiceFramework::RegisterHandlers. // TODO: File is not a real service, but it can still utilize ServiceFramework::RegisterHandlers.
// Consider splitting ServiceFramework interface. // Consider splitting ServiceFramework interface.
class File final : public ServiceFramework<File> { class File final : public ServiceFramework<File, FileSessionSlot> {
public: public:
File(std::unique_ptr<FileSys::FileBackend>&& backend, const FileSys::Path& path); File(std::unique_ptr<FileSys::FileBackend>&& backend, const FileSys::Path& path);
~File() = default; ~File() = default;
@ -67,8 +74,6 @@ public:
/// Creates a new session to this File and returns the ClientSession part of the connection. /// Creates a new session to this File and returns the ClientSession part of the connection.
Kernel::SharedPtr<Kernel::ClientSession> Connect(); Kernel::SharedPtr<Kernel::ClientSession> Connect();
void ClientDisconnected(Kernel::SharedPtr<Kernel::ServerSession> server_session) override;
private: private:
void Read(Kernel::HLERequestContext& ctx); void Read(Kernel::HLERequestContext& ctx);
void Write(Kernel::HLERequestContext& ctx); void Write(Kernel::HLERequestContext& ctx);
@ -80,18 +85,6 @@ private:
void GetPriority(Kernel::HLERequestContext& ctx); void GetPriority(Kernel::HLERequestContext& ctx);
void OpenLinkFile(Kernel::HLERequestContext& ctx); void OpenLinkFile(Kernel::HLERequestContext& ctx);
void OpenSubFile(Kernel::HLERequestContext& ctx); void OpenSubFile(Kernel::HLERequestContext& ctx);
struct SessionSlot {
Kernel::SharedPtr<Kernel::ServerSession> session; ///< The session that this slot refers to.
u32 priority; ///< Priority of the file. TODO(Subv): Find out what this means
u64 offset; ///< Offset that this session will start reading from.
u64 size; ///< Max size of the file that this session is allowed to access
bool subfile; ///< Whether this file was opened via OpenSubFile or not.
};
std::vector<SessionSlot> session_slots;
SessionSlot& GetSessionSlot(Kernel::SharedPtr<Kernel::ServerSession> session);
}; };
class Directory final : public Kernel::SessionRequestHandler { class Directory final : public Kernel::SessionRequestHandler {
@ -108,6 +101,10 @@ public:
protected: protected:
void HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) override; void HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) override;
std::unique_ptr<SessionDataBase> MakeSessionData() const override {
return nullptr;
}
}; };
/** /**

View file

@ -87,6 +87,10 @@ public:
protected: protected:
void HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) override; void HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) override;
std::unique_ptr<SessionDataBase> MakeSessionData() const override {
return nullptr;
}
/** /**
* Registers the functions in the service * Registers the functions in the service
*/ */
@ -144,7 +148,7 @@ protected:
using HandlerFnP = void (Self::*)(Kernel::HLERequestContext&); using HandlerFnP = void (Self::*)(Kernel::HLERequestContext&);
private: private:
template <typename T> template <typename T, typename SessionData>
friend class ServiceFramework; friend class ServiceFramework;
struct FunctionInfoBase { struct FunctionInfoBase {
@ -190,7 +194,7 @@ private:
* of the passed in function pointers and then delegate the actual work to the implementation in the * of the passed in function pointers and then delegate the actual work to the implementation in the
* base class. * base class.
*/ */
template <typename Self> template <typename Self, typename SessionData = Kernel::SessionRequestHandler::SessionDataBase>
class ServiceFramework : public ServiceFrameworkBase { class ServiceFramework : public ServiceFrameworkBase {
protected: protected:
/// Contains information about a request type which is handled by the service. /// Contains information about a request type which is handled by the service.
@ -236,6 +240,14 @@ protected:
RegisterHandlersBase(functions, n); RegisterHandlersBase(functions, n);
} }
std::unique_ptr<SessionDataBase> MakeSessionData() const override {
return std::make_unique<SessionData>();
}
SessionData* GetSessionData(Kernel::SharedPtr<Kernel::ServerSession> server_session) {
return ServiceFrameworkBase::GetSessionData<SessionData>(server_session);
}
private: private:
/** /**
* This function is used to allow invocation of pointers to handlers stored in the base class * This function is used to allow invocation of pointers to handlers stored in the base class