Add Multiplayer support

This commit is contained in:
Snowiiii committed 2024-08-06 16:19:03 +02:00
1 parent d1c3b2f10c
commit 32569bafe5
34 files changed
+445 -243

No files matched your search

+16 -12
View File
@@ -29,7 +29,7 @@ pub mod util;
#[cfg(not(target_os = "wasi"))]
fn main() -> io::Result<()> {
use std::time::Instant;
use std::{cell::RefCell, time::Instant};
let time = Instant::now();
let basic_config = BasicConfiguration::load("configuration.toml");
@@ -63,7 +63,7 @@ fn main() -> io::Result<()> {
let use_console = advanced_configuration.commands.use_console;
let mut connections: HashMap<Token, Client> = HashMap::new();
let mut connections: HashMap<Token, Rc<RefCell<Client>>> = HashMap::new();
let server = Arc::new(Mutex::new(Server::new((
basic_config,
@@ -73,7 +73,6 @@ fn main() -> io::Result<()> {
log::info!("You now can connect to the server");
if use_console {
let server_clone = server.clone();
thread::spawn(move || {
let stdin = std::io::stdin();
loop {
@@ -81,11 +80,7 @@ fn main() -> io::Result<()> {
stdin
.read_line(&mut out)
.expect("Failed to read console line");
handle_command(
&mut commands::CommandSender::Console,
out,
&mut server_clone.lock().unwrap(),
);
handle_command(&mut commands::CommandSender::Console, out);
}
});
}
@@ -127,14 +122,22 @@ fn main() -> io::Result<()> {
Interest::READABLE.add(Interest::WRITABLE),
)?;
let rc_token = Rc::new(token);
let client = Client::new(Rc::clone(&rc_token), connection, addr);
// server.add_client(rc_token, Rc::clone(&client));
let client = Rc::new(RefCell::new(Client::new(
Rc::clone(&rc_token),
connection,
addr,
)));
server
.lock()
.unwrap()
.add_client(rc_token, Rc::clone(&client));
connections.insert(token, client);
},
token => {
// Maybe received an event for a TCP connection.
let done = if let Some(client) = connections.get_mut(&token) {
let mut client = client.borrow_mut();
client.poll(&mut server.lock().unwrap(), event);
client.closed
} else {
@@ -142,8 +145,9 @@ fn main() -> io::Result<()> {
false
};
if done {
if let Some(mut client) = connections.remove(&token) {
// server.remove_client(&token);
if let Some(client) = connections.remove(&token) {
let mut client = client.borrow_mut();
server.lock().unwrap().remove_client(&token);
poll.registry().deregister(&mut client.connection)?;
}
}