Struct netcode::Server
[−]
[src]
pub struct Server<I, S> { /* fields omitted */ }
Netcode server object.
Example
use netcode::{UdpServer, ServerEvent}; fn run_server() { const PROTOCOL_ID: u64 = 0xFFEE; const MAX_CLIENTS: usize = 32; let mut server = UdpServer::new("127.0.0.1:0", MAX_CLIENTS, PROTOCOL_ID, &netcode::generate_key()).unwrap(); loop { server.update(1.0 / 10.0); let mut packet_data = [0; netcode::NETCODE_MAX_PAYLOAD_SIZE]; match server.next_event(&mut packet_data) { Ok(Some(e)) => { match e { ServerEvent::ClientConnect(_id) => {}, ServerEvent::ClientDisconnect(_id) => {}, ServerEvent::Packet(_id,_size) => {}, _ => () } }, Ok(None) => (), Err(err) => Err(err).unwrap() } //Tick world/gamestate/etc. //Sleep till next frame. } }
Methods
impl<I, S> Server<I, S> where
I: SocketProvider<I, S>,
[src]
I: SocketProvider<I, S>,
fn new<A>(
local_addr: A,
max_clients: usize,
protocol_id: u64,
private_key: &[u8; 32]
) -> Result<Server<I, S>, CreateError> where
A: ToSocketAddrs,
local_addr: A,
max_clients: usize,
protocol_id: u64,
private_key: &[u8; 32]
) -> Result<Server<I, S>, CreateError> where
A: ToSocketAddrs,
Constructs a new Server bound to local_addr
with max_clients
and supplied private_key
for authentication.
fn generate_token(
&mut self,
expire_secs: usize,
client_id: u64,
user_data: Option<&[u8; 256]>
) -> Result<ConnectToken, GenerateError>
&mut self,
expire_secs: usize,
client_id: u64,
user_data: Option<&[u8; 256]>
) -> Result<ConnectToken, GenerateError>
Generates a connection token with client_id
that expires after expire_secs
with an optional user_data.
fn get_local_addr(&self) -> Result<SocketAddr, Error>
Gets the local port that this server is bound to.
fn send(&mut self, client_id: u64, packet: &[u8]) -> Result<usize, SendError>
Sends a packet to client_id
specified.
fn update(&mut self, elapsed: f64)
Updates time elapsed since last server iteration.
fn next_event(
&mut self,
out_packet: &mut [u8; 1175]
) -> Result<Option<ServerEvent>, UpdateError>
&mut self,
out_packet: &mut [u8; 1175]
) -> Result<Option<ServerEvent>, UpdateError>
Checks for incoming packets, client connection and disconnections. Returns None
when no more events
are pending.
fn disconnect(&mut self, client_id: u64) -> Result<(), SendError>
Sends a disconnect and removes client.