2019-06-05 13:13:24 +00:00
|
|
|
use client::Client;
|
2019-06-05 15:22:06 +00:00
|
|
|
use common::clock::Clock;
|
2019-08-15 22:07:09 +00:00
|
|
|
use crossbeam::channel::{unbounded, Receiver, Sender, TryRecvError};
|
2019-05-09 17:58:16 +00:00
|
|
|
use log::info;
|
2019-06-29 16:41:26 +00:00
|
|
|
use server::{Event, Input, Server, ServerSettings};
|
2019-05-06 14:26:10 +00:00
|
|
|
use std::{
|
|
|
|
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 {
|
2019-07-02 21:29:38 +00:00
|
|
|
_server_thread: JoinHandle<()>,
|
2019-04-17 14:57:23 +00:00
|
|
|
sender: Sender<Msg>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Singleplayer {
|
2019-10-11 12:19:55 +00:00
|
|
|
pub fn new(client: Option<&Client>) -> (Self, ServerSettings) {
|
2019-08-15 22:07:09 +00:00
|
|
|
let (sender, receiver) = unbounded();
|
2019-05-06 14:26:10 +00:00
|
|
|
|
2019-05-21 22:31:17 +00:00
|
|
|
// Create server
|
2019-10-11 12:19:55 +00:00
|
|
|
let settings = ServerSettings::singleplayer();
|
2019-06-05 13:13:24 +00:00
|
|
|
|
2019-10-18 20:05:37 +00:00
|
|
|
let thread_pool = client.map(|c| c.thread_pool().clone());
|
|
|
|
let settings2 = settings.clone();
|
2019-05-21 22:31:17 +00:00
|
|
|
|
2019-04-17 14:57:23 +00:00
|
|
|
let thread = thread::spawn(move || {
|
2019-10-18 20:05:37 +00:00
|
|
|
let server = Server::new(settings2).expect("Failed to create server instance!");
|
|
|
|
|
|
|
|
let server = match thread_pool {
|
|
|
|
Some(pool) => server.with_thread_pool(pool),
|
|
|
|
None => server,
|
|
|
|
};
|
|
|
|
|
2019-05-31 01:33:54 +00:00
|
|
|
run_server(server, receiver);
|
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 {
|
2019-07-02 21:29:38 +00:00
|
|
|
_server_thread: thread,
|
2019-05-09 17:58:16 +00:00
|
|
|
sender,
|
|
|
|
},
|
2019-10-11 12:19:55 +00:00
|
|
|
settings,
|
2019-05-09 17:58:16 +00:00
|
|
|
)
|
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-21 22:31:17 +00:00
|
|
|
fn run_server(mut server: Server, rec: Receiver<Msg>) {
|
2019-04-17 13:41:34 +00:00
|
|
|
info!("Starting server-cli...");
|
|
|
|
|
|
|
|
// Set up an fps clock
|
2019-07-01 20:42:43 +00:00
|
|
|
let mut clock = Clock::start();
|
2019-07-05 11:58:52 +00:00
|
|
|
|
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 {
|
2019-07-02 21:29:38 +00:00
|
|
|
Event::ClientConnected { .. } => info!("Client connected!"),
|
|
|
|
Event::ClientDisconnected { .. } => info!("Client disconnected!"),
|
|
|
|
Event::Chat { entity: _, msg } => info!("[Client] {}", msg),
|
2019-04-17 13:41:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
2019-06-06 14:48:41 +00:00
|
|
|
Ok(_msg) => break,
|
2019-04-17 14:57:23 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|