예제 #1
0
    def __init__ (self, f, mode = 'r', bufsize = -1):

        self.uv_hub = proxy(get_hub())

        if not isinstance(f, (basestring, int, file)):
            raise TypeError('f(ile) should be int, str, unicode or file, not %r' % f)

        if isinstance(f, basestring):
            self._path = f
            f = open(f, mode, 0)
            fileno = f.fileno()

        if isinstance(f, int):
            fileno = f
            self._name = "<fd:%d>" % fileno
            self._path = None
        else:
            fileno = _os_orig.dup(f.fileno())
            self._name = self._path = f.name
            if f.mode != mode:
                raise ValueError('file.mode %r does not match mode parameter %r' % (f.mode, mode))
            f.close()       ## close the file provided: we keep our dupped version...

        assert isinstance(fileno, int)
        self._fileobj = _os_orig.fdopen(fileno, mode)
        
        super(GreenPipe, self).__init__(_SocketDuckForFd(fileno), mode, bufsize)
        set_nonblocking(self)
        self.softspace = 0
예제 #2
0
    def __init__(self,
                 family=socket.AF_INET,
                 type=socket.SOCK_STREAM,
                 proto=0,
                 _sock=None,
                 _hub=None):
        """
        Initialize the UV socket

        :param family_or_realsock: a socket descriptor or a socket family
        """
        self.uv_fd = None
        self.uv_handle = None
        self.uv_hub = None
        self.uv_recv_string = ''  # buffer for receiving data...

        if isinstance(family, (int, long)):
            self.uv_fd = _original_socket(family, type, proto, _sock)
        elif isinstance(family, GreenSocket):
            _sock = family
            self.uv_fd = _sock.uv_fd
            if hasattr(_sock, 'uv_hub') and _sock.uv_hub:
                _hub = _sock.uv_hub
        else:
            _sock = family
            self.uv_fd = _sock

        if not self.uv_hub:
            if _hub:
                self.uv_hub = _hub
            else:
                self.uv_hub = weakref.proxy(get_hub())

        ## check if the socket type is supported by pyUV and we can create a pyUV socket...
        if not self.uv_handle:
            if self.type == socket.SOCK_STREAM:
                self.uv_handle = pyuv.TCP(self.uv_hub.uv_loop)
                self.uv_handle.open(self.fileno())
            elif self.type == socket.SOCK_DGRAM:
                self.uv_handle = pyuv.UDP(self.uv_hub.uv_loop)
                self.uv_handle.open(self.fileno())

        # import timeout from other socket, if it was there
        try:
            self._timeout = self.uv_fd.gettimeout(
            ) or socket.getdefaulttimeout()
        except AttributeError:
            self._timeout = socket.getdefaulttimeout()

        assert self.uv_fd, 'the socket descriptor must be not null'

        set_nonblocking(self.uv_fd)

        # when client calls setblocking(0) or settimeout(0) the socket must act non-blocking
        self.act_non_blocking = False
예제 #3
0
파일: sockets.py 프로젝트: inercia/evy
    def dup (self, *args, **kw):
        sock = self.uv_fd.dup(*args, **kw)
        set_nonblocking(sock)
        newsock = type(self)(sock)
        newsock.settimeout(self.gettimeout())

        #if self.uv_handle:
        #    new_handle = pyuv.TCP(self.uv_hub.uv_loop)
        #    return GreenSocket(family = socket.AF_INET, type = socket.SOCK_STREAM, uv_hub = self.uv_hub, uv_handle = new_handle)

        return newsock
예제 #4
0
    def dup(self, *args, **kw):
        sock = self.uv_fd.dup(*args, **kw)
        set_nonblocking(sock)
        newsock = type(self)(sock)
        newsock.settimeout(self.gettimeout())

        #if self.uv_handle:
        #    new_handle = pyuv.TCP(self.uv_hub.uv_loop)
        #    return GreenSocket(family = socket.AF_INET, type = socket.SOCK_STREAM, uv_hub = self.uv_hub, uv_handle = new_handle)

        return newsock
예제 #5
0
파일: sockets.py 프로젝트: inercia/evy
    def __init__ (self, family = socket.AF_INET, type = socket.SOCK_STREAM, proto = 0, _sock = None,
                  _hub = None):
        """
        Initialize the UV socket

        :param family_or_realsock: a socket descriptor or a socket family
        """
        self.uv_fd = None
        self.uv_handle = None
        self.uv_hub = None
        self.uv_recv_string = ''                    # buffer for receiving data...

        if isinstance(family, (int, long)):
            self.uv_fd = _original_socket(family, type, proto, _sock)
        elif isinstance(family, GreenSocket):
            _sock = family
            self.uv_fd = _sock.uv_fd
            if hasattr(_sock, 'uv_hub') and _sock.uv_hub:
                _hub = _sock.uv_hub
        else:
            _sock = family
            self.uv_fd = _sock

        if not self.uv_hub:
            if _hub:
                self.uv_hub = _hub
            else:
                self.uv_hub = weakref.proxy(get_hub())

        ## check if the socket type is supported by pyUV and we can create a pyUV socket...
        if not self.uv_handle:
            if self.type == socket.SOCK_STREAM:
                self.uv_handle = pyuv.TCP(self.uv_hub.uv_loop)
                self.uv_handle.open(self.fileno())
            elif self.type == socket.SOCK_DGRAM:
                self.uv_handle = pyuv.UDP(self.uv_hub.uv_loop)
                self.uv_handle.open(self.fileno())

        # import timeout from other socket, if it was there
        try:
            self._timeout = self.uv_fd.gettimeout() or socket.getdefaulttimeout()
        except AttributeError:
            self._timeout = socket.getdefaulttimeout()

        assert self.uv_fd, 'the socket descriptor must be not null'

        set_nonblocking(self.uv_fd)

        # when client calls setblocking(0) or settimeout(0) the socket must act non-blocking
        self.act_non_blocking = False
예제 #6
0
파일: sockets.py 프로젝트: inercia/evy
 def accept (self):
     """
     Accept a new connection when we are listening
     :return: a socket and remote address pair
     """
     if self.act_non_blocking:
         return self.uv_fd.accept()
     else:
         fd = self.uv_fd
         while True:
             res = socket_accept(fd)
             if not res:
                 wait_read(fd, self.gettimeout(), socket.timeout("timed out"))
             else:
                 client, addr = res
                 set_nonblocking(client)
                 return GreenSocket(client, _hub = self.uv_hub), addr
예제 #7
0
 def accept(self):
     """
     Accept a new connection when we are listening
     :return: a socket and remote address pair
     """
     if self.act_non_blocking:
         return self.uv_fd.accept()
     else:
         fd = self.uv_fd
         while True:
             res = socket_accept(fd)
             if not res:
                 wait_read(fd, self.gettimeout(),
                           socket.timeout("timed out"))
             else:
                 client, addr = res
                 set_nonblocking(client)
                 return GreenSocket(client, _hub=self.uv_hub), addr
예제 #8
0
파일: ssl.py 프로젝트: inercia/evy
 def accept (self):
     """
     Accepts a new connection from a remote client, and returns
     a tuple containing that new connection wrapped with a server-side
     SSL channel, and the address of the remote client.
     """
     # RDW grr duplication of code from greenio
     if self.act_non_blocking:
         newsock, addr = socket.accept(self)
     else:
         while True:
             try:
                 newsock, addr = socket.accept(self)
                 set_nonblocking(newsock)
                 break
             except orig_socket.error, e:
                 if get_errno(e) != errno.EWOULDBLOCK:
                     raise
                 trampoline(self, read = True, timeout = self.gettimeout(),
                            timeout_exc = timeout_exc('timed out'))