chiark / gitweb /
reorg SlipStreamDecoder again
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 22 Mar 2017 23:49:53 +0000 (23:49 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 22 Mar 2017 23:49:53 +0000 (23:49 +0000)
hippotat/__init__.py

index 3f62113..e48083a 100644 (file)
@@ -123,29 +123,36 @@ def ipnetwork(input):
 
 #---------- ipif (SLIP) subprocess ----------
 
-class SlipProtocol(twisted.internet.protocol.ProcessProtocol):
-  # caller must define method receivedPacket(packet)
-  def __init__(self):
+class SlipStreamDecoder():
+  def __init__(self, on_packet):
+    # we will call packet(<packet>)
     self._buffer = b''
-  def connectionMade(self): pass
-  def outReceived(self, data):
+    self._on_packet = on_packet
+
+  def inputdata(self, data):
     #print('SLIP-GOT ', repr(data))
     self._buffer += data
     packets = slip.decode(self._buffer)
     self._buffer = packets.pop()
     for packet in packets:
-      if not len(packet): continue
-      self.receivedPacket(packet)
+      self._maybe_packet(packet)
+
+  def _maybe_packet(self, packet):
+      if len(packet):
+        self._on_packet(packet)
+
   def flush(self):
-    if len(self._buffer):
-      self.receivedPacket(self._buffer)
-      self._buffer = ''
+    self._maybe_packet(self._buffer)
+    self._buffer = b''
 
 class _IpifProcessProtocol(SlipProtocol):
   def __init__(self, router):
     self._router = router
-    super.__init__()
-  def receivedPacket(self, packet):
+    self._decoder = SlipStreamDecoder(self.slip_on_packet)
+  def connectionMade(self): pass
+  def outReceived(self, data):
+    self._decoder.inputdata(data)
+  def slip_on_packet(self, packet):
     (saddr, daddr) = packet_addrs(packet)
     if saddr.is_link_local or daddr.is_link_local:
       log_discard(packet, saddr, daddr, 'link-local')