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