use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
use ghast_auth::connect::{Connector, TargetAddr};
use ghast_auth::offline::OfflineProfile;
use ghast_protocol::{FrameDecoder, FrameEncoder, PacketFilter, RawPacket, State};
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use tokio::time::{Instant, MissedTickBehavior};
use crate::common::{
ClientSettings, ConfigInbound, ConfigOutbound, Inbound, KnownPack, LoginInbound, chat_to_plain,
};
use crate::engine::{Behavior, CoreOptions, Engine};
use crate::error::{ClientError, SessionEnd};
use crate::interest::Interests;
use crate::sim::{Sim, SimError};
#[cfg(feature = "v1_8")]
use crate::version::V1_8;
#[cfg(feature = "v1_12_2")]
use crate::version::V1_12_2;
#[cfg(feature = "v1_16_5")]
use crate::version::V1_16_5;
#[cfg(feature = "v26_1")]
use crate::version::V26_1;
use crate::version::{ClientVersion, GameVersion, HasSim};
pub const TICK: Duration = Duration::from_millis(50);
const READ_CHUNK: usize = 16 * 1024;
#[derive(Clone)]
#[non_exhaustive]
pub enum Account {
Offline(OfflineProfile),
#[cfg(feature = "microsoft")]
Microsoft {
session: ghast_auth::microsoft::MinecraftSession,
auth: Arc<ghast_auth::microsoft::MicrosoftAuth>,
},
}
impl Account {
pub fn offline(name: &str) -> Result<Self, ClientError> {
Ok(Account::Offline(OfflineProfile::new(name)?))
}
#[must_use]
pub fn name(&self) -> &str {
match self {
Account::Offline(p) => p.name(),
#[cfg(feature = "microsoft")]
Account::Microsoft { session, .. } => &session.profile.name,
}
}
#[must_use]
pub fn uuid(&self) -> uuid::Uuid {
match self {
Account::Offline(p) => p.uuid(),
#[cfg(feature = "microsoft")]
Account::Microsoft { session, .. } => session.profile.id,
}
}
}
impl std::fmt::Debug for Account {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Account::Offline(p) => f.debug_tuple("Offline").field(&p.name()).finish(),
#[cfg(feature = "microsoft")]
Account::Microsoft { session, .. } => f
.debug_struct("Microsoft")
.field("name", &session.profile.name)
.finish_non_exhaustive(),
}
}
}
#[derive(Debug, Clone)]
pub struct Client {
address: TargetAddr,
version: GameVersion,
account: Account,
connector: Connector,
settings: ClientSettings,
brand: Option<String>,
sensitivity: f32,
silent_hold: Duration,
read_timeout: Duration,
login_timeout: Duration,
record: Option<std::path::PathBuf>,
force_step_position: crate::sim::ForceStepPosition,
hold_turn_on_action: bool,
world_config: Option<ghast_world::WorldConfig>,
accept_required_packs: bool,
}
impl Client {
#[must_use]
pub fn new(address: TargetAddr, version: GameVersion, account: Account) -> Self {
Client {
address,
version,
account,
connector: Connector::new(),
settings: ClientSettings::default(),
brand: Some("vanilla".to_owned()),
sensitivity: 0.5,
silent_hold: Duration::from_millis(1500),
read_timeout: Duration::from_secs(30),
login_timeout: Duration::from_secs(30),
record: None,
force_step_position: crate::sim::ForceStepPosition::Off,
hold_turn_on_action: true,
world_config: None,
accept_required_packs: true,
}
}
#[must_use]
pub fn connector(mut self, connector: Connector) -> Self {
self.connector = connector;
self
}
#[must_use]
pub fn settings(mut self, settings: ClientSettings) -> Self {
self.settings = settings;
self
}
#[must_use]
pub fn brand(mut self, brand: Option<String>) -> Self {
self.brand = brand;
self
}
#[must_use]
pub fn sensitivity(mut self, sensitivity: f32) -> Self {
self.sensitivity = sensitivity;
self
}
#[must_use]
pub fn silent_hold(mut self, d: Duration) -> Self {
self.silent_hold = d;
self
}
#[must_use]
pub fn read_timeout(mut self, d: Duration) -> Self {
self.read_timeout = d;
self
}
#[must_use]
pub fn login_timeout(mut self, d: Duration) -> Self {
self.login_timeout = d;
self
}
#[must_use]
pub fn record(mut self, path: impl Into<std::path::PathBuf>) -> Self {
self.record = Some(path.into());
self
}
#[must_use]
pub fn world_config(mut self, config: ghast_world::WorldConfig) -> Self {
self.world_config = Some(config);
self
}
#[must_use]
pub fn hold_turn_on_action(mut self, on: bool) -> Self {
self.hold_turn_on_action = on;
self
}
#[must_use]
pub fn force_step_position(mut self, on: bool) -> Self {
self.force_step_position = if on {
crate::sim::ForceStepPosition::Always
} else {
crate::sim::ForceStepPosition::Off
};
self
}
#[must_use]
pub fn force_step_position_mode(mut self, mode: crate::sim::ForceStepPosition) -> Self {
self.force_step_position = mode;
self
}
#[must_use]
pub fn accept_required_resource_packs(mut self, on: bool) -> Self {
self.accept_required_packs = on;
self
}
#[must_use]
pub fn address(&self) -> &TargetAddr {
&self.address
}
#[must_use]
pub fn version(&self) -> GameVersion {
self.version
}
#[must_use]
pub fn account(&self) -> &Account {
&self.account
}
pub async fn run<B: Behavior>(&self, behavior: &mut B) -> Result<SessionEnd, ClientError> {
self.run_until(behavior, std::future::pending()).await
}
pub async fn run_until<B, S>(
&self,
behavior: &mut B,
shutdown: S,
) -> Result<SessionEnd, ClientError>
where
B: Behavior,
S: Future<Output = ()>,
{
let shutdown = std::pin::pin!(shutdown);
match self.version {
#[cfg(feature = "v1_8")]
GameVersion::V1_8_9 => session::<V1_8, B, S>(self, behavior, shutdown).await,
#[cfg(feature = "v1_12_2")]
GameVersion::V1_12_2 => session::<V1_12_2, B, S>(self, behavior, shutdown).await,
#[cfg(feature = "v1_16_5")]
GameVersion::V1_16_5 => session::<V1_16_5, B, S>(self, behavior, shutdown).await,
#[cfg(feature = "v26_1")]
GameVersion::V26_1 => session::<V26_1, B, S>(self, behavior, shutdown).await,
#[allow(unreachable_patterns)]
v => {
let _ = (behavior, shutdown);
Err(ClientError::VersionNotEnabled(v))
}
}
}
}
async fn read_more<R: AsyncRead + Unpin>(
rd: &mut R,
dec: &mut FrameDecoder,
) -> std::io::Result<usize> {
let want = dec.needed().unwrap_or(0).max(READ_CHUNK);
let buf = dec.buffer_mut();
buf.reserve(want);
let old = buf.len();
let n = rd.read_buf(buf).await?;
dec.commit(old);
Ok(n)
}
async fn flush<W: AsyncWrite + Unpin>(wr: &mut W, enc: &mut FrameEncoder) -> std::io::Result<()> {
let out = enc.out_mut();
if !out.is_empty() {
wr.write_all_buf(out).await?;
}
Ok(())
}
async fn session<V, B, S>(
client: &Client,
behavior: &mut B,
mut shutdown: Pin<&mut S>,
) -> Result<SessionEnd, ClientError>
where
V: ClientVersion + HasSim,
B: Behavior,
S: Future<Output = ()>,
{
let name: Arc<str> = client.account.name().into();
let mut dec = FrameDecoder::new();
let mut enc = FrameEncoder::new();
let mut wire = V::Wire::default();
let mut registries = Vec::new();
let login = async {
let stream = client.connector.connect(&client.address).await?;
let (mut rd, mut wr) = stream.into_split();
login::<V, _, _>(
client,
&mut rd,
&mut wr,
&mut dec,
&mut enc,
&mut wire,
&mut registries,
)
.await?;
Ok::<_, ClientError>((rd, wr))
};
let (rd, wr) = tokio::select! {
r = tokio::time::timeout(client.login_timeout, login) => match r {
Ok(r) => r?,
Err(_) => return Err(ClientError::Timeout("connect and login")),
},
() = shutdown.as_mut() => return Ok(SessionEnd::Shutdown),
};
log::info!("[{name}] logged in to {} ({})", client.address, V::GAME);
let opts = CoreOptions {
name,
caps: V::GAME.caps(),
settings: client.settings.clone(),
brand: client.brand.clone(),
sensitivity: client.sensitivity,
silent_hold: client.silent_hold,
inventory: V::INVENTORY,
hold_turn_on_action: client.hold_turn_on_action,
accept_required_packs: client.accept_required_packs,
};
let mut play = Play::<V, _, _> {
rd,
wr,
dec,
enc,
engine: Engine::new(
opts,
V::new_sim(&crate::sim::SimOptions {
record: client.record.clone(),
force_step_position: client.force_step_position,
entities: entity_config(behavior.interests()),
world: client.world_config,
}),
),
read_timeout: client.read_timeout,
interests: Interests::NONE,
raw_ids: Vec::new(),
raw_mask: [0; 4],
world_errors: 0,
wire,
client,
reconfigure: false,
};
for raw in ®istries {
play.engine.sim.registry_data(raw);
}
drop(registries);
V::configure_inventory(play.engine.inventory_mut(), &play.wire);
let result = play.run(behavior, shutdown).await;
let reason = match &result {
Ok(SessionEnd::Kicked { reason, .. }) => format!("kicked: {reason}"),
Ok(SessionEnd::ServerClosed) => "server closed the connection".to_owned(),
Ok(SessionEnd::Quit(r)) => format!("quit: {r}"),
Ok(SessionEnd::Shutdown) => "shutdown".to_owned(),
Err(e) => format!("error: {e}"),
};
play.engine.stop(behavior, &reason);
let stats = play.dec.stats();
log::info!(
"[{}] session ended after {} ticks ({reason}); frames {} skipped {} (compressed {}), inflated {} B",
play.engine.core_name(),
play.engine.tick_count(),
stats.frames,
stats.skipped,
stats.skipped_compressed,
stats.inflated_bytes
);
result
}
async fn login<V, R, W>(
client: &Client,
rd: &mut R,
wr: &mut W,
dec: &mut FrameDecoder,
enc: &mut FrameEncoder,
wire: &mut V::Wire,
registries: &mut Vec<RawPacket>,
) -> Result<(), ClientError>
where
V: ClientVersion,
R: AsyncRead + Unpin,
W: AsyncWrite + Unpin,
{
V::write_handshake(enc, &client.address.host(), client.address.port())?;
V::write_login_start(enc, client.account.name(), client.account.uuid())?;
flush(wr, enc).await?;
loop {
while let Some(raw) = dec.next_packet()? {
let p = V::decode_login(&raw).map_err(|source| ClientError::Decode {
state: State::Login,
id: raw.id,
source,
})?;
match p {
None => {}
Some(LoginInbound::Disconnect(reason)) => {
return Err(ClientError::LoginRefused(chat_to_plain(&reason)));
}
Some(LoginInbound::Compress(threshold)) => {
dec.set_compression(threshold);
enc.set_compression(threshold);
}
Some(LoginInbound::PluginRequest {
message_id,
channel,
}) => {
log::debug!("login plugin request {message_id} on {channel}: not understood");
V::write_plugin_response(enc, message_id)?;
flush(wr, enc).await?;
}
Some(LoginInbound::EncryptionRequest {
server_id,
public_key,
verify_token,
}) => {
let secret =
encryption::<V>(client, enc, &server_id, &public_key, &verify_token)
.await?;
flush(wr, enc).await?;
enc.enable_encryption(&secret);
dec.enable_encryption(&secret);
}
Some(LoginInbound::CookieRequest(key)) => {
V::write_login_cookie_response(enc, &key)?;
flush(wr, enc).await?;
}
Some(LoginInbound::Success { uuid, username }) => {
log::debug!("login success: {username} {uuid:?}");
if V::GAME.caps().configuration_state {
V::write_login_acknowledged(enc)?;
if let Some(brand) = &client.brand {
let b = ConfigOutbound::Brand(brand.clone());
V::write_configuration(enc, &b, wire)?;
}
let settings = ConfigOutbound::Settings(client.settings.clone());
V::write_configuration(enc, &settings, wire)?;
flush(wr, enc).await?;
configure::<V, _, _>(client, rd, wr, dec, enc, wire, registries).await?;
}
return Ok(());
}
}
}
if read_more(rd, dec).await? == 0 {
return Err(ClientError::LoginClosed);
}
}
}
const VANILLA_PACKS: &[&str] = &[
"core",
"minecart_improvements",
"redstone_experiments",
"trade_rebalance",
];
async fn configure<V, R, W>(
client: &Client,
rd: &mut R,
wr: &mut W,
dec: &mut FrameDecoder,
enc: &mut FrameEncoder,
wire: &mut V::Wire,
registries: &mut Vec<RawPacket>,
) -> Result<(), ClientError>
where
V: ClientVersion,
R: AsyncRead + Unpin,
W: AsyncWrite + Unpin,
{
let family = V::GAME.protocol_info().game_versions;
loop {
while let Some(raw) = dec.next_packet()? {
let p = V::decode_configuration(&raw, wire).map_err(|source| ClientError::Decode {
state: State::Configuration,
id: raw.id,
source,
})?;
let Some(p) = p else { continue };
let reply = match p {
ConfigInbound::KeepAlive(id) => vec![ConfigOutbound::KeepAlive(id)],
ConfigInbound::Ping(id) => vec![ConfigOutbound::Pong(id)],
ConfigInbound::Finish => {
V::write_configuration(enc, &ConfigOutbound::Finish, wire)?;
flush(wr, enc).await?;
log::debug!("configuration done ({} registries)", registries.len());
return Ok(());
}
ConfigInbound::Disconnect(reason) => return Err(ClientError::LoginRefused(reason)),
ConfigInbound::SelectKnownPacks(packs) => {
let known: Vec<KnownPack> = packs
.into_iter()
.filter(|k| {
&*k.namespace == "minecraft"
&& VANILLA_PACKS.contains(&&*k.id)
&& family.contains(&&*k.version)
})
.collect();
vec![ConfigOutbound::SelectKnownPacks(known)]
}
ConfigInbound::RegistryData(raw) => {
V::note_registry(&raw, wire);
registries.push(raw);
continue;
}
ConfigInbound::ResourcePackPush { id, required } => {
if required && client.accept_required_packs {
[3, 4, 0]
.into_iter()
.map(|result| ConfigOutbound::ResourcePackStatus { id, result })
.collect()
} else {
vec![ConfigOutbound::ResourcePackStatus { id, result: 1 }]
}
}
ConfigInbound::CookieRequest(key) => vec![ConfigOutbound::CookieResponse { key }],
ConfigInbound::CodeOfConduct(text) => {
log::info!("server code of conduct (accepted): {text}");
vec![ConfigOutbound::AcceptCodeOfConduct]
}
ConfigInbound::Transfer(to) => {
log::warn!("server transfer to {to} is not supported; staying");
continue;
}
};
for o in &reply {
V::write_configuration(enc, o, wire)?;
}
flush(wr, enc).await?;
}
if read_more(rd, dec).await? == 0 {
return Err(ClientError::LoginClosed);
}
}
}
#[cfg_attr(
not(feature = "microsoft"),
allow(clippy::extra_unused_type_parameters)
)]
async fn encryption<V: ClientVersion>(
client: &Client,
enc: &mut FrameEncoder,
server_id: &str,
public_key: &[u8],
verify_token: &[u8],
) -> Result<[u8; 16], ClientError> {
match &client.account {
Account::Offline(_) => {
let _ = (enc, server_id, public_key, verify_token);
Err(ClientError::OnlineModeRequired)
}
#[cfg(feature = "microsoft")]
Account::Microsoft { session, auth } => {
let resp = ghast_auth::crypto::respond_to_encryption_request(
server_id,
public_key,
verify_token,
)?;
auth.join_server(&session.access_token, session.profile.id, &resp.server_hash)
.await?;
V::write_encryption_response(
enc,
&resp.encrypted_shared_secret,
&resp.encrypted_verify_token,
)?;
Ok(*resp.shared_secret.as_bytes())
}
}
}
struct Play<'c, V: ClientVersion + HasSim, R, W> {
rd: R,
wr: W,
dec: FrameDecoder,
enc: FrameEncoder,
engine: Engine<V::Sim>,
read_timeout: Duration,
interests: Interests,
raw_ids: Vec<i32>,
raw_mask: [u64; 4],
world_errors: u64,
wire: V::Wire,
client: &'c Client,
reconfigure: bool,
}
fn entity_config(interests: Interests) -> Option<ghast_entity::Config> {
interests.contains(Interests::ENTITIES).then(|| {
ghast_entity::Config::default()
.with_metadata(true)
.with_equipment(false)
.with_effects(false)
.with_player_list(interests.contains(Interests::PLAYER_LIST))
.with_events(false)
.with_skins(interests.contains(Interests::PLAYER_SKINS))
})
}
enum Wake {
Read(std::io::Result<usize>),
Tick,
Shutdown,
}
impl<V, R, W> Play<'_, V, R, W>
where
V: ClientVersion + HasSim,
R: AsyncRead + Unpin,
W: AsyncWrite + Unpin,
{
fn set_filter(&mut self, interests: Interests, raw: &[i32]) {
self.interests = interests;
raw.clone_into(&mut self.raw_ids);
self.raw_mask = [0; 4];
for &id in raw {
if (0..256).contains(&id) {
self.raw_mask[id as usize >> 6] |= 1 << (id & 63);
}
}
self.engine
.sim
.collect_block_updates(interests.contains(Interests::BLOCK_UPDATES));
self.engine.sim.set_entities(entity_config(interests));
let mut filter: PacketFilter = V::play_filter(interests);
for id in self.engine.sim.entity_packet_ids() {
filter.insert(id);
}
for id in self.engine.sim.world_packet_ids() {
filter.insert(id);
}
for &id in raw {
filter.insert(id);
}
self.dec.set_filter(filter);
}
fn bad_world_packet(&mut self, raw: &RawPacket, e: &ghast_world::WorldError) {
self.world_errors += 1;
let n = self.world_errors;
if n <= 3 || n.is_power_of_two() {
log::warn!(
"[{}] bad world packet 0x{:02x} ({} bytes): {e} ({n} so far)",
self.engine.name(),
raw.id,
raw.body.len()
);
}
if n <= 8
&& let Some(dir) = std::env::var_os("GHAST_DUMP_BAD_CHUNK")
{
let path = std::path::Path::new(&dir).join(format!(
"ghast-bad-world-{}-0x{:02x}-{n}.bin",
std::process::id(),
raw.id
));
match std::fs::write(&path, &raw.body) {
Ok(()) => log::warn!("[{}] wrote {}", self.engine.name(), path.display()),
Err(err) => log::warn!("cannot write {}: {err}", path.display()),
}
}
}
fn encode_outbound(&mut self) -> Result<(), ClientError> {
let tick = self.engine.tick_count();
let out = self.engine.outbound();
let mut res = Ok(());
for p in out.drain(..) {
log::trace!(target: "ghast::packets", "t{tick} -> {p:?}");
if let Err(e) = V::write_play(&mut self.enc, &p, &mut self.wire) {
res = Err(e);
break;
}
}
out.clear();
res.map_err(ClientError::from)
}
fn drain_inbound(&mut self) -> Result<Option<SessionEnd>, ClientError> {
let mut at = None;
while let Some(raw) = self.dec.next_packet()? {
log::trace!(
target: "ghast::packets",
"t{} <- 0x{:02x} ({} B)",
self.engine.tick_count(),
raw.id,
raw.body.len()
);
if (0..256).contains(&raw.id)
&& self.raw_mask[raw.id as usize >> 6] & (1 << (raw.id & 63)) != 0
{
let at = *at.get_or_insert_with(std::time::Instant::now);
self.engine.push_raw(crate::engine::RawInbound {
id: raw.id,
body: raw.body.clone(),
at,
});
}
match self.engine.sim.world_packet(&raw) {
Ok(()) => {}
Err(SimError::Decode(source)) => {
return Err(ClientError::Decode {
state: State::Play,
id: raw.id,
source,
});
}
Err(SimError::World(e)) => {
self.bad_world_packet(&raw, &e);
continue;
}
}
let inv = V::inventory_packet(self.engine.inventory_mut(), &raw).map_err(|source| {
ClientError::Decode {
state: State::Play,
id: raw.id,
source,
}
})?;
if let Some(o) = inv {
self.engine
.outbound()
.push(crate::common::Outbound::Inventory(o));
}
let p = V::decode_play(&raw, &mut self.wire).map_err(|source| ClientError::Decode {
state: State::Play,
id: raw.id,
source,
})?;
V::write_wire_replies(&mut self.enc, &mut self.wire)?;
match p {
None => {}
Some(Inbound::Disconnect(json)) => {
let reason = chat_to_plain(&json);
log::warn!("[{}] kicked: {reason}", self.engine.core_name());
return Ok(Some(SessionEnd::Kicked {
reason,
json: json.to_string(),
}));
}
Some(Inbound::StartConfiguration) => {
self.engine.handle(Inbound::StartConfiguration);
self.reconfigure = true;
return Ok(None);
}
Some(Inbound::SetCompression(t)) => {
self.dec.set_compression(t);
self.encode_outbound()?;
self.enc.set_compression(t);
}
Some(p) => self.engine.handle(p),
}
}
Ok(None)
}
async fn reconfigure_if_asked(&mut self) -> Result<(), ClientError> {
if !self.reconfigure {
return Ok(());
}
self.reconfigure = false;
log::info!("[{}] server asked for reconfiguration", self.engine.name());
self.encode_outbound()?;
V::write_configuration_acknowledged(&mut self.enc)?;
flush(&mut self.wr, &mut self.enc).await?;
let play_filter = *self.dec.filter();
self.dec.set_filter(PacketFilter::all());
let mut registries = Vec::new();
configure::<V, _, _>(
self.client,
&mut self.rd,
&mut self.wr,
&mut self.dec,
&mut self.enc,
&mut self.wire,
&mut registries,
)
.await?;
for raw in ®istries {
self.engine.sim.registry_data(raw);
}
V::configure_inventory(self.engine.inventory_mut(), &self.wire);
self.dec.set_filter(play_filter);
self.engine.note_inbound(Instant::now().into_std());
Ok(())
}
async fn close(&mut self) {
let _ = self.wr.shutdown().await;
let mut buf = [0u8; 16 * 1024];
let drain = async {
loop {
match self.rd.read(&mut buf).await {
Ok(0) | Err(_) => break,
Ok(_) => {}
}
}
};
let _ = tokio::time::timeout(Duration::from_secs(1), drain).await;
}
async fn read_ready(&mut self) -> Option<std::io::Result<usize>> {
let mut fut = std::pin::pin!(read_more(&mut self.rd, &mut self.dec));
std::future::poll_fn(|cx| {
std::task::Poll::Ready(match fut.as_mut().poll(cx) {
std::task::Poll::Ready(r) => Some(r),
std::task::Poll::Pending => None,
})
})
.await
}
async fn run<B: Behavior, S: Future<Output = ()>>(
&mut self,
behavior: &mut B,
mut shutdown: Pin<&mut S>,
) -> Result<SessionEnd, ClientError> {
let interests = self.engine.interests(behavior);
let raw = self.engine.raw_packets(behavior);
self.set_filter(interests, &raw);
self.engine.note_inbound(Instant::now().into_std());
if let Some(end) = self.drain_inbound()? {
return Ok(end);
}
self.reconfigure_if_asked().await?;
self.encode_outbound()?;
flush(&mut self.wr, &mut self.enc).await?;
let mut ticker = tokio::time::interval_at(Instant::now() + TICK, TICK);
ticker.set_missed_tick_behavior(MissedTickBehavior::Skip);
loop {
let wake = tokio::select! {
biased;
() = shutdown.as_mut() => Wake::Shutdown,
_ = ticker.tick() => Wake::Tick,
r = read_more(&mut self.rd, &mut self.dec) => Wake::Read(r),
};
match wake {
Wake::Shutdown => {
self.encode_outbound()?;
flush(&mut self.wr, &mut self.enc).await?;
self.close().await;
return Ok(SessionEnd::Shutdown);
}
Wake::Read(r) => {
if r? == 0 {
return Ok(SessionEnd::ServerClosed);
}
let now = Instant::now().into_std();
self.engine.note_inbound(now);
if let Some(end) = self.drain_inbound()? {
return Ok(end);
}
self.reconfigure_if_asked().await?;
self.encode_outbound()?;
flush(&mut self.wr, &mut self.enc).await?;
}
Wake::Tick => {
for _ in 0..64 {
match self.read_ready().await {
None => break,
Some(r) => {
if r? == 0 {
return Ok(SessionEnd::ServerClosed);
}
self.engine.note_inbound(Instant::now().into_std());
if let Some(end) = self.drain_inbound()? {
return Ok(end);
}
self.reconfigure_if_asked().await?;
}
}
}
let now = Instant::now().into_std();
if let Some(last) = self.engine.last_inbound()
&& now.saturating_duration_since(last) > self.read_timeout
{
return Err(ClientError::Timeout("no data from the server"));
}
self.engine.tick(behavior, now);
self.encode_outbound()?;
flush(&mut self.wr, &mut self.enc).await?;
if let Some(reason) = self.engine.quit_requested() {
let reason = reason.to_owned();
self.close().await;
return Ok(SessionEnd::Quit(reason));
}
let interests = self.engine.interests(behavior);
let raw = self.engine.raw_packets(behavior);
if interests != self.interests || *raw != *self.raw_ids {
self.set_filter(interests, &raw);
}
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn entity_config_follows_the_interests() {
assert_eq!(entity_config(Interests::PLAYER_LIST), None);
let c = entity_config(Interests::ENTITIES).unwrap();
assert!(c.metadata && !c.player_list && !c.skins);
let c =
entity_config(Interests::ENTITIES | Interests::PLAYER_LIST | Interests::PLAYER_SKINS)
.unwrap();
assert!(c.player_list && c.skins);
}
}