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

@ -137,6 +137,16 @@ class MessageTest(MessageBase):
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):
def __init__(self, rtt: float):
self.outlet = ChannelOutletTest(mdu=500, rtt=rtt)
@ -280,6 +290,11 @@ class TestChannel(unittest.TestCase):
self.assertEqual(2, handler1_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]):
decoded: [MessageBase] = []
@ -287,6 +302,7 @@ class TestChannel(unittest.TestCase):
def handle_message(message: MessageBase):
decoded.append(message)
self.h.channel.register_message_type(message.__class__)
self.h.channel.add_message_handler(handle_message)
self.assertEqual(len(self.h.outlet.packets), 0)