Connection#

class minecraft.networking.Connection(client)#

Handles sending and recieving packets from the server.

Variables:
  • client (Client) – The client that this connection is attached to.

  • host (str) – The host that the connection is connected to.

  • port (int) – The port that the connection is on.

  • state (State) – The current state of the connection.

  • closed (bool) – Whether or not the connection has been closed.

  • loop (asyncio.AbstractEventLoop) – The event loop that the connection is running on.

  • reader (asyncio.StreamReader) – The reader that the connection is using to read packets.

  • writer (asyncio.StreamWriter) – The writer that the connection is using to write packets.

  • outgoing_packets (asyncio.Queue) – The queue of packets that are waiting to be sent.

  • dispatcher (Dispatcher) – The dispatcher that is handling incoming packets.

  • using_compression (bool) – Whether or not the connection is using compression.

  • use_encryption (bool) – Whether or not the connection is using encryption.

  • shared_secret (bytes) – The shared secret that is being used for encryption.

  • cipher (cryptography.hazmat.primitives.ciphers.Cipher) – The cipher that is being used for encryption.

  • reactor (Reactor) – The reactor that is currently handling packets.

async close(*, error: Exception = None)#

Closes the connection.

Parameters:

error (Exception) – An error to raise after the connection is closed.

compress(data: bytes) tuple[bytes, bool]#

Compresses the given data, if necessary.

Params data:

The data to compress.

Returns:

The compressed data, as well as a boolean indicating whether or not the data was actually compressed.

Return type:

tuple[bytes, bool]

async connect(host: str, port: int) None#

Connects to the given host and port.

Params host:

The host to connect to.

Params port:

The port to connect to.

decompress(data: bytes) bytes#

Decompresses the given data, if compression is enabled.

Params data:

The data to decompress.

Returns:

The decompressed data.

Return type:

bytes

decrypt(data: bytes) bytes#

Decrypts the given data using the connection’s cipher.

Params data:

The data to decrypt.

Returns:

The decrypted data.

Return type:

bytes

encrypt(data: bytes) bytes#

Encrypts the given data using the connection’s cipher.

Params data:

The data to encrypt.

Returns:

The encrypted data.

Return type:

bytes

async login()#

Begins the login flow.

Raises:
  • RuntimeError – The connection is not in the handshake state.

  • RuntimeError – The client does not have an access token set.

async send_packet(packet: PACKET, *, immediate: bool = False) None#

Sends the given packet to the server.

Note

Unless immediate is True, this does not wait for the packet to be sent. It instead adds the packet to a queue. Keep in mind that immediate will skip the queue and write the packet directly.

If you want to ensure the packet is sent, you should consider using wait_for().

Parameters:
  • packet (Packet) – The packet to send.

  • immediate (bool) – Whether to immediately write the packet or not.

async wait_for_state(state: State, *, timeout: float = None) None#

Waits for the connection to change to the given state.

Parameters:
  • state (State) – The state to wait for.

  • timeout (float) – The timeout in seconds.

Raises:

asyncio.TimeoutError – The timeout was reached.