Ejemplo n.º 1
0
 def send(self, data, flags=0, timeout=timeout_default):
     if timeout is timeout_default:
         timeout = self.timeout
     if self._sslobj:
         if flags != 0:
             raise ValueError(
                 "non-zero flags not allowed in calls to send() on %s" %
                 self.__class__)
         while True:
             try:
                 v = self._sslobj.write(data)
             except SSLError, x:
                 if x.args[0] == SSL_ERROR_WANT_READ:
                     if self.timeout == 0.0:
                         raise timeout(str(x))
                     sys.exc_clear()
                     wait_read(self.fileno(), timeout=timeout)
                 elif x.args[0] == SSL_ERROR_WANT_WRITE:
                     if self.timeout == 0.0:
                         raise timeout(str(x))
                     sys.exc_clear()
                     wait_write(self.fileno(), timeout=timeout)
                 else:
                     raise
             else:
                 return v
Ejemplo n.º 2
0
 def write(self, data):
     """Write DATA to the underlying SSL channel.  Returns
     number of bytes of DATA actually transmitted."""
     while True:
         try:
             return self._sslobj.write(data)
         except SSLError, ex:
             if ex.args[0] == SSL_ERROR_WANT_READ:
                 if self.timeout == 0.0:
                     raise
                 sys.exc_clear()
                 try:
                     wait_read(self.fileno(),
                               timeout=self.timeout,
                               timeout_exc=_SSLErrorWriteTimeout,
                               event=self._read_event)
                 except socket_error, ex:
                     if ex[0] == EBADF:
                         return 0
                     raise
             elif ex.args[0] == SSL_ERROR_WANT_WRITE:
                 if self.timeout == 0.0:
                     raise
                 sys.exc_clear()
                 try:
                     wait_write(self.fileno(),
                                timeout=self.timeout,
                                timeout_exc=_SSLErrorWriteTimeout,
                                event=self._write_event)
                 except socket_error, ex:
                     if ex[0] == EBADF:
                         return 0
                     raise
Ejemplo n.º 3
0
 def read(self, len=1024):
     """Read up to LEN bytes and return them.
     Return zero-length string on EOF."""
     while True:
         try:
             return self._sslobj.read(len)
         except SSLError, ex:
             if ex.args[0] == SSL_ERROR_EOF and self.suppress_ragged_eofs:
                 return ''
             elif ex.args[0] == SSL_ERROR_WANT_READ:
                 if self.timeout == 0.0:
                     raise
                 sys.exc_clear()
                 try:
                     wait_read(self.fileno(), timeout=self.timeout, timeout_exc=_SSLErrorReadTimeout, event=self._read_event)
                 except socket_error, ex:
                     if ex[0] == EBADF:
                         return ''
                     raise
             elif ex.args[0] == SSL_ERROR_WANT_WRITE:
                 if self.timeout == 0.0:
                     raise
                 sys.exc_clear()
                 try:
                     # note: using _SSLErrorReadTimeout rather than _SSLErrorWriteTimeout below is intentional
                     wait_write(self.fileno(), timeout=self.timeout, timeout_exc=_SSLErrorReadTimeout, event=self._write_event)
                 except socket_error, ex:
                     if ex[0] == EBADF:
                         return ''
                     raise
Ejemplo n.º 4
0
Archivo: ssl.py Proyecto: strogo/pylibs
 def send(self, data, flags=0, timeout=timeout_default):
     if timeout is timeout_default:
         timeout = self.timeout
     if self._sslobj:
         if flags != 0:
             raise ValueError(
                 "non-zero flags not allowed in calls to send() on %s" %
                 self.__class__)
         while True:
             try:
                 v = self._sslobj.write(data)
             except SSLError, x:
                 if x.args[0] == SSL_ERROR_WANT_READ:
                     if self.timeout == 0.0:
                         raise timeout(str(x))
                     sys.exc_clear()
                     wait_read(self.fileno(), timeout=timeout)
                 elif x.args[0] == SSL_ERROR_WANT_WRITE:
                     if self.timeout == 0.0:
                         raise timeout(str(x))
                     sys.exc_clear()
                     wait_write(self.fileno(), timeout=timeout)
                 else:
                     raise
             else:
                 return v
