2019-05-09 17:58:16 +00:00
|
|
|
use common::clock::Clock;
|
|
|
|
use log::info;
|
|
|
|
use portpicker::pick_unused_port;
|
|
|
|
use server::{Event, Input, Server};
|
2019-05-06 14:26:10 +00:00
|
|
|
use std::{
|
2019-05-09 17:58:16 +00:00
|
|
|
net::SocketAddr,
|
2019-05-06 14:26:10 +00:00
|
|
|
sync::mpsc::{channel, Receiver, Sender, TryRecvError},
|
|
|
|
thread::{self, JoinHandle},
|
2019-05-09 17:58:16 +00:00
|
|
|
time::Duration,
|
2019-05-06 14:26:10 +00:00
|
|
|
};
|
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 17:58:16 +00:00
|
|
|
let sock = SocketAddr::from((
|
|
|
|
[127, 0, 0, 1],
|
2019-05-17 09:22:32 +00:00
|
|
|
pick_unused_port().expect("Failed to find unused port!"),
|
2019-05-09 17:58:16 +00:00
|
|
|
));
|
2019-05-06 14:26:10 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2019-05-09 17:58:16 +00:00
|
|
|
(
|
|
|
|
Singleplayer {
|
|
|
|
server_thread: thread,
|
|
|
|
sender,
|
|
|
|
},
|
|
|
|
sock,
|
|
|
|
)
|
2019-04-17 14:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Singleplayer {
|
|
|
|
fn drop(&mut self) {
|
2019-05-17 07:55:51 +00:00
|
|
|
// Ignore the result
|
|
|
|
let _ = self.sender.send(Msg::Stop);
|
2019-04-17 14:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-17 09:22:32 +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-05-17 09:22:32 +00:00
|
|
|
.expect("Failed to tick server!");
|
2019-04-17 13:41:34 +00:00
|
|
|
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Clean up the server after a tick.
|
2019-04-17 13:41:34 +00:00
|
|
|
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-05-17 09:22:32 +00:00
|
|
|
// Wait for the next tick.
|
2019-04-17 13:41:34 +00:00
|
|
|
clock.tick(Duration::from_millis(1000 / TPS));
|
|
|
|
}
|
|
|
|
}
|