dolor.connection

Contains Connection.

class Connection(bound)[source]

Bases: object

A connection between a client and a server.

Parameters

bound – Where read packets are bound. Either serverbound or clientbound.

bound

Either ServerboundPacket or ClientboundPacket. Used by gen_packet_info() to populate the packet_info attribute.

packet_info

A dictionary whose keys are packet id’s and whose values are subclasses of Packet. Populated by gen_packet_info() and used by read_packet() to determine which id corresponds to which subclass of Packet.

Type

dict

read_lock

The lock to make sure packet reads don’t overlap.

Type

asyncio.Lock

specific_reads

A dictionary whose keys are subclasses of Packet and whose values are AsyncValueHolder.

Used in dispatch_packet() to send packets to specific reads that were made with read_packet().

Type

dict

reader

The reader for receiving packet data.

Type

asyncio.StreamReader

writer

The writer for writing packet data.

Type

asyncio.StreamWriter

comp_threshold

The maximum size of a packet before it’s compressed.

If less than or equal to 0, then compression is disabled.

Type

int

gen_packet_info(state, *, ctx=None)[source]

Generates the packet_info.

Parameters
  • state (State) – Which state the packet info is for.

  • ctx (PacketContext, optional) – Which context the packet info is for.

Returns

The packet info. See packet_info for a more thorough description.

Return type

dict

property ctx

The connection’s PacketContext.

property current_state

The current State of the connection.

property comp_enabled

Whether compression is enabled.

is_closing()[source]

Checks if the connection is closed or being closed.

Returns

Whether the connection is closed or being closed.

Return type

bool

close()[source]

Closes the connection.

Should be used alongside the wait_closed() method.

async wait_closed()[source]

Waits until the connection is closed.

enable_encryption(shared_secret)[source]

Enables encryption for the connection.

Parameters

shared_secret – The shared secret, either gotten from gen_shared_secret() or decrypted from EncryptionResponsePacket.

create_packet(pack_class, **kwargs)[source]

Creates a packet with the connection’s ctx attribute.

Parameters
  • pack_class (subclass of Packet) – The packet to create.

  • **kwargs – The attributes of the packet to set and their corresponding values.

Returns

The created packet.

Return type

Packet

dispatch_packet(packet)[source]

Dispatches a packet to calls of read_packet() which specified read_class.

Parameters

packet (Packet) – The packet to dispatch.

async wait_for_incoming_packet(pack_class)[source]

Waits for an incoming packet read with read_packet().

Parameters

pack_class (subclass of Packet) – The packet to wait for.

Returns

Returns None if the connection is closed, else returns the packet.

Return type

Packet or None

async decompress_packet_data(data)[source]

Decompresses raw packet data.

Parameters

data – The raw packet data.

Returns

The raw decompressed packet data.

Return type

io.BytesIO

Raises

ValueError – If compression is enabled and the data length of the packet is greater than 0 but less than or equal to the comp_threshold attribute.

async read_packet(read_class=None)[source]

Reads a packet.

Parameters

read_class (subclass of Packet, optional) – The packet you want to read. If unspecified, whatever the next packet is will be returned. Requires this method to be called elsewhere with read_class unspecified to work.

Returns

If EOF is reached when reading the packet, then the connection will be closed and None will be returned. Otherwise the read packet will be returned.

Return type

Packet or None

compress_packet_data(data)[source]

Compresses raw packet data.

Parameters

data (bytes) – The raw, uncompressed packet data.

Returns

;class – The raw, potentially compressed packet data.

Return type

bytes

async write_packet(packet, **kwargs)[source]

Writes a packet.

Parameters
  • packet (subclass of Packet or Packet) –

    If a subclass of Packet, then the packet to write will be created by forwarding packet and **kwargs to the create_packet() method. Otherwise, packet is the packet to write.

    packet being a subclass of Packet is preferred so that the packet is created with the correct context for the connection.

  • **kwargs – The packet attributes to set and their corresponding values. Only able to be passed if packet is a subclass of Packet.

Returns

The written packet.

Return type

Packet

Raises

TypeError – If **kwargs is passed but packet isn’t a subclass of Packet.