Ejemplo n.º 5
0
Archivo: ssl.py Proyecto: 0rt/XX-Net
 def write(self, data):
     """Write DATA to the underlying SSL channel.  Returns
     number of bytes of DATA actually transmitted."""
     while True:
         try:
             return self._sslobj.write(data)
         except SSLError, ex:
             if ex.args[0] == SSL_ERROR_WANT_READ:
                 if self.timeout == 0.0:
                     raise
                 sys.exc_clear()
                 try:
                     wait_read(self.fileno(), timeout=self.timeout, timeout_exc=_SSLErrorWriteTimeout, event=self._read_event)
                 except socket_error, ex:
                     if ex[0] == EBADF:
                         return 0
                     raise
             elif ex.args[0] == SSL_ERROR_WANT_WRITE:
                 if self.timeout == 0.0:
                     raise
                 sys.exc_clear()
                 try:
                     wait_write(self.fileno(), timeout=self.timeout, timeout_exc=_SSLErrorWriteTimeout, event=self._write_event)
                 except socket_error, ex:
                     if ex[0] == EBADF:
                         return 0
                     raise
Ejemplo n.º 6
0
 def send(self, data, flags=0, timeout=timeout_default):
     if timeout is timeout_default:
         timeout = self.timeout
     if self._sslobj:
         if flags != 0:
             raise ValueError(
                 "non-zero flags not allowed in calls to send() on %s" %
                 self.__class__)
         while True:
             try:
                 v = self._sslobj.write(data)
             except SSLError, x:
                 if x.args[0] == SSL_ERROR_WANT_READ:
                     if self.timeout == 0.0:
                         return 0
                     sys.exc_clear()
                     try:
                         wait_read(self.fileno(), timeout=timeout, event=self._read_event)
                     except socket_error, ex:
                         if ex[0] == EBADF:
                             return 0
                         raise
                 elif x.args[0] == SSL_ERROR_WANT_WRITE:
                     if self.timeout == 0.0:
                         return 0
                     sys.exc_clear()
                     try:
                         wait_write(self.fileno(), timeout=timeout, event=self._write_event)
                     except socket_error, ex:
                         if ex[0] == EBADF:
                             return 0
                         raise
Ejemplo n.º 7
0
Archivo: ssl.py Proyecto: 0rt/XX-Net
 def _sslobj_shutdown(self):
     while True:
         try:
             return self._sslobj.shutdown()
         except SSLError, ex:
             if ex.args[0] == SSL_ERROR_EOF and self.suppress_ragged_eofs:
                 return ''
             elif ex.args[0] == SSL_ERROR_WANT_READ:
                 if self.timeout == 0.0:
                     raise
                 sys.exc_clear()
                 try:
                     wait_read(self.fileno(), timeout=self.timeout, timeout_exc=_SSLErrorReadTimeout, event=self._read_event)
                 except socket_error, ex:
                     if ex[0] == EBADF:
                         return ''
                     raise
             elif ex.args[0] == SSL_ERROR_WANT_WRITE:
                 if self.timeout == 0.0:
                     raise
                 sys.exc_clear()
                 try:
                     wait_write(self.fileno(), timeout=self.timeout, timeout_exc=_SSLErrorWriteTimeout, event=self._write_event)
                 except socket_error, ex:
                     if ex[0] == EBADF:
                         return ''
                     raise
Ejemplo n.º 8
0
 def _sslobj_shutdown(self):
     while True:
         try:
             return self._sslobj.shutdown()
         except SSLError, ex:
             if ex.args[0] == SSL_ERROR_EOF and self.suppress_ragged_eofs:
                 return ''
             elif ex.args[0] == SSL_ERROR_WANT_READ:
                 if self.timeout == 0.0:
                     raise
                 sys.exc_clear()
                 try:
                     wait_read(self.fileno(),
                               timeout=self.timeout,
                               timeout_exc=_SSLErrorReadTimeout,
                               event=self._read_event)
                 except socket_error, ex:
                     if ex[0] == EBADF:
                         return ''
                     raise
             elif ex.args[0] == SSL_ERROR_WANT_WRITE:
                 if self.timeout == 0.0:
                     raise
                 sys.exc_clear()
                 try:
                     wait_write(self.fileno(),
                                timeout=self.timeout,
                                timeout_exc=_SSLErrorWriteTimeout,
                                event=self._write_event)
                 except socket_error, ex:
                     if ex[0] == EBADF:
                         return ''
                     raise
