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
12pub struct ConnectOptions {
13    pub name: String,
14    pub server: SocketAddr,
15    pub auth: AuthCredential,
16    pub character_id: Option<Uuid>,
17}
18
19impl ConnectOptions {
20    pub fn to_server(name: impl Into<String>, server: SocketAddr) -> Self {
21        Self {
22            name: name.into(),
23            server,
24            auth: AuthCredential::DevLocal,
25            character_id: None,
26        }
27    }
28
29    pub fn with_auth(mut self, auth: AuthCredential, character_id: Option<Uuid>) -> Self {
30        self.auth = auth;
31        self.character_id = character_id;
32        self
33    }
34
35    pub fn hello(&self) -> Hello {
36        Hello {
37            client_name: self.name.clone(),
38            protocol_version: PROTOCOL_VERSION,
39            auth: self.auth.clone(),
40            character_id: self.character_id,
41        }
42    }
43}
44
45pub fn default_server_addr() -> SocketAddr {
46    "127.0.0.1:7373".parse().expect("valid default addr")
47}
48
49pub async fn connect(opts: ConnectOptions) -> anyhow::Result<RemoteSession> {
50    connect_tcp(opts.server, &opts.hello()).await
51}