dolor.packet_handler

Code for packet handling.

packet_listener(*checkers, **kwargs)[source]

Decorator for internal packet listeners, packet listeners that are methods.

Parameters
Returns

The actual decorator to be used.

Return type

function

Examples

>>> import dolor
>>> class MyPacketHandler(dolor.packet_handler.PacketHandler):
...     @dolor.packet_listener(dolor.packets.Packet)
...     async def my_listener(self, p):
...         pass
...
class PacketHandler[source]

Bases: abc.ABC

A generic packet handler.

to_real_packet_checker(checker)[source]

Turns a packet checker into a function that checks a packet.

Parameters

checker – See register_packet_listener().

Returns

A function that returns a bool and takes two arguments, the first being the relevant Connection, and the second being the packet to check.

Return type

function

join_checkers(first, second)[source]

Joins two real packet checkers into one.

Parameters
Returns

See to_real_packet_checker().

Return type

function

register_packet_listener(func, *checkers, **kwargs)[source]

Registers a packet listener.

Parameters
  • func (coroutine function) – The packet listener.

  • *checkers (subclass of Packet or int or function) –

    If a subclass of Packet, then the listener will be called if the packet is an instance of that class.

    If an int, then the listener will be called if the id of the packet is equal to the passed int.

    If a function, then the function can either return a bool and take one argument, which represents the packet to check, or it can return a bool and take two arguments, the first being the relevant Connection, and the second being the packet to check.

  • **kwargs – Keyword arguments that must match the keyword arguments passed to listeners_for_packet() for the packet listener to be included in its return.

Raises
  • TypeError – If func isn’t a coroutine function.

  • ValueError – If no checkers are specified.

unregister_packet_listener(func)[source]

Unregisters a packet listener.

Parameters

func (coroutine function) – The listener registered with register_packet_listener().

external_packet_listener(*checkers, **kwargs)[source]

Decorator for external packet listeners.

For internal packet listeners, packet listeners that are methods, see packet_listener().

Parameters
Returns

The actual decorator to be used.

Return type

function

Examples

>>> import dolor
>>> class MyPacketHandler(dolor.packet_handler.PacketHandler):
...     pass
...
>>> handler = MyPacketHandler()
>>> @handler.external_packet_listener
... def my_listener(p):
...     pass
...
register_internal_listeners()[source]

Registers internal packet listeners.

See packet_listener().

Called on __init__().

listeners_for_packet(c, p, **kwargs)[source]

Gets the packet listeners for a packet.

Parameters
Returns

The list of packet listeners for the packet.

Return type

list