Ejemplo n.º 9
0
def _gevent_sendfile(fdout, fdin, offset, nbytes):
    while True:
        try:
            return os.sendfile(fdout, fdin, offset, nbytes)
        except OSError as e:
            if e.args[0] == errno.EAGAIN:
                socket.wait_write(fdout)
            else:
                raise
Ejemplo n.º 10
0
def _gevent_sendfile(fdout, fdin, offset, nbytes):
    while True:
        try:
            return o_sendfile(fdout, fdin, offset, nbytes)
        except OSError as e:
            if e.args[0] == errno.EAGAIN:
                wait_write(fdout)
            else:
                raise
Ejemplo n.º 11
0
def popenNonblock(args, data='', stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=None, logFile=None):
    """Communicate with the process non-blockingly.

    If logFile is defined print the process's output line by line to that file
    """
    if logFile:
        f = open(logFile, 'a')
        stdout = f
        stderr = f
    p = subprocess.Popen(args, stdin=stdin, stdout=stdout, stderr=stderr, cwd=cwd)
    real_stdin = p.stdin if stdin == subprocess.PIPE else stdin
    fcntl.fcntl(real_stdin, fcntl.F_SETFL, os.O_NONBLOCK)  # make the file nonblocking
    real_stdout = p.stdout if stdout == subprocess.PIPE else stdout
    fcntl.fcntl(real_stdout, fcntl.F_SETFL, os.O_NONBLOCK)  # make the file nonblocking

    if data:
        bytes_total = len(data)
        bytes_written = 0
        while bytes_written < bytes_total:
            try:
                # p.stdin.write() doesn't return anything, so use os.write.
                bytes_written += os.write(p.stdin.fileno(), data[bytes_written:])
            except IOError:
                ex = sys.exc_info()[1]
                if ex.args[0] != errno.EAGAIN:
                    raise
                sys.exc_clear()
            socket.wait_write(p.stdin.fileno())
        p.stdin.close()

    chunks = []
    if stdout == subprocess.PIPE:
        while True:
            try:
                chunk = p.stdout.read(4096)
                if not chunk:
                    break
                chunks.append(chunk)
            except IOError:
                ex = sys.exc_info()[1]
                if ex[0] != errno.EAGAIN:
                    raise
                sys.exc_clear()
            socket.wait_read(p.stdout.fileno())
        p.stdout.close()

    output = ''.join(chunks)

    while True:
        returncode = p.poll()
        if returncode is not None:
            break
        else:
            gevent.sleep(1)

    return (returncode, output)
Ejemplo n.º 12
0
 def __iowait(self, io_func, *args, **kwargs):
     fd = self._sock.fileno()
     timeout = self._sock.gettimeout()
     while True:
         try:
             return io_func(*args, **kwargs)
         except (OpenSSL.SSL.WantReadError, OpenSSL.SSL.WantX509LookupError):
             wait_read(fd, timeout=timeout)
         except OpenSSL.SSL.WantWriteError:
             wait_write(fd, timeout=timeout)
Ejemplo n.º 13
0
 def _send_copy(self, msg, flags=0):
     flags |= __zmq__.NOBLOCK
     while True:
         try:
             super(Socket, self)._send_copy(msg, flags)
             return
         except __zmq__.ZMQError, e:
             if e.errno != EAGAIN:
                 raise
         wait_write(self.getsockopt(__zmq__.FD))
Ejemplo n.º 14
0
def _gevent_sendfile(fdout, fdin, offset, nbytes):
    while True:
        try:
            return os.sendfile(fdout, fdin, offset, nbytes)
        except OSError as e:
            if e.args[0] == errno.EAGAIN:
                # Block the current greenlet until *fileno* is ready to write
                socket.wait_write(fdout)
            else:
                raise
Ejemplo n.º 15
0
Archivo: job.py Proyecto: mdellavo/jobs
    def writer(self, f, queue):
        fd = f.fileno()

        while self.running:
            data = queue.get()

            total = len(data)
            while total > 0:
                socket.wait_write(fd)
                total -= os.write(fd, data)
