2019-05-06 14:26:10 +00:00
|
|
|
use std::{
|
|
|
|
sync::mpsc::{channel, Receiver, Sender, TryRecvError},
|
|
|
|
time::Duration,
|
|
|
|
thread::{self, JoinHandle},
|
|
|
|
net::SocketAddr,
|
|
|
|
};
|
2019-04-29 20:37:19 +00:00
|
|
|
use log::info;
|
2019-05-06 14:26:10 +00:00
|
|
|
use portpicker::pick_unused_port;
|
|
|
|
use common::clock::Clock;
|
2019-04-29 20:37:19 +00:00
|
|
|
use server::{Event, Input, Server};
|
2019-04-17 13:41:34 +00:00
|
|
|
|
|
|
|
const TPS: u64 = 30;
|
|
|
|
|
2019-04-17 14:57:23 +00:00
|
|
|
enum Msg {
|
|
|
|
Stop,
|
|
|
|
}
|
|
|
|
|
2019-04-17 15:22:26 +00:00
|
|
|
/// Used to start and stop the background thread running the server
|
|
|
|
/// when in singleplayer mode.
|
2019-04-17 14:57:23 +00:00
|
|
|
pub struct Singleplayer {
|
|
|
|
server_thread: JoinHandle<()>,
|
|
|
|
sender: Sender<Msg>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Singleplayer {
|
2019-05-06 14:26:10 +00:00
|
|
|
pub fn new() -> (Self, SocketAddr) {
|
2019-04-17 14:57:23 +00:00
|
|
|
let (sender, reciever) = channel();
|
2019-05-06 14:26:10 +00:00
|
|
|
|
2019-05-09 10:32:44 +00:00
|
|
|
let sock = SocketAddr::from(([127,0,0,1], pick_unused_port()
|
2019-05-06 14:26:10 +00:00
|
|
|
.expect("Failed to find unused port")));
|
|
|
|
|
|
|
|
let sock2 = sock.clone();
|
2019-04-17 14:57:23 +00:00
|
|
|
let thread = thread::spawn(move || {
|
2019-05-06 14:26:10 +00:00
|
|
|
run_server(sock2, reciever);
|
2019-04-17 14:57:23 +00:00
|
|
|
});
|
2019-05-06 14:26:10 +00:00
|
|
|
|
|
|
|
(Singleplayer {
|
2019-04-17 14:57:23 +00:00
|
|
|
server_thread: thread,
|
|
|
|
sender,
|
2019-05-06 14:26:10 +00:00
|
|
|
}, sock)
|
2019-04-17 14:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Singleplayer {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.sender.send(Msg::Stop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-06 14:26:10 +00:00
|
|
|
fn run_server(sock: SocketAddr, rec: Receiver<Msg>) {
|
2019-04-17 13:41:34 +00:00
|
|
|
info!("Starting server-cli...");
|
|
|
|
|
|
|
|
// Set up an fps clock
|
|
|
|
let mut clock = Clock::new();
|
|
|
|
|
|
|
|
// Create server
|
2019-05-06 14:26:10 +00:00
|
|
|
let mut server = Server::bind(sock).expect("Failed to create server instance");
|
2019-04-17 13:41:34 +00:00
|
|
|
|
|
|
|
loop {
|
2019-04-29 20:37:19 +00:00
|
|
|
let events = server
|
|
|
|
.tick(Input::default(), clock.get_last_delta())
|
2019-04-17 13:41:34 +00:00
|
|
|
.expect("Failed to tick server");
|
|
|
|
|
|
|
|
for event in events {
|
|
|
|
match event {
|
|
|
|
Event::ClientConnected { entity } => info!("Client connected!"),
|
|
|
|
Event::ClientDisconnected { entity } => info!("Client disconnected!"),
|
|
|
|
Event::Chat { entity, msg } => info!("[Client] {}", msg),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up the server after a tick
|
|
|
|
server.cleanup();
|
|
|
|
|
2019-04-17 14:57:23 +00:00
|
|
|
match rec.try_recv() {
|
|
|
|
Ok(msg) => break,
|
|
|
|
Err(err) => match err {
|
2019-04-17 15:22:26 +00:00
|
|
|
TryRecvError::Empty => (),
|
|
|
|
TryRecvError::Disconnected => break,
|
2019-04-17 14:57:23 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-04-17 13:41:34 +00:00
|
|
|
// Wait for the next tick
|
|
|
|
clock.tick(Duration::from_millis(1000 / TPS));
|
|
|
|
}
|
|
|
|
}
|