Skip to content

Commit

Permalink
supports host name in addition to IP address
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotaku committed Oct 2, 2024
1 parent 2d97fc6 commit 34164ca
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 113 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ target_link_libraries(moonlight-lib PUBLIC commons-ss4s-modules-list commons-sps

target_link_libraries(moonlight-lib PUBLIC commons-logging commons-gamecontrollerdb-updater commons-lazy
commons-refcounter commons-executor commons-linked-list commons-wol commons-ini-writer commons-copyfile
commons-sockaddr)
commons-sockaddr commons-host)

configure_file(src/app/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h @ONLY)
target_include_directories(moonlight-lib PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
Expand Down
6 changes: 3 additions & 3 deletions src/app/backend/pcmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "libgamestream/client.h"
#include "uuidstr.h"
#include "sockaddr.h"
#include "host.h"

typedef struct app_t app_t;
typedef struct pcmanager_t pcmanager_t;
Expand Down Expand Up @@ -77,7 +78,7 @@ void pcmanager_unregister_listener(pcmanager_t *manager, const pcmanager_listene
* @param userdata
* @return
*/
bool pcmanager_manual_add(pcmanager_t *manager, sockaddr_t *address, pcmanager_callback_t callback, void *userdata);
bool pcmanager_manual_add(pcmanager_t *manager, host_t *host, pcmanager_callback_t callback, void *userdata);

/**
* @brief Generates a PIN code, and start pairing process.
Expand Down Expand Up @@ -133,6 +134,5 @@ bool pcmanager_send_wol(pcmanager_t *manager, const uuidstr_t *uuid, pcmanager_c
* @param userdata
* @return
*/
int pcmanager_update_by_ip(worker_context_t *context, const char *ip, uint16_t port, bool force);
int pcmanager_update_by_host(worker_context_t *context, const char *ip, uint16_t port, bool force);

int pcmanager_update_by_addr(worker_context_t *context, sockaddr_t *addr, bool force);
17 changes: 7 additions & 10 deletions src/app/backend/pcmanager/known_hosts.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,20 @@ void pcmanager_save_known_hosts(pcmanager_t *manager) {
if (!cur->server || !cur->known) {
continue;
}
char address_buf[64] = {0};
const SERVER_DATA *server = cur->server;
host_t *address = host_new(server->serverInfo.address, server->extPort);
if (address == NULL) {
continue;
}
ini_write_section(fp, server->uuid);

ini_write_string(fp, "mac", server->mac);
ini_write_string(fp, "hostname", server->hostname);

struct sockaddr *address = sockaddr_new();
if (sockaddr_set_ip_str(address, strchr(server->serverInfo.address, ':') ? AF_INET6 : AF_INET,
server->serverInfo.address) != 0) {
free(address);
continue;
}
sockaddr_set_port(address, server->extPort);
sockaddr_to_string(address, address_buf, sizeof(address_buf));
char address_buf[260] = {0};
host_to_string(address, address_buf, sizeof(address_buf));
ini_write_string(fp, "address", address_buf);
free(address);
host_free(address);

if (!selected_set && cur->selected) {
ini_write_bool(fp, "selected", true);
Expand Down
8 changes: 4 additions & 4 deletions src/app/backend/pcmanager/pairing.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ bool pcmanager_pair(pcmanager_t *manager, const uuidstr_t *uuid, char *pin, pcma
return true;
}

bool pcmanager_manual_add(pcmanager_t *manager, sockaddr_t *address, pcmanager_callback_t callback, void *userdata) {
if (address == NULL) {
bool pcmanager_manual_add(pcmanager_t *manager, host_t *host, pcmanager_callback_t callback, void *userdata) {
if (host == NULL) {
return false;
}
worker_context_t *ctx = worker_context_new(manager, NULL, callback, userdata);
ctx->arg1 = address;
pcmanager_worker_queue(manager, worker_add_by_ip, ctx);
ctx->arg1 = host;
pcmanager_worker_queue(manager, worker_add_by_host, ctx);
return true;
}

Expand Down
19 changes: 8 additions & 11 deletions src/app/backend/pcmanager/worker/manual_add.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
#include <assert.h>
#include "worker.h"
#include "backend/pcmanager/priv.h"
#include "errors.h"
#include "sockaddr.h"

static int updated_by_addr(worker_context_t *context, bool force);
static int updated_by_host(worker_context_t *context, bool force);

int worker_add_by_ip(worker_context_t *context) {
return updated_by_addr(context, true);
int worker_add_by_host(worker_context_t *context) {
assert(context->arg1 != NULL);
return updated_by_host(context, true);
}

int worker_host_discovered(worker_context_t *context) {
return updated_by_addr(context, false);
}

int updated_by_addr(worker_context_t *context, bool force) {
return pcmanager_update_by_addr(context, context->arg1, force);
int updated_by_host(worker_context_t *context, bool force) {
const host_t *host = context->arg1;
return pcmanager_update_by_host(context, host_get_hostname(host), host_get_port(host), force);
}
85 changes: 16 additions & 69 deletions src/app/backend/pcmanager/worker/update.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "worker.h"
#include "backend/pcmanager/priv.h"
#include "backend/pcmanager/pclist.h"
#include "backend/pcmanager/listeners.h"

#include <assert.h>

Expand All @@ -11,91 +10,39 @@
#include "logging.h"
#include "ui/fatal_error.h"

static void notify_querying(pclist_update_context_t *args);

int worker_host_update(worker_context_t *context) {
const pclist_t *node = pcmanager_node(context->manager, &context->uuid);
if (node == NULL) {
return GS_FAILED;
}
return pcmanager_update_by_ip(context, node->server->serverInfo.address, node->server->extPort, true);
}

int pcmanager_update_by_ip(worker_context_t *context, const char *ip, uint16_t port, bool force) {
return 0;
return pcmanager_update_by_host(context, node->server->serverInfo.address, node->server->extPort, true);
}

int pcmanager_update_by_addr(worker_context_t *context, sockaddr_t *addr, bool force) {
int pcmanager_update_by_host(worker_context_t *context, const char *ip, uint16_t port, bool force) {
assert(context != NULL);
assert(context->manager != NULL);
assert(addr != NULL);
assert(ip != NULL);
int ret = 0;

pcmanager_t *manager = context->manager;
// FIXME: Implement this
char *ip_dup = strdup("");
pclist_t *existing = pclist_find_by_ip(manager, ip_dup);
if (existing) {
SERVER_STATE_ENUM state = existing->state.code;
if (state == SERVER_STATE_QUERYING) {
commons_log_verbose("PCManager", "Skip upsert for querying node. ip: %s", ip_dup);
goto done;
}
if (!force && state & SERVER_STATE_ONLINE) {
goto done;
}
pcmanager_lock(manager);
pclist_update_context_t args = {
.manager = manager,
.uuid = existing->id,
.state = {.code = SERVER_STATE_QUERYING},
};
existing->state = args.state;
pcmanager_unlock(manager);
app_bus_post_sync(context->app, (bus_actionfunc) notify_querying, &args);
}
GS_CLIENT client = app_gs_client_new(context->app);
PSERVER_DATA server = serverdata_new();
ret = gs_get_status(client, server, ip_dup, sockaddr_get_port(addr), app_configuration->unsupported);
ip_dup = NULL;
gs_destroy(client);
if (existing) {
pcmanager_lock(manager);
bool should_remove = ret == GS_OK && !uuidstr_t_equals_s(&existing->id, server->uuid);
pcmanager_unlock(manager);
if (should_remove) {
pclist_remove(manager, &existing->id);
existing = NULL;
} else {
pcmanager_lock(manager);
existing->state.code = SERVER_STATE_NONE;
pcmanager_unlock(manager);
}
}

// Fetch server info
GS_CLIENT client = app_gs_client_new(manager->app);
SERVER_DATA *server = serverdata_new();
ret = gs_get_status(client, server, strdup(ip), port, app_configuration->unsupported);
if (ret == GS_OK) {
commons_log_info("PCManager", "Finished updating status from %s", server->serverInfo.address);
uuidstr_t uuid;
uuidstr_fromstr(&uuid, server->uuid);
SERVER_STATE state = {.code = server->paired ? SERVER_STATE_AVAILABLE : SERVER_STATE_NOT_PAIRED};
pclist_upsert(manager, &uuid, &state, server);
pclist_upsert(manager, (const uuidstr_t *) server->uuid, &state, server);
} else {
const char *gs_error = NULL;
ret = gs_get_error(&gs_error);
commons_log_warn("PCManager", "Error %d while updating status from %s: %s", ret, server->serverInfo.address,
gs_error);
context->error = gs_error != NULL ? strdup(gs_error) : NULL;
if (existing && ret == GS_IO_ERROR) {
SERVER_STATE state = {.code = SERVER_STATE_OFFLINE};
pclist_upsert(manager, &existing->id, &state, NULL);
const char *error = NULL;
gs_get_error(&error);
if (error) {
context->error = strdup(error);
}
serverdata_free(server);
}
done:
if (ip_dup) {
free(ip_dup);
}
gs_destroy(client);

return ret;
}

static void notify_querying(pclist_update_context_t *args) {
pcmanager_listeners_notify(args->manager, &args->uuid, PCMANAGER_NOTIFY_UPDATED);
}
4 changes: 1 addition & 3 deletions src/app/backend/pcmanager/worker/worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ int worker_quit_app(worker_context_t *context);

int worker_wol(worker_context_t *context);

int worker_add_by_ip(worker_context_t *context);

int worker_host_discovered(worker_context_t *context);
int worker_add_by_host(worker_context_t *context);

int worker_host_update(worker_context_t *context);

Expand Down
2 changes: 1 addition & 1 deletion src/app/stream/session_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ int session_worker(session_t *session) {
.app = app,
.manager = pcmanager,
};
pcmanager_update_by_ip(&update_ctx, server->serverInfo.address, server->extPort, true);
pcmanager_update_by_host(&update_ctx, server->serverInfo.address, server->extPort, true);

// Don't always reset status as error state should be kept
session_set_state(session, STREAMING_NONE);
Expand Down
2 changes: 1 addition & 1 deletion src/app/stream/session_worker_embedded.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ int session_worker_embedded(session_t *session) {
.app = app,
.manager = pcmanager,
};
pcmanager_update_by_ip(&update_ctx, server->serverInfo.address, server->extPort, true);
pcmanager_update_by_host(&update_ctx, server->serverInfo.address, server->extPort, true);

// Don't always reset status as error state should be kept
session_set_state(session, STREAMING_NONE);
Expand Down
11 changes: 6 additions & 5 deletions src/app/ui/launcher/add.dialog.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,26 @@ static void dialog_cb(lv_event_t *event) {
}
uint16_t btn = lv_msgbox_get_active_btn(dialog);
if (btn == 1) {
sockaddr_t *address = sockaddr_parse(lv_textarea_get_text(controller->input));
if (!address) {
host_t *host = host_parse(lv_textarea_get_text(controller->input));
if (!host) {
return;
}
lv_obj_add_state(controller->btns, LV_STATE_DISABLED);
lv_obj_add_state(controller->input, LV_STATE_DISABLED);
lv_obj_clear_flag(controller->progress, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(controller->error, LV_OBJ_FLAG_HIDDEN);
pcmanager_manual_add(pcmanager, address, add_cb, controller);
pcmanager_manual_add(pcmanager, host, add_cb, controller);
} else {
lv_msgbox_close_async(dialog);
}
}

static void input_changed_cb(lv_event_t *event) {
add_dialog_controller_t *controller = lv_event_get_user_data(event);
sockaddr_t *address = sockaddr_parse(lv_textarea_get_text(controller->input));
if (address) {
host_t *host = host_parse(lv_textarea_get_text(controller->input));
if (host) {
lv_btnmatrix_clear_btn_ctrl(controller->btns, 1, LV_BTNMATRIX_CTRL_DISABLED);
host_free(host);
} else {
lv_btnmatrix_set_btn_ctrl(controller->btns, 1, LV_BTNMATRIX_CTRL_DISABLED);
}
Expand Down
8 changes: 4 additions & 4 deletions src/app/ui/launcher/apps.controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ static void on_host_updated(const uuidstr_t *uuid, void *userdata) {
if (controller != current_instance) { return; }
if (!uuidstr_t_equals_t(&controller->uuid, uuid)) { return; }
const SERVER_STATE *state = pcmanager_state(pcmanager, uuid);
SDL_assert_release(state != NULL);
assert(state != NULL);
if (state->code == SERVER_STATE_AVAILABLE) {
apploader_load(controller->apploader);
}
Expand All @@ -346,7 +346,7 @@ static void send_wol_cb(int result, const char *error, const uuidstr_t *uuid, vo
if (!controller->base.managed->obj_created) { return; }
lv_btnmatrix_clear_btn_ctrl_all(controller->actions, LV_BTNMATRIX_CTRL_DISABLED);
const SERVER_STATE *state = pcmanager_state(pcmanager, &controller->uuid);
SDL_assert_release(state != NULL);
assert(state != NULL);
if (state->code & SERVER_STATE_ONLINE || result != GS_OK) { return; }
pcmanager_request_update(pcmanager, &controller->uuid, host_info_cb, controller);
}
Expand All @@ -360,7 +360,7 @@ static void update_view_state(apps_fragment_t *controller) {
lv_obj_t *appload = controller->appload;
lv_obj_t *apperror = controller->apperror;
const SERVER_STATE *state = pcmanager_state(pcmanager, &controller->uuid);
SDL_assert_release(state != NULL);
assert(state != NULL);
switch (state->code) {
case SERVER_STATE_NONE:
case SERVER_STATE_QUERYING: {
Expand Down Expand Up @@ -527,7 +527,7 @@ static void appitem_bind(apps_fragment_t *controller, lv_obj_t *item, apploader_
controller->col_width, controller->col_height);
lv_label_set_text(holder->title, app->base.name);

SDL_assert_release(controller->node);
assert(controller->node);
int current_id = pcmanager_node_current_app(controller->node);
if (current_id == app->base.id) {
lv_obj_clear_flag(holder->play_indicator, LV_OBJ_FLAG_HIDDEN);
Expand Down
2 changes: 1 addition & 1 deletion third_party/commons

0 comments on commit 34164ca

Please sign in to comment.