Sunshine0.0.5917
Sunshine 0.0.5917
Self-hosted game stream host for Moonlight.
Loading...
Searching...
No Matches
Go to the documentation of this file.
1
5#pragma once
6
7#ifndef __kernel_entry
12 #define __kernel_entry
13#endif
14
15// standard includes
16#include <optional>
17#include <unordered_map>
18
19// local includes
20#include "config.h"
21#include "platform/common.h"
22#include "rtsp.h"
24#include "utility.h"
25
30#define DEFAULT_APP_IMAGE_PATH SUNSHINE_ASSETS_DIR "/box.png"
31
32namespace proc {
37
42
55 struct ctx_t {
56 std::vector<cmd_t> prep_cmds;
57
70 std::vector<std::string> detached;
71
72 std::string name;
73 std::string cmd;
74 std::string working_dir;
75 std::string output;
76 std::string image_path;
77 std::string id;
78 bool elevated;
80 bool wait_all;
81 std::chrono::seconds exit_timeout;
82 };
83
87 class proc_t {
88 public:
90
91
98 boost::process::v1::environment &&env,
99 std::vector<ctx_t> &&apps
100 ):
101 _app_id(0),
102 _env(std::move(env)),
103 _apps(std::move(apps)) {
104 }
105
113 int execute(int app_id, std::shared_ptr<rtsp_stream::launch_session_t> launch_session);
114
118 int running();
119
120 ~proc_t();
121
127 const std::vector<ctx_t> &get_apps() const;
133 std::vector<ctx_t> &get_apps();
140 std::string get_app_image(int app_id);
146 std::string get_last_run_app_name();
150 void terminate();
151
161 void update_apps_and_env(proc_t &&other);
162
163#ifdef SUNSHINE_TESTS
167 const boost::process::v1::environment &get_env() const {
168 return _env;
169 }
170#endif
171
172 private:
173 int _app_id;
174
176 std::vector<ctx_t> _apps;
177 ctx_t _app;
178 std::chrono::steady_clock::time_point _app_launch_time;
179
180 // If no command associated with _app_id, yet it's still running
181 bool placebo {};
182
183 boost::process::v1::child _process;
184 boost::process::v1::group _process_group;
185
186 file_t _pipe;
187 std::vector<cmd_t>::const_iterator _app_prep_it;
188 std::vector<cmd_t>::const_iterator _app_prep_begin;
189 };
190
199 std::tuple<std::string, std::string> calculate_app_id(const std::string &app_name, std::string app_image_path, int index);
200
207 std::string prepare_command(const std::string &command);
208
209 bool check_valid_png(const std::filesystem::path &path);
216 std::string validate_app_image_path(std::string app_image_path);
222 void refresh(const std::string &file_name);
229 std::optional<proc::proc_t> parse(const std::string &file_name);
230
235 std::unique_ptr<platf::deinit_t> init();
236
243 void terminate_process_group(boost::process::v1::child &child_process, boost::process::v1::group &group, std::chrono::seconds exit_timeout);
244
245 extern proc_t proc;
246} // namespace proc
Includes Boost.Process while isolating third-party deprecation diagnostics.
Tracks launched child processes and terminates them during shutdown.
Definition process.h:87
std::string get_app_image(int app_id)
Get app image.
Definition process.cpp:409
int running()
Definition process.cpp:311
void update_apps_and_env(proc_t &&other)
Update the cached app list and environment without disturbing running process state.
Definition process.cpp:422
void terminate()
Terminate the launched application process.
Definition process.cpp:346
int execute(int app_id, std::shared_ptr< rtsp_stream::launch_session_t > launch_session)
Launch the configured application process.
Definition process.cpp:173
std::string get_last_run_app_name()
Get last run app name.
Definition process.cpp:418
const std::vector< ctx_t > & get_apps() const
Return the configured applications.
Definition process.cpp:397
proc_t(proc_t &&)=default
Defaulted move constructor for the generated type.
Declarations for common platform specific utilities.
basic_environment< char > environment
Map of environment variable names to values.
Definition common.h:68
Declarations for the configuration of Sunshine.
void refresh(const std::string &file_name)
Refresh cached platform state from the operating system.
Definition process.cpp:853
std::string prepare_command(const std::string &command)
Prepare a configured command for execution.
Definition process.cpp:156
std::unique_ptr< platf::deinit_t > init()
Initialize proc functions.
Definition process.cpp:66
proc_t proc
Global process registry used to track and terminate child processes.
Definition process.cpp:51
bool check_valid_png(const std::filesystem::path &path)
Validates a path whether it is a valid PNG.
Definition process.cpp:540
std::tuple< std::string, std::string > calculate_app_id(const std::string &app_name, std::string app_image_path, int index)
Calculate a stable id based on name and image data.
Definition process.cpp:672
std::optional< proc::proc_t > parse(const std::string &file_name)
Parse serialized text into the corresponding runtime representation.
Definition process.cpp:706
void terminate_process_group(boost::process::v1::child &child_process, boost::process::v1::group &group, std::chrono::seconds exit_timeout)
Terminates all child processes in a process group.
Definition process.cpp:70
std::string validate_app_image_path(std::string app_image_path)
Validate app image path.
Definition process.cpp:572
config::prep_cmd_t cmd_t
Parsed command arguments used when launching a child process.
Definition process.h:41
util::safe_ptr_v2< FILE, int, &fclose > file_t
Boost.Process pipe stream used for child-process I/O.
Definition process.h:36
Declarations for RTSP streaming.
External preparation command plus its privilege requirement.
Definition config.h:342
Definition process.h:55
std::string id
Stable identifier for the configured application.
Definition process.h:77
std::string working_dir
Working dir.
Definition process.h:74
bool elevated
Whether the process should be launched elevated.
Definition process.h:78
bool wait_all
Whether Sunshine waits for all child processes.
Definition process.h:80
std::vector< cmd_t > prep_cmds
Prep cmds.
Definition process.h:56
std::chrono::seconds exit_timeout
Exit timeout.
Definition process.h:81
std::vector< std::string > detached
Definition process.h:70
std::string cmd
Command line used to launch the application.
Definition process.h:73
std::string image_path
Image path.
Definition process.h:76
std::string name
Human-readable name for this item.
Definition process.h:72
bool auto_detach
Whether the process should detach automatically.
Definition process.h:79
std::string output
Captured output from the launched process.
Definition process.h:75
Declarations for utility functions.
uniq_ptr< T, Destroy< T *, ReturnType, function > > safe_ptr_v2
Safe pointer wrapper for APIs whose release function returns a status value.
Definition utility.h:1597
#define KITTY_DEFAULT_CONSTR_MOVE_THROW(x)
Declare defaulted move construction and assignment that may throw.
Definition utility.h:133