2019-03-03 22:02:38 +00:00
|
|
|
#![feature(drain_filter)]
|
2019-01-02 17:23:31 +00:00
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
pub mod client;
|
|
|
|
pub mod error;
|
|
|
|
pub mod input;
|
|
|
|
|
|
|
|
// Reexports
|
|
|
|
pub use crate::{
|
|
|
|
error::Error,
|
|
|
|
input::Input,
|
|
|
|
};
|
|
|
|
|
|
|
|
use std::{
|
|
|
|
time::Duration,
|
|
|
|
net::SocketAddr,
|
|
|
|
};
|
2019-03-04 19:50:26 +00:00
|
|
|
use specs::{
|
|
|
|
Entity as EcsEntity,
|
|
|
|
world::EntityBuilder as EcsEntityBuilder,
|
|
|
|
Builder,
|
|
|
|
join::Join,
|
|
|
|
saveload::MarkedBuilder,
|
|
|
|
};
|
|
|
|
use vek::*;
|
2019-03-03 22:02:38 +00:00
|
|
|
use common::{
|
2019-03-04 19:50:26 +00:00
|
|
|
comp,
|
2019-03-03 22:02:38 +00:00
|
|
|
state::State,
|
|
|
|
net::PostOffice,
|
|
|
|
msg::{ServerMsg, ClientMsg},
|
|
|
|
};
|
2019-01-15 15:13:11 +00:00
|
|
|
use world::World;
|
2019-03-04 19:50:26 +00:00
|
|
|
use crate::client::{
|
|
|
|
Client,
|
|
|
|
Clients,
|
|
|
|
};
|
2019-01-02 17:23:31 +00:00
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
const CLIENT_TIMEOUT: f64 = 5.0; // Seconds
|
2019-01-02 17:23:31 +00:00
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
pub enum Event {
|
|
|
|
ClientConnected {
|
|
|
|
ecs_entity: EcsEntity,
|
|
|
|
},
|
|
|
|
ClientDisconnected {
|
|
|
|
ecs_entity: EcsEntity,
|
|
|
|
},
|
|
|
|
Chat {
|
|
|
|
ecs_entity: EcsEntity,
|
|
|
|
msg: String,
|
|
|
|
},
|
2019-01-02 17:23:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Server {
|
2019-01-02 19:22:01 +00:00
|
|
|
state: State,
|
2019-01-15 15:13:11 +00:00
|
|
|
world: World,
|
2019-01-02 17:23:31 +00:00
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
postoffice: PostOffice<ServerMsg, ClientMsg>,
|
2019-03-04 19:50:26 +00:00
|
|
|
clients: Clients,
|
2019-01-02 17:23:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Server {
|
2019-01-12 15:57:19 +00:00
|
|
|
/// Create a new `Server`.
|
2019-03-03 22:02:38 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn new() -> Result<Self, Error> {
|
2019-03-04 19:50:26 +00:00
|
|
|
let mut state = State::new();
|
|
|
|
|
|
|
|
state.ecs_world_mut().add_resource(comp::UidAllocator::new());
|
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
Ok(Self {
|
2019-03-04 19:50:26 +00:00
|
|
|
state,
|
2019-01-15 15:13:11 +00:00
|
|
|
world: World::new(),
|
2019-03-03 22:02:38 +00:00
|
|
|
|
|
|
|
postoffice: PostOffice::new(SocketAddr::from(([0; 4], 59003)))?,
|
2019-03-04 19:50:26 +00:00
|
|
|
clients: Clients::empty(),
|
2019-03-03 22:02:38 +00:00
|
|
|
})
|
2019-01-02 17:23:31 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 15:13:11 +00:00
|
|
|
/// Get a reference to the server's game state.
|
2019-03-03 22:02:38 +00:00
|
|
|
#[allow(dead_code)]
|
2019-01-12 15:57:19 +00:00
|
|
|
pub fn state(&self) -> &State { &self.state }
|
2019-01-15 15:13:11 +00:00
|
|
|
/// Get a mutable reference to the server's game state.
|
2019-03-03 22:02:38 +00:00
|
|
|
#[allow(dead_code)]
|
2019-01-12 15:57:19 +00:00
|
|
|
pub fn state_mut(&mut self) -> &mut State { &mut self.state }
|
|
|
|
|
2019-03-04 19:50:26 +00:00
|
|
|
/// Build a new entity with a generated UID
|
|
|
|
pub fn build_entity(&mut self) -> EcsEntityBuilder {
|
|
|
|
self.state.ecs_world_mut().create_entity()
|
|
|
|
.marked::<comp::Uid>()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Build a new player with a generated UID
|
|
|
|
pub fn build_player(&mut self) -> EcsEntityBuilder {
|
|
|
|
self.build_entity()
|
|
|
|
.with(comp::phys::Pos(Vec3::zero()))
|
|
|
|
.with(comp::phys::Vel(Vec3::zero()))
|
|
|
|
.with(comp::phys::Dir(Vec3::unit_y()))
|
|
|
|
}
|
|
|
|
|
2019-01-15 15:13:11 +00:00
|
|
|
/// Get a reference to the server's world.
|
2019-03-03 22:02:38 +00:00
|
|
|
#[allow(dead_code)]
|
2019-01-15 15:13:11 +00:00
|
|
|
pub fn world(&self) -> &World { &self.world }
|
|
|
|
/// Get a mutable reference to the server's world.
|
2019-03-03 22:02:38 +00:00
|
|
|
#[allow(dead_code)]
|
2019-01-15 15:13:11 +00:00
|
|
|
pub fn world_mut(&mut self) -> &mut World { &mut self.world }
|
|
|
|
|
2019-01-02 17:23:31 +00:00
|
|
|
/// Execute a single server tick, handle input and update the game state by the given duration
|
2019-03-03 22:02:38 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn tick(&mut self, input: Input, dt: Duration) -> Result<Vec<Event>, Error> {
|
2019-01-02 17:23:31 +00:00
|
|
|
// This tick function is the centre of the Veloren universe. Most server-side things are
|
|
|
|
// managed from here, and as such it's important that it stays organised. Please consult
|
|
|
|
// the core developers before making significant changes to this code. Here is the
|
|
|
|
// approximate order of things. Please update it as this code changes.
|
|
|
|
//
|
|
|
|
// 1) Collect input from the frontend, apply input effects to the state of the game
|
|
|
|
// 2) Go through any events (timer-driven or otherwise) that need handling and apply them
|
|
|
|
// to the state of the game
|
|
|
|
// 3) Go through all incoming client network communications, apply them to the game state
|
|
|
|
// 4) Perform a single LocalState tick (i.e: update the world and entities in the world)
|
|
|
|
// 5) Go through the terrain update queue and apply all changes to the terrain
|
|
|
|
// 6) Send relevant state updates to all clients
|
|
|
|
// 7) Finish the tick, passing control of the main thread back to the frontend
|
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
// Build up a list of events for this frame, to be passed to the frontend
|
|
|
|
let mut frontend_events = Vec::new();
|
|
|
|
|
|
|
|
// If networking has problems, handle them
|
|
|
|
if let Some(err) = self.postoffice.status() {
|
|
|
|
return Err(err.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle new client connections (step 2)
|
|
|
|
frontend_events.append(&mut self.handle_new_connections()?);
|
|
|
|
|
|
|
|
// Handle new messages from clients
|
|
|
|
frontend_events.append(&mut self.handle_new_messages()?);
|
|
|
|
|
2019-01-02 17:23:31 +00:00
|
|
|
// Tick the client's LocalState (step 3)
|
|
|
|
self.state.tick(dt);
|
|
|
|
|
2019-03-04 19:50:26 +00:00
|
|
|
// Synchronise clients with the new state of the world
|
|
|
|
self.sync_clients();
|
|
|
|
|
2019-01-02 17:23:31 +00:00
|
|
|
// Finish the tick, pass control back to the frontend (step 6)
|
2019-03-03 22:02:38 +00:00
|
|
|
Ok(frontend_events)
|
2019-01-02 17:23:31 +00:00
|
|
|
}
|
2019-01-30 12:11:34 +00:00
|
|
|
|
|
|
|
/// Clean up the server after a tick
|
2019-03-03 22:02:38 +00:00
|
|
|
#[allow(dead_code)]
|
2019-01-30 12:11:34 +00:00
|
|
|
pub fn cleanup(&mut self) {
|
|
|
|
// Cleanup the local state
|
|
|
|
self.state.cleanup();
|
|
|
|
}
|
2019-03-03 22:02:38 +00:00
|
|
|
|
|
|
|
/// Handle new client connections
|
|
|
|
fn handle_new_connections(&mut self) -> Result<Vec<Event>, Error> {
|
|
|
|
let mut frontend_events = Vec::new();
|
|
|
|
|
|
|
|
for postbox in self.postoffice.new_connections() {
|
2019-03-04 19:50:26 +00:00
|
|
|
let ecs_entity = self.build_player()
|
|
|
|
.build();
|
2019-03-03 22:02:38 +00:00
|
|
|
|
|
|
|
frontend_events.push(Event::ClientConnected {
|
|
|
|
ecs_entity,
|
|
|
|
});
|
|
|
|
|
2019-03-04 19:50:26 +00:00
|
|
|
self.clients.add(Client {
|
2019-03-03 22:02:38 +00:00
|
|
|
ecs_entity,
|
|
|
|
postbox,
|
|
|
|
last_ping: self.state.get_time(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(frontend_events)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle new client messages
|
|
|
|
fn handle_new_messages(&mut self) -> Result<Vec<Event>, Error> {
|
|
|
|
let mut frontend_events = Vec::new();
|
|
|
|
|
|
|
|
let state = &mut self.state;
|
|
|
|
let mut new_chat_msgs = Vec::new();
|
|
|
|
|
2019-03-04 19:50:26 +00:00
|
|
|
self.clients.remove_if(|client| {
|
2019-03-03 22:02:38 +00:00
|
|
|
let mut disconnected = false;
|
|
|
|
let new_msgs = client.postbox.new_messages();
|
|
|
|
|
|
|
|
// Update client ping
|
|
|
|
if new_msgs.len() > 0 {
|
|
|
|
client.last_ping = state.get_time();
|
|
|
|
|
|
|
|
// Process incoming messages
|
|
|
|
for msg in new_msgs {
|
|
|
|
match msg {
|
|
|
|
ClientMsg::Chat(msg) => new_chat_msgs.push((client.ecs_entity, msg)),
|
|
|
|
ClientMsg::Disconnect => disconnected = true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if
|
2019-03-04 19:50:26 +00:00
|
|
|
state.get_time() - client.last_ping > CLIENT_TIMEOUT || // Timeout
|
|
|
|
client.postbox.status().is_some() // Postbox eror
|
2019-03-03 22:02:38 +00:00
|
|
|
{
|
|
|
|
disconnected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if disconnected {
|
|
|
|
state.delete_entity(client.ecs_entity);
|
|
|
|
frontend_events.push(Event::ClientDisconnected {
|
|
|
|
ecs_entity: client.ecs_entity,
|
|
|
|
});
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Handle new chat messages
|
|
|
|
for (ecs_entity, msg) in new_chat_msgs {
|
2019-03-04 19:50:26 +00:00
|
|
|
self.clients.notify_all(ServerMsg::Chat(msg.clone()));
|
2019-03-03 22:02:38 +00:00
|
|
|
|
|
|
|
frontend_events.push(Event::Chat {
|
|
|
|
ecs_entity,
|
|
|
|
msg,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(frontend_events)
|
|
|
|
}
|
2019-03-04 19:50:26 +00:00
|
|
|
|
|
|
|
/// Sync client states with the most up to date information
|
|
|
|
fn sync_clients(&mut self) {
|
|
|
|
for (&uid, &pos, &vel, &dir) in (
|
|
|
|
&self.state.ecs_world().read_storage::<comp::Uid>(),
|
|
|
|
&self.state.ecs_world().read_storage::<comp::phys::Pos>(),
|
|
|
|
&self.state.ecs_world().read_storage::<comp::phys::Vel>(),
|
|
|
|
&self.state.ecs_world().read_storage::<comp::phys::Dir>(),
|
|
|
|
).join() {
|
|
|
|
self.clients.notify_all(ServerMsg::EntityPhysics {
|
|
|
|
uid: uid.into(),
|
|
|
|
pos,
|
|
|
|
vel,
|
|
|
|
dir,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-01-02 17:23:31 +00:00
|
|
|
}
|