tlnbase: create main loop - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit 98707a362477e31cfb165d31842dc9989ad9140b DIR parent bcb268d554e6a122443b3d70ec2dd9f7642a24c6 HTML Author: ThomasV <thomasv@electrum.org> Date: Wed, 11 Apr 2018 06:11:07 +0200 lnbase: create main loop Diffstat: M lib/lnbase.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) --- DIR diff --git a/lib/lnbase.py b/lib/lnbase.py t@@ -9,6 +9,7 @@ from collections import OrderedDict import asyncio import sys import os +import time import binascii import hashlib import hmac t@@ -235,6 +236,10 @@ class Peer(PrintError): self.privkey = privkey self.pubkey = pubkey self.read_buffer = b'' + self.ping_time = 0 + + def ping_required(self): + return time.time() - self.ping_time > 120 def send_message(self, msg): print("Sending %d bytes: "%len(msg), binascii.hexlify(msg)) t@@ -302,12 +307,13 @@ class Peer(PrintError): # send init init_msg = gen_msg("init", gflen=0, lflen=0) self.send_message(init_msg) - # send ping - ping_msg = gen_msg("ping", num_pong_bytes=4, byteslen=4) - self.send_message(ping_msg) - # read pong - msg = await self.read_message() - process_message(msg) + while True: + if self.ping_required(): + self.send_message(gen_msg("ping", num_pong_bytes=4, byteslen=4)) + self.ping_time = time.time() + + msg = await self.read_message() + process_message(msg) # close socket self.writer.close()