Catch ChecklistError & CooldownError on every listener call

This commit is contained in:
Ben 2019-12-02 23:18:23 +00:00
parent 551cf016b4
commit 3f1dd2a2f1
2 changed files with 46 additions and 44 deletions

View File

@ -3,8 +3,9 @@ import enum
import itertools import itertools
from types import FunctionType from types import FunctionType
from houdini.converters import _listener, _ArgumentDeserializer, get_converter, do_conversion, _ConverterContext from houdini.converters import _listener, _ArgumentDeserializer, get_converter, \
from houdini.cooldown import _Cooldown, _CooldownMapping, BucketType do_conversion, _ConverterContext, ChecklistError
from houdini.cooldown import _Cooldown, _CooldownMapping, BucketType, CooldownError
from houdini import plugins, _AbstractManager, get_package_modules from houdini import plugins, _AbstractManager, get_package_modules
@ -73,14 +74,19 @@ class _XTListener(_Listener):
self.pre_login = kwargs.get('pre_login') self.pre_login = kwargs.get('pre_login')
async def __call__(self, p, packet_data): async def __call__(self, p, packet_data):
if not self.pre_login and not p.joined_world: try:
await p.close() if not self.pre_login and not p.joined_world:
raise AuthorityError(f'{p} tried sending XT packet before authentication!') await p.close()
raise AuthorityError(f'{p} tried sending XT packet before authentication!')
await super()._check_cooldown(p) await super()._check_cooldown(p)
super()._check_list(p) super()._check_list(p)
await super().__call__(p, packet_data) await super().__call__(p, packet_data)
except CooldownError:
p.logger.debug(f'{p} tried to send a packet during a cooldown')
except ChecklistError:
p.logger.debug(f'{p} sent a packet without meeting checklist requirements')
class _XMLListener(_Listener): class _XMLListener(_Listener):
@ -89,27 +95,35 @@ class _XMLListener(_Listener):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
async def __call__(self, p, packet_data): async def __call__(self, p, packet_data):
await super()._check_cooldown(p) try:
super()._check_list(p) await super()._check_cooldown(p)
super()._check_list(p)
handler_call_arguments = [self.instance, p] if self.instance is not None else [p] handler_call_arguments = [self.instance, p] if self.instance is not None else [p]
ctx = _ConverterContext(None, None, packet_data, p) ctx = _ConverterContext(None, None, packet_data, p)
for ctx.component in itertools.islice(self._signature, len(handler_call_arguments), len(self._signature)): for ctx.component in itertools.islice(self._signature, len(handler_call_arguments), len(self._signature)):
if ctx.component.default is not ctx.component.empty: if ctx.component.default is not ctx.component.empty:
handler_call_arguments.append(ctx.component.default) handler_call_arguments.append(ctx.component.default)
elif ctx.component.kind == ctx.component.POSITIONAL_OR_KEYWORD: elif ctx.component.kind == ctx.component.POSITIONAL_OR_KEYWORD:
converter = get_converter(ctx.component) converter = get_converter(ctx.component)
handler_call_arguments.append(await do_conversion(converter, ctx)) handler_call_arguments.append(await do_conversion(converter, ctx))
return await self.callback(*handler_call_arguments) return await self.callback(*handler_call_arguments)
except CooldownError:
p.logger.debug(f'{p} tried to send a packet during a cooldown')
except ChecklistError:
p.logger.debug(f'{p} sent a packet without meeting checklist requirements')
class _DummyListener(_Listener): class _DummyListener(_Listener):
async def __call__(self, p, *_): async def __call__(self, p, *_):
super()._check_list(p) try:
handler_call_arguments = [self.instance, p] if self.instance is not None else [p] super()._check_list(p)
return await self.callback(*handler_call_arguments) handler_call_arguments = [self.instance, p] if self.instance is not None else [p]
return await self.callback(*handler_call_arguments)
except ChecklistError:
p.logger.debug(f'{p} sent a packet without meeting checklist requirements')
class _ListenerManager(_AbstractManager): class _ListenerManager(_AbstractManager):

View File

@ -7,8 +7,6 @@ from xml.etree.cElementTree import Element, SubElement, tostring
from houdini.constants import ClientType from houdini.constants import ClientType
from houdini.handlers import AuthorityError, DummyEventPacket from houdini.handlers import AuthorityError, DummyEventPacket
from houdini.converters import ChecklistError
from houdini.cooldown import CooldownError
class Spheniscidae: class Spheniscidae:
@ -142,27 +140,21 @@ class Spheniscidae:
async def _client_connected(self): async def _client_connected(self):
self.logger.info(f'Client {self.peer_name} connected') self.logger.info(f'Client {self.peer_name} connected')
try: dummy_event = DummyEventPacket('connected')
dummy_event = DummyEventPacket('connected') if dummy_event in self.server.dummy_event_listeners:
if dummy_event in self.server.dummy_event_listeners: dummy_event_listeners = self.server.dummy_event_listeners[dummy_event]
dummy_event_listeners = self.server.dummy_event_listeners[dummy_event] for listener in dummy_event_listeners:
for listener in dummy_event_listeners: await listener(self)
await listener(self)
except ChecklistError:
self.logger.debug(f'{self} sent a packet without meeting checklist requirements')
async def _client_disconnected(self): async def _client_disconnected(self):
del self.server.peers_by_ip[self.peer_name] del self.server.peers_by_ip[self.peer_name]
self.logger.info(f'Client {self.peer_name} disconnected') self.logger.info(f'Client {self.peer_name} disconnected')
try: dummy_event = DummyEventPacket('disconnected')
dummy_event = DummyEventPacket('disconnected') if dummy_event in self.server.dummy_event_listeners:
if dummy_event in self.server.dummy_event_listeners: dummy_event_listeners = self.server.dummy_event_listeners[dummy_event]
dummy_event_listeners = self.server.dummy_event_listeners[dummy_event] for listener in dummy_event_listeners:
for listener in dummy_event_listeners: await listener(self)
await listener(self)
except ChecklistError:
self.logger.debug(f'{self} sent a packet without meeting checklist requirements')
async def __data_received(self, data): async def __data_received(self, data):
data = data.decode()[:-1] data = data.decode()[:-1]
@ -173,10 +165,6 @@ class Spheniscidae:
await self.__handle_xt_data(data) await self.__handle_xt_data(data)
except AuthorityError: except AuthorityError:
self.logger.debug(f'{self} tried to send game packet before authentication') self.logger.debug(f'{self} tried to send game packet before authentication')
except CooldownError:
self.logger.debug(f'{self} tried to send a packet during a cooldown')
except ChecklistError:
self.logger.debug(f'{self} sent a packet without meeting checklist requirements')
async def run(self): async def run(self):
await self._client_connected() await self._client_connected()