def run(self): try: while not self._halt: self._ready.acquire() if self._messages: m = self._messages.pop(0) self._ready.release() if not (m.id == protocol.PIECE and self._peer.choking): _logger.debug("Sending message %s to peer %s" %\ (m, self._peer)) self._out.send(m.payload()) if m.id == protocol.PIECE: self._peer.sent(\ m.index, m.offset, len(m.block)) else: self._ready.wait(KEEP_ALIVE_TIMEOUT) if not self._messages: self._messages.append(\ protocol.keep_alive_message()) self._ready.release() except socket.error: #Connection closed. Notify peer! self._peer.disconnected() _logger.info("Peer %s disconnected" % self._peer)
def keep_alive(self): self._out.add_message(protocol.keep_alive_message())