System-reserved channel message types

- a message handler can return logical True to prevent subsequent message handlers from running
- Message types >= 0xff00 are reserved for system/framework messages
This commit is contained in:
Aaron Heise 2023-02-26 11:39:49 -06:00
parent e005826151
commit c00b592ed9
No known key found for this signature in database
GPG Key ID: 6BA54088C41DE8BF
3 changed files with 22 additions and 2 deletions

View File

@ -46,7 +46,8 @@ class StringMessage(RNS.MessageBase):
# message arrives over the channel. # message arrives over the channel.
# #
# MSGTYPE must be unique across all message types we # MSGTYPE must be unique across all message types we
# register with the channel # register with the channel. MSGTYPEs >= 0xff00 are
# reserved for the system.
MSGTYPE = 0x0101 MSGTYPE = 0x0101
# The constructor of our object must be callable with # The constructor of our object must be callable with

View File

@ -149,7 +149,7 @@ class Channel(contextlib.AbstractContextManager):
self.shutdown() self.shutdown()
return False return False
def register_message_type(self, message_class: Type[MessageBase]): def register_message_type(self, message_class: Type[MessageBase], *, is_system_type: bool = False):
with self._lock: with self._lock:
if not issubclass(message_class, MessageBase): if not issubclass(message_class, MessageBase):
raise ChannelException(CEType.ME_INVALID_MSG_TYPE, raise ChannelException(CEType.ME_INVALID_MSG_TYPE,
@ -157,6 +157,9 @@ class Channel(contextlib.AbstractContextManager):
if message_class.MSGTYPE is None: if message_class.MSGTYPE is None:
raise ChannelException(CEType.ME_INVALID_MSG_TYPE, raise ChannelException(CEType.ME_INVALID_MSG_TYPE,
f"{message_class} has invalid MSGTYPE class attribute.") f"{message_class} has invalid MSGTYPE class attribute.")
if message_class.MSGTYPE >= 0xff00 and not is_system_type:
raise ChannelException(CEType.ME_INVALID_MSG_TYPE,
f"{message_class} has system-reserved message type.")
try: try:
message_class() message_class()
except Exception as ex: except Exception as ex:

View File

@ -137,6 +137,16 @@ class MessageTest(MessageBase):
self.id, self.data = umsgpack.unpackb(raw) self.id, self.data = umsgpack.unpackb(raw)
class SystemMessage(MessageBase):
MSGTYPE = 0xffff
def pack(self) -> bytes:
return bytes()
def unpack(self, raw):
pass
class ProtocolHarness(contextlib.AbstractContextManager): class ProtocolHarness(contextlib.AbstractContextManager):
def __init__(self, rtt: float): def __init__(self, rtt: float):
self.outlet = ChannelOutletTest(mdu=500, rtt=rtt) self.outlet = ChannelOutletTest(mdu=500, rtt=rtt)
@ -280,6 +290,11 @@ class TestChannel(unittest.TestCase):
self.assertEqual(2, handler1_called) self.assertEqual(2, handler1_called)
self.assertEqual(1, handler2_called) self.assertEqual(1, handler2_called)
def test_system_message_check(self):
with self.assertRaises(RNS.Channel.ChannelException):
self.h.channel.register_message_type(SystemMessage)
self.h.channel.register_message_type(SystemMessage, is_system_type=True)
def eat_own_dog_food(self, message: MessageBase, checker: typing.Callable[[MessageBase], None]): def eat_own_dog_food(self, message: MessageBase, checker: typing.Callable[[MessageBase], None]):
decoded: [MessageBase] = [] decoded: [MessageBase] = []
@ -287,6 +302,7 @@ class TestChannel(unittest.TestCase):
def handle_message(message: MessageBase): def handle_message(message: MessageBase):
decoded.append(message) decoded.append(message)
self.h.channel.register_message_type(message.__class__)
self.h.channel.add_message_handler(handle_message) self.h.channel.add_message_handler(handle_message)
self.assertEqual(len(self.h.outlet.packets), 0) self.assertEqual(len(self.h.outlet.packets), 0)