Get the connection info from server over udp broadcast.
41 def getConnInfo(self, retry=0):
42 r"""!Get the connection info from server over udp broadcast
43
44 This function will send broadcast package(s)
45 to get connection info from the server.
46
47 - send the magic packet <BW-Request> on broadcast address.
48 - wait for a <BW-Result> magic packet.
49 - extract the connection data from the magic packet and return
50
51 @param retry: Count of retry - 0 is infinite (0)
52
53 @return True or False"""
54 sendPackages = 0
55 while sendPackages < retry or retry == 0:
56 try:
57 logging.debug("send magic <BW3-Request> as broadcast - Try: %d", sendPackages)
58 self._socket.sendto("<BW3-Request>".encode(), ('255.255.255.255', self._broadcastPort))
59 sendPackages += 1
60 payload, address = self._socket.recvfrom(1024)
61 payload = str(payload, "UTF-8")
62
63 if payload.startswith("<BW3-Result>"):
64 logging.debug("received magic <BW3-Result> from: %s", address[0])
65 self._serverIP = address[0]
66 self._serverPort = int(payload.split(";")[1])
67 logging.info("got connection info from server: %s:%d", self._serverIP, self._serverPort)
68 return True
69 except socket.timeout:
70 logging.debug("no magic packet received")
71 logging.warning("cannot fetch connection info after %d tries", sendPackages)
72 return False
73