Ejemplo n.º 16
0
 def push_statsd_metrics():
     collector = StatsdCollector(Collector(conn))
     while True:
         try:
             sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
             for line in collector.collect():
                 socket.wait_write(sock.fileno())
                 sock.sendto(six.b(line), (args.statsd_host, args.statsd_port))
         except Exception as e:
             logging.getLogger('vadvisor').error(e)
         sleep(args.statsd_interval)
def wait_callback(conn, timeout=None):
    while 1:
        state = conn.poll()
        if state == extensions.POLL_OK:
            return
        elif state == extensions.POLL_READ:
            socket.wait_read(conn.fileno(), timeout=timeout)
        elif state == extensions.POLL_WRITE:
            socket.wait_write(conn.fileno(), timeout=timeout)
        else:
            raise psycopg2.OperationalError("Bad result from poll: %r" % state)
Ejemplo n.º 18
0
 def do_handshake(self):
     while True:
         try:
             _Connection.do_handshake(self)
             break
         except WantReadError:
             exc_clear()
             wait_read(self._sock.fileno(), timeout=self._timeout)
         except WantWriteError:
             exc_clear()
             wait_write(self._sock.fileno(), timeout=self._timeout)
Ejemplo n.º 19
0
def wait_callback(conn, timeout=None):
    while True:
        state = conn.poll()
        if state == POLL_OK:
            break
        elif state == POLL_READ:
            wait_read(conn.fileno(), timeout=timeout)
        elif state == POLL_WRITE:
            wait_write(conn.fileno(), timeout=timeout)
        else:
            raise psycopg2.OperationalError("Bad result from poll: %r" % state)
Ejemplo n.º 20
0
 def connect(self, *args, **kwargs):
     while True:
         try:
             self._connection.connect(*args, **kwargs)
             break
         except (WantReadError, WantX509LookupError):
             sys.exc_clear()
             wait_read(self._sock.fileno(), timeout=self._timeout)
         except WantWriteError:
             sys.exc_clear()
             wait_write(self._sock.fileno(), timeout=self._timeout)
Ejemplo n.º 21
0
 def connect(self, *args, **kwargs):
     while True:
         try:
             _Connection.connect(self, *args, **kwargs)
             break
         except WantReadError:
             exc_clear()
             wait_read(self._sock.fileno(), timeout=self._timeout)
         except WantWriteError:
             exc_clear()
             wait_write(self._sock.fileno(), timeout=self._timeout)
Ejemplo n.º 22
0
 def __iowait(self, io_func, *args, **kwargs):
     fd = self._sock.fileno()
     timeout = self._sock.gettimeout()
     while True:
         try:
             return io_func(*args, **kwargs)
         except (OpenSSL.SSL.WantReadError,
                 OpenSSL.SSL.WantX509LookupError):
             wait_read(fd, timeout=timeout)
         except OpenSSL.SSL.WantWriteError:
             wait_write(fd, timeout=timeout)
Ejemplo n.º 23
0
def _psycopg_gevent_wait(conn, timeout=None):
    while True:
        state = conn.poll()
        if state == extensions.POLL_OK:
            break
        elif state == extensions.POLL_READ:
            wait_read(conn.fileno(), timeout=timeout)
        elif state == extensions.POLL_WRITE:
            wait_write(conn.fileno(), timeout=timeout)
        else:
            raise psycopg2.OperationalError("Bad result from poll: %r" % state)
Ejemplo n.º 24
0
 def connect(self, *args, **kwargs):
     while True:
         try:
             self._connection.connect(*args, **kwargs)
             break
         except (WantReadError, WantX509LookupError):
             sys.exc_clear()
             wait_read(self._sock.fileno(), timeout=self._timeout)
         except WantWriteError:
             sys.exc_clear()
             wait_write(self._sock.fileno(), timeout=self._timeout)
Ejemplo n.º 25
0
 def do_handshake(self):
     while True:
         try:
             _Connection.do_handshake(self)
             break
         except WantReadError:
             exc_clear()
             wait_read(self._sock.fileno(), timeout=self._timeout)
         except WantWriteError:
             exc_clear()
             wait_write(self._sock.fileno(), timeout=self._timeout)
