ServerSocketConfig

Struct ServerSocketConfig 

Source
pub struct ServerSocketConfig {
    pub needs_encryption: bool,
    pub public_addresses: Vec<SocketAddr>,
}
Expand description

Configuration details for a socket associated with a netcode server.

Fields§

§needs_encryption: bool

If true then netcode packets sent/received to/from this socket will be encrypted/decrypted.

true by default.

§public_addresses: Vec<SocketAddr>

Publicly available addresses to which clients will attempt to connect.

Implementations§

Source§

impl ServerSocketConfig

Source

pub fn new(public_addresses: Vec<SocketAddr>) -> Self

Makes a new config with default settings.

Examples found in repository?
examples/echo_netcode.rs (line 134)
128fn server(addr: SocketAddr, private_key: [u8; NETCODE_KEY_BYTES]) {
129    let current_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
130    let config = ServerConfig {
131        current_time,
132        max_clients: 16,
133        protocol_id: PROTOCOL_ID,
134        sockets: vec![ServerSocketConfig::new(vec![addr])],
135        authentication: ServerAuthentication::Secure { private_key },
136    };
137    let mut server: NetcodeServer = NetcodeServer::new(config);
138    let udp_socket = UdpSocket::bind(addr).unwrap();
139    udp_socket.set_nonblocking(true).unwrap();
140    let mut received_messages = vec![];
141    let mut last_updated = Instant::now();
142    let mut buffer = [0u8; NETCODE_MAX_PACKET_BYTES];
143    let mut usernames: HashMap<u64, String> = HashMap::new();
144    loop {
145        server.update(Instant::now() - last_updated);
146        received_messages.clear();
147
148        loop {
149            match udp_socket.recv_from(&mut buffer) {
150                Ok((len, addr)) => {
151                    // println!("Received decrypted message {:?} from {}.", &buffer[..len], addr);
152                    let server_result = server.process_packet(0, addr, &mut buffer[..len]);
153                    handle_server_result(server_result, &udp_socket, &mut received_messages, &mut usernames);
154                }
155                Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => break,
156                Err(e) => panic!("Socket error: {}", e),
157            };
158        }
159
160        for text in received_messages.iter() {
161            for client_id in server.clients_id().iter() {
162                let (_, addr, payload) = server.generate_payload_packet(*client_id, text.as_bytes()).unwrap();
163                udp_socket.send_to(payload, addr).unwrap();
164            }
165        }
166
167        for client_id in server.clients_id().into_iter() {
168            let server_result = server.update_client(client_id);
169            handle_server_result(server_result, &udp_socket, &mut received_messages, &mut usernames);
170        }
171
172        last_updated = Instant::now();
173        thread::sleep(Duration::from_millis(50));
174    }
175}

Trait Implementations§

Source§

impl Debug for ServerSocketConfig

Source§

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

Formats the value using the given formatter. Read more

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> 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, 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.