HLE/Services: Allow specifying a SessionData template parameter to ServiceFramework.

Some services can have multiple clients at the same time, and they identify the different clients using the server session as a key.
This parameter (if present) should be a structure that contains the per-session data for each service.
The data can be retrieved using ServiceFramework::GetSessionData(session)
This commit is contained in:
Subv
2017-12-13 13:56:53 -05:00
parent aecd2b85fe
commit daecd812b0
4 changed files with 57 additions and 11 deletions

View File

@ -4,6 +4,7 @@
#pragma once
#include <algorithm>
#include <array>
#include <memory>
#include <vector>
@ -54,13 +55,36 @@ public:
* associated ServerSession.
* @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:
/// 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<SharedPtr<ServerSession>> connected_sessions;
/// Creates the storage for the session data of the service.
virtual std::unique_ptr<SessionDataBase> MakeSessionData() const = 0;
/// 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 {