Ejemplo n.º 26
0
 def connect(self, *args, **kwargs):
     while True:
         try:
             _Connection.connect(self, *args, **kwargs)
             break
         except WantReadError:
             exc_clear()
             wait_read(self._sock.fileno(), timeout=self._timeout)
         except WantWriteError:
             exc_clear()
             wait_write(self._sock.fileno(), timeout=self._timeout)
Ejemplo n.º 27
0
 def send_loop(self):
     while self.state == AsyncTCPClient.CONNECTED_STATE:
         try:
             msg = self.msg_queue.get(timeout=1)
         except gevent.queue.Empty:
             continue
         try:
             socket.wait_write(self.sock.fileno(), timeout=1)
             self.sock.sendall(str(msg))
         except _socket.error:
             continue
Ejemplo n.º 28
0
 def send_loop(self):
     while self.state == AsyncTCPClient.CONNECTED_STATE:
         try:
             msg = self.msg_queue.get(timeout=1)
         except gevent.queue.Empty:
             continue
         try:
             socket.wait_write(self.sock.fileno(), timeout=1)
             self.sock.sendall(str(msg))
         except _socket.error:
             continue
Ejemplo n.º 29
0
    def feed(self, data):
        """
        Write input data into the file descriptor.

        :param data: New data from the user
        :type data: string
        """

        wait_write(self.fd)
        self.stream_out.write(data.encode('utf-8'))
        self.stream_out.flush()
 def write(self, data):
     while len(data) > 0:
         try:
             bytes_written = os.write(self._fd, data)
             data = data[bytes_written:]
         except OSError as e:
             if e.errno == errno.EPIPE:
                 self.close()
                 raise IOError(e)
             if e.errno != errno.EAGAIN:
                 raise
             wait_write(self._fd)
Ejemplo n.º 31
0
def send_with_fd(dest_sock, message, subject_fd):
    buf = ffi.new('char[]', message)
    dest_fd = dest_sock.fileno()
    while 1:
        ret = C.send_with_fd(dest_fd, buf, len(message), subject_fd)
        if ret >= 0:
            break
        if -ret in (errno.EPERM, errno.EWOULDBLOCK, errno.EAGAIN):
            socket.wait_write(dest_fd)
        else:
            raise os.error(-ret, os.strerror(-ret))
    return ret
Ejemplo n.º 32
0
 def write(self, data):
     if data:
         bytes_total = len(data)
         bytes_written = 0
         while bytes_written < bytes_total:
             try:
                 bytes_written += os.write(self.fileno(), data[bytes_written:])
             except IOError, ex:
                 if ex[0] != errno.EAGAIN:
                     raise
                 sys.exc_clear()
             socket.wait_write(self.fileno())
Ejemplo n.º 33
0
def async_sendfile(fdout, fdin, offset, nbytes):
    total_sent = 0
    while total_sent < nbytes:
        try:
            sent = sendfile(fdout, fdin, offset + total_sent,
                            nbytes - total_sent)
            total_sent += sent
        except OSError, e:
            if e.args[0] == errno.EAGAIN:
                wait_write(fdout)
            else:
                raise
Ejemplo n.º 34
0
def gevent_wait_callback(conn, timeout=None):
    """A wait callback useful to allow gevent to work with Psycopg."""
    while 1:
        state = conn.poll()
        if state == extensions.POLL_OK:
            break
        elif state == extensions.POLL_READ:
            wait_read(conn.fileno(), timeout=timeout)
        elif state == extensions.POLL_WRITE:
            wait_write(conn.fileno(), timeout=timeout)
        else:
            raise psycopg2.OperationalError("Bad result from poll: %r" % state)
Ejemplo n.º 35
0
def _gevent_wait_callback(conn, timeout=None):
    # From https://github.com/gevent/gevent/blob/master/examples/psycopg2_pool.py#L19
    while 1:
        state = conn.poll()
        if state == extensions.POLL_OK:
            break
        elif state == extensions.POLL_READ:
            wait_read(conn.fileno(), timeout=timeout)
        elif state == extensions.POLL_WRITE:
            wait_write(conn.fileno(), timeout=timeout)
        else:
            raise OperationalError('Bad result from poll: %r' % state)
