Skip to main content

NetworkConnection

Trait NetworkConnection 

Source
pub trait NetworkConnection: Send + Sync {
    // Required methods
    fn compression(&self) -> Option<CompressionInfo>;
    fn send_encoded(&self, packet: EncodedPacket);
    fn send_encoded_bundle(&self, packets: Vec<EncodedPacket>);
    fn disconnect_with_reason(&self, reason: TextComponent);
    fn tick(&self);
    fn latency(&self) -> i32;
    fn close(&self);
    fn closed(&self) -> bool;
}
Expand description

An object-safe trait for player connections.

This abstracts the connection layer so that:

  • JavaConnection can handle real network traffic
  • Test connections (like FlintConnection) can record events for assertions

§Object Safety

This trait uses type erasure for packet sending - packets must be pre-encoded into EncodedPacket before being sent. The Player struct provides a generic send_packet<P: ClientPacket>() helper that handles encoding.

Required Methods§

Source

fn compression(&self) -> Option<CompressionInfo>

Returns compression info for packet encoding.

Returns None if compression is disabled (e.g., for test connections).

Source

fn send_encoded(&self, packet: EncodedPacket)

Sends a pre-encoded packet.

This is the object-safe method that accepts already-encoded packets. Use Player::send_packet() for the generic version that handles encoding.

Source

fn send_encoded_bundle(&self, packets: Vec<EncodedPacket>)

Sends multiple pre-encoded packets as an atomic bundle.

The implementation wraps the packets with bundle delimiter packets so the client processes them together in a single game tick. Use Player::send_bundle() for the generic version that handles encoding.

Source

fn disconnect_with_reason(&self, reason: TextComponent)

Disconnects the player with a reason.

Source

fn tick(&self)

Performs per-tick connection maintenance (e.g., keep-alive).

Source

fn latency(&self) -> i32

Returns the current latency in milliseconds.

Source

fn close(&self)

Closes the connection.

Source

fn closed(&self) -> bool

Returns whether the connection is closed.

Trait Implementations§

Source§

impl From<Box<dyn NetworkConnection>> for PlayerConnection

Source§

fn from(v: Box<dyn NetworkConnection>) -> PlayerConnection

Converts to this type from the input type.
Source§

impl NetworkConnection for Box<dyn NetworkConnection>

Source§

fn compression(&self) -> Option<CompressionInfo>

Returns compression info for packet encoding. Read more
Source§

fn send_encoded(&self, packet: EncodedPacket)

Sends a pre-encoded packet. Read more
Source§

fn send_encoded_bundle(&self, packets: Vec<EncodedPacket>)

Sends multiple pre-encoded packets as an atomic bundle. Read more
Source§

fn disconnect_with_reason(&self, reason: TextComponent)

Disconnects the player with a reason.
Source§

fn tick(&self)

Performs per-tick connection maintenance (e.g., keep-alive).
Source§

fn latency(&self) -> i32

Returns the current latency in milliseconds.
Source§

fn close(&self)

Closes the connection.
Source§

fn closed(&self) -> bool

Returns whether the connection is closed.
Source§

impl TryInto<Box<dyn NetworkConnection>> for PlayerConnection

Source§

type Error = &'static str

The type returned in the event of a conversion error.
Source§

fn try_into( self, ) -> Result<Box<dyn NetworkConnection>, <Self as TryInto<Box<dyn NetworkConnection>>>::Error>

Performs the conversion.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl NetworkConnection for Box<dyn NetworkConnection>

Source§

fn compression(&self) -> Option<CompressionInfo>

Source§

fn send_encoded(&self, packet: EncodedPacket)

Source§

fn send_encoded_bundle(&self, packets: Vec<EncodedPacket>)

Source§

fn disconnect_with_reason(&self, reason: TextComponent)

Source§

fn tick(&self)

Source§

fn latency(&self) -> i32

Source§

fn close(&self)

Source§

fn closed(&self) -> bool

Implementors§