Move ping functionality into its own function.
First step in cleaning up the run() function. Calls stop() rather than setting is_connected to False on case of timeout, which cleanly closes the socket.
This commit is contained in:
parent
3b357b6a6d
commit
6920747a5d
|
@ -63,6 +63,7 @@ class TcpInterface(threading.Thread):
|
||||||
self.unanswered_requests = {}
|
self.unanswered_requests = {}
|
||||||
# are we waiting for a pong?
|
# are we waiting for a pong?
|
||||||
self.is_ping = False
|
self.is_ping = False
|
||||||
|
self.ping_time = 0
|
||||||
# parse server
|
# parse server
|
||||||
self.server = server
|
self.server = server
|
||||||
self.host, self.port, self.protocol = self.server.split(':')
|
self.host, self.port, self.protocol = self.server.split(':')
|
||||||
|
@ -276,6 +277,17 @@ class TcpInterface(threading.Thread):
|
||||||
self.connected = False
|
self.connected = False
|
||||||
self.print_error("stopped")
|
self.print_error("stopped")
|
||||||
|
|
||||||
|
def maybe_ping(self):
|
||||||
|
# ping the server with server.version?
|
||||||
|
if time.time() - self.ping_time > 60:
|
||||||
|
if self.is_ping:
|
||||||
|
self.print_error("ping timeout")
|
||||||
|
self.stop()
|
||||||
|
else:
|
||||||
|
self.send_request({'method':'server.version', 'params':[ELECTRUM_VERSION, PROTOCOL_VERSION]})
|
||||||
|
self.is_ping = True
|
||||||
|
self.ping_time = time.time()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.s = self.get_socket()
|
self.s = self.get_socket()
|
||||||
if self.s:
|
if self.s:
|
||||||
|
@ -288,21 +300,12 @@ class TcpInterface(threading.Thread):
|
||||||
if not self.connected:
|
if not self.connected:
|
||||||
return
|
return
|
||||||
|
|
||||||
# ping timer
|
|
||||||
ping_time = 0
|
|
||||||
# request timer
|
# request timer
|
||||||
request_time = False
|
request_time = False
|
||||||
while self.connected:
|
while self.connected:
|
||||||
# ping the server with server.version
|
self.maybe_ping()
|
||||||
if time.time() - ping_time > 60:
|
if not self.connected:
|
||||||
if self.is_ping:
|
|
||||||
self.print_error("ping timeout")
|
|
||||||
self.connected = False
|
|
||||||
break
|
break
|
||||||
else:
|
|
||||||
self.send_request({'method':'server.version', 'params':[ELECTRUM_VERSION, PROTOCOL_VERSION]})
|
|
||||||
self.is_ping = True
|
|
||||||
ping_time = time.time()
|
|
||||||
try:
|
try:
|
||||||
response = self.pipe.get()
|
response = self.pipe.get()
|
||||||
except util.timeout:
|
except util.timeout:
|
||||||
|
|
Loading…
Reference in New Issue