From: burris Date: Mon, 20 Jan 2003 04:22:35 +0000 (+0000) Subject: fixed reset connection handling X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=c030f4d75c42e44eb874508016ee59d6800e9505;p=quix0rs-apt-p2p.git fixed reset connection handling --- diff --git a/airhook.py b/airhook.py index 3f1f80b..c100cdf 100644 --- a/airhook.py +++ b/airhook.py @@ -115,17 +115,16 @@ class AirhookConnection(protocol.ConnectedDatagramProtocol, interfaces.IUDPConne def resetMessages(self): self.weMissed = [] self.inMsg = 0 # next incoming message number - self.outMsgNums = [None] * 256 # outgoing message numbers i = outNum % 256 + self.outMsgNums = [0] * 256 # outgoing message numbers i = outNum % 256 self.next = 0 # next outgoing message number def datagramReceived(self, datagram): if not datagram: return + if self.noisy: + print `datagram` p = AirhookPacket(datagram) - # check to make sure sequence number isn't out of order - if (p.seq - self.inSeq) % 2**16 >= 256: - return # check for state change if self.state == pending: @@ -138,7 +137,6 @@ class AirhookConnection(protocol.ConnectedDatagramProtocol, interfaces.IUDPConne return elif p.session != None: self.observed = p.session - self.state = sent self.response = 1 elif self.state == sent: if p.observed != None and p.session != None: @@ -157,10 +155,15 @@ class AirhookConnection(protocol.ConnectedDatagramProtocol, interfaces.IUDPConne if p.session != None or p.observed != None : if (p.session != None and p.session != self.observed) or (p.observed != None and p.observed != self.sessionID): self.state = pending + self.observed = p.session self.resetMessages() self.inSeq = p.seq + + # check to make sure sequence number isn't out of order + if (p.seq - self.inSeq) % 2**16 >= 256: + return - if self.state != pending: + if self.state == confirmed: msgs = [] missed = [] @@ -208,6 +211,9 @@ class AirhookConnection(protocol.ConnectedDatagramProtocol, interfaces.IUDPConne # session / observed logic if self.state == pending: + if self.observed != None: + flags = flags | FLAG_OBSERVED + ids += pack("!L", self.observed) flags = flags | FLAG_SESSION ids += pack("!L", self.sessionID) self.state = sent @@ -231,7 +237,7 @@ class AirhookConnection(protocol.ConnectedDatagramProtocol, interfaces.IUDPConne if self.obSeq >= 0: self.weMissed = filter(lambda a: a[0] > self.obSeq, self.weMissed) - if self.weMissed: + if len(self.weMissed) > 0: flags = flags | FLAG_MISSED missed += chr(len(self.weMissed) - 1) for i in self.weMissed: @@ -271,8 +277,10 @@ class AirhookConnection(protocol.ConnectedDatagramProtocol, interfaces.IUDPConne self.schedule() def timeToSend(self): + if self.state == pending: + return (1, 0) # any outstanding messages and are we not too far ahead of our counterparty? - if len(self.omsgq) > 0 and self.state != sent and (self.next + 1) % 256 != self.outMsgNums[self.obSeq % 256] and (self.outSeq - self.obSeq) % 2**16 < 256: + elif len(self.omsgq) > 0 and self.state != sent and (self.next + 1) % 256 != self.outMsgNums[self.obSeq % 256] and (self.outSeq - self.obSeq) % 2**16 < 256: return (1, 0) # do we explicitly need to send a response? elif self.response: @@ -281,8 +289,6 @@ class AirhookConnection(protocol.ConnectedDatagramProtocol, interfaces.IUDPConne # have we not sent anything in a while? elif time() - self.lastTransmit > 1.0: return (1, 1) - elif self.state == pending: - return (1, 1) # nothing to send return (0, 0) diff --git a/test_airhook.py b/test_airhook.py index e491d84..d6cfb13 100644 --- a/test_airhook.py +++ b/test_airhook.py @@ -225,10 +225,10 @@ class SimpleTest(unittest.TestCase): self.assertEqual(a.obSeq, 0) b.datagramReceived(msg) - self.assertEqual(b.state, sent) self.assertEqual(b.inSeq, 0) self.assertEqual(b.obSeq, 0) msg = swap(b, '<', self.noisy) + self.assertEqual(b.state, sent) self.assertEqual(b.outSeq, 1) a.datagramReceived(msg) @@ -443,9 +443,9 @@ class BasicTests(unittest.TestCase): b.datagramReceived(msg) - self.assertEqual(b.state, sent) msg = swap(b, '<', self.noisy) + self.assertEqual(b.state, sent) a.datagramReceived(msg) msg = swap(a, '>', self.noisy) @@ -546,6 +546,48 @@ class BasicTests(unittest.TestCase): self.assertEqual(a.next, 255) self.assertEqual(a.outMsgNums[(a.outSeq-1) % 256], 254) + def testConnectionReset(self): + a = self.a + b = self.b + msg = swap(a, noisy=self.noisy) + b.datagramReceived(msg) + + msg = swap(b, noisy=self.noisy) + a.datagramReceived(msg) + + a.omsgq.append("TESTING") + msg = swap(a, noisy=self.noisy) + b.datagramReceived(msg) + + msg = swap(b, noisy=self.noisy) + a.datagramReceived(msg) + + self.assertEqual(b.protocol.q[0], "TESTING") + self.assertEqual(b.state, confirmed) + + self.a = AirhookConnection() + self.a.makeConnection(DummyTransport()) + self.a.addr = ('127.0.0.1', 4444) + a = self.a + + a.omsgq.append("TESTING2") + msg = swap(a, noisy=self.noisy) + b.datagramReceived(msg) + + msg = swap(b, noisy=self.noisy) + a.datagramReceived(msg) + + self.assertEqual(len(b.protocol.q), 1) + msg = swap(a, noisy=self.noisy) + b.datagramReceived(msg) + + msg = swap(b, noisy=self.noisy) + a.datagramReceived(msg) + + self.assertEqual(len(b.protocol.q), 2) + self.assertEqual(b.protocol.q[1], "TESTING2") + + class StreamTests(unittest.TestCase): def setUp(self): self.noisy = 0 @@ -585,12 +627,15 @@ class SimpleReactor(unittest.TestCase): self.b = makeReceiver(2021) self.ac = self.a.connectionForAddr(('127.0.0.1', 2021)) self.bc = self.b.connectionForAddr(('127.0.0.1', 2020)) + self.ac.noisy = self.noisy + self.bc.noisy = self.noisy def testSimple(self): msg = "Testing 1, 2, 3" self.ac.write(msg) reactor.iterate() reactor.iterate() reactor.iterate() + self.assertEqual(self.bc.state, confirmed) self.assertEqual(self.bc.protocol.q, [msg]) class SimpleReactorEcho(unittest.TestCase):