Struct ConnectToken

Source
pub struct ConnectToken {
    pub client_id: u64,
    pub version_info: [u8; 13],
    pub protocol_id: u64,
    pub create_timestamp: u64,
    pub expire_timestamp: u64,
    pub xnonce: [u8; 24],
    pub server_addresses: [Option<SocketAddr>; 32],
    pub client_to_server_key: [u8; 32],
    pub server_to_client_key: [u8; 32],
    pub private_data: [u8; 1024],
    pub timeout_seconds: i32,
}
Expand description

A public connect token that the client receives to start connecting to the server. How the client receives ConnectToken is up to you, could be from a matchmaking system or from a call to a REST API as an example.

Fields§

§client_id: u64§version_info: [u8; 13]§protocol_id: u64§create_timestamp: u64§expire_timestamp: u64§xnonce: [u8; 24]§server_addresses: [Option<SocketAddr>; 32]§client_to_server_key: [u8; 32]§server_to_client_key: [u8; 32]§private_data: [u8; 1024]§timeout_seconds: i32

Implementations§

Source§

impl ConnectToken

Source

pub fn generate( current_time: Duration, protocol_id: u64, expire_seconds: u64, client_id: u64, timeout_seconds: i32, server_addresses: Vec<SocketAddr>, user_data: Option<&[u8; 256]>, private_key: &[u8; 32], ) -> Result<Self, TokenGenerationError>

Generate a token to be sent to an client. The user data is available to the server after an successfull conection. The private key and the protocol id must be the same used in server.

Examples found in repository?
examples/echo.rs (lines 58-67)
45fn main() {
46    println!("Usage: server [SERVER_PORT] or client [SERVER_PORT] [USER_NAME]");
47    let args: Vec<String> = std::env::args().collect();
48    let private_key = b"an example very very secret key."; // 32-bytes
49
50    let exec_type = &args[1];
51    match exec_type.as_str() {
52        "client" => {
53            let server_addr: SocketAddr = format!("127.0.0.1:{}", args[2]).parse().unwrap();
54            let username = Username(args[3].clone());
55            let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
56            println!("Stating connecting at {:?} with username {}", now, username.0,);
57            let client_id = now.as_millis() as u64;
58            let connect_token = ConnectToken::generate(
59                now,
60                PROTOCOL_ID,
61                300,
62                client_id,
63                15,
64                vec![server_addr],
65                Some(&username.to_netcode_user_data()),
66                private_key,
67            )
68            .unwrap();
69            let auth = ClientAuthentication::Secure { connect_token };
70            client(auth);
71        }
72        "server" => {
73            let server_addr: SocketAddr = format!("127.0.0.1:{}", args[2]).parse().unwrap();
74            server(server_addr, *private_key);
75        }
76        _ => {
77            println!("Invalid argument, first one must be \"client\" or \"server\".");
78        }
79    }
80}
Source

pub fn write(&self, writer: &mut impl Write) -> Result<(), Error>

Source

pub fn read(src: &mut impl Read) -> Result<Self, NetcodeError>

Trait Implementations§

Source§

impl Clone for ConnectToken

Source§

fn clone(&self) -> ConnectToken

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ConnectToken

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for ConnectToken

Source§

fn eq(&self, other: &ConnectToken) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for ConnectToken

Source§

impl StructuralPartialEq for ConnectToken

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.