Skip to content

Commit

Permalink
fix tick system (#169)
Browse files Browse the repository at this point in the history
* fix tick system

* also async wait on stdin
  • Loading branch information
kralverde authored Oct 22, 2024
1 parent 746b656 commit 43116c5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ tokio = { version = "1.40", features = [
"net",
"rt-multi-thread",
"sync",
"io-std",
] }

# Concurrency/Parallelism and Synchronization
Expand Down
8 changes: 6 additions & 2 deletions pumpkin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use log::LevelFilter;
use client::Client;
use server::{ticker::Ticker, Server};
use std::io::{self};
use tokio::io::{AsyncBufReadExt, BufReader};

// Setup some tokens to allow us to identify which event is for which socket.

Expand Down Expand Up @@ -140,11 +141,14 @@ async fn main() -> io::Result<()> {
if use_console {
let server = server.clone();
tokio::spawn(async move {
let stdin = std::io::stdin();
let stdin = tokio::io::stdin();
let mut reader = BufReader::new(stdin);
loop {
let mut out = String::new();
stdin

reader
.read_line(&mut out)
.await
.expect("Failed to read console line");

if !out.is_empty() {
Expand Down
4 changes: 3 additions & 1 deletion pumpkin/src/server/ticker.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::time::{Duration, Instant};

use tokio::time::sleep;

use super::Server;

pub struct Ticker {
Expand Down Expand Up @@ -28,7 +30,7 @@ impl Ticker {
} else {
// Wait for the remaining time until the next tick
let sleep_time = self.tick_interval - elapsed;
std::thread::sleep(sleep_time);
sleep(sleep_time).await;
}
}
}
Expand Down

0 comments on commit 43116c5

Please sign in to comment.