tlnbase: save buffer for next read - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit bcb268d554e6a122443b3d70ec2dd9f7642a24c6 DIR parent 7421bffaa23b6a79b83a7a99784837c43b1602f3 HTML Author: ThomasV <thomasv@electrum.org> Date: Wed, 11 Apr 2018 06:02:26 +0200 lnbase: save buffer for next read Diffstat: M lib/lnbase.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) --- DIR diff --git a/lib/lnbase.py b/lib/lnbase.py t@@ -234,6 +234,7 @@ class Peer(PrintError): self.port = port self.privkey = privkey self.pubkey = pubkey + self.read_buffer = b'' def send_message(self, msg): print("Sending %d bytes: "%len(msg), binascii.hexlify(msg)) t@@ -246,16 +247,16 @@ class Peer(PrintError): self.sn += 2 async def read_message(self): - rspns = b'' while True: - rspns += await self.reader.read(2**10) - print("buffer %d bytes:"%len(rspns), binascii.hexlify(rspns)) - lc = rspns[:18] + self.read_buffer += await self.reader.read(2**10) + lc = self.read_buffer[:18] l = aead_decrypt(self.rk, self.rn, b'', lc) length = decode(l) - if len(rspns) < 18 + length + 16: + offset = 18 + length + 16 + if len(self.read_buffer) < offset: continue - c = rspns[18:18 + length + 16] + c = self.read_buffer[18:offset] + self.read_buffer = self.read_buffer[offset:] msg = aead_decrypt(self.rk, self.rn+1, b'', c) self.rn += 2 return msg