def _udp_proc(self, location, addrinfo, task=None):
"""
Internal use only.
"""
task.set_daemon()
sock = addrinfo.udp_sock
while 1:
msg, addr = yield sock.recvfrom(1024)
if not msg.startswith('ping:'):
logger.warning('ignoring UDP message from %s:%s', addr[0], addr[1])
continue
try:
ping_info = deserialize(msg[len('ping:'):])
except:
continue
peer_location = ping_info.get('location', None)
if not isinstance(peer_location, Location) or peer_location in self._locations:
continue
if ping_info['version'] != __version__:
logger.warning('Peer %s version %s is not %s',
peer_location, ping_info['version'], __version__)
continue
if self._ignore_peers:
continue
if self._secret is None:
auth_code = None
else:
auth_code = ping_info.get('signature', '') + self._secret
auth_code = hashlib.sha1(auth_code.encode()).hexdigest()
_Peer._lock.acquire()
peer = _Peer.peers.get((peer_location.addr, peer_location.port), None)
_Peer._lock.release()
if peer and peer.auth != auth_code:
_Peer.remove(peer_location)
peer = None
if not peer:
SysTask(self._acquaint_, peer_location, ping_info['signature'], addrinfo)
评论列表
文章目录