Exemplo n.º 1
0
    def transfer_from_dest(self, r_conn: socket_t, mask: int,
                           buf: List[deque]) -> None:
        """receive data from dest and store in buffer"""
        w_conn = self.working_pool.inv.get(r_conn)
        if w_conn is None:
            self._sel.unregister(r_conn)
            r_conn.close()
            return

        data = b''
        need_close = False

        try:
            data = r_conn.recv(BUF_SIZE)
        except ConnectionError:
            need_close = True

        if data == b'' or need_close:
            try:
                peer = r_conn.getpeername()
                logger.info(
                    f'closing dest connection from {format_addr(peer)}')
            except OSError as e:
                logger.warn(e)
            self._sel.unregister(r_conn)
            self._sel.modify(w_conn, selectors.EVENT_WRITE,
                             partial(self.send_to_tunnel, buf=buf))
            r_conn.close()
            buf[NEG].append(sentinel)
            del self.working_pool.inv[r_conn]
            return

        buf[NEG].append(data)
Exemplo n.º 2
0
    def transfer_from_tunnel(self, r_conn: socket_t, mask: int,
                             buf: List[deque]) -> None:
        """receive data from tunnel and store in buffer"""
        w_conn = self.work_pool.inv.get(r_conn)
        if w_conn is None:
            self._sel.unregister(r_conn)
            r_conn.close()
            return

        data = b''
        need_close = False

        try:
            data = r_conn.recv(BUF_SIZE)
        except ConnectionError:
            need_close = True

        if data == b'' or need_close:
            try:
                peer = r_conn.getpeername()
                logger.info(
                    f'closing tunnel connection from {format_addr(peer)}'
                )  # noqa
            except OSError as e:
                logger.warn(e)
            self._sel.unregister(r_conn)
            r_conn.close()
            buf[NEG].append(sentinel)
            del self.work_pool.inv[r_conn]
            return

        buf[NEG].append(data)
Exemplo n.º 3
0
    def _connect_tunnel(self) -> bool:
        """establish tunnel connection"""
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            sock.connect(self.tunnel_addr)
        except ConnectionError:
            self.next_timeout()
            logger.warn(f'master is unreachable, '
                        f'retry after {self._timeout}sec...')  # noqa
            return False

        self.reset_timeout()
        self._sel.register(sock, selectors.EVENT_READ, self.prepare_transfer)
        self.tunnel_pool.append(sock)
        logger.info(f'connect to tunnel {format_addr(self.tunnel_addr)}, '
                    f'poolsize is {len(self.tunnel_pool)}')
        return True