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
impl ConnectToken
Sourcepub 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>
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}
pub fn write(&self, writer: &mut impl Write) -> Result<(), Error>
pub fn read(src: &mut impl Read) -> Result<Self, NetcodeError>
Trait Implementations§
Source§impl Clone for ConnectToken
impl Clone for ConnectToken
Source§fn clone(&self) -> ConnectToken
fn clone(&self) -> ConnectToken
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for ConnectToken
impl Debug for ConnectToken
Source§impl PartialEq for ConnectToken
impl PartialEq for ConnectToken
impl Eq for ConnectToken
impl StructuralPartialEq for ConnectToken
Auto Trait Implementations§
impl Freeze for ConnectToken
impl RefUnwindSafe for ConnectToken
impl Send for ConnectToken
impl Sync for ConnectToken
impl Unpin for ConnectToken
impl UnwindSafe for ConnectToken
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more