Skip to main content

flatland_client_lib/
connect.rs

1//! TCP session connect for remote flatland-server.
2
3use std::net::SocketAddr;
4
5use flatland_protocol::{AuthCredential, Hello, PROTOCOL_VERSION};
6use uuid::Uuid;
7
8use crate::remote::{self, RemoteSession};
9
10pub use remote::connect_tcp;
11
12#[derive(Debug, Clone)]
13pub struct ConnectOptions {
14    pub name: String,
15    pub server: SocketAddr,
16    pub auth: AuthCredential,
17    pub character_id: Option<Uuid>,
18}
19
20impl ConnectOptions {
21    pub fn to_server(name: impl Into<String>, server: SocketAddr) -> Self {
22        Self {
23            name: name.into(),
24            server,
25            auth: AuthCredential::DevLocal,
26            character_id: None,
27        }
28    }
29
30    pub fn with_auth(mut self, auth: AuthCredential, character_id: Option<Uuid>) -> Self {
31        self.auth = auth;
32        self.character_id = character_id;
33        self
34    }
35
36    pub fn hello(&self) -> Hello {
37        Hello {
38            client_name: self.name.clone(),
39            protocol_version: PROTOCOL_VERSION,
40            auth: self.auth.clone(),
41            character_id: self.character_id,
42        }
43    }
44}
45
46pub fn default_server_addr() -> SocketAddr {
47    "127.0.0.1:7373".parse().expect("valid default addr")
48}
49
50pub async fn connect(opts: ConnectOptions) -> anyhow::Result<RemoteSession> {
51    connect_tcp(opts.server, &opts.hello()).await
52}