Ejemplo n.º 1
0
 def __listen_cb(self, handle, error):
     if error is not None:
         log.debug('listen failed: %d %s', convert_errno(error), pyuv.errno.strerror(error))
         return
     pipe_handle = pyuv.Pipe(self._handle.loop)
     try:
         self._handle.accept(pipe_handle)
     except pyuv.error.PipeError as e:
         log.debug('accept failed: %d %s', convert_errno(e.args[0]), pyuv.errno.strerror(e.args[1]))
         pipe_handle.close()
     else:
         conn = self.connection_class(pipe_handle)
         conn._set_accepted(self)
         self.handle_connection(conn)
Ejemplo n.º 2
0
 def open(self, fd):
     try:
         self._handle.open(fd)
     except pyuv.error.PipeError as e:
         raise PipeError(convert_errno(e.args[0]), e.args[1])
     else:
         self._set_connected()
Ejemplo n.º 3
0
    def _read(self, n):
        read_result = Result()
        def cb(handle, data, error):
            self._handle.stop_read()
            if error is not None:
                if error != pyuv.errno.UV_EOF:
                    read_result.set_exception(PipeError(convert_errno(error), pyuv.errno.strerror(error)))
                else:
                    read_result.set_result(b'')
            else:
                read_result.set_result(data)

        try:
            self._handle.start_read(cb)
        except pyuv.error.PipeError as e:
            self.close()
            raise PipeError(convert_errno(e.args[0]), e.args[1])
        try:
            data = read_result.wait()
        except PipeError as e:
            self.close()
            raise
        else:
            if not data:
                self.close()
                return
            self._read_buffer.feed(data)
Ejemplo n.º 4
0
 def __read_cb(self, handle, data, error):
     self._handle.stop_read()
     if error is not None:
         if error != pyuv.errno.UV_EOF:
             self._read_result.set_exception(PipeError(convert_errno(error), pyuv.errno.strerror(error)))
         else:
             self._read_result.set_result(b'')
     else:
         self._read_result.set_result(data)
Ejemplo n.º 5
0
    def connect(self, target, source_address=None):
        if self._connected:
            raise TCPError('already connected')
        host, port = target
        try:
            r = socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM)
        except socket.error as e:
            raise TCPError(e)
        if not r:
            raise TCPError('getaddrinfo returned no result')

        connect_result = Result()
        def cb(handle, error):
            if error is not None:
                connect_result.set_exception(TCPError(convert_errno(error), pyuv.errno.strerror(error)))
            else:
                connect_result.set_result(None)

        err = None
        loop = self._handle.loop
        for item in r:
            addr = item[-1]
            if '%' in addr[0]:
                # Skip addresses such as 'fe80::1%lo0'
                # TODO: handle this properly
                continue
            handle = pyuv.TCP(loop)
            try:
                if source_address:
                    handle.bind(source_address)
                handle.connect(addr, cb)
            except pyuv.error.TCPError as e:
                err = TCPError(convert_errno(e.args[0]), e.args[1])
                handle.close()
                continue
            try:
                connect_result.wait()
            except TCPError as e:
                err = e
                handle.close()
                connect_result.clear()
                continue
            else:
                self._handle.close()
                self._handle = handle
                break
        if err is not None:
            raise err
        self._set_connected()
Ejemplo n.º 6
0
 def connect(self, target):
     if self._connected:
         raise PipeError('already connected')
     self._connect_result = Result()
     try:
         self._handle.connect(target, self.__connect_cb)
     except pyuv.error.PipeError as e:
         raise PipeError(convert_errno(e.args[0]), e.args[1])
     try:
         self._connect_result.wait()
     except PipeError:
         self.close()
         raise
     else:
         self._set_connected()
     finally:
         self._connect_result = None
Ejemplo n.º 7
0
 def _read(self, n):
     try:
         self._handle.start_read(self.__read_cb)
     except pyuv.error.PipeError as e:
         self.close()
         raise PipeError(convert_errno(e.args[0]), e.args[1])
     try:
         data = self._read_result.wait()
     except PipeError as e:
         self.close()
         raise
     else:
         if not data:
             self.close()
             return
         self._read_buffer.feed(data)
     finally:
         self._read_result.clear()
Ejemplo n.º 8
0
    def connect(self, target):
        if self._connected:
            raise PipeError('already connected')

        connect_result = Result()
        def cb(handle, error):
            if error is not None:
                connect_result.set_exception(PipeError(convert_errno(error), pyuv.errno.strerror(error)))
            else:
                connect_result.set_result(None)

        try:
            self._handle.connect(target, cb)
        except pyuv.error.PipeError as e:
            raise PipeError(convert_errno(e.args[0]), e.args[1])
        try:
            connect_result.wait()
        except PipeError:
            self.close()
            raise
        else:
            self._set_connected()
Ejemplo n.º 9
0
 def _write(self, data):
     try:
         self._handle.write(data, self.__write_cb)
     except pyuv.error.PipeError as e:
         self.close()
         raise PipeError(convert_errno(e.args[0]), e.args[1])
Ejemplo n.º 10
0
 def _serve(self, backlog):
     try:
         self._handle.listen(self.__listen_cb, backlog)
     except pyuv.error.PipeError as e:
         raise PipeError(convert_errno(e.args[0]), e.args[1])
Ejemplo n.º 11
0
 def __connect_cb(self, handle, error):
     if error is not None:
         self._connect_result.set_exception(PipeError(convert_errno(error), pyuv.errno.strerror(error)))
     else:
         self._connect_result.set_result(None)
Ejemplo n.º 12
0
 def __write_cb(self, handle, error):
     if error is not None:
         log.debug('write failed: %d %s', convert_errno(error), pyuv.errno.strerror(error))
         evergreen.current.loop.call_soon(self.close)
Ejemplo n.º 13
0
 def cb(handle, error):
     if error is not None:
         connect_result.set_exception(TCPError(convert_errno(error), pyuv.errno.strerror(error)))
     else:
         connect_result.set_result(None)