mirror of
https://github.com/LizardByte/Sunshine.git
synced 2025-03-29 22:20:24 +00:00
refactor(main): move remaining entry related code (#2127)
This commit is contained in:
parent
a420760d36
commit
8689469ea8
@ -45,8 +45,12 @@ set(SUNSHINE_TARGET_FILES
|
||||
"${CMAKE_SOURCE_DIR}/src/uuid.h"
|
||||
"${CMAKE_SOURCE_DIR}/src/config.h"
|
||||
"${CMAKE_SOURCE_DIR}/src/config.cpp"
|
||||
"${CMAKE_SOURCE_DIR}/src/entry_handler.cpp"
|
||||
"${CMAKE_SOURCE_DIR}/src/entry_handler.h"
|
||||
"${CMAKE_SOURCE_DIR}/src/file_handler.cpp"
|
||||
"${CMAKE_SOURCE_DIR}/src/file_handler.h"
|
||||
"${CMAKE_SOURCE_DIR}/src/globals.cpp"
|
||||
"${CMAKE_SOURCE_DIR}/src/globals.h"
|
||||
"${CMAKE_SOURCE_DIR}/src/logging.cpp"
|
||||
"${CMAKE_SOURCE_DIR}/src/logging.h"
|
||||
"${CMAKE_SOURCE_DIR}/src/main.cpp"
|
||||
|
5
docs/source/source_code/src/entry_handler.rst
Normal file
5
docs/source/source_code/src/entry_handler.rst
Normal file
@ -0,0 +1,5 @@
|
||||
entry_handler
|
||||
=============
|
||||
|
||||
.. doxygenfile:: entry_handler.h
|
||||
:allow-dot-graphs:
|
5
docs/source/source_code/src/globals.rst
Normal file
5
docs/source/source_code/src/globals.rst
Normal file
@ -0,0 +1,5 @@
|
||||
globals
|
||||
=======
|
||||
|
||||
.. doxygenfile:: globals.h
|
||||
:allow-dot-graphs:
|
@ -10,8 +10,8 @@
|
||||
|
||||
#include "audio.h"
|
||||
#include "config.h"
|
||||
#include "globals.h"
|
||||
#include "logging.h"
|
||||
#include "main.h"
|
||||
#include "thread_safe.h"
|
||||
#include "utility.h"
|
||||
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
@ -15,9 +16,9 @@
|
||||
#include <boost/property_tree/ptree.hpp>
|
||||
|
||||
#include "config.h"
|
||||
#include "entry_handler.h"
|
||||
#include "file_handler.h"
|
||||
#include "logging.h"
|
||||
#include "main.h"
|
||||
#include "nvhttp.h"
|
||||
#include "rtsp.h"
|
||||
#include "utility.h"
|
||||
|
@ -30,9 +30,9 @@
|
||||
#include "confighttp.h"
|
||||
#include "crypto.h"
|
||||
#include "file_handler.h"
|
||||
#include "globals.h"
|
||||
#include "httpcommon.h"
|
||||
#include "logging.h"
|
||||
#include "main.h"
|
||||
#include "network.h"
|
||||
#include "nvhttp.h"
|
||||
#include "platform/common.h"
|
||||
|
384
src/entry_handler.cpp
Normal file
384
src/entry_handler.cpp
Normal file
@ -0,0 +1,384 @@
|
||||
/**
|
||||
* @file entry_handler.cpp
|
||||
* @brief Entry point related functions.
|
||||
*/
|
||||
|
||||
// standard includes
|
||||
#include <csignal>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
// local includes
|
||||
#include "config.h"
|
||||
#include "confighttp.h"
|
||||
#include "entry_handler.h"
|
||||
#include "globals.h"
|
||||
#include "httpcommon.h"
|
||||
#include "logging.h"
|
||||
#include "network.h"
|
||||
#include "platform/common.h"
|
||||
#include "version.h"
|
||||
|
||||
extern "C" {
|
||||
#ifdef _WIN32
|
||||
#include <iphlpapi.h>
|
||||
#endif
|
||||
}
|
||||
|
||||
using namespace std::literals;
|
||||
|
||||
/**
|
||||
* @brief Launch the Web UI.
|
||||
*
|
||||
* EXAMPLES:
|
||||
* ```cpp
|
||||
* launch_ui();
|
||||
* ```
|
||||
*/
|
||||
void
|
||||
launch_ui() {
|
||||
std::string url = "https://localhost:" + std::to_string(net::map_port(confighttp::PORT_HTTPS));
|
||||
platf::open_url(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Launch the Web UI at a specific endpoint.
|
||||
*
|
||||
* EXAMPLES:
|
||||
* ```cpp
|
||||
* launch_ui_with_path("/pin");
|
||||
* ```
|
||||
*/
|
||||
void
|
||||
launch_ui_with_path(std::string path) {
|
||||
std::string url = "https://localhost:" + std::to_string(net::map_port(confighttp::PORT_HTTPS)) + path;
|
||||
platf::open_url(url);
|
||||
}
|
||||
|
||||
namespace args {
|
||||
/**
|
||||
* @brief Reset the user credentials.
|
||||
*
|
||||
* @param name The name of the program.
|
||||
* @param argc The number of arguments.
|
||||
* @param argv The arguments.
|
||||
*
|
||||
* EXAMPLES:
|
||||
* ```cpp
|
||||
* creds("sunshine", 2, {"new_username", "new_password"});
|
||||
* ```
|
||||
*/
|
||||
int
|
||||
creds(const char *name, int argc, char *argv[]) {
|
||||
if (argc < 2 || argv[0] == "help"sv || argv[1] == "help"sv) {
|
||||
help(name, argc, argv);
|
||||
}
|
||||
|
||||
http::save_user_creds(config::sunshine.credentials_file, argv[0], argv[1]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print help to stdout, then exit.
|
||||
* @param name The name of the program.
|
||||
* @param argc The number of arguments. (Unused)
|
||||
* @param argv The arguments. (Unused)
|
||||
*
|
||||
* EXAMPLES:
|
||||
* ```cpp
|
||||
* print_help("sunshine", 0, nullptr);
|
||||
* ```
|
||||
*/
|
||||
int
|
||||
help(const char *name, int argc, char *argv[]) {
|
||||
print_help(name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print the version to stdout, then exit.
|
||||
* @param name The name of the program. (Unused)
|
||||
* @param argc The number of arguments. (Unused)
|
||||
* @param argv The arguments. (Unused)
|
||||
*
|
||||
* EXAMPLES:
|
||||
* ```cpp
|
||||
* version("sunshine", 0, nullptr);
|
||||
* ```
|
||||
*/
|
||||
int
|
||||
version(const char *name, int argc, char *argv[]) {
|
||||
std::cout << PROJECT_NAME << " version: v" << PROJECT_VER << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
/**
|
||||
* @brief Restore global NVIDIA control panel settings.
|
||||
*
|
||||
* If Sunshine was improperly terminated, this function restores
|
||||
* the global NVIDIA control panel settings to the undo file left
|
||||
* by Sunshine. This function is typically called by the uninstaller.
|
||||
*
|
||||
* @param name The name of the program. (Unused)
|
||||
* @param argc The number of arguments. (Unused)
|
||||
* @param argv The arguments. (Unused)
|
||||
*
|
||||
* EXAMPLES:
|
||||
* ```cpp
|
||||
* restore_nvprefs_undo("sunshine", 0, nullptr);
|
||||
* ```
|
||||
*/
|
||||
int
|
||||
restore_nvprefs_undo(const char *name, int argc, char *argv[]) {
|
||||
if (nvprefs_instance.load()) {
|
||||
nvprefs_instance.restore_from_and_delete_undo_file_if_exists();
|
||||
nvprefs_instance.unload();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
} // namespace args
|
||||
|
||||
namespace lifetime {
|
||||
char **argv;
|
||||
std::atomic_int desired_exit_code;
|
||||
|
||||
/**
|
||||
* @brief Terminates Sunshine gracefully with the provided exit code.
|
||||
* @param exit_code The exit code to return from main().
|
||||
* @param async Specifies whether our termination will be non-blocking.
|
||||
*/
|
||||
void
|
||||
exit_sunshine(int exit_code, bool async) {
|
||||
// Store the exit code of the first exit_sunshine() call
|
||||
int zero = 0;
|
||||
desired_exit_code.compare_exchange_strong(zero, exit_code);
|
||||
|
||||
// Raise SIGINT to start termination
|
||||
std::raise(SIGINT);
|
||||
|
||||
// Termination will happen asynchronously, but the caller may
|
||||
// have wanted synchronous behavior.
|
||||
while (!async) {
|
||||
std::this_thread::sleep_for(1s);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Gets the argv array passed to main().
|
||||
*/
|
||||
char **
|
||||
get_argv() {
|
||||
return argv;
|
||||
}
|
||||
} // namespace lifetime
|
||||
|
||||
#ifdef _WIN32
|
||||
/**
|
||||
* @brief Check if NVIDIA's GameStream software is running.
|
||||
* @return `true` if GameStream is enabled, `false` otherwise.
|
||||
*/
|
||||
bool
|
||||
is_gamestream_enabled() {
|
||||
DWORD enabled;
|
||||
DWORD size = sizeof(enabled);
|
||||
return RegGetValueW(
|
||||
HKEY_LOCAL_MACHINE,
|
||||
L"SOFTWARE\\NVIDIA Corporation\\NvStream",
|
||||
L"EnableStreaming",
|
||||
RRF_RT_REG_DWORD,
|
||||
nullptr,
|
||||
&enabled,
|
||||
&size) == ERROR_SUCCESS &&
|
||||
enabled != 0;
|
||||
}
|
||||
|
||||
namespace service_ctrl {
|
||||
class service_controller {
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor for service_controller class.
|
||||
* @param service_desired_access SERVICE_* desired access flags.
|
||||
*/
|
||||
service_controller(DWORD service_desired_access) {
|
||||
scm_handle = OpenSCManagerA(nullptr, nullptr, SC_MANAGER_CONNECT);
|
||||
if (!scm_handle) {
|
||||
auto winerr = GetLastError();
|
||||
BOOST_LOG(error) << "OpenSCManager() failed: "sv << winerr;
|
||||
return;
|
||||
}
|
||||
|
||||
service_handle = OpenServiceA(scm_handle, "SunshineService", service_desired_access);
|
||||
if (!service_handle) {
|
||||
auto winerr = GetLastError();
|
||||
BOOST_LOG(error) << "OpenService() failed: "sv << winerr;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
~service_controller() {
|
||||
if (service_handle) {
|
||||
CloseServiceHandle(service_handle);
|
||||
}
|
||||
|
||||
if (scm_handle) {
|
||||
CloseServiceHandle(scm_handle);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Asynchronously starts the Sunshine service.
|
||||
*/
|
||||
bool
|
||||
start_service() {
|
||||
if (!service_handle) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!StartServiceA(service_handle, 0, nullptr)) {
|
||||
auto winerr = GetLastError();
|
||||
if (winerr != ERROR_SERVICE_ALREADY_RUNNING) {
|
||||
BOOST_LOG(error) << "StartService() failed: "sv << winerr;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Query the service status.
|
||||
* @param status The SERVICE_STATUS struct to populate.
|
||||
*/
|
||||
bool
|
||||
query_service_status(SERVICE_STATUS &status) {
|
||||
if (!service_handle) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!QueryServiceStatus(service_handle, &status)) {
|
||||
auto winerr = GetLastError();
|
||||
BOOST_LOG(error) << "QueryServiceStatus() failed: "sv << winerr;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
SC_HANDLE scm_handle = NULL;
|
||||
SC_HANDLE service_handle = NULL;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Check if the service is running.
|
||||
*
|
||||
* EXAMPLES:
|
||||
* ```cpp
|
||||
* is_service_running();
|
||||
* ```
|
||||
*/
|
||||
bool
|
||||
is_service_running() {
|
||||
service_controller sc { SERVICE_QUERY_STATUS };
|
||||
|
||||
SERVICE_STATUS status;
|
||||
if (!sc.query_service_status(status)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return status.dwCurrentState == SERVICE_RUNNING;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Start the service and wait for startup to complete.
|
||||
*
|
||||
* EXAMPLES:
|
||||
* ```cpp
|
||||
* start_service();
|
||||
* ```
|
||||
*/
|
||||
bool
|
||||
start_service() {
|
||||
service_controller sc { SERVICE_QUERY_STATUS | SERVICE_START };
|
||||
|
||||
std::cout << "Starting Sunshine..."sv;
|
||||
|
||||
// This operation is asynchronous, so we must wait for it to complete
|
||||
if (!sc.start_service()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SERVICE_STATUS status;
|
||||
do {
|
||||
Sleep(1000);
|
||||
std::cout << '.';
|
||||
} while (sc.query_service_status(status) && status.dwCurrentState == SERVICE_START_PENDING);
|
||||
|
||||
if (status.dwCurrentState != SERVICE_RUNNING) {
|
||||
BOOST_LOG(error) << SERVICE_NAME " failed to start: "sv << status.dwWin32ExitCode;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Wait for the UI to be ready after Sunshine startup.
|
||||
*
|
||||
* EXAMPLES:
|
||||
* ```cpp
|
||||
* wait_for_ui_ready();
|
||||
* ```
|
||||
*/
|
||||
bool
|
||||
wait_for_ui_ready() {
|
||||
std::cout << "Waiting for Web UI to be ready...";
|
||||
|
||||
// Wait up to 30 seconds for the web UI to start
|
||||
for (int i = 0; i < 30; i++) {
|
||||
PMIB_TCPTABLE tcp_table = nullptr;
|
||||
ULONG table_size = 0;
|
||||
ULONG err;
|
||||
|
||||
auto fg = util::fail_guard([&tcp_table]() {
|
||||
free(tcp_table);
|
||||
});
|
||||
|
||||
do {
|
||||
// Query all open TCP sockets to look for our web UI port
|
||||
err = GetTcpTable(tcp_table, &table_size, false);
|
||||
if (err == ERROR_INSUFFICIENT_BUFFER) {
|
||||
free(tcp_table);
|
||||
tcp_table = (PMIB_TCPTABLE) malloc(table_size);
|
||||
}
|
||||
} while (err == ERROR_INSUFFICIENT_BUFFER);
|
||||
|
||||
if (err != NO_ERROR) {
|
||||
BOOST_LOG(error) << "Failed to query TCP table: "sv << err;
|
||||
return false;
|
||||
}
|
||||
|
||||
uint16_t port_nbo = htons(net::map_port(confighttp::PORT_HTTPS));
|
||||
for (DWORD i = 0; i < tcp_table->dwNumEntries; i++) {
|
||||
auto &entry = tcp_table->table[i];
|
||||
|
||||
// Look for our port in the listening state
|
||||
if (entry.dwLocalPort == port_nbo && entry.dwState == MIB_TCP_STATE_LISTEN) {
|
||||
std::cout << std::endl;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Sleep(1000);
|
||||
std::cout << '.';
|
||||
}
|
||||
|
||||
std::cout << "timed out"sv << std::endl;
|
||||
return false;
|
||||
}
|
||||
} // namespace service_ctrl
|
||||
#endif
|
60
src/entry_handler.h
Normal file
60
src/entry_handler.h
Normal file
@ -0,0 +1,60 @@
|
||||
/**
|
||||
* @file entry_handler.h
|
||||
* @brief Header file for entry point functions.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
// standard includes
|
||||
#include <atomic>
|
||||
#include <string_view>
|
||||
|
||||
// local includes
|
||||
#include "thread_pool.h"
|
||||
#include "thread_safe.h"
|
||||
|
||||
// functions
|
||||
void
|
||||
launch_ui();
|
||||
void
|
||||
launch_ui_with_path(std::string path);
|
||||
|
||||
#ifdef _WIN32
|
||||
// windows only functions
|
||||
bool
|
||||
is_gamestream_enabled();
|
||||
#endif
|
||||
|
||||
namespace args {
|
||||
int
|
||||
creds(const char *name, int argc, char *argv[]);
|
||||
int
|
||||
help(const char *name, int argc, char *argv[]);
|
||||
int
|
||||
version(const char *name, int argc, char *argv[]);
|
||||
#ifdef _WIN32
|
||||
int
|
||||
restore_nvprefs_undo(const char *name, int argc, char *argv[]);
|
||||
#endif
|
||||
} // namespace args
|
||||
|
||||
namespace lifetime {
|
||||
extern char **argv;
|
||||
extern std::atomic_int desired_exit_code;
|
||||
void
|
||||
exit_sunshine(int exit_code, bool async);
|
||||
char **
|
||||
get_argv();
|
||||
} // namespace lifetime
|
||||
|
||||
#ifdef _WIN32
|
||||
namespace service_ctrl {
|
||||
bool
|
||||
is_service_running();
|
||||
|
||||
bool
|
||||
start_service();
|
||||
|
||||
bool
|
||||
wait_for_ui_ready();
|
||||
} // namespace service_ctrl
|
||||
#endif
|
27
src/globals.cpp
Normal file
27
src/globals.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
/**
|
||||
* @file globals.cpp
|
||||
* @brief Implementation for globally accessible variables and functions.
|
||||
*/
|
||||
#include "globals.h"
|
||||
|
||||
/**
|
||||
* @brief A process-wide communication mechanism.
|
||||
*/
|
||||
safe::mail_t mail::man;
|
||||
|
||||
/**
|
||||
* @brief A thread pool for processing tasks.
|
||||
*/
|
||||
thread_pool_util::ThreadPool task_pool;
|
||||
|
||||
/**
|
||||
* @brief A boolean flag to indicate whether the cursor should be displayed.
|
||||
*/
|
||||
bool display_cursor = true;
|
||||
|
||||
#ifdef _WIN32
|
||||
/**
|
||||
* @brief A global singleton used for NVIDIA control panel modifications.
|
||||
*/
|
||||
nvprefs::nvprefs_interface nvprefs_instance;
|
||||
#endif
|
42
src/globals.h
Normal file
42
src/globals.h
Normal file
@ -0,0 +1,42 @@
|
||||
/**
|
||||
* @file globals.h
|
||||
* @brief Header for globally accessible variables and functions.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "entry_handler.h"
|
||||
#include "thread_pool.h"
|
||||
|
||||
extern thread_pool_util::ThreadPool task_pool;
|
||||
extern bool display_cursor;
|
||||
|
||||
#ifdef _WIN32
|
||||
// Declare global singleton used for NVIDIA control panel modifications
|
||||
#include "platform/windows/nvprefs/nvprefs_interface.h"
|
||||
extern nvprefs::nvprefs_interface nvprefs_instance;
|
||||
#endif
|
||||
|
||||
namespace mail {
|
||||
#define MAIL(x) \
|
||||
constexpr auto x = std::string_view { \
|
||||
#x \
|
||||
}
|
||||
|
||||
extern safe::mail_t man;
|
||||
|
||||
// Global mail
|
||||
MAIL(shutdown);
|
||||
MAIL(broadcast_shutdown);
|
||||
MAIL(video_packets);
|
||||
MAIL(audio_packets);
|
||||
MAIL(switch_display);
|
||||
|
||||
// Local mail
|
||||
MAIL(touch_port);
|
||||
MAIL(idr);
|
||||
MAIL(invalidate_ref_frames);
|
||||
MAIL(gamepad_feedback);
|
||||
MAIL(hdr);
|
||||
#undef MAIL
|
||||
|
||||
} // namespace mail
|
@ -17,9 +17,9 @@ extern "C" {
|
||||
#include <unordered_map>
|
||||
|
||||
#include "config.h"
|
||||
#include "globals.h"
|
||||
#include "input.h"
|
||||
#include "logging.h"
|
||||
#include "main.h"
|
||||
#include "platform/common.h"
|
||||
#include "thread_pool.h"
|
||||
#include "utility.h"
|
||||
|
353
src/main.cpp
353
src/main.cpp
@ -5,31 +5,22 @@
|
||||
|
||||
// standard includes
|
||||
#include <csignal>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
// lib includes
|
||||
#include <boost/log/attributes/clock.hpp>
|
||||
#include <boost/log/common.hpp>
|
||||
#include <boost/log/expressions.hpp>
|
||||
#include <boost/log/sinks.hpp>
|
||||
#include <boost/log/sources/severity_logger.hpp>
|
||||
|
||||
// local includes
|
||||
#include "config.h"
|
||||
#include "confighttp.h"
|
||||
#include "entry_handler.h"
|
||||
#include "globals.h"
|
||||
#include "httpcommon.h"
|
||||
#include "logging.h"
|
||||
#include "main.h"
|
||||
#include "network.h"
|
||||
#include "nvhttp.h"
|
||||
#include "platform/common.h"
|
||||
#include "process.h"
|
||||
#include "rtsp.h"
|
||||
#include "system_tray.h"
|
||||
#include "thread_pool.h"
|
||||
#include "upnp.h"
|
||||
#include "version.h"
|
||||
#include "video.h"
|
||||
@ -37,26 +28,11 @@
|
||||
extern "C" {
|
||||
#include <libavutil/log.h>
|
||||
#include <rs.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <iphlpapi.h>
|
||||
#endif
|
||||
}
|
||||
|
||||
safe::mail_t mail::man;
|
||||
|
||||
using namespace std::literals;
|
||||
namespace bl = boost::log;
|
||||
|
||||
#ifdef _WIN32
|
||||
// Define global singleton used for NVIDIA control panel modifications
|
||||
nvprefs::nvprefs_interface nvprefs_instance;
|
||||
#endif
|
||||
|
||||
thread_pool_util::ThreadPool task_pool;
|
||||
|
||||
bool display_cursor = true;
|
||||
|
||||
struct NoDelete {
|
||||
void
|
||||
operator()(void *) {}
|
||||
@ -64,309 +40,6 @@ struct NoDelete {
|
||||
|
||||
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", int)
|
||||
|
||||
namespace help {
|
||||
int
|
||||
entry(const char *name, int argc, char *argv[]) {
|
||||
print_help(name);
|
||||
return 0;
|
||||
}
|
||||
} // namespace help
|
||||
|
||||
namespace version {
|
||||
int
|
||||
entry(const char *name, int argc, char *argv[]) {
|
||||
std::cout << PROJECT_NAME << " version: v" << PROJECT_VER << std::endl;
|
||||
return 0;
|
||||
}
|
||||
} // namespace version
|
||||
|
||||
#ifdef _WIN32
|
||||
namespace restore_nvprefs_undo {
|
||||
int
|
||||
entry(const char *name, int argc, char *argv[]) {
|
||||
// Restore global NVIDIA control panel settings to the undo file
|
||||
// left by improper termination of sunshine.exe, if it exists.
|
||||
// This entry point is typically called by the uninstaller.
|
||||
if (nvprefs_instance.load()) {
|
||||
nvprefs_instance.restore_from_and_delete_undo_file_if_exists();
|
||||
nvprefs_instance.unload();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
} // namespace restore_nvprefs_undo
|
||||
#endif
|
||||
|
||||
namespace lifetime {
|
||||
static char **argv;
|
||||
static std::atomic_int desired_exit_code;
|
||||
|
||||
/**
|
||||
* @brief Terminates Sunshine gracefully with the provided exit code.
|
||||
* @param exit_code The exit code to return from main().
|
||||
* @param async Specifies whether our termination will be non-blocking.
|
||||
*/
|
||||
void
|
||||
exit_sunshine(int exit_code, bool async) {
|
||||
// Store the exit code of the first exit_sunshine() call
|
||||
int zero = 0;
|
||||
desired_exit_code.compare_exchange_strong(zero, exit_code);
|
||||
|
||||
// Raise SIGINT to start termination
|
||||
std::raise(SIGINT);
|
||||
|
||||
// Termination will happen asynchronously, but the caller may
|
||||
// have wanted synchronous behavior.
|
||||
while (!async) {
|
||||
std::this_thread::sleep_for(1s);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Gets the argv array passed to main().
|
||||
*/
|
||||
char **
|
||||
get_argv() {
|
||||
return argv;
|
||||
}
|
||||
} // namespace lifetime
|
||||
|
||||
#ifdef _WIN32
|
||||
namespace service_ctrl {
|
||||
class service_controller {
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor for service_controller class.
|
||||
* @param service_desired_access SERVICE_* desired access flags.
|
||||
*/
|
||||
service_controller(DWORD service_desired_access) {
|
||||
scm_handle = OpenSCManagerA(nullptr, nullptr, SC_MANAGER_CONNECT);
|
||||
if (!scm_handle) {
|
||||
auto winerr = GetLastError();
|
||||
BOOST_LOG(error) << "OpenSCManager() failed: "sv << winerr;
|
||||
return;
|
||||
}
|
||||
|
||||
service_handle = OpenServiceA(scm_handle, "SunshineService", service_desired_access);
|
||||
if (!service_handle) {
|
||||
auto winerr = GetLastError();
|
||||
BOOST_LOG(error) << "OpenService() failed: "sv << winerr;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
~service_controller() {
|
||||
if (service_handle) {
|
||||
CloseServiceHandle(service_handle);
|
||||
}
|
||||
|
||||
if (scm_handle) {
|
||||
CloseServiceHandle(scm_handle);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Asynchronously starts the Sunshine service.
|
||||
*/
|
||||
bool
|
||||
start_service() {
|
||||
if (!service_handle) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!StartServiceA(service_handle, 0, nullptr)) {
|
||||
auto winerr = GetLastError();
|
||||
if (winerr != ERROR_SERVICE_ALREADY_RUNNING) {
|
||||
BOOST_LOG(error) << "StartService() failed: "sv << winerr;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Query the service status.
|
||||
* @param status The SERVICE_STATUS struct to populate.
|
||||
*/
|
||||
bool
|
||||
query_service_status(SERVICE_STATUS &status) {
|
||||
if (!service_handle) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!QueryServiceStatus(service_handle, &status)) {
|
||||
auto winerr = GetLastError();
|
||||
BOOST_LOG(error) << "QueryServiceStatus() failed: "sv << winerr;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
SC_HANDLE scm_handle = NULL;
|
||||
SC_HANDLE service_handle = NULL;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Check if the service is running.
|
||||
*
|
||||
* EXAMPLES:
|
||||
* ```cpp
|
||||
* is_service_running();
|
||||
* ```
|
||||
*/
|
||||
bool
|
||||
is_service_running() {
|
||||
service_controller sc { SERVICE_QUERY_STATUS };
|
||||
|
||||
SERVICE_STATUS status;
|
||||
if (!sc.query_service_status(status)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return status.dwCurrentState == SERVICE_RUNNING;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Start the service and wait for startup to complete.
|
||||
*
|
||||
* EXAMPLES:
|
||||
* ```cpp
|
||||
* start_service();
|
||||
* ```
|
||||
*/
|
||||
bool
|
||||
start_service() {
|
||||
service_controller sc { SERVICE_QUERY_STATUS | SERVICE_START };
|
||||
|
||||
std::cout << "Starting Sunshine..."sv;
|
||||
|
||||
// This operation is asynchronous, so we must wait for it to complete
|
||||
if (!sc.start_service()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SERVICE_STATUS status;
|
||||
do {
|
||||
Sleep(1000);
|
||||
std::cout << '.';
|
||||
} while (sc.query_service_status(status) && status.dwCurrentState == SERVICE_START_PENDING);
|
||||
|
||||
if (status.dwCurrentState != SERVICE_RUNNING) {
|
||||
BOOST_LOG(error) << SERVICE_NAME " failed to start: "sv << status.dwWin32ExitCode;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Wait for the UI to be ready after Sunshine startup.
|
||||
*
|
||||
* EXAMPLES:
|
||||
* ```cpp
|
||||
* wait_for_ui_ready();
|
||||
* ```
|
||||
*/
|
||||
bool
|
||||
wait_for_ui_ready() {
|
||||
std::cout << "Waiting for Web UI to be ready...";
|
||||
|
||||
// Wait up to 30 seconds for the web UI to start
|
||||
for (int i = 0; i < 30; i++) {
|
||||
PMIB_TCPTABLE tcp_table = nullptr;
|
||||
ULONG table_size = 0;
|
||||
ULONG err;
|
||||
|
||||
auto fg = util::fail_guard([&tcp_table]() {
|
||||
free(tcp_table);
|
||||
});
|
||||
|
||||
do {
|
||||
// Query all open TCP sockets to look for our web UI port
|
||||
err = GetTcpTable(tcp_table, &table_size, false);
|
||||
if (err == ERROR_INSUFFICIENT_BUFFER) {
|
||||
free(tcp_table);
|
||||
tcp_table = (PMIB_TCPTABLE) malloc(table_size);
|
||||
}
|
||||
} while (err == ERROR_INSUFFICIENT_BUFFER);
|
||||
|
||||
if (err != NO_ERROR) {
|
||||
BOOST_LOG(error) << "Failed to query TCP table: "sv << err;
|
||||
return false;
|
||||
}
|
||||
|
||||
uint16_t port_nbo = htons(net::map_port(confighttp::PORT_HTTPS));
|
||||
for (DWORD i = 0; i < tcp_table->dwNumEntries; i++) {
|
||||
auto &entry = tcp_table->table[i];
|
||||
|
||||
// Look for our port in the listening state
|
||||
if (entry.dwLocalPort == port_nbo && entry.dwState == MIB_TCP_STATE_LISTEN) {
|
||||
std::cout << std::endl;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Sleep(1000);
|
||||
std::cout << '.';
|
||||
}
|
||||
|
||||
std::cout << "timed out"sv << std::endl;
|
||||
return false;
|
||||
}
|
||||
} // namespace service_ctrl
|
||||
|
||||
/**
|
||||
* @brief Checks if NVIDIA's GameStream software is running.
|
||||
* @return `true` if GameStream is enabled.
|
||||
*/
|
||||
bool
|
||||
is_gamestream_enabled() {
|
||||
DWORD enabled;
|
||||
DWORD size = sizeof(enabled);
|
||||
return RegGetValueW(
|
||||
HKEY_LOCAL_MACHINE,
|
||||
L"SOFTWARE\\NVIDIA Corporation\\NvStream",
|
||||
L"EnableStreaming",
|
||||
RRF_RT_REG_DWORD,
|
||||
nullptr,
|
||||
&enabled,
|
||||
&size) == ERROR_SUCCESS &&
|
||||
enabled != 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Launch the Web UI.
|
||||
*
|
||||
* EXAMPLES:
|
||||
* ```cpp
|
||||
* launch_ui();
|
||||
* ```
|
||||
*/
|
||||
void
|
||||
launch_ui() {
|
||||
std::string url = "https://localhost:" + std::to_string(net::map_port(confighttp::PORT_HTTPS));
|
||||
platf::open_url(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Launch the Web UI at a specific endpoint.
|
||||
*
|
||||
* EXAMPLES:
|
||||
* ```cpp
|
||||
* launch_ui_with_path("/pin");
|
||||
* ```
|
||||
*/
|
||||
void
|
||||
launch_ui_with_path(std::string path) {
|
||||
std::string url = "https://localhost:" + std::to_string(net::map_port(confighttp::PORT_HTTPS)) + path;
|
||||
platf::open_url(url);
|
||||
}
|
||||
|
||||
std::map<int, std::function<void()>> signal_handlers;
|
||||
void
|
||||
on_signal_forwarder(int sig) {
|
||||
@ -381,26 +54,12 @@ on_signal(int sig, FN &&fn) {
|
||||
std::signal(sig, on_signal_forwarder);
|
||||
}
|
||||
|
||||
namespace gen_creds {
|
||||
int
|
||||
entry(const char *name, int argc, char *argv[]) {
|
||||
if (argc < 2 || argv[0] == "help"sv || argv[1] == "help"sv) {
|
||||
print_help(name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
http::save_user_creds(config::sunshine.credentials_file, argv[0], argv[1]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
} // namespace gen_creds
|
||||
|
||||
std::map<std::string_view, std::function<int(const char *name, int argc, char **argv)>> cmd_to_func {
|
||||
{ "creds"sv, gen_creds::entry },
|
||||
{ "help"sv, help::entry },
|
||||
{ "version"sv, version::entry },
|
||||
{ "creds"sv, args::creds },
|
||||
{ "help"sv, args::help },
|
||||
{ "version"sv, args::version },
|
||||
#ifdef _WIN32
|
||||
{ "restore-nvprefs-undo"sv, restore_nvprefs_undo::entry },
|
||||
{ "restore-nvprefs-undo"sv, args::restore_nvprefs_undo },
|
||||
#endif
|
||||
};
|
||||
|
||||
|
67
src/main.h
67
src/main.h
@ -6,73 +6,6 @@
|
||||
// macros
|
||||
#pragma once
|
||||
|
||||
// standard includes
|
||||
#include <filesystem>
|
||||
#include <string_view>
|
||||
|
||||
// local includes
|
||||
#include "thread_pool.h"
|
||||
#include "thread_safe.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
// Declare global singleton used for NVIDIA control panel modifications
|
||||
#include "platform/windows/nvprefs/nvprefs_interface.h"
|
||||
extern nvprefs::nvprefs_interface nvprefs_instance;
|
||||
#endif
|
||||
|
||||
extern thread_pool_util::ThreadPool task_pool;
|
||||
extern bool display_cursor;
|
||||
|
||||
// functions
|
||||
int
|
||||
main(int argc, char *argv[]);
|
||||
void
|
||||
launch_ui();
|
||||
void
|
||||
launch_ui_with_path(std::string path);
|
||||
|
||||
// namespaces
|
||||
namespace mail {
|
||||
#define MAIL(x) \
|
||||
constexpr auto x = std::string_view { \
|
||||
#x \
|
||||
}
|
||||
|
||||
extern safe::mail_t man;
|
||||
|
||||
// Global mail
|
||||
MAIL(shutdown);
|
||||
MAIL(broadcast_shutdown);
|
||||
MAIL(video_packets);
|
||||
MAIL(audio_packets);
|
||||
MAIL(switch_display);
|
||||
|
||||
// Local mail
|
||||
MAIL(touch_port);
|
||||
MAIL(idr);
|
||||
MAIL(invalidate_ref_frames);
|
||||
MAIL(gamepad_feedback);
|
||||
MAIL(hdr);
|
||||
#undef MAIL
|
||||
|
||||
} // namespace mail
|
||||
|
||||
namespace lifetime {
|
||||
void
|
||||
exit_sunshine(int exit_code, bool async);
|
||||
char **
|
||||
get_argv();
|
||||
} // namespace lifetime
|
||||
|
||||
#ifdef _WIN32
|
||||
namespace service_ctrl {
|
||||
bool
|
||||
is_service_running();
|
||||
|
||||
bool
|
||||
start_service();
|
||||
|
||||
bool
|
||||
wait_for_ui_ready();
|
||||
} // namespace service_ctrl
|
||||
#endif
|
||||
|
@ -23,9 +23,9 @@
|
||||
#include "config.h"
|
||||
#include "crypto.h"
|
||||
#include "file_handler.h"
|
||||
#include "globals.h"
|
||||
#include "httpcommon.h"
|
||||
#include "logging.h"
|
||||
#include "main.h"
|
||||
#include "network.h"
|
||||
#include "nvhttp.h"
|
||||
#include "platform/common.h"
|
||||
|
@ -4,6 +4,7 @@
|
||||
*/
|
||||
#include <bitset>
|
||||
#include <sstream>
|
||||
#include <thread>
|
||||
|
||||
#include <boost/regex.hpp>
|
||||
|
||||
@ -15,7 +16,6 @@
|
||||
|
||||
#include "src/config.h"
|
||||
#include "src/logging.h"
|
||||
#include "src/main.h"
|
||||
#include "src/thread_safe.h"
|
||||
|
||||
namespace platf {
|
||||
|
@ -3,10 +3,9 @@
|
||||
* @brief todo
|
||||
*/
|
||||
#include <bitset>
|
||||
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <filesystem>
|
||||
#include <thread>
|
||||
|
||||
#include <NvFBC.h>
|
||||
#include <ffnvcodec/dynlink_loader.h>
|
||||
@ -20,7 +19,6 @@ extern "C" {
|
||||
#include "cuda.h"
|
||||
#include "graphics.h"
|
||||
#include "src/logging.h"
|
||||
#include "src/main.h"
|
||||
#include "src/utility.h"
|
||||
#include "src/video.h"
|
||||
#include "wayland.h"
|
||||
|
@ -20,11 +20,11 @@
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <thread>
|
||||
|
||||
#include "src/config.h"
|
||||
#include "src/input.h"
|
||||
#include "src/logging.h"
|
||||
#include "src/main.h"
|
||||
#include "src/platform/common.h"
|
||||
#include "src/utility.h"
|
||||
|
||||
|
@ -13,9 +13,9 @@
|
||||
#include <xf86drmMode.h>
|
||||
|
||||
#include <filesystem>
|
||||
#include <thread>
|
||||
|
||||
#include "src/logging.h"
|
||||
#include "src/main.h"
|
||||
#include "src/platform/common.h"
|
||||
#include "src/round_robin.h"
|
||||
#include "src/utility.h"
|
||||
|
@ -26,8 +26,8 @@
|
||||
#include "graphics.h"
|
||||
#include "misc.h"
|
||||
#include "src/config.h"
|
||||
#include "src/entry_handler.h"
|
||||
#include "src/logging.h"
|
||||
#include "src/main.h"
|
||||
#include "src/platform/common.h"
|
||||
#include "vaapi.h"
|
||||
|
||||
|
@ -2,10 +2,11 @@
|
||||
* @file src/platform/linux/wlgrab.cpp
|
||||
* @brief todo
|
||||
*/
|
||||
#include <thread>
|
||||
|
||||
#include "src/platform/common.h"
|
||||
|
||||
#include "src/logging.h"
|
||||
#include "src/main.h"
|
||||
#include "src/video.h"
|
||||
|
||||
#include "cuda.h"
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include "src/platform/common.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <thread>
|
||||
|
||||
#include <X11/X.h>
|
||||
#include <X11/Xlib.h>
|
||||
@ -17,8 +18,8 @@
|
||||
#include <xcb/xfixes.h>
|
||||
|
||||
#include "src/config.h"
|
||||
#include "src/globals.h"
|
||||
#include "src/logging.h"
|
||||
#include "src/main.h"
|
||||
#include "src/task_pool.h"
|
||||
#include "src/video.h"
|
||||
|
||||
|
@ -18,8 +18,8 @@
|
||||
#include <pwd.h>
|
||||
|
||||
#include "misc.h"
|
||||
#include "src/entry_handler.h"
|
||||
#include "src/logging.h"
|
||||
#include "src/main.h"
|
||||
#include "src/platform/common.h"
|
||||
|
||||
#include <boost/asio/ip/address.hpp>
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include <cmath>
|
||||
#include <codecvt>
|
||||
#include <initguid.h>
|
||||
#include <thread>
|
||||
|
||||
#include <boost/process.hpp>
|
||||
|
||||
@ -16,7 +17,6 @@ typedef long NTSTATUS;
|
||||
#include "misc.h"
|
||||
#include "src/config.h"
|
||||
#include "src/logging.h"
|
||||
#include "src/main.h"
|
||||
#include "src/platform/common.h"
|
||||
#include "src/stat_trackers.h"
|
||||
#include "src/video.h"
|
||||
|
@ -6,14 +6,15 @@
|
||||
#include <windows.h>
|
||||
|
||||
#include <cmath>
|
||||
#include <thread>
|
||||
|
||||
#include <ViGEm/Client.h>
|
||||
|
||||
#include "keylayout.h"
|
||||
#include "misc.h"
|
||||
#include "src/config.h"
|
||||
#include "src/globals.h"
|
||||
#include "src/logging.h"
|
||||
#include "src/main.h"
|
||||
#include "src/platform/common.h"
|
||||
|
||||
#ifdef __MINGW32__
|
||||
|
@ -35,8 +35,9 @@
|
||||
#define NTDDI_VERSION NTDDI_WIN10
|
||||
#include <Shlwapi.h>
|
||||
|
||||
#include "src/entry_handler.h"
|
||||
#include "src/globals.h"
|
||||
#include "src/logging.h"
|
||||
#include "src/main.h"
|
||||
#include "src/platform/common.h"
|
||||
#include "src/utility.h"
|
||||
#include <iterator>
|
||||
|
@ -14,7 +14,6 @@
|
||||
#include "misc.h"
|
||||
#include "src/config.h"
|
||||
#include "src/logging.h"
|
||||
#include "src/main.h"
|
||||
#include "src/network.h"
|
||||
#include "src/nvhttp.h"
|
||||
#include "src/platform/common.h"
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include <boost/algorithm/string.hpp>
|
||||
@ -23,7 +24,6 @@
|
||||
#include "config.h"
|
||||
#include "crypto.h"
|
||||
#include "logging.h"
|
||||
#include "main.h"
|
||||
#include "platform/common.h"
|
||||
#include "system_tray.h"
|
||||
#include "utility.h"
|
||||
|
@ -16,9 +16,9 @@ extern "C" {
|
||||
#include <boost/bind.hpp>
|
||||
|
||||
#include "config.h"
|
||||
#include "globals.h"
|
||||
#include "input.h"
|
||||
#include "logging.h"
|
||||
#include "main.h"
|
||||
#include "network.h"
|
||||
#include "rtsp.h"
|
||||
#include "stream.h"
|
||||
|
@ -18,9 +18,9 @@ extern "C" {
|
||||
}
|
||||
|
||||
#include "config.h"
|
||||
#include "globals.h"
|
||||
#include "input.h"
|
||||
#include "logging.h"
|
||||
#include "main.h"
|
||||
#include "network.h"
|
||||
#include "stat_trackers.h"
|
||||
#include "stream.h"
|
||||
|
@ -38,9 +38,9 @@
|
||||
// local includes
|
||||
#include "confighttp.h"
|
||||
#include "logging.h"
|
||||
#include "main.h"
|
||||
#include "platform/common.h"
|
||||
#include "process.h"
|
||||
#include "src/entry_handler.h"
|
||||
#include "version.h"
|
||||
|
||||
using namespace std::literals;
|
||||
|
@ -7,8 +7,8 @@
|
||||
|
||||
#include "config.h"
|
||||
#include "confighttp.h"
|
||||
#include "globals.h"
|
||||
#include "logging.h"
|
||||
#include "main.h"
|
||||
#include "network.h"
|
||||
#include "nvhttp.h"
|
||||
#include "rtsp.h"
|
||||
|
@ -19,9 +19,9 @@ extern "C" {
|
||||
|
||||
#include "cbs.h"
|
||||
#include "config.h"
|
||||
#include "globals.h"
|
||||
#include "input.h"
|
||||
#include "logging.h"
|
||||
#include "main.h"
|
||||
#include "nvenc/nvenc_base.h"
|
||||
#include "platform/common.h"
|
||||
#include "sync.h"
|
||||
|
Loading…
x
Reference in New Issue
Block a user