2020-07-11 14:08:25 +00:00
|
|
|
use crate::error::Error;
|
|
|
|
use common::msg::{ClientMsg, ClientState, RequestStateError, ServerMsg};
|
2019-10-15 04:06:14 +00:00
|
|
|
use hashbrown::HashSet;
|
2020-07-05 23:29:28 +00:00
|
|
|
use network::{Participant, Stream};
|
2019-10-06 17:35:47 +00:00
|
|
|
use specs::{Component, FlaggedStorage};
|
2020-07-06 05:56:02 +00:00
|
|
|
use specs_idvs::IdvStorage;
|
2020-07-11 14:08:25 +00:00
|
|
|
use std::sync::{
|
|
|
|
atomic::{AtomicBool, Ordering},
|
|
|
|
Mutex,
|
|
|
|
};
|
|
|
|
use tracing::debug;
|
2019-10-06 17:35:47 +00:00
|
|
|
use vek::*;
|
2019-03-03 22:02:38 +00:00
|
|
|
|
|
|
|
pub struct Client {
|
2019-04-19 19:32:47 +00:00
|
|
|
pub client_state: ClientState,
|
2020-07-09 07:58:21 +00:00
|
|
|
pub participant: Mutex<Option<Participant>>,
|
2020-07-04 09:52:21 +00:00
|
|
|
pub singleton_stream: Stream,
|
2020-07-11 14:08:25 +00:00
|
|
|
pub network_error: AtomicBool,
|
2019-03-03 22:02:38 +00:00
|
|
|
pub last_ping: f64,
|
2019-12-31 08:10:51 +00:00
|
|
|
pub login_msg_sent: bool,
|
2019-03-03 22:02:38 +00:00
|
|
|
}
|
2019-03-04 19:50:26 +00:00
|
|
|
|
2019-10-15 04:06:14 +00:00
|
|
|
impl Component for Client {
|
2020-07-06 05:56:02 +00:00
|
|
|
type Storage = FlaggedStorage<Self, IdvStorage<Self>>;
|
2019-10-15 04:06:14 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 17:23:27 +00:00
|
|
|
impl Client {
|
2020-07-11 14:08:25 +00:00
|
|
|
pub fn notify(&mut self, msg: ServerMsg) {
|
|
|
|
if !self.network_error.load(Ordering::Relaxed) {
|
|
|
|
if let Err(e) = self.singleton_stream.send(msg) {
|
|
|
|
debug!(?e, "got a network error with client");
|
|
|
|
self.network_error.store(true, Ordering::Relaxed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn recv(&mut self) -> Result<ClientMsg, Error> {
|
|
|
|
if !self.network_error.load(Ordering::Relaxed) {
|
|
|
|
match self.singleton_stream.recv().await {
|
|
|
|
Ok(r) => Ok(r),
|
|
|
|
Err(e) => {
|
|
|
|
debug!(?e, "got a network error with client while recv");
|
|
|
|
self.network_error.store(true, Ordering::Relaxed);
|
|
|
|
Err(Error::StreamErr(e))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(Error::StreamErr(network::StreamError::StreamClosed))
|
|
|
|
}
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2019-10-15 04:06:14 +00:00
|
|
|
pub fn is_registered(&self) -> bool {
|
2020-08-17 09:08:11 +00:00
|
|
|
matches!(
|
|
|
|
self.client_state,
|
|
|
|
ClientState::Registered | ClientState::Spectator | ClientState::Character
|
|
|
|
)
|
2019-10-15 04:06:14 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2019-10-15 04:06:14 +00:00
|
|
|
pub fn is_ingame(&self) -> bool {
|
2020-08-17 09:08:11 +00:00
|
|
|
matches!(
|
|
|
|
self.client_state,
|
|
|
|
ClientState::Spectator | ClientState::Character
|
|
|
|
)
|
2019-10-15 04:06:14 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2019-04-20 17:54:37 +00:00
|
|
|
pub fn allow_state(&mut self, new_state: ClientState) {
|
|
|
|
self.client_state = new_state;
|
2020-07-01 09:51:37 +00:00
|
|
|
let _ = self
|
|
|
|
.singleton_stream
|
|
|
|
.send(ServerMsg::StateAnswer(Ok(new_state)));
|
2019-04-20 17:54:37 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2019-04-20 17:54:37 +00:00
|
|
|
pub fn error_state(&mut self, error: RequestStateError) {
|
2020-07-11 14:08:25 +00:00
|
|
|
let _ = self.notify(ServerMsg::StateAnswer(Err((error, self.client_state))));
|
2019-04-20 17:54:37 +00:00
|
|
|
}
|
2019-04-10 17:23:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-06 17:35:47 +00:00
|
|
|
// Distance from fuzzy_chunk before snapping to current chunk
|
|
|
|
pub const CHUNK_FUZZ: u32 = 2;
|
|
|
|
// Distance out of the range of a region before removing it from subscriptions
|
|
|
|
pub const REGION_FUZZ: u32 = 16;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct RegionSubscription {
|
|
|
|
pub fuzzy_chunk: Vec2<i32>,
|
|
|
|
pub regions: HashSet<Vec2<i32>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for RegionSubscription {
|
2020-07-06 05:56:02 +00:00
|
|
|
type Storage = FlaggedStorage<Self, IdvStorage<Self>>;
|
2019-10-06 17:35:47 +00:00
|
|
|
}
|