use select to check for network data

This commit is contained in:
j 2016-01-08 16:56:35 +05:30
parent 11a20a5063
commit 165f9650ec

View file

@ -8,6 +8,7 @@ import struct
import _thread
from threading import Thread
import time
import select
from utils import valid, get_public_ipv6, get_local_ipv4, get_interface
from settings import preferences, server, USER_ID, sk
@ -51,7 +52,6 @@ class LocalNodesBase(Thread):
_PORT = 9851
_TTL = 1
_TIMEOUT = 30
def __init__(self, nodes):
self._socket = None
@ -85,12 +85,12 @@ class LocalNodesBase(Thread):
def receive(self):
last = time.mktime(time.localtime())
s = self.get_socket()
s.bind(('', self._PORT))
while self._active:
try:
s = self.get_socket()
s.settimeout(self._TIMEOUT)
s.bind(('', self._PORT))
while self._active:
r, _, _ = select.select([s], [], [], 3)
if r:
data, addr = s.recvfrom(1024)
if self._active:
while data[-1] == 0:
@ -98,8 +98,6 @@ class LocalNodesBase(Thread):
data = self.verify(data)
if data:
self.update_node(data)
except socket.timeout:
pass
except OSError: # no local interface exists
self.wait(60)
except: