2019-06-05 13:13:24 +00:00
|
|
|
use client::Client;
|
2019-06-05 15:22:06 +00:00
|
|
|
use common::clock::Clock;
|
2019-05-09 17:58:16 +00:00
|
|
|
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 {
|
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-06-05 13:13:24 +00:00
|
|
|
pub fn new(client: Option<&Client>) -> (Self, SocketAddr) {
|
2019-05-31 01:33:54 +00:00
|
|
|
let (sender, receiver) = 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
|
|
|
|
2019-05-21 22:31:17 +00:00
|
|
|
// Create server
|
2019-06-05 15:22:06 +00:00
|
|
|
let server = Server::bind(sock.clone()).expect("Failed to create server instance!");
|
2019-06-05 13:13:24 +00:00
|
|
|
|
|
|
|
let server = match client {
|
|
|
|
Some(client) => server.with_thread_pool(client.thread_pool().clone()),
|
|
|
|
None => server,
|
|
|
|
};
|
2019-05-21 22:31:17 +00:00
|
|
|
|
2019-04-17 14:57:23 +00:00
|
|
|
let thread = thread::spawn(move || {
|
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,
|
|
|
|
},
|
|
|
|
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-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-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));
|
|
|
|
}
|
|
|
|
}
|