#![deny(unsafe_code)]
#![deny(clippy::clone_on_ref_ptr)]
#[cfg(all(
target_os = "windows",
not(feature = "hot-agent"),
not(feature = "hot-site"),
))]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
mod cli;
mod settings;
mod shutdown_coordinator;
mod tui_runner;
mod tuilog;
mod web;
use crate::{
cli::{
Admin, ArgvApp, ArgvCommand, BenchParams, Message, MessageReturn, SharedCommand, Shutdown,
},
settings::Settings,
shutdown_coordinator::ShutdownCoordinator,
tui_runner::Tui,
tuilog::TuiLog,
};
use common::{
clock::Clock,
comp::{ChatType, Player},
consts::MIN_RECOMMENDED_TOKIO_THREADS,
};
use common_base::span;
use core::sync::atomic::{AtomicUsize, Ordering};
use server::{persistence::DatabaseSettings, settings::Protocol, Event, Input, Server};
use std::{
io,
sync::{atomic::AtomicBool, Arc},
time::{Duration, Instant},
};
use tokio::sync::Notify;
use tracing::{info, trace};
lazy_static::lazy_static! {
pub static ref LOG: TuiLog<'static> = TuiLog::default();
}
const TPS: u64 = 30;
fn main() -> io::Result<()> {
#[cfg(feature = "tracy")]
common_base::tracy_client::Client::start();
use clap::Parser;
let app = ArgvApp::parse();
let basic = !app.tui || app.command.is_some();
let noninteractive = app.non_interactive;
let no_auth = app.no_auth;
let sql_log_mode = app.sql_log_mode;
let basic = basic || noninteractive;
let shutdown_signal = Arc::new(AtomicBool::new(false));
let (_guards, _guards2) = if basic {
(Vec::new(), common_frontend::init_stdout(None))
} else {
(common_frontend::init(None, &|| LOG.clone()), Vec::new())
};
let settings = settings::Settings::load();
#[cfg(any(target_os = "linux", target_os = "macos"))]
{
for signal in &settings.shutdown_signals {
let _ = signal_hook::flag::register(signal.to_signal(), Arc::clone(&shutdown_signal));
}
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
if !settings.shutdown_signals.is_empty() {
tracing::warn!(
"Server configuration contains shutdown signals, but your platform does not support \
them"
);
}
let server_data_dir = {
let mut path = common_base::userdata_dir_workspace!();
info!("Using userdata folder at {}", path.display());
path.push(server::DEFAULT_DATA_DIR_NAME);
path
};
let runtime = Arc::new(
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.worker_threads((num_cpus::get() / 4).max(MIN_RECOMMENDED_TOKIO_THREADS))
.thread_name_fn(|| {
static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0);
let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst);
format!("tokio-server-{}", id)
})
.build()
.unwrap(),
);
#[cfg(feature = "hot-agent")]
{
agent::init();
}
#[cfg(feature = "hot-site")]
{
world::init();
}
let mut server_settings = server::Settings::load(&server_data_dir);
let mut editable_settings = server::EditableSettings::load(&server_data_dir);
if no_auth {
server_settings.auth_server_address = None;
}
const PERSISTENCE_DB_DIR: &str = "saves";
let database_settings = DatabaseSettings {
db_dir: server_data_dir.join(PERSISTENCE_DB_DIR),
sql_log_mode,
};
let mut bench = None;
if let Some(command) = app.command {
match command {
ArgvCommand::Shared(SharedCommand::Admin { command }) => {
let login_provider = server::login_provider::LoginProvider::new(
server_settings.auth_server_address,
runtime,
);
return match command {
Admin::Add { username, role } => {
let _ = server::add_admin(
&username,
role,
&login_provider,
&mut editable_settings,
&server_data_dir,
);
Ok(())
},
Admin::Remove { username } => {
let _ = server::remove_admin(
&username,
&login_provider,
&mut editable_settings,
&server_data_dir,
);
Ok(())
},
};
},
ArgvCommand::Bench(params) => {
bench = Some(params);
server_settings.max_view_distance = None;
},
};
}
if !basic {
let hook = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
Tui::shutdown(basic);
hook(info);
}));
}
let tui = (!noninteractive).then(|| Tui::run(basic));
info!("Starting server...");
let protocols_and_addresses = server_settings.gameserver_protocols.clone();
let web_port = &settings.web_address.port();
#[cfg_attr(not(feature = "worldgen"), allow(unused_mut))]
let mut server = Server::new(
server_settings,
editable_settings,
database_settings,
&server_data_dir,
&|_| {},
Arc::clone(&runtime),
)
.expect("Failed to create server instance!");
let registry = Arc::clone(server.metrics_registry());
let chat = server.chat_cache().clone();
let metrics_shutdown = Arc::new(Notify::new());
let metrics_shutdown_clone = Arc::clone(&metrics_shutdown);
let web_chat_secret = settings.web_chat_secret.clone();
let ui_api_secret = settings.ui_api_secret.clone().unwrap_or_else(|| {
use rand::distributions::{Alphanumeric, DistString};
Alphanumeric.sample_string(&mut rand::thread_rng(), 32)
});
let (web_ui_request_s, web_ui_request_r) = tokio::sync::mpsc::channel(1000);
runtime.spawn(async move {
web::run(
registry,
chat,
web_chat_secret,
ui_api_secret,
web_ui_request_s,
settings.web_address,
metrics_shutdown_clone.notified(),
)
.await
});
let gameserver_addresses = protocols_and_addresses
.into_iter()
.map(|protocol| match protocol {
Protocol::Tcp { address } => ("TCP", address),
Protocol::Quic {
address,
cert_file_path: _,
key_file_path: _,
} => ("QUIC", address),
});
info!(
?web_port,
?gameserver_addresses,
"Server is ready to accept connections."
);
#[cfg(feature = "worldgen")]
if let Some(bench) = bench {
server.create_centered_persister(bench.view_distance);
}
server_loop(
server,
bench,
settings,
tui,
web_ui_request_r,
shutdown_signal,
)?;
metrics_shutdown.notify_one();
Ok(())
}
fn server_loop(
mut server: Server,
bench: Option<BenchParams>,
settings: Settings,
tui: Option<Tui>,
mut web_ui_request_r: tokio::sync::mpsc::Receiver<(
Message,
tokio::sync::oneshot::Sender<MessageReturn>,
)>,
shutdown_signal: Arc<AtomicBool>,
) -> io::Result<()> {
let mut clock = Clock::new(Duration::from_secs_f64(1.0 / TPS as f64));
let mut shutdown_coordinator = ShutdownCoordinator::new(Arc::clone(&shutdown_signal));
let mut bench_exit_time = None;
let mut tick_no = 0u64;
'outer: loop {
span!(guard, "work");
if let Some(bench) = bench {
if let Some(t) = bench_exit_time {
if Instant::now() > t {
break;
}
} else if tick_no != 0 && !server.chunks_pending() {
println!("Chunk loading complete");
bench_exit_time = Some(Instant::now() + Duration::from_secs(bench.duration.into()));
}
};
tick_no += 1;
if shutdown_coordinator.check(&mut server, &settings) {
break;
}
let events = server
.tick(Input::default(), clock.dt())
.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),
}
}
server.cleanup();
if tick_no.rem_euclid(1000) == 0 {
trace!(?tick_no, "keepalive")
}
let mut handle_msg = |msg, response: tokio::sync::oneshot::Sender<MessageReturn>| {
use specs::{Join, WorldExt};
match msg {
Message::Shutdown {
command: Shutdown::Cancel,
} => shutdown_coordinator.abort_shutdown(&mut server),
Message::Shutdown {
command: Shutdown::Graceful { seconds, reason },
} => {
shutdown_coordinator.initiate_shutdown(
&mut server,
Duration::from_secs(seconds),
reason,
);
},
Message::Shutdown {
command: Shutdown::Immediate,
} => {
return true;
},
Message::Shared(SharedCommand::Admin {
command: Admin::Add { username, role },
}) => {
server.add_admin(&username, role);
},
Message::Shared(SharedCommand::Admin {
command: Admin::Remove { username },
}) => {
server.remove_admin(&username);
},
#[cfg(feature = "worldgen")]
Message::LoadArea { view_distance } => {
server.create_centered_persister(view_distance);
},
Message::SqlLogMode { mode } => {
server.set_sql_log_mode(mode);
},
Message::DisconnectAllClients => {
server.disconnect_all_clients();
},
Message::ListPlayers => {
let players: Vec<String> = server
.state()
.ecs()
.read_storage::<Player>()
.join()
.map(|p| p.alias.clone())
.collect();
let _ = response.send(MessageReturn::Players(players));
},
Message::ListLogs => {
let log = LOG.inner.lock().unwrap();
let lines: Vec<_> = log
.lines
.iter()
.rev()
.take(30)
.map(|l| l.to_string())
.collect();
let _ = response.send(MessageReturn::Logs(lines));
},
Message::SendGlobalMsg { msg } => {
use server::state_ext::StateExt;
let msg = ChatType::Meta.into_plain_msg(msg);
server.state().send_chat(msg);
},
}
false
};
if let Some(tui) = tui.as_ref() {
while let Ok(msg) = tui.msg_r.try_recv() {
let (sender, mut recv) = tokio::sync::oneshot::channel();
if handle_msg(msg, sender) {
info!("Closing the server");
break 'outer;
}
if let Ok(msg_answ) = recv.try_recv() {
match msg_answ {
MessageReturn::Players(players) => info!("Players: {:?}", players),
MessageReturn::Logs(_) => info!("skipp sending logs to tui"),
};
}
}
}
while let Ok((msg, sender)) = web_ui_request_r.try_recv() {
if handle_msg(msg, sender) {
info!("Closing the server");
break 'outer;
}
}
drop(guard);
clock.tick();
#[cfg(feature = "tracy")]
common_base::tracy_client::frame_mark();
}
Ok(())
}