pub struct ChunkSender {
pub pending_chunks: FxHashSet<ChunkPos>,
sent_chunks: FxHashSet<ChunkPos>,
pub unacknowledged_batches: u16,
pub desired_chunks_per_tick: f32,
pub batch_quota: f32,
pub max_unacknowledged_batches: u16,
}Expand description
This struct is responsible for sending chunks to the client.
Fields§
§pending_chunks: FxHashSet<ChunkPos>A list of chunks that are waiting to be sent to the client.
sent_chunks: FxHashSet<ChunkPos>Chunks whose initial chunk packet has been queued for this client.
unacknowledged_batches: u16The number of batches that have been sent to the client but have not been acknowledged yet.
desired_chunks_per_tick: f32The number of chunks that should be sent to the client per tick. This is dynamically adjusted based on client feedback.
batch_quota: f32The number of chunks that can be sent to the client in the current batch.
max_unacknowledged_batches: u16The maximum number of unacknowledged batches allowed.
Starts at 1 and increases to MAX_UNACKNOWLEDGED_BATCHES after first ack.
Implementations§
Source§impl ChunkSender
impl ChunkSender
Sourcepub fn mark_chunk_pending_to_send(&mut self, pos: ChunkPos)
pub fn mark_chunk_pending_to_send(&mut self, pos: ChunkPos)
Marks a chunk as pending to be sent to the client.
Sourcepub fn drop_chunk(&mut self, connection: &PlayerConnection, pos: ChunkPos)
pub fn drop_chunk(&mut self, connection: &PlayerConnection, pos: ChunkPos)
Drops a chunk from the client’s view.
Sourcefn send_packet<P: ClientPacket>(connection: &PlayerConnection, packet: P)
fn send_packet<P: ClientPacket>(connection: &PlayerConnection, packet: P)
Encodes and sends a packet through the connection.
Sourcepub fn prepare_batch(
&mut self,
world: &Arc<World>,
player_chunk_pos: ChunkPos,
chunk_send_epoch: &SyncMutex<u32>,
) -> Option<PreparedBatch>
pub fn prepare_batch( &mut self, world: &Arc<World>, player_chunk_pos: ChunkPos, chunk_send_epoch: &SyncMutex<u32>, ) -> Option<PreparedBatch>
Phase 1: Lock briefly to drain pending chunks and snapshot state.
Returns None if there is nothing to send this tick.
Sourcepub fn encode_batch(
batch: &PreparedBatch,
cache: &mut FxHashMap<ChunkPos, EncodedChunk>,
compression: Option<CompressionInfo>,
encoding_pool: &ThreadPool,
) -> Vec<EncodedChunk>
pub fn encode_batch( batch: &PreparedBatch, cache: &mut FxHashMap<ChunkPos, EncodedChunk>, compression: Option<CompressionInfo>, encoding_pool: &ThreadPool, ) -> Vec<EncodedChunk>
Phase 2: Encode chunks without holding any lock. Called between prepare and commit.
Uses the dedicated encoding pool to encode chunks in parallel. A per-tick local cache prevents multiple players sharing the same chunks from re-encoding them within the same sending tick.
§Panics
Panics if a chunk packet fails to encode.
Sourcepub fn commit_batch(
&mut self,
batch: &PreparedBatch,
encoded_chunks: Vec<EncodedChunk>,
connection: &PlayerConnection,
chunk_send_epoch: &SyncMutex<u32>,
) -> Vec<ChunkPos>
pub fn commit_batch( &mut self, batch: &PreparedBatch, encoded_chunks: Vec<EncodedChunk>, connection: &PlayerConnection, chunk_send_epoch: &SyncMutex<u32>, ) -> Vec<ChunkPos>
Phase 3: Lock briefly to verify generation counter and send the batch.
If the player teleported between prepare and commit (generation counter changed), the batch is discarded.
fn collect_candidates( &mut self, world: &Arc<World>, player_chunk_pos: ChunkPos, ) -> Vec<PreparedChunk>
fn chunk_distance_squared(pos: ChunkPos, player_chunk_pos: ChunkPos) -> u64
Sourcepub const fn on_chunk_batch_received_by_client(
&mut self,
desired_chunks_per_tick: f32,
) -> bool
pub const fn on_chunk_batch_received_by_client( &mut self, desired_chunks_per_tick: f32, ) -> bool
Handles the acknowledgement of a chunk batch from the client.
The client sends back its desired chunks per tick based on how fast it can process chunks. We clamp this value and use it to adjust our sending rate.
Sourcepub fn is_chunk_sent(&self, pos: ChunkPos) -> bool
pub fn is_chunk_sent(&self, pos: ChunkPos) -> bool
Returns whether the client has been queued the initial chunk packet.
Sourcepub fn sent_chunks_snapshot(&self) -> FxHashSet<ChunkPos>
pub fn sent_chunks_snapshot(&self) -> FxHashSet<ChunkPos>
Returns a snapshot of all sent chunks for this player.
Trait Implementations§
Source§impl Debug for ChunkSender
impl Debug for ChunkSender
Auto Trait Implementations§
impl Freeze for ChunkSender
impl RefUnwindSafe for ChunkSender
impl Send for ChunkSender
impl Sync for ChunkSender
impl Unpin for ChunkSender
impl UnsafeUnpin for ChunkSender
impl UnwindSafe for ChunkSender
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreArc<SyncMutex<>>