Ejemplo n.º 36
0
def send_with_fd(dest_sock, message, subject_fd):
    buf = ffi.new('char[]', message)
    dest_fd = dest_sock.fileno()
    while 1:
        ret = C.send_with_fd(dest_fd, buf, len(message), subject_fd)
        if ret >= 0:
            break
        if -ret in (errno.EPERM, errno.EWOULDBLOCK, errno.EAGAIN):
            socket.wait_write(dest_fd)
        else:
            raise os.error(-ret, os.strerror(-ret))
    return ret
Ejemplo n.º 37
0
 def push_statsd_metrics():
     collector = StatsdCollector(Collector(conn))
     while True:
         try:
             sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
             for line in collector.collect():
                 socket.wait_write(sock.fileno())
                 sock.sendto(six.b(line),
                             (args.statsd_host, args.statsd_port))
         except Exception as e:
             logging.getLogger('vadvisor').error(e)
         sleep(args.statsd_interval)
def wait_callback(conn, timeout=None):
    while 1:
        state = conn.poll()
        if state == extensions.POLL_OK:
            return
        elif state == extensions.POLL_READ:
            socket.wait_read(conn.fileno(), timeout=timeout)
        elif state == extensions.POLL_WRITE:
            socket.wait_write(conn.fileno(), timeout=timeout)
        else:
            raise psycopg2.OperationalError(
                "Bad result from poll: %r" % state)
Ejemplo n.º 39
0
def async_sendfile(fdout, fdin, offset, nbytes):
    total_sent = 0
    while total_sent < nbytes:
        try:
            sent = sendfile(fdout, fdin, offset + total_sent, 
                    nbytes - total_sent)
            total_sent += sent
        except OSError, e:
            if e.args[0] == errno.EAGAIN:
                wait_write(fdout)
            else:
                raise
Ejemplo n.º 40
0
def gevent_sendfile(out_fd, in_fd, offset, count):
    total_sent = 0
    while total_sent < count:
        try:
            _offset, sent = original_sendfile(out_fd, in_fd, offset + total_sent, count - total_sent)
            #print '%s: sent %s [%d%%]' % (out_fd, sent, 100*total_sent/count)
            total_sent += sent
        except OSError, ex:
            if ex[0] == EAGAIN:
                wait_write(out_fd)
            else:
                raise
Ejemplo n.º 41
0
def gevent_wait_callback(conn, timeout=None):
    """A wait callback useful to allow gevent to work with Psycopg."""
    while 1:
        state = conn.poll()
        if state == extensions.POLL_OK:
            break
        elif state == extensions.POLL_READ:
            wait_read(conn.fileno(), timeout=timeout)
        elif state == extensions.POLL_WRITE:
            wait_write(conn.fileno(), timeout=timeout)
        else:
            raise OperationalError("Bad result from poll: %r" % state)
Ejemplo n.º 42
0
 def send(self, data):
     """Writes raw outgoing data. Blocks until done."""
     if isinstance(data, unicode):
         data = data.encode('utf-8')
     elif not isinstance(data, str):
         data = str(data)
     socket.wait_write(self._sock.fileno())
     try:
         self._sock.sendall(data)
         logger.debug('sent: %s' % str(data).decode('utf-8'))
     except (socket_error, IOError):
         logger.debug('disconnected from server')
         raise
Ejemplo n.º 43
0
def gevent_sendfile(out_fd, in_fd, offset, count):
    total_sent = 0
    while total_sent < count:
        try:
            _offset, sent = original_sendfile(out_fd, in_fd,
                                              offset + total_sent,
                                              count - total_sent)
            #print '%s: sent %s [%d%%]' % (out_fd, sent, 100*total_sent/count)
            total_sent += sent
        except OSError, ex:
            if ex[0] == EAGAIN:
                wait_write(out_fd)
            else:
                raise
Ejemplo n.º 44
0
 def _write_pipe(self, f, input):
     # writes the given input to f without blocking
     if input:
         bytes_total = len(input)
         bytes_written = 0
         while bytes_written < bytes_total:
             try:
                 # f.write() doesn't return anything, so use os.write.
                 bytes_written += os.write(f.fileno(), input[bytes_written:])
             except IOError, ex:
                 if ex[0] != errno.EAGAIN:
                     raise
                 sys.exc_clear()
             socket.wait_write(f.fileno())
Ejemplo n.º 45
0
 def send_ack(self):
     """
     Send the SIP ACK as a notification that the MCU SDP offer is accepted by the browser.
     """
     reply = "ACK " + self.contact + " SIP/2.0\r\n" + \
             "Via: SIP/2.0/TCP " + self.hostname + ":" + str(self.port) + ";rport;branch=z9hG4bK1465117333\r\n" + \
             "Max-Forwards: 70\r\n" + \
             "To: " + self.to_hdr + "\r\n" + \
             "From: " + self.from_hdr + "\r\n" + \
             "Call-ID: " + str(self.callid) + "\r\n" + \
             "CSeq: 20 ACK\r\n" + \
             "Content-Length: 0\r\n\r\n"
     gsocket.wait_write(self.socket.fileno())
     self.socket.send(reply)
Ejemplo n.º 46
0
 def __iowait(self, io_func, *args, **kwargs):
     fd = self._sock.fileno()
     timeout = self._sock.gettimeout()
     while True:
         try:
             return io_func(*args, **kwargs)
         except (OpenSSL.SSL.WantReadError, OpenSSL.SSL.WantX509LookupError):
             wait_read(fd, timeout=timeout)
         except OpenSSL.SSL.WantWriteError:
             wait_write(fd, timeout=timeout)
         except OpenSSL.SSL.SysCallError as e:
             if e.args == (-1, 'Unexpected EOF'):
                 return b''
             raise
Ejemplo n.º 47
0
 def write(self, data):
     # use buffer
     bytes_total = len(data)
     bytes_written = 0
     fileno = self.fileno()
     while bytes_written < bytes_total:
         try:
             # fileobj.write() doesn't return anything, so use os.write.
             bytes_written += os.write(fileno, data[bytes_written:])
         except IOError, ex:
             if ex[0] != errno.EAGAIN:
                 raise
             sys.exc_clear()
         socket.wait_write(fileno)
Ejemplo n.º 48
0
 def send_ack(self):
     """
     Send the SIP ACK as a notification that the MCU SDP offer is accepted by the browser.
     """
     reply = "ACK " + self.contact + " SIP/2.0\r\n" + \
             "Via: SIP/2.0/TCP " + self.hostname + ":" + str(self.port) + ";rport;branch=z9hG4bK1465117333\r\n" + \
             "Max-Forwards: 70\r\n" + \
             "To: " + self.to_hdr + "\r\n" + \
             "From: " + self.from_hdr + "\r\n" + \
             "Call-ID: " + str(self.callid) + "\r\n" + \
             "CSeq: 20 ACK\r\n" + \
             "Content-Length: 0\r\n\r\n"
     gsocket.wait_write(self.socket.fileno())
     self.socket.send(reply)
Ejemplo n.º 49
0
 def recv(self, bufsiz, flags=0):
     pending = self._connection.pending()
     if pending:
         return self._connection.recv(min(pending, bufsiz))
     while True:
         try:
             return self._connection.recv(bufsiz, flags)
         except (WantReadError, WantX509LookupError):
             sys.exc_clear()
             wait_read(self._sock.fileno(), timeout=self._timeout)
         except WantWriteError:
             sys.exc_clear()
             wait_write(self._sock.fileno(), timeout=self._timeout)
         except ZeroReturnError:
             return ''
Ejemplo n.º 50
0
 def recv(self, bufsiz, flags=0):
     pending = _Connection.pending(self)
     if pending:
         return _Connection.recv(min(pending, bufsiz))
     while True:
         try:
             return _Connection.recv(self, buflen, flags)
         except WantReadError:
             exc_clear()
             wait_read(self._sock.fileno(), timeout=self._timeout)
         except WantWriteError:
             exc_clear()
             wait_write(self._sock.fileno(), timeout=self._timeout)
         except ZeroReturnError:
             return ''
Ejemplo n.º 51
0
 def do_handshake(self):
     while True:
         try:
             self._sock.do_handshake()
             break
         except SSL.WantReadError:
             sys.exc_clear()
             wait_read(self.fileno())
         except SSL.WantWriteError:
             sys.exc_clear()
             wait_write(self.fileno())
         except SSL.SysCallError, ex:
             raise sslerror(SysCallError_code_mapping.get(ex.args[0], ex.args[0]), ex.args[1])
         except SSL.Error, ex:
             raise sslerror(str(ex))
Ejemplo n.º 52
0
 def recv(self, bufsiz, flags=0):
     pending = self._connection.pending()
     if pending:
         return self._connection.recv(min(pending, bufsiz))
     while True:
         try:
             return self._connection.recv(bufsiz, flags)
         except (WantReadError, WantX509LookupError):
             sys.exc_clear()
             wait_read(self._sock.fileno(), timeout=self._timeout)
         except WantWriteError:
             sys.exc_clear()
             wait_write(self._sock.fileno(), timeout=self._timeout)
         except ZeroReturnError:
             return ''
Ejemplo n.º 53
0
def _writeAll(writefun, to, data):
    while data:
        try:
            if to is None:
                count = writefun(data)
            else:
                gsocket.wait_write(to)
                count = writefun(to, data)
            if count == 0:
                return False
            data = data[count:]
        except OSError as e:
            if e.errno not in (errno.EAGAIN, errno.EINTR):
                raise
    return True
Ejemplo n.º 54
0
def gevent_sendfile(out_fd, in_fd, offset, count):
    total_sent = 0
    sent = 0
    while total_sent < count:
        try:
            sent = original_sendfile(out_fd, in_fd, offset + total_sent,
                                     count - total_sent)
            total_sent += sent
        except OSError as ex:
            if ex.args[0] == EAGAIN:
                sent = 0
                # TODO: there is performance problem maybe
                wait_write(out_fd)
            else:
                raise
    return total_sent
Ejemplo n.º 55
0
 def gevent_wait_callback(conn, timeout=None):
     """A wait callback useful to allow gevent to work with Psycopg."""
     # Copyright (C) 2010-2012 Daniele Varrazzo <*****@*****.**>
     # This function is borrowed from psycogreen module which is licensed
     # under the BSD license (see in odoo/debian/copyright)
     while 1:
         state = conn.poll()
         if state == psycopg2.extensions.POLL_OK:
             break
         elif state == psycopg2.extensions.POLL_READ:
             wait_read(conn.fileno(), timeout=timeout)
         elif state == psycopg2.extensions.POLL_WRITE:
             wait_write(conn.fileno(), timeout=timeout)
         else:
             raise psycopg2.OperationalError("Bad result from poll: %r" %
                                             state)
Ejemplo n.º 56
0
def gevent_wait_callback(conn, timeout=None):
    """A wait callback useful to allow gevent to work with Psycopg."""

    while True:
        state = conn.poll()
        if state == extensions.POLL_OK:
            break
        elif state == extensions.POLL_READ:
            #print 'Greenlet waiting on READ'
            socket.wait_read(conn.fileno(), timeout=timeout)
        elif state == extensions.POLL_WRITE:
            #print 'Greenlet waiting on WRITE'
            socket.wait_write(conn.fileno(), timeout=timeout)
        else:
            raise psycopg2.OperationalError(
                "Bad result from poll: %r" % state)
Ejemplo n.º 57
0
 def send(self, data, flags=0):
     while True:
         try:
             _Connection.send(self, data, flags)
             break
         except WantReadError:
             exc_clear()
             wait_read(self._sock.fileno(), timeout=self._timeout)
         except WantWriteError:
             exc_clear()
             wait_write(self._sock.fileno(), timeout=self._timeout)
         except SysCallError as e:
             if e[0] == -1 and not data:
                 # errors when writing empty strings are expected and can be ignored
                 return 0
             raise
Ejemplo n.º 58
0
 def do_handshake(self):
     while True:
         try:
             self._sock.do_handshake()
             break
         except SSL.WantReadError:
             sys.exc_clear()
             wait_read(self.fileno())
         except SSL.WantWriteError:
             sys.exc_clear()
             wait_write(self.fileno())
         except SSL.SysCallError, ex:
             raise sslerror(
                 SysCallError_code_mapping.get(ex.args[0], ex.args[0]),
                 ex.args[1])
         except SSL.Error, ex:
             raise sslerror(str(ex))