def test_two(self): """Unpack two packets from the same buffer """ msg1='\x00\x01\x00\x08\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x00\x07' \ 'hello w\x00' msg2 = '\x00\x02\x00\x00\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x00\x07' msg3 = 'testing' buf = msg1 + msg2 + msg3 exp1 = CAmessage(cmd=1, size=8, dtype=2, count=3, p1=0x40005, p2=0x60007, body='hello w\x00') exp2 = CAmessage(cmd=2, size=0, dtype=2, count=3, p1=0x40005, p2=0x60007) pkt1, rem = CAmessage.unpack(buf) self.assertEqual(pkt1, exp1) self.assertEqual(rem, buffer(msg2 + msg3)) pkt2, rem = CAmessage.unpack(rem) self.assertEqual(pkt2, exp2) self.assertEqual(rem, buffer(msg3))
def datagramReceived(self, msg, peer): while msg is not None and len(msg)>=16: pkt, msg = CAmessage.unpack(msg) self.handler(pkt, self, peer) if len(msg)>0: log.warning('dropping incomplete message %s',repr(msg))
def datagramReceived(self, msg, addr): while msg is not None and len(msg) >= 16: pkt, msg = CAmessage.unpack(msg) if pkt.cmd == 23: self.transport.write(pkt.pack()) else: log.warning('Unexpected message %d on fanout node', pkt.cmd)
def dataReceived(self, msg): msg=self.in_buffer+msg while msg is not None and len(msg)>=16: pkt, msg = CAmessage.unpack(msg) hdl = self._circ.get(pkt.cmd, self.server.dispatch) hdl(pkt, self, self.peer) self.in_buffer=msg # save remaining
def datagramReceived(self, msg, peer): while msg is not None and len(msg) >= 16: pkt, msg = CAmessage.unpack(msg) if pkt.cmd != 17: for h in self.handlers: h(pkt, peer) else: self.ackreg(pkt) if len(msg) > 0: log.warning('dropping incomplete message')
def datagramReceived(self, msg, peer): if len(msg) == 0: self.regreq(peer) while msg is not None and len(msg) >= 16: pkt, msg = CAmessage.unpack(msg) if pkt.cmd == 24: self.regreq(peer) elif pkt.cmd == 13 and pkt.p2 == 0: pkt.p2 = addr2int(peer[0]) self.repeat(pkt) else: self.repeat(pkt)
def test_canceldead(self): """Connect, lose connection, cancel """ g = self.get = CAGet(self.chan) self.check = ref(self.get) self.chan.doCon() pkt, _ = self.chan._circ._sent.pop(0) self.assertEqual(len(pkt), 16) pkt, extra = CAmessage.unpack(pkt) self.assertEqual(len(extra), 0) self.assertEqual(pkt.cmd, 15) self.chan.doLost() self.get.close() del self.get
def dataReceived(self, msg): msg = self.in_buffer + msg while msg is not None and len(msg) >= 16: try: pkt, msg = CAmessage.unpack(msg) except CAIncomplete: break hdl = self._circ.get(pkt.cmd, self.client.dispatch) try: hdl(pkt, self) except: # this is usually a alignment error when the socket buffer overflows log.warning('Processing error, circuit reset (%d)', pkt.cmd) self.transport.loseConnection() return self.in_buffer = str(msg) # save remaining
def test_normal(self): data = [ ('\x00' * 16, CAmessage()), ('\x00\x01\x00\x00\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x00\x07', CAmessage(cmd=1, size=0, dtype=2, count=3, p1=0x40005, p2=0x60007)), ('\x00\x01\x00\x08\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x00\x07' + 'hello w\x00', CAmessage(cmd=1, size=8, dtype=2, count=3, p1=0x40005, p2=0x60007, body='hello w\x00')), ] for raw, cook in data: raw2 = cook.pack() self.assertEqual(raw, raw2) cook2, rem = CAmessage.unpack(raw) self.assertEqual(cook, cook2) self.assertEqual(len(rem), 0)
def test_cancellive(self): """Connect but cancel after data request sent """ g = self.get = CAGet(self.chan) self.check = ref(self.get) self.chan.doCon() self.assertEqual(len(self.chan._circ._sent), 1) self.assertEqual(len(self.chan._circ.pendingActions), 1) pkt, _ = self.chan._circ._sent[0] self.assertEqual(len(pkt), 16) pkt, extra = CAmessage.unpack(pkt) self.assertEqual(len(extra), 0) self.assertEqual(pkt.cmd, 15) self.assertEqual(pkt.p1, self.chan.sid) self.assertIn(pkt.p2, self.chan._circ.pendingActions) self.get.close() del self.get