dolor.connection¶
Contains Connection.
- class Connection(bound)[source]¶
Bases:
objectA connection between a client and a server.
- Parameters
bound – Where read packets are bound. Either
serverboundorclientbound.
- bound¶
Either
ServerboundPacketorClientboundPacket. Used bygen_packet_info()to populate thepacket_infoattribute.
- packet_info¶
A dictionary whose keys are packet id’s and whose values are subclasses of
Packet. Populated bygen_packet_info()and used byread_packet()to determine which id corresponds to which subclass ofPacket.- 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
Packetand whose values areAsyncValueHolder.Used in
dispatch_packet()to send packets to specific reads that were made withread_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_infofor a more thorough description.- Return type
dict
- property ctx¶
The connection’s
PacketContext.
- 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.
- enable_encryption(shared_secret)[source]¶
Enables encryption for the connection.
- Parameters
shared_secret – The shared secret, either gotten from
gen_shared_secret()or decrypted fromEncryptionResponsePacket.
- dispatch_packet(packet)[source]¶
Dispatches a packet to calls of
read_packet()which specifiedread_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().
- 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_thresholdattribute.
- 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 withread_classunspecified to work.- Returns
If EOF is reached when reading the packet, then the connection will be closed and
Nonewill be returned. Otherwise the read packet will be returned.- Return type
PacketorNone
- 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
PacketorPacket) –If a subclass of
Packet, then the packet to write will be created by forwardingpacketand**kwargsto thecreate_packet()method. Otherwise,packetis the packet to write.packetbeing a subclass ofPacketis 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
packetis a subclass ofPacket.
- Returns
The written packet.
- Return type
- Raises
TypeError – If
**kwargsis passed butpacketisn’t a subclass ofPacket.