pub struct ChunkSender {
pub pending_chunks: FxHashSet<ChunkPos>,
sent_chunks: FxHashSet<ChunkPos>,
pacing: ChunkBatchPacing,
}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.
pacing: ChunkBatchPacingImplementations§
Source§impl ChunkSender
impl ChunkSender
Sourcepub(crate) fn clear_world_chunks(&mut self)
pub(crate) fn clear_world_chunks(&mut self)
Clears chunk membership from the previous world while preserving connection pacing.
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.
fn send_packet<P: ClientPacket>(connection: &PlayerConnection, packet: P)
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.
The caller must complete or discard the returned batch before preparing
another one for this sender; the server’s sending pass enforces this.
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.
Sourcefn resolve_valid_chunks(
prepared: &[PreparedChunk],
encoded_chunks: Vec<EncodedChunk>,
pending: &FxHashSet<ChunkPos>,
) -> Vec<EncodedChunk>
fn resolve_valid_chunks( prepared: &[PreparedChunk], encoded_chunks: Vec<EncodedChunk>, pending: &FxHashSet<ChunkPos>, ) -> Vec<EncodedChunk>
Keeps the encoded chunks that are still pending and still match their prepared source.
encoded_chunks is a subsequence of prepared in the same relative order:
Self::encode_batch maps over prepared with an order-preserving parallel
iterator and only drops entries. A single forward cursor over prepared therefore
resolves every encoded chunk in one pass, instead of restarting the scan per chunk.
fn collect_candidates( &mut self, world: &Arc<World>, player_chunk_pos: ChunkPos, max_batch_size: usize, ) -> Vec<PreparedChunk>
fn chunk_distance_squared(pos: ChunkPos, player_chunk_pos: ChunkPos) -> u64
Sourcepub fn on_chunk_batch_received_by_client(
&mut self,
desired_chunks_per_tick: f32,
) -> bool
pub fn on_chunk_batch_received_by_client( &mut self, desired_chunks_per_tick: f32, ) -> bool
Queues accepted client rate feedback for the next prepare boundary.
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
Source§impl Default for ChunkSender
impl Default for ChunkSender
Source§fn default() -> ChunkSender
fn default() -> 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<>>