Skip to content

Protocol

This module manages the low-level communication with the server and provides functions to send and receive messages.

pyown.protocol.OWNProtocol

OWNProtocol(
    on_connection_start: Future[Transport],
    on_connection_end: Future[Exception | None],
)

Bases: Protocol

Parameters:

Name Type Description Default
on_connection_start Future

The future to set when the connection starts.

required
on_connection_end Future

The future to set when the connection ends.

required

connection_made

connection_made(transport: Transport)

Called by the transport class when the socket is connected to the server.

connection_lost

connection_lost(exc: Exception | None)

Called by the transport class when the connection is lost or closed.

data_received

data_received(raw: bytes)

Called by the transport class when a packet with data is received.

The data argument is a bytes object containing the incoming data. It tries to parse the data, and if a valid message is found, it is added to the message queue.

Parameters:

Name Type Description Default
raw bytes

The incoming data

required

pause_writing

pause_writing()

Called when the transport's buffer goes over the high-water mark.

resume_writing

resume_writing()

Called when the transport buffer drains below the low-water mark.

send_message async

send_message(msg: BaseMessage, delay: float = 0.1)

Sends a message to the server.

Parameters:

Name Type Description Default
msg BaseMessage

The message to send

required
delay float

The delay to wait before sending the message. If the messages are sent too fast, certain servers will respond with invalid messages.

0.1

receive_messages async

receive_messages() -> BaseMessage

Awaits a message from the server and returns it.

Returns:

Name Type Description
BaseMessage BaseMessage

The message from the server, it will be a subclass of BaseMessage