2019-08-19 12:39:23 +00:00
|
|
|
#![deny(unsafe_code)]
|
2020-09-27 16:20:40 +00:00
|
|
|
#![deny(clippy::clone_on_ref_ptr)]
|
2019-08-19 12:39:23 +00:00
|
|
|
|
2020-10-03 19:10:34 +00:00
|
|
|
mod shutdown_coordinator;
|
2020-08-31 08:41:11 +00:00
|
|
|
mod tui_runner;
|
|
|
|
mod tuilog;
|
|
|
|
|
2020-07-27 16:26:34 +00:00
|
|
|
#[macro_use] extern crate lazy_static;
|
|
|
|
|
2020-08-31 08:41:11 +00:00
|
|
|
use crate::{
|
2020-10-03 19:10:34 +00:00
|
|
|
shutdown_coordinator::ShutdownCoordinator,
|
2020-08-31 08:41:11 +00:00
|
|
|
tui_runner::{Message, Tui},
|
|
|
|
tuilog::TuiLog,
|
|
|
|
};
|
2019-01-30 12:11:34 +00:00
|
|
|
use common::clock::Clock;
|
2019-06-29 16:41:26 +00:00
|
|
|
use server::{Event, Input, Server, ServerSettings};
|
2020-10-03 19:10:34 +00:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
|
|
|
use signal_hook::SIGUSR1;
|
2020-08-31 12:38:54 +00:00
|
|
|
use tracing::{info, Level};
|
2020-06-29 08:53:43 +00:00
|
|
|
use tracing_subscriber::{filter::LevelFilter, EnvFilter, FmtSubscriber};
|
2020-08-29 07:00:26 +00:00
|
|
|
#[cfg(feature = "tracy")]
|
|
|
|
use tracing_subscriber::{layer::SubscriberExt, prelude::*};
|
2019-01-30 12:11:34 +00:00
|
|
|
|
2020-07-28 13:47:01 +00:00
|
|
|
use clap::{App, Arg};
|
2020-10-03 19:10:34 +00:00
|
|
|
use std::{
|
|
|
|
io,
|
|
|
|
sync::{atomic::AtomicBool, mpsc, Arc},
|
|
|
|
time::Duration,
|
|
|
|
};
|
2020-07-27 16:26:34 +00:00
|
|
|
|
2019-03-19 19:53:35 +00:00
|
|
|
const TPS: u64 = 30;
|
2020-06-29 08:53:43 +00:00
|
|
|
const RUST_LOG_ENV: &str = "RUST_LOG";
|
2019-01-30 12:11:34 +00:00
|
|
|
|
2020-07-27 16:26:34 +00:00
|
|
|
lazy_static! {
|
|
|
|
static ref LOG: TuiLog<'static> = TuiLog::default();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> io::Result<()> {
|
2020-07-28 13:47:01 +00:00
|
|
|
let matches = App::new("Veloren server cli")
|
2020-09-16 10:50:55 +00:00
|
|
|
.version(common::util::DISPLAY_VERSION_LONG.as_str())
|
2020-07-28 13:47:01 +00:00
|
|
|
.author("The veloren devs <https://gitlab.com/veloren/veloren>")
|
2020-07-28 15:02:36 +00:00
|
|
|
.about("The veloren server cli provides an easy to use interface to start a veloren server")
|
2020-07-28 13:47:01 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("basic")
|
|
|
|
.short("b")
|
|
|
|
.long("basic")
|
|
|
|
.help("Disables the tui")
|
|
|
|
.takes_value(false),
|
|
|
|
)
|
|
|
|
.get_matches();
|
|
|
|
|
|
|
|
let basic = matches.is_present("basic");
|
2020-10-03 19:10:34 +00:00
|
|
|
|
|
|
|
let sigusr1_signal = Arc::new(AtomicBool::new(false));
|
|
|
|
|
|
|
|
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
|
|
|
let _ = signal_hook::flag::register(SIGUSR1, Arc::clone(&sigusr1_signal));
|
|
|
|
|
2020-08-31 08:41:11 +00:00
|
|
|
let (mut tui, msg_r) = Tui::new();
|
2020-07-28 13:47:01 +00:00
|
|
|
|
2019-01-30 12:11:34 +00:00
|
|
|
// Init logging
|
2020-09-16 08:13:37 +00:00
|
|
|
let base_exceptions = |env: EnvFilter| {
|
|
|
|
env.add_directive("veloren_world::sim=info".parse().unwrap())
|
|
|
|
.add_directive("veloren_world::civ=info".parse().unwrap())
|
|
|
|
.add_directive("uvth=warn".parse().unwrap())
|
|
|
|
.add_directive("tiny_http=warn".parse().unwrap())
|
|
|
|
.add_directive("mio::sys::windows=debug".parse().unwrap())
|
|
|
|
.add_directive(LevelFilter::INFO.into())
|
|
|
|
};
|
|
|
|
|
2020-08-29 04:33:20 +00:00
|
|
|
#[cfg(not(feature = "tracy"))]
|
2020-06-29 08:53:43 +00:00
|
|
|
let filter = match std::env::var_os(RUST_LOG_ENV).map(|s| s.into_string()) {
|
|
|
|
Some(Ok(env)) => {
|
2020-09-16 08:13:37 +00:00
|
|
|
let mut filter = base_exceptions(EnvFilter::new(""));
|
2020-06-29 08:53:43 +00:00
|
|
|
for s in env.split(',').into_iter() {
|
|
|
|
match s.parse() {
|
|
|
|
Ok(d) => filter = filter.add_directive(d),
|
|
|
|
Err(err) => println!("WARN ignoring log directive: `{}`: {}", s, err),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
filter
|
|
|
|
},
|
2020-09-16 08:13:37 +00:00
|
|
|
_ => base_exceptions(EnvFilter::from_env(RUST_LOG_ENV)),
|
2020-06-29 08:53:43 +00:00
|
|
|
};
|
2020-06-21 14:26:06 +00:00
|
|
|
|
2020-08-29 04:33:20 +00:00
|
|
|
#[cfg(feature = "tracy")]
|
|
|
|
tracing_subscriber::registry()
|
|
|
|
.with(tracing_tracy::TracyLayer::new().with_stackdepth(0))
|
|
|
|
.init();
|
|
|
|
|
|
|
|
#[cfg(not(feature = "tracy"))]
|
2020-08-29 07:00:26 +00:00
|
|
|
// TODO: when tracing gets per Layer filters re-enable this when the tracy feature is being
|
|
|
|
// used (and do the same in voxygen)
|
2020-08-29 04:33:20 +00:00
|
|
|
{
|
|
|
|
let subscriber = FmtSubscriber::builder()
|
|
|
|
.with_max_level(Level::ERROR)
|
|
|
|
.with_env_filter(filter);
|
|
|
|
|
|
|
|
if basic {
|
|
|
|
subscriber.init();
|
|
|
|
} else {
|
|
|
|
subscriber.with_writer(|| LOG.clone()).init();
|
|
|
|
}
|
2020-07-28 13:47:01 +00:00
|
|
|
}
|
2019-01-30 12:11:34 +00:00
|
|
|
|
2020-10-03 19:10:34 +00:00
|
|
|
// Panic hook to ensure that console mode is set back correctly if in non-basic
|
|
|
|
// mode
|
|
|
|
let hook = std::panic::take_hook();
|
|
|
|
std::panic::set_hook(Box::new(move |info| {
|
|
|
|
Tui::shutdown(basic);
|
|
|
|
hook(info);
|
|
|
|
}));
|
|
|
|
|
2020-09-02 11:47:17 +00:00
|
|
|
tui.run(basic);
|
2020-07-28 10:02:48 +00:00
|
|
|
|
|
|
|
info!("Starting server...");
|
|
|
|
|
|
|
|
// Set up an fps clock
|
|
|
|
let mut clock = Clock::start();
|
|
|
|
|
|
|
|
// Load settings
|
|
|
|
let settings = ServerSettings::load();
|
|
|
|
let server_port = &settings.gameserver_address.port();
|
|
|
|
let metrics_port = &settings.metrics_address.port();
|
|
|
|
// Create server
|
|
|
|
let mut server = Server::new(settings).expect("Failed to create server instance!");
|
|
|
|
|
2020-10-03 19:10:34 +00:00
|
|
|
info!(
|
|
|
|
?server_port,
|
|
|
|
?metrics_port,
|
|
|
|
"Server is ready to accept connections."
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut shutdown_coordinator = ShutdownCoordinator::new(Arc::clone(&sigusr1_signal));
|
2020-07-28 10:02:48 +00:00
|
|
|
|
|
|
|
loop {
|
2020-10-03 19:10:34 +00:00
|
|
|
// Terminate the server if instructed to do so by the shutdown coordinator
|
|
|
|
if shutdown_coordinator.check(&mut server) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-07-28 10:02:48 +00:00
|
|
|
let events = server
|
|
|
|
.tick(Input::default(), clock.get_last_delta())
|
|
|
|
.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();
|
2020-08-29 07:00:26 +00:00
|
|
|
#[cfg(feature = "tracy")]
|
|
|
|
common::util::tracy_client::finish_continuous_frame!();
|
2020-07-28 10:02:48 +00:00
|
|
|
|
2020-08-31 08:41:11 +00:00
|
|
|
match msg_r.try_recv() {
|
2020-07-28 10:02:48 +00:00
|
|
|
Ok(msg) => match msg {
|
2020-10-03 19:10:34 +00:00
|
|
|
Message::AbortShutdown => shutdown_coordinator.abort_shutdown(&mut server),
|
|
|
|
Message::Shutdown { grace_period } => {
|
|
|
|
// TODO: The TUI parser doesn't support quoted strings so it is not currently
|
|
|
|
// possible to provide a shutdown reason from the console.
|
|
|
|
let message = "The server is shutting down".to_owned();
|
|
|
|
shutdown_coordinator.initiate_shutdown(&mut server, grace_period, message);
|
|
|
|
},
|
2020-07-28 10:02:48 +00:00
|
|
|
Message::Quit => {
|
|
|
|
info!("Closing the server");
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
},
|
2020-09-07 13:50:48 +00:00
|
|
|
Err(mpsc::TryRecvError::Empty) | Err(mpsc::TryRecvError::Disconnected) => {},
|
2020-07-28 10:02:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Wait for the next tick.
|
|
|
|
clock.tick(Duration::from_millis(1000 / TPS));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|