veloren/client/examples/chat-cli/main.rs
Marcel Märtens 514d5db038 Update Network Protocol
- now last digit version is compatible 0.6.0 will connect to 0.6.1
 - the TCP DATA Frames no longer contain START field, as it's not needed
 - the TCP OPENSTREAM Frames will now contain the BANDWIDTH field
 - MID is not Protocol internal

Update network
 - update API with Bandwidth

Update veloren
 - introduce better runtime and `async` things that are IO bound.
 - Remove `uvth` and instead use `tokio::runtime::Runtime::spawn_blocking`
 - remove futures_execute from client and server use tokio::runtime::Runtime instead
 - give threads a Name
2021-02-22 17:34:55 +01:00

118 lines
2.9 KiB
Rust

#![deny(unsafe_code)]
#![allow(clippy::option_map_unit_fn)]
#![deny(clippy::clone_on_ref_ptr)]
use common::{clock::Clock, comp};
use std::{
io,
net::ToSocketAddrs,
sync::{mpsc, Arc},
thread,
time::Duration,
};
use tokio::runtime::Runtime;
use tracing::{error, info};
use veloren_client::{Client, Event};
const TPS: u64 = 10; // Low value is okay, just reading messages.
fn read_input() -> String {
let mut buffer = String::new();
io::stdin()
.read_line(&mut buffer)
.expect("Failed to read input");
buffer.trim().to_string()
}
fn main() {
// Initialize logging.
tracing_subscriber::fmt::init();
info!("Starting chat-cli...");
// Set up an fps clock.
let mut clock = Clock::new(Duration::from_secs_f64(1.0 / TPS as f64));
println!("Enter your username");
let username = read_input();
println!("Enter the server address");
let server_addr = read_input();
println!("Enter your password");
let password = read_input();
let runtime = Arc::new(Runtime::new().unwrap());
let runtime2 = Arc::clone(&runtime);
// Create a client.
let mut client = runtime
.block_on(Client::new(
server_addr
.to_socket_addrs()
.expect("Invalid server address")
.next()
.unwrap(),
None,
runtime2,
))
.expect("Failed to create client instance");
println!("Server info: {:?}", client.server_info());
println!("Players online: {:?}", client.get_players());
runtime
.block_on(client.register(username, password, |provider| {
provider == "https://auth.veloren.net"
}))
.unwrap();
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
loop {
let msg = read_input();
tx.send(msg).unwrap();
}
});
loop {
for msg in rx.try_iter() {
client.send_chat(msg)
}
let events = match client.tick(comp::ControllerInputs::default(), clock.dt(), |_| {}) {
Ok(events) => events,
Err(err) => {
error!("Error: {:?}", err);
break;
},
};
const SHOW_NAME: bool = false;
for event in events {
match event {
Event::Chat(m) => println!("{}", client.format_message(&m, SHOW_NAME)),
Event::Disconnect => {}, // TODO
Event::DisconnectionNotification(time) => {
let message = match time {
0 => String::from("Goodbye!"),
_ => format!("Connection lost. Kicking in {} seconds", time),
};
println!("{}", message)
},
_ => {},
}
}
// Clean up the server after a tick.
client.cleanup();
// Wait for the next tick.
clock.tick();
}
}