def listenerThread(self): listener = TCPSocket() listener.listen("0.0.0.0", 32102) while True: requestSocket = listener.accept() requestHandlerThread = Thread(target=self.receiverThread, args=(requestSocket,)) requestHandlerThread.start()
def responseListenerThread(self): while True: listenerSocket = TCPSocket() listenerSocket.listen("0.0.0.0", DEFAULT_DISCOVERY_PORT) newConnection = listenerSocket.accept() discoveredThread = threading.Thread(target=self.peerDiscoveredThread, args=(newConnection,)) discoveredThread.start()
def responseSenderThread(): listenerSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) listenerSocket.bind(("0.0.0.0", DEFAULT_MULTICAST_PORT)) socketSettings = struct.pack("4sL", socket.inet_aton(DEFAULT_MULTICAST_GROUP), socket.INADDR_ANY) listenerSocket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, socketSettings) while True: data, address = listenerSocket.recvfrom(1024) responder = TCPSocket() myName = getStoredName() or DEFAULT_NAME try: responder.connect(address[0], DEFAULT_DISCOVERY_PORT) responder.send((getStoredGuid() + ":" + myName).encode("utf-8")) except NetworkingError: pass
def sendRequestToPeer(self, peer, type, data, timeout=None): peerSocket = TCPSocket() peerSocket.connect(self.peers[peer].lastKnownIP, 32102) self.sendRequestToSocket(peerSocket, type, data, timeout) return peerSocket