2018-04-18 16:57:06 +02:00
|
|
|
#pragma once
|
|
|
|
#include <switch.h>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
#include "iserviceobject.hpp"
|
2018-04-18 19:41:17 +02:00
|
|
|
#include "iwaitable.hpp"
|
2018-04-18 16:57:06 +02:00
|
|
|
#include "servicesession.hpp"
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class ServiceSession;
|
|
|
|
|
|
|
|
template <typename T>
|
2018-04-18 20:10:45 +02:00
|
|
|
class ServiceServer : public IWaitable {
|
2018-04-18 16:57:06 +02:00
|
|
|
static_assert(std::is_base_of<IServiceObject, T>::value, "Service Objects must derive from IServiceObject");
|
|
|
|
|
|
|
|
Handle port_handle;
|
|
|
|
unsigned int max_sessions;
|
2018-04-18 19:41:17 +02:00
|
|
|
unsigned int num_sessions;
|
2018-04-18 16:57:06 +02:00
|
|
|
ServiceSession<T> **sessions;
|
|
|
|
|
|
|
|
public:
|
2018-04-19 00:24:40 +02:00
|
|
|
ServiceServer(const char *service_name, unsigned int max_s) : max_sessions(max_s) {
|
2018-04-18 20:10:45 +02:00
|
|
|
if (R_FAILED(smRegisterService(&this->port_handle, service_name, false, this->max_sessions))) {
|
|
|
|
/* TODO: Panic. */
|
|
|
|
}
|
|
|
|
this->sessions = new ServiceSession<T> *[this->max_sessions];
|
|
|
|
for (unsigned int i = 0; i < this->max_sessions; i++) {
|
|
|
|
this->sessions[i] = NULL;
|
|
|
|
}
|
|
|
|
this->num_sessions = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~ServiceServer() {
|
|
|
|
for (unsigned int i = 0; i < this->max_sessions; i++) {
|
|
|
|
if (this->sessions[i]) {
|
|
|
|
delete this->sessions[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
delete this->sessions;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (port_handle) {
|
|
|
|
svcCloseHandle(port_handle);
|
|
|
|
}
|
|
|
|
}
|
2018-04-18 16:57:06 +02:00
|
|
|
|
2018-04-18 19:41:17 +02:00
|
|
|
/* IWaitable */
|
2018-04-18 20:10:45 +02:00
|
|
|
virtual unsigned int get_num_waitables() {
|
|
|
|
unsigned int n = 1;
|
|
|
|
for (unsigned int i = 0; i < this->max_sessions; i++) {
|
|
|
|
if (this->sessions[i]) {
|
|
|
|
n += this->sessions[i]->get_num_waitables();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void get_waitables(IWaitable **dst) {
|
|
|
|
dst[0] = this;
|
|
|
|
unsigned int n = 0;
|
|
|
|
for (unsigned int i = 0; i < this->max_sessions; i++) {
|
|
|
|
if (this->sessions[i]) {
|
|
|
|
this->sessions[i]->get_waitables(&dst[1 + n]);
|
|
|
|
n += this->sessions[i]->get_num_waitables();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void delete_child(IWaitable *child) {
|
|
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < this->max_sessions; i++) {
|
|
|
|
if (this->sessions[i] == child) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == this->max_sessions) {
|
|
|
|
/* TODO: Panic, because this isn't our child. */
|
|
|
|
} else {
|
|
|
|
delete this->sessions[i];
|
|
|
|
this->sessions[i] = NULL;
|
|
|
|
this->num_sessions--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Handle get_handle() {
|
|
|
|
return this->port_handle;
|
|
|
|
}
|
|
|
|
|
2018-04-18 20:53:04 +02:00
|
|
|
virtual Result handle_signaled(u64 timeout) {
|
2018-04-18 20:10:45 +02:00
|
|
|
/* If this server's port was signaled, accept a new session. */
|
|
|
|
Handle session_h;
|
|
|
|
svcAcceptSession(&session_h, this->port_handle);
|
2018-04-19 00:24:40 +02:00
|
|
|
|
|
|
|
fprintf(stderr, "Accept %08X -> %08X\n", this->port_handle, session_h);
|
|
|
|
fprintf(stderr, "Sessions: %08X/%08X\n", this->num_sessions, this->max_sessions);
|
2018-04-18 20:10:45 +02:00
|
|
|
|
|
|
|
if (this->num_sessions >= this->max_sessions) {
|
2018-04-19 00:24:40 +02:00
|
|
|
fprintf(stderr, "Closing because of max sessions...\n");
|
2018-04-18 20:10:45 +02:00
|
|
|
svcCloseHandle(session_h);
|
|
|
|
return 0x10601;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < this->max_sessions; i++) {
|
|
|
|
if (this->sessions[i] == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->sessions[i] = new ServiceSession<T>(this, session_h, 0);
|
2018-04-19 00:24:40 +02:00
|
|
|
this->sessions[i]->set_parent(this);
|
2018-04-18 20:10:45 +02:00
|
|
|
this->num_sessions++;
|
|
|
|
return 0;
|
|
|
|
}
|
2018-04-18 16:57:06 +02:00
|
|
|
};
|