Beispiel #1
0
 def __init__(self):
     super(BaseStream, self).__init__()
     self._read_result = Result()
     self._shutdown_result = Result()
     self._pending_writes = 0
     self._flush_event = Event()
     self._flush_event.set()
Beispiel #2
0
 def __init__(self):
     super(BaseStream, self).__init__()
     self._read_result = Result()
     self._shutdown_result = Result()
     self._pending_writes = 0
     self._flush_event = Event()
     self._flush_event.set()
Beispiel #3
0
 def __init__(self):
     loop = evergreen.current.loop
     self._handle = pyuv.UDP(loop._loop)
     self._closed = False
     self._receive_result = Result()
     self._pending_writes = 0
     self._flush_event = Event()
     self._flush_event.set()
     self._sockname = None
Beispiel #4
0
 def __init__(self):
     loop = evergreen.current.loop
     self._handle = pyuv.UDP(loop._loop)
     self._closed = False
     self._receive_result = Result()
     self._pending_writes = 0
     self._flush_event = Event()
     self._flush_event.set()
     self._sockname = None
Beispiel #5
0
class PipeClient(BasePipeStream):
    def __init__(self):
        loop = evergreen.current.loop
        handle = pyuv.Pipe(loop._loop)
        super(PipeClient, self).__init__(handle)
        self._connect_result = Result()

    def connect(self, target):
        if self._connected:
            raise PipeError('already connected')
        with self._connect_result:
            try:
                self._handle.connect(target, self.__connect_cb)
            except PipeError:
                self.close()
                raise
            try:
                self._connect_result.get()
            except PipeError:
                self.close()
                raise
        self._set_connected()

    def __connect_cb(self, handle, error):
        if error is not None:
            self._connect_result.set_exception(
                PipeError(error, errno.strerror(error)))
        else:
            self._connect_result.set_value(None)
Beispiel #6
0
class PipeClient(BasePipeStream):

    def __init__(self):
        loop = evergreen.current.loop
        handle = pyuv.Pipe(loop._loop)
        super(PipeClient, self).__init__(handle)
        self._connect_result = Result()

    def connect(self, target):
        if self._connected:
            raise PipeError('already connected')
        with self._connect_result:
            try:
                self._handle.connect(target, self.__connect_cb)
            except PipeError:
                self.close()
                raise
            try:
                self._connect_result.get()
            except PipeError:
                self.close()
                raise
        self._set_connected()

    def __connect_cb(self, handle, error):
        if error is not None:
            self._connect_result.set_exception(PipeError(error, errno.strerror(error)))
        else:
            self._connect_result.set_value(None)
Beispiel #7
0
class TCPClient(TCPStream):

    def __init__(self):
        loop = evergreen.current.loop
        handle = pyuv.TCP(loop._loop)
        super(TCPClient, self).__init__(handle)
        self._connect_result = Result()

    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')

        err = None
        loop = self._handle.loop
        for item in r:
            with self._connect_result:
                addr = item[-1]
                idx = addr[0].find('%')
                if idx != -1:
                    host, rest = addr[0], addr[1:]
                    addr = (host[:idx],) + rest
                handle = pyuv.TCP(loop)
                try:
                    if source_address:
                        handle.bind(source_address)
                    handle.connect(addr, self.__connect_cb)
                except TCPError as e:
                    err = e
                    handle.close()
                    continue
                try:
                    self._connect_result.get()
                except TCPError as e:
                    err = e
                    handle.close()
                    continue
                else:
                    self._handle.close()
                    self._handle = handle
                    break
        if err is not None:
            raise err
        self._set_connected()

    def __connect_cb(self, handle, error):
        if error is not None:
            self._connect_result.set_exception(TCPError(error, errno.strerror(error)))
        else:
            self._connect_result.set_value(None)
Beispiel #8
0
 def __init__(self):
     loop = evergreen.current.loop
     handle = pyuv.Pipe(loop._loop)
     super(PipeClient, self).__init__(handle)
     self._connect_result = Result()
Beispiel #9
0
 def __init__(self):
     loop = evergreen.current.loop
     handle = pyuv.TCP(loop._loop)
     super(TCPClient, self).__init__(handle)
     self._connect_result = Result()
Beispiel #10
0
class UDPEndpoint(object):
    def __init__(self):
        loop = evergreen.current.loop
        self._handle = pyuv.UDP(loop._loop)
        self._closed = False
        self._receive_result = Result()
        self._pending_writes = 0
        self._flush_event = Event()
        self._flush_event.set()
        self._sockname = None

    @property
    def sockname(self):
        self._check_closed()
        if self._sockname is None:
            self._sockname = self._handle.getsockname()
        return self._sockname

    def bind(self, addr):
        self._check_closed()
        self._handle.bind(addr)

    def send(self, data, addr):
        self._check_closed()
        self._handle.send(addr, data, self.__send_cb)
        if self._pending_writes == 0:
            self._flush_event.clear()
        self._pending_writes += 1

    def receive(self):
        self._check_closed()
        with self._receive_result:
            self._handle.start_recv(self.__receive_cb)
            return self._receive_result.get()

    def flush(self):
        self._check_closed()
        self._flush_event.wait()

    def close(self):
        if self._closed:
            return
        self._closed = True
        self._handle.close()

    def _check_closed(self):
        if self._closed:
            raise UDPError("endpoint is closed")

    def __send_cb(self, handle, error):
        self._pending_writes -= 1
        if self._pending_writes == 0:
            self._flush_event.set()
        if error is not None:
            log.debug("send failed: %d %s", error, pyuv.errno.strerror(error))
            evergreen.current.loop.call_soon(self.close)

    def __receive_cb(self, handle, addr, flags, data, error):
        self._handle.stop_recv()
        if error is not None:
            self._receive_result.set_exception(UDPError(error, errno.strerror(error)))
        else:
            self._receive_result.set_value((data, addr))
Beispiel #11
0
class BaseStream(AbstractBaseStream):
    """Base class for streams implemented using pyuv Stream objects as the underlying mechanism
    """

    def __init__(self):
        super(BaseStream, self).__init__()
        self._read_result = Result()
        self._shutdown_result = Result()
        self._pending_writes = 0
        self._flush_event = Event()
        self._flush_event.set()

    def flush(self):
        self._check_closed()
        self._flush_event.wait()

    def _read(self, n):
        with self._read_result:
            try:
                self._handle.start_read(self.__read_cb)
            except self.error_cls:
                self.close()
                raise
            try:
                data = self._read_result.get()
            except self.error_cls as e:
                self.close()
                if e.args[0] != errno.EOF:
                    raise
            else:
                self._read_buffer.feed(data)

    def _write(self, data):
        try:
            self._handle.write(data, self.__write_cb)
        except self.error_cls:
            self.close()
            raise
        if self._pending_writes == 0:
            self._flush_event.clear()
        self._pending_writes += 1
        return self._handle.write_queue_size == 0

    def _shutdown(self):
        with self._shutdown_result:
            self._handle.shutdown(self.__shutdown_cb)
            self._shutdown_result.get()

    def _close(self):
        self._handle.close()

    def __read_cb(self, handle, data, error):
        self._handle.stop_read()
        if error is not None:
            self._read_result.set_exception(self.error_cls(error, pyuv.errno.strerror(error)))
        else:
            self._read_result.set_value(data)

    def __write_cb(self, handle, error):
        self._pending_writes -= 1
        if self._pending_writes == 0:
            self._flush_event.set()
        if error is not None:
            log.debug('write failed: %d %s', error, pyuv.errno.strerror(error))
            evergreen.current.loop.call_soon(self.close)

    def __shutdown_cb(self, handle, error):
        if error is not None:
            self._shutdown_result.set_exception(self.error_cls(error, pyuv.errno.strerror(error)))
        else:
            self._shutdown_result.set_value(None)
Beispiel #12
0
class BaseStream(AbstractBaseStream):
    """Base class for streams implemented using pyuv Stream objects as the underlying mechanism
    """
    def __init__(self):
        super(BaseStream, self).__init__()
        self._read_result = Result()
        self._shutdown_result = Result()
        self._pending_writes = 0
        self._flush_event = Event()
        self._flush_event.set()

    def flush(self):
        self._check_closed()
        self._flush_event.wait()

    def _read(self, n):
        with self._read_result:
            try:
                self._handle.start_read(self.__read_cb)
            except self.error_cls:
                self.close()
                raise
            try:
                data = self._read_result.get()
            except self.error_cls as e:
                self.close()
                if e.args[0] != errno.EOF:
                    raise
            else:
                self._read_buffer.feed(data)

    def _write(self, data):
        try:
            self._handle.write(data, self.__write_cb)
        except self.error_cls:
            self.close()
            raise
        if self._pending_writes == 0:
            self._flush_event.clear()
        self._pending_writes += 1
        return self._handle.write_queue_size == 0

    def _shutdown(self):
        with self._shutdown_result:
            self._handle.shutdown(self.__shutdown_cb)
            self._shutdown_result.get()

    def _close(self):
        self._handle.close()

    def __read_cb(self, handle, data, error):
        self._handle.stop_read()
        if error is not None:
            self._read_result.set_exception(
                self.error_cls(error, pyuv.errno.strerror(error)))
        else:
            self._read_result.set_value(data)

    def __write_cb(self, handle, error):
        self._pending_writes -= 1
        if self._pending_writes == 0:
            self._flush_event.set()
        if error is not None:
            log.debug('write failed: %d %s', error, pyuv.errno.strerror(error))
            evergreen.current.loop.call_soon(self.close)

    def __shutdown_cb(self, handle, error):
        if error is not None:
            self._shutdown_result.set_exception(
                self.error_cls(error, pyuv.errno.strerror(error)))
        else:
            self._shutdown_result.set_value(None)
Beispiel #13
0
class UDPEndpoint(object):
    def __init__(self):
        loop = evergreen.current.loop
        self._handle = pyuv.UDP(loop._loop)
        self._closed = False
        self._receive_result = Result()
        self._pending_writes = 0
        self._flush_event = Event()
        self._flush_event.set()
        self._sockname = None

    @property
    def sockname(self):
        self._check_closed()
        if self._sockname is None:
            self._sockname = self._handle.getsockname()
        return self._sockname

    def bind(self, addr):
        self._check_closed()
        self._handle.bind(addr)

    def send(self, data, addr):
        self._check_closed()
        self._handle.send(addr, data, self.__send_cb)
        if self._pending_writes == 0:
            self._flush_event.clear()
        self._pending_writes += 1

    def receive(self):
        self._check_closed()
        with self._receive_result:
            self._handle.start_recv(self.__receive_cb)
            return self._receive_result.get()

    def flush(self):
        self._check_closed()
        self._flush_event.wait()

    def close(self):
        if self._closed:
            return
        self._closed = True
        self._handle.close()

    def _check_closed(self):
        if self._closed:
            raise UDPError('endpoint is closed')

    def __send_cb(self, handle, error):
        self._pending_writes -= 1
        if self._pending_writes == 0:
            self._flush_event.set()
        if error is not None:
            log.debug('send failed: %d %s', error, pyuv.errno.strerror(error))
            evergreen.current.loop.call_soon(self.close)

    def __receive_cb(self, handle, addr, flags, data, error):
        self._handle.stop_recv()
        if error is not None:
            self._receive_result.set_exception(
                UDPError(error, errno.strerror(error)))
        else:
            self._receive_result.set_value((data, addr))