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: boolIf 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
impl ServerSocketConfig
Sourcepub fn new(public_addresses: Vec<SocketAddr>) -> Self
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§
Auto Trait Implementations§
impl Freeze for ServerSocketConfig
impl RefUnwindSafe for ServerSocketConfig
impl Send for ServerSocketConfig
impl Sync for ServerSocketConfig
impl Unpin for ServerSocketConfig
impl UnwindSafe for ServerSocketConfig
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