commit 98707a362477e31cfb165d31842dc9989ad9140b
parent bcb268d554e6a122443b3d70ec2dd9f7642a24c6
Author: ThomasV <thomasv@electrum.org>
Date: Wed, 11 Apr 2018 06:11:07 +0200
lnbase: create main loop
Diffstat:
1 file changed, 12 insertions(+), 6 deletions(-)
diff --git a/lib/lnbase.py b/lib/lnbase.py
@@ -9,6 +9,7 @@ from collections import OrderedDict
import asyncio
import sys
import os
+import time
import binascii
import hashlib
import hmac
@@ -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))
@@ -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()