#networking #gamedev #renet #udp #server-client #multiplayer-game #authentication

renet_udp

使用Renet和UDP的多玩家游戏服务器/客户端网络库

2个版本

0.0.2 2021年12月27日
0.0.1 2021年12月25日

#31 in #multiplayer-game

MIT/Apache

145KB
3K SLoC

Renet UDP

使用UDP和Renet实现的Server/Client(目前尚不支持身份验证)。

回声示例

服务器

let socket = UdpSocket::bind("127.0.0.0:5000").unwrap();
let server_config = ServerConfig::default();
// Create one channel for each type reliable (0), unreliable(1), block(2)
let connection_config = ConnectionConfig::default();
let mut server: UdpServer = UdpServer::new(server_config, connection_config, socket)?;
    
let frame_duration = Duration::from_millis(100);
loop {
  server.update(frame_duration)?;
  while let Some(event) = server.get_event() {
    match event {
      ServerEvent::ClientConnected(id) => println!("Client {} connected.", id),
      ServerEvent::ClientDisconnected(id, reason) => println!("Client {} disconnected: {}", id, reason)
    }
  }

  for client_id in server.clients_id().iter() {
    while let Some(message) = server.receive_message(client_id, 0) {
      let text = String::from_utf8(message)?;
      println!("Client {} sent text: {}", client_id, text);
      server.broadcast_message(0, text.as_bytes().to_vec());
    }
  }
        
  server.send_packets()?;
  thread::sleep(frame_duration);
}

客户端

let socket = UdpSocket::bind("127.0.0.1:0")?;
// Create one channel for each type reliable (0), unreliable(1), block(2)
let connection_config = ConnectionConfig::default();
let server_addr = "127.0.0.1:5000".parse().unwrap();
let mut client = UdpClient::new(socket, server_addr, connection_config)?;
let stdin_channel = spawn_stdin_channel();

let frame_duration = Duration::from_millis(100);
loop {
  client.update(frame_duration)?;
  match stdin_channel.try_recv() {
    Ok(text) => client.send_message(0, text.as_bytes().to_vec())?,
    Err(TryRecvError::Empty) => {}
    Err(TryRecvError::Disconnected) => panic!("Channel disconnected"),
  }

  while let Some(text) = client.receive_message(0) {
    let text = String::from_utf8(text).unwrap();
    println!("Message from server: {}", text);
  }

  client.send_packets()?;
  thread::sleep(frame_duration);
}

fn spawn_stdin_channel() -> Receiver<String> {
  let (tx, rx) = mpsc::channel::<String>();
  thread::spawn(move || loop {
    let mut buffer = String::new();
    std::io::stdin().read_line(&mut buffer).unwrap();
    tx.send(buffer).unwrap();
  });
  rx
}

依赖项

~0.7–1.3MB
~31K SLoC