Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
scripts
.vscode

/private
/private
.megamemory/*
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ pub const CONFIG_FILE_NAME: &str = "pocket-relay-client.json";
pub struct ClientConfig {
/// The saved connection URL to use
pub connection_url: String,
/// Whether to automatically connect to the saved URL on startup
#[serde(default)]
pub auto_connect: bool,
}

/// Provides a [`PathBuf`] to the configuration file
Expand Down
Binary file added src/resources/dot_green.ico
Binary file not shown.
Binary file added src/resources/dot_red.ico
Binary file not shown.
64 changes: 59 additions & 5 deletions src/servers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,52 @@ use crate::{
ui::error_message,
};
use log::error;
use std::{future::Future, sync::Arc};
use native_windows_gui::NoticeSender;
use std::{
future::Future,
sync::{
atomic::{AtomicU8, Ordering},
Arc,
},
};

/// State of the tunnel server task
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TunnelState {
/// Server doesn't offer tunneling (or no servers are running)
Off,
/// Tunnel task is running
Active,
/// Tunnel task has stopped (gave up reconnecting)
Failed,
}

static TUNNEL_STATE: AtomicU8 = AtomicU8::new(0);

fn set_tunnel_state(state: TunnelState) {
TUNNEL_STATE.store(state as u8, Ordering::SeqCst);
}

/// Gets the current state of the tunnel server task
pub fn tunnel_state() -> TunnelState {
match TUNNEL_STATE.load(Ordering::SeqCst) {
1 => TunnelState::Active,
2 => TunnelState::Failed,
_ => TunnelState::Off,
}
}

/// Resets the tunnel state (used when disconnecting)
pub fn reset_tunnel_state() {
set_tunnel_state(TunnelState::Off);
}

/// Starts all the servers in their own tasks
///
/// ## Arguments
/// * `ctx` - The client context
pub fn start_all_servers(ctx: Arc<ClientContext>) {
/// * `ctx` - The client context
/// * `tunnel_notice` - Sender used to wake the UI when the tunnel state changes
pub fn start_all_servers(ctx: Arc<ClientContext>, tunnel_notice: NoticeSender) {
// Stop existing servers and tasks if they are running
stop_server_tasks();

Expand All @@ -30,8 +69,23 @@ pub fn start_all_servers(ctx: Arc<ClientContext>) {
run_server(qos, "qos");

// Spawn tunnel server
let tunnel = start_tunnel_server(ctx.clone());
run_server(tunnel, "tunnel");
// The tunnel is only used when the server provides a UDP tunnel port or
// an association token (required by the HTTP tunnel)
if ctx.tunnel_port.is_some() || ctx.association.is_some() {
set_tunnel_state(TunnelState::Active);
let tunnel = start_tunnel_server(ctx.clone());
spawn_server_task(async move {
let result = tunnel.await;
set_tunnel_state(TunnelState::Failed);
tunnel_notice.notice();
if let Err(err) = result {
error_message("Failed to start tunnel server", &err.to_string());
error!("Failed to start tunnel server: {err}");
}
});
} else {
set_tunnel_state(TunnelState::Off);
}

// Spawn telemetry server
let telemetry = telemetry::start_telemetry_server(ctx);
Expand Down
116 changes: 95 additions & 21 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
reqwest::Client,
servers::{has_server_tasks, stop_server_tasks},
},
servers::start_all_servers,
servers::{reset_tunnel_state, start_all_servers, tunnel_state, TunnelState},
threads::resume_all_threads,
update,
};
Expand All @@ -17,11 +17,15 @@ use std::{cell::RefCell, sync::Arc};
use tokio::task::JoinHandle;

/// Size of the created window
pub const WINDOW_SIZE: (i32, i32) = (500, 225);
pub const WINDOW_SIZE: (i32, i32) = (500, 260);
/// Title used for the created window
pub const WINDOW_TITLE: &str = concat!("Pocket Relay Client v", env!("CARGO_PKG_VERSION"));
/// Window icon bytes
pub const ICON_BYTES: &[u8] = include_bytes!("resources/icon.ico");
/// Green status dot icon bytes
pub const DOT_GREEN_BYTES: &[u8] = include_bytes!("resources/dot_green.ico");
/// Red status dot icon bytes
pub const DOT_RED_BYTES: &[u8] = include_bytes!("resources/dot_red.ico");

/// Native GUI app
#[derive(NwgUi, Default)]
Expand All @@ -41,41 +45,76 @@ pub struct App {
#[nwg_events(OnWindowClose: [stop_thread_dispatch()], OnKeyEnter: [App::handle_set])]
window: Window,

/// Green status dot icon
#[nwg_resource(source_bin: Some(DOT_GREEN_BYTES))]
dot_green: Icon,

/// Red status dot icon
#[nwg_resource(source_bin: Some(DOT_RED_BYTES))]
dot_red: Icon,

/// Grid layout for all the content
#[nwg_layout(parent: window)]
#[nwg_layout(parent: window, max_column: Some(24))]
grid: GridLayout,

/// Label for the connection URL input
#[nwg_control(text: "Please put the server Connection URL below and press 'Set'")]
#[nwg_layout_item(layout: grid, col: 0, row: 0, col_span: 2)]
#[nwg_layout_item(layout: grid, col: 0, row: 0, col_span: 24)]
target_url_label: Label,

/// Input for the connection URL
#[nwg_control(focus: true)]
#[nwg_layout_item(layout: grid, col: 0, row: 1, col_span: 2)]
#[nwg_layout_item(layout: grid, col: 0, row: 1, col_span: 16)]
target_url_input: TextInput,

/// Button for connecting
#[nwg_control(text: "Connect")]
#[nwg_layout_item(layout: grid, col: 2, row: 1, col_span: 1)]
#[nwg_layout_item(layout: grid, col: 16, row: 1, col_span: 8)]
#[nwg_events(OnButtonClick: [App::handle_set])]
set_button: Button,

/// Checkbox for whether to remember the connection URL
#[nwg_control(text: "Save connection URL")]
#[nwg_layout_item(layout: grid, col: 0, row: 2, col_span: 3)]
#[nwg_layout_item(layout: grid, col: 0, row: 2, col_span: 12)]
remember_checkbox: CheckBox,

/// Checkbox for whether to automatically connect on startup
#[nwg_control(text: "Auto connect on startup")]
#[nwg_layout_item(layout: grid, col: 12, row: 2, col_span: 12)]
auto_connect_checkbox: CheckBox,

/// Server status dot
#[nwg_control(icon: Some(&data.dot_red))]
#[nwg_layout_item(layout: grid, col: 0, row: 3, col_span: 2)]
server_dot: ImageFrame,

/// Connection state label
#[nwg_control(text: "Not connected")]
#[nwg_layout_item(layout: grid, col: 0, row: 3, col_span: 3)]
#[nwg_control(text: "Server: Not connected")]
#[nwg_layout_item(layout: grid, col: 2, row: 3, col_span: 22)]
connection_label: Label,

/// Tunnel status dot
#[nwg_control(icon: Some(&data.dot_red))]
#[nwg_layout_item(layout: grid, col: 0, row: 4, col_span: 2)]
tunnel_dot: ImageFrame,

/// Tunnel state label (empty when the server has no tunneling)
#[nwg_control(text: "Tunnel: Not available")]
#[nwg_layout_item(layout: grid, col: 2, row: 4, col_span: 22)]
tunnel_label: Label,

/// Label about connecting
#[nwg_control(text: "Your game will start after you connect. If you don't want to connect to\n a Pocket Relay server close this window and you will connect to the\n official servers")]
#[nwg_layout_item(layout: grid, col: 0, row: 4, col_span: 3, row_span: 3)]
#[nwg_control(text: "Your game will start after you connect. If you don't want to connect to
a Pocket Relay server close this window and you will connect to the
official servers")]
#[nwg_layout_item(layout: grid, col: 0, row: 5, col_span: 24, row_span: 3)]
connect_label: Label,

/// Notice for tunnel state changes
#[nwg_control]
#[nwg_events(OnNotice: [App::handle_tunnel_notice])]
tunnel_notice: Notice,

/// Notice for connection completion
#[nwg_control]
#[nwg_events(OnNotice: [App::handle_connect_notice])]
Expand All @@ -101,12 +140,15 @@ impl App {
// Handle disconnecting
if has_server_tasks() {
stop_server_tasks();
self.connection_label.set_text("Not connected");
reset_tunnel_state();
self.connection_label.set_text("Server: Not connected");
self.server_dot.set_icon(Some(&self.dot_red));
self.update_tunnel_status();
self.set_button.set_text("Connect");
return;
}

self.connection_label.set_text("Connecting...");
self.connection_label.set_text("Server: Connecting...");
let target = self.target_url_input.text().to_string();
let sender = self.connect_notice.sender();
let http_client = self.http_client.clone();
Expand All @@ -120,6 +162,22 @@ impl App {
*self.connect_task.borrow_mut() = Some(task);
}

/// Handles the tunnel task changing state
fn handle_tunnel_notice(&self) {
self.update_tunnel_status();
}

/// Updates the tunnel dot and label from the current tunnel state
fn update_tunnel_status(&self) {
let (icon, text) = match tunnel_state() {
TunnelState::Active => (&self.dot_green, "Tunnel: Connected"),
TunnelState::Failed => (&self.dot_red, "Tunnel: Disconnected"),
TunnelState::Off => (&self.dot_red, "Tunnel: Not available"),
};
self.tunnel_dot.set_icon(Some(icon));
self.tunnel_label.set_text(text);
}

/// Handles the connection complete notice updating the UI
/// with the new connection state from the task result
fn handle_connect_notice(&self) {
Expand All @@ -138,7 +196,7 @@ impl App {
let mut lookup = match result {
Ok(value) => value,
Err(err) => {
self.connection_label.set_text("Failed to connect");
self.connection_label.set_text("Server: Failed to connect");
error_message("Failed to connect", &err.to_string());
return;
}
Expand All @@ -152,23 +210,29 @@ impl App {
});

// Start the servers
start_all_servers(ctx);
start_all_servers(ctx, self.tunnel_notice.sender());

let remember = self.remember_checkbox.check_state() == CheckBoxState::Checked;
let auto_connect = self.auto_connect_checkbox.check_state() == CheckBoxState::Checked;

// Save the connection URL
if remember {
// Save the connection URL (auto connect requires a saved URL)
if remember || auto_connect {
let connection_url = lookup.url.to_string();
write_config_file(ClientConfig { connection_url });
write_config_file(ClientConfig {
connection_url,
auto_connect,
});
}

let text = format!(
"Connected: {} {} version v{}",
"Server: Connected - {} {} version v{}",
lookup.url.scheme(),
lookup.url.authority(),
lookup.version
);
self.connection_label.set_text(&text);
self.server_dot.set_icon(Some(&self.dot_green));
self.update_tunnel_status();
self.set_button.set_text("Disconnect");

// Resume game threads
Expand Down Expand Up @@ -207,8 +271,8 @@ pub fn init(config: Option<ClientConfig>, client: Client) {
})
.expect("Failed to build native UI");

let (target, remember) = config
.map(|value| (value.connection_url, true))
let (target, remember, auto_connect) = config
.map(|value| (value.connection_url, true, value.auto_connect))
.unwrap_or_default();

app.target_url_input.set_text(&target);
Expand All @@ -218,6 +282,16 @@ pub fn init(config: Option<ClientConfig>, client: Client) {
.set_check_state(CheckBoxState::Checked);
}

if auto_connect {
app.auto_connect_checkbox
.set_check_state(CheckBoxState::Checked);

// Connect right away to the saved server
if !target.is_empty() {
app.handle_set();
}
}

dispatch_thread_events();

// Resume the game threads if we close the UI
Expand Down