pak.io.connection¶
Contains io.Connection.
- class Connection(*, reader=None, writer=None, ctx)[source]¶
Bases:
ABCA connection between two
Packetsources.This class models a protocol structure that is relatively common, where there is a stream of incoming
Packets that aren’t expected to be any specific type ofPacket.This may not model your protocol structure adequately. This in particular may be the case if you are not able to read and send data asynchronously. In such a case, you should not use this class.
- Parameters:
reader (
asyncio.StreamReaderorNone) – The stream for incoming data.writer (
asyncio.StreamWriterorNone) – The stream for outgoing data.ctx (
Packet.Context) – The context for incoming and outgoingPackets.
- reader¶
The stream for incoming data.
- Type:
asyncio.StreamReaderorNone
- writer¶
The stream for outgoing data.
- Type:
asyncio.StreamWriterorNone
- ctx¶
The context for incoming and outgoing
Packets.This should always be passed to
Packetoperations, such asPacket.unpack()andPacket.pack().- Type:
Examples
A
Connectioncan be used in anasync withstatement, like so:connection = ... async with connection: ...
This will make sure that
connectionis closed by the end of the of theasync withstatement.- is_closing()[source]¶
Gets whether the
Connectionis closed or in the process of closing.- Returns:
Whether the
Connectionis closed or in the process of closing.- Return type:
bool
- close()[source]¶
Closes the
Connection.This method should be used along with the
wait_closed()method.
- async wait_closed()[source]¶
Waits until the
Connectionis closed.
- create_packet(packet_cls, /, **fields)[source]¶
Creates a
Packetfor theConnection.
- async read_data(size)[source]¶
Reads incoming data out of the
readerattribute.- Parameters:
size (
int) – How many bytes to read.- Returns:
The incoming data.
If EOF is reached on the
readerattribute andsizebytes cannot be read, thenNoneis returned.- Return type:
bytesorNone
- abstract async _read_next_packet()[source]¶
Reads the next incoming
Packet.Note
In your implementation, you do not need to ensure that reading is atomic.
See also
- Returns:
The next incoming
Packet.If
None, then that means that there is no nextPacketand thatcontinuously_read_packets()should end. This should be when EOF is reached on thereaderattribute, which will be whenread_data()returnsNone.- Return type:
PacketorNone
- async continuously_read_packets()[source]¶
Continuously reads and yields all incoming
Packets.Note
This must be iterated over for
watch_for_packet()to function.This will continue to yield
Packets until theConnectionis closed or EOF is reached.Warning
This method should not be called twice concurrently.
Doing so may cause data to be read incorrectly.
Examples
connection = ... async for packet in connection.continuously_read_packets(): ...
- async watch_for_packet(packet_cls)[source]¶
Watches for a specific type of
Packetfrom the incoming stream ofPackets.Requires
continuously_read_packets()to be iterated over.- Parameters:
packet_cls (subclass of
Packet) – The type ofPacketto watch for.- Returns:
The specified incoming
Packet.Returns
Nonewhen theConnectionis closed or EOF is reached.- Return type:
PacketorNone
- is_watching_for_packet(packet_cls)[source]¶
Gets whether a specific type of
Packetis being watched for.See also
- async write_data(data)[source]¶
Writes outgoing data to the
writerattribute.- Parameters:
data (
bytes) – The data to write.
- async write_packet(packet_cls, /, **fields)[source]¶
Writes an outgoing
Packet.This method uses
create_packet()to create thePacketto write. It then passes it towrite_packet_instance().If you have an already created
Packetyou wish to write, then you should usewrite_packet_instance().
- abstract async write_packet_instance(packet)[source]¶
Writes an outgoing
Packetinstance.Warning
In most cases, the
write_packet()method should be used instead. This method should only be used if you have a pre-existingPacketinstance.Note
In your implementation, writes should be atomic.
It is thus recommended to only write data in one fell swoop.
See also