def sendall (self, data, flags = 0): # *NOTE: gross, copied code from ssl.py becase it's not factored well enough to be used as-is if self._sslobj: if flags != 0: raise ValueError( "non-zero flags not allowed in calls to sendall() on %s" % self.__class__) amount = len(data) count = 0 while (count < amount): v = self.send(data[count:]) count += v return amount else: while True: try: return socket.sendall(self, buflen, flags) except orig_socket.error, e: if self.act_non_blocking: raise if get_errno(e) == errno.EWOULDBLOCK: trampoline(self, write = True, timeout = self.gettimeout(), timeout_exc = timeout_exc('timed out')) if get_errno(e) in SOCKET_CLOSED: return '' raise
def send (self, data, flags = 0): if self._sslobj: return self._call_trampolining( super(GreenSSLSocket, self).send, data, flags) else: trampoline(self, write = True, timeout_exc = timeout_exc('timed out')) return socket.send(self, data, flags)
def sendto (self, data, addr, flags = 0): # *NOTE: gross, copied code from ssl.py becase it's not factored well enough to be used as-is if self._sslobj: raise ValueError("sendto not allowed on instances of %s" % self.__class__) else: trampoline(self, write = True, timeout_exc = timeout_exc('timed out')) return socket.sendto(self, data, addr, flags)
def go(): desc = connect(('127.0.0.1', bound_port)) try: hubs.trampoline(desc, read=True, timeout=0.1) except TimeoutError: assert False, "Timed out" server.close() desc.close() done.send()
def go (): desc = connect(('127.0.0.1', bound_port)) try: hubs.trampoline(desc, read = True, timeout = 0.1) except TimeoutError: assert False, "Timed out" server.close() desc.close() done.send()
def evy_wait_callback (conn, timeout = -1): """A wait callback useful to allow evy to work with Psycopg.""" while 1: state = conn.poll() if state == extensions.POLL_OK: break elif state == extensions.POLL_READ: trampoline(conn.fileno(), read = True) elif state == extensions.POLL_WRITE: trampoline(conn.fileno(), write = True) else: raise psycopg2.OperationalError( "Bad result from poll: %r" % state)
def shutdown(self): if self.act_non_blocking: return self.fd.shutdown() while True: try: return self.fd.shutdown() except WantReadError: trampoline(self.fd.fileno(), read=True, timeout=self.gettimeout(), timeout_exc=socket.timeout) except WantWriteError: trampoline(self.fd.fileno(), write=True, timeout=self.gettimeout(), timeout_exc=socket.timeout)
def shutdown (self): if self.act_non_blocking: return self.fd.shutdown() while True: try: return self.fd.shutdown() except WantReadError: trampoline(self.fd.fileno(), read = True, timeout = self.gettimeout(), timeout_exc = socket.timeout) except WantWriteError: trampoline(self.fd.fileno(), write = True, timeout = self.gettimeout(), timeout_exc = socket.timeout)
def _socket_connect (self, addr): real_connect = socket.connect if self.act_non_blocking: return real_connect(self, addr) else: # *NOTE: gross, copied code from greenio because it's not factored # well enough to reuse if self.gettimeout() is None: while True: try: return real_connect(self, addr) except orig_socket.error, exc: if get_errno(exc) in CONNECT_ERR: trampoline(self, write = True) elif get_errno(exc) in CONNECT_SUCCESS: return else: raise else:
def do_handshake(self): """ Perform an SSL handshake (usually called after renegotiate or one of set_accept_state or set_accept_state). This can raise the same exceptions as send and recv. """ if self.act_non_blocking: return self.fd.do_handshake() while True: try: return self.fd.do_handshake() except WantReadError: trampoline(self.fd.fileno(), read=True, timeout=self.gettimeout(), timeout_exc=socket.timeout) except WantWriteError: trampoline(self.fd.fileno(), write=True, timeout=self.gettimeout(), timeout_exc=socket.timeout)
def do_handshake (self): """ Perform an SSL handshake (usually called after renegotiate or one of set_accept_state or set_accept_state). This can raise the same exceptions as send and recv. """ if self.act_non_blocking: return self.fd.do_handshake() while True: try: return self.fd.do_handshake() except WantReadError: trampoline(self.fd.fileno(), read = True, timeout = self.gettimeout(), timeout_exc = socket.timeout) except WantWriteError: trampoline(self.fd.fileno(), write = True, timeout = self.gettimeout(), timeout_exc = socket.timeout)
def _call_trampolining (self, func, *a, **kw): if self.act_non_blocking: return func(*a, **kw) else: while True: try: return func(*a, **kw) except __ssl.SSLError, exc: if get_errno(exc) == __ssl.SSL_ERROR_WANT_READ: trampoline(self, read = True, timeout = self.gettimeout(), timeout_exc = timeout_exc('timed out')) elif get_errno(exc) == __ssl.SSL_ERROR_WANT_WRITE: trampoline(self, write = True, timeout = self.gettimeout(), timeout_exc = timeout_exc('timed out')) else: raise
def write (self, data): """Works like a blocking call to SSL_write(), whose behavior is described here: http://www.openssl.org/docs/ssl/SSL_write.html""" if not data: return 0 # calling SSL_write() with 0 bytes to be sent is undefined if self.act_non_blocking: return self.fd.write(data) while True: try: return self.fd.write(data) except WantReadError: trampoline(self.fd.fileno(), read = True, timeout = self.gettimeout(), timeout_exc = socket.timeout) except WantWriteError: trampoline(self.fd.fileno(), write = True, timeout = self.gettimeout(), timeout_exc = socket.timeout)
def write(self, data): """Works like a blocking call to SSL_write(), whose behavior is described here: http://www.openssl.org/docs/ssl/SSL_write.html""" if not data: return 0 # calling SSL_write() with 0 bytes to be sent is undefined if self.act_non_blocking: return self.fd.write(data) while True: try: return self.fd.write(data) except WantReadError: trampoline(self.fd.fileno(), read=True, timeout=self.gettimeout(), timeout_exc=socket.timeout) except WantWriteError: trampoline(self.fd.fileno(), write=True, timeout=self.gettimeout(), timeout_exc=socket.timeout)
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'))
def test_001_trampoline_timeout (self): server_sock = listen(('127.0.0.1', 0)) bound_port = server_sock.getsockname()[1] def server (sock): client, addr = sock.accept() sleep(0.1) server_evt = spawn(server, server_sock) sleep(0) try: desc = connect(('127.0.0.1', bound_port)) hubs.trampoline(desc, read = True, write = False, timeout = 0.001) except TimeoutError: pass # test passed else: assert False, "Didn't timeout" server_evt.wait() check_hub()
def read(self, size): """Works like a blocking call to SSL_read(), whose behavior is described here: http://www.openssl.org/docs/ssl/SSL_read.html""" if self.act_non_blocking: return self.fd.read(size) while True: try: return self.fd.read(size) except WantReadError: trampoline(self.fd.fileno(), read=True, timeout=self.gettimeout(), timeout_exc=socket.timeout) except WantWriteError: trampoline(self.fd.fileno(), write=True, timeout=self.gettimeout(), timeout_exc=socket.timeout) except SysCallError, e: if get_errno(e) == -1 or get_errno(e) > 0: return ''
def read (self, size): """Works like a blocking call to SSL_read(), whose behavior is described here: http://www.openssl.org/docs/ssl/SSL_read.html""" if self.act_non_blocking: return self.fd.read(size) while True: try: return self.fd.read(size) except WantReadError: trampoline(self.fd.fileno(), read = True, timeout = self.gettimeout(), timeout_exc = socket.timeout) except WantWriteError: trampoline(self.fd.fileno(), write = True, timeout = self.gettimeout(), timeout_exc = socket.timeout) except SysCallError, e: if get_errno(e) == -1 or get_errno(e) > 0: return ''
def test_001_trampoline_timeout(self): server_sock = listen(('127.0.0.1', 0)) bound_port = server_sock.getsockname()[1] def server(sock): client, addr = sock.accept() sleep(0.1) server_evt = spawn(server, server_sock) sleep(0) try: desc = connect(('127.0.0.1', bound_port)) hubs.trampoline(desc, read=True, write=False, timeout=0.001) except TimeoutError: pass # test passed else: assert False, "Didn't timeout" server_evt.wait() check_hub()
def recv (self, buflen = 1024, flags = 0): # *NOTE: gross, copied code from ssl.py becase it's not factored well enough to be used as-is if self._sslobj: if flags != 0: raise ValueError( "non-zero flags not allowed in calls to recv() on %s" % self.__class__) read = self.read(buflen) return read else: while True: try: return socket.recv(self, buflen, flags) except orig_socket.error, e: if self.act_non_blocking: raise if get_errno(e) == errno.EWOULDBLOCK: trampoline(self, read = True, timeout = self.gettimeout(), timeout_exc = timeout_exc('timed out')) if get_errno(e) in SOCKET_CLOSED: return '' raise
def read(fd, n): """ read(fd, buffersize) -> string Read a file descriptor.""" while True: try: return __original_read__(fd, n) except (OSError, IOError), e: if get_errno(e) != errno.EAGAIN: raise except socket.error, e: if get_errno(e) == errno.EPIPE: return '' raise hubs.trampoline(fd, read=True) __original_write__ = os_orig.write def write(fd, st): """ write(fd, string) -> byteswritten Write a string to a file descriptor. """ while True: try: return __original_write__(fd, st) except (OSError, IOError), e:
did_connect.send_exception( last_socket_error(error, msg = 'connect error')) else: did_connect.send(0) except Exception, e: did_connect.send_exception(e) self.uv_handle.connect(resolve_address(address), connect_callback) did_connect.wait(self.gettimeout(), socket.timeout(errno.ETIME, "timed out")) except pyuv.error.TCPError, e: raise socket.error(*last_socket_error(e.args[0], msg = 'connect error')) else: fd = self.uv_fd if self.gettimeout() is None: while not socket_connect(fd, address): trampoline(fd, write = True) socket_checkerr(fd) else: end = time.time() + self.gettimeout() while True: if socket_connect(fd, address): return if time.time() >= end: raise socket.timeout("timed out") wait_write(fd, end - time.time(), socket.timeout("timed out")) socket_checkerr(fd) def connect_ex (self, address): """ Connects to a remote address not raising exceptions :param address: the remote address, as a IP and port tuple
def read (fd, n): """ read(fd, buffersize) -> string Read a file descriptor.""" while True: try: return __original_read__(fd, n) except (OSError, IOError), e: if get_errno(e) != errno.EAGAIN: raise except socket.error, e: if get_errno(e) == errno.EPIPE: return '' raise hubs.trampoline(fd, read = True) __original_write__ = os_orig.write def write (fd, st): """ write(fd, string) -> byteswritten Write a string to a file descriptor. """ while True: try: return __original_write__(fd, st) except (OSError, IOError), e: if get_errno(e) != errno.EAGAIN: raise
did_connect.send(0) except Exception, e: did_connect.send_exception(e) self.uv_handle.connect(resolve_address(address), connect_callback) did_connect.wait(self.gettimeout(), socket.timeout(errno.ETIME, "timed out")) except pyuv.error.TCPError, e: raise socket.error( *last_socket_error(e.args[0], msg='connect error')) else: fd = self.uv_fd if self.gettimeout() is None: while not socket_connect(fd, address): trampoline(fd, write=True) socket_checkerr(fd) else: end = time.time() + self.gettimeout() while True: if socket_connect(fd, address): return if time.time() >= end: raise socket.timeout("timed out") wait_write(fd, end - time.time(), socket.timeout("timed out")) socket_checkerr(fd) def connect_ex(self, address): """ Connects to a remote address not raising exceptions
def recvfrom_into (self, buffer, nbytes = None, flags = 0): if not self.act_non_blocking: trampoline(self, read = True, timeout = self.gettimeout(), timeout_exc = timeout_exc('timed out')) return super(GreenSSLSocket, self).recvfrom_into(buffer, nbytes, flags)
def recvfrom (self, addr, buflen = 1024, flags = 0): if not self.act_non_blocking: trampoline(self, read = True, timeout = self.gettimeout(), timeout_exc = timeout_exc('timed out')) return super(GreenSSLSocket, self).recvfrom(addr, buflen, flags)
except orig_socket.error, exc: if get_errno(exc) in CONNECT_ERR: trampoline(self, write = True) elif get_errno(exc) in CONNECT_SUCCESS: return else: raise else: end = time.time() + self.gettimeout() while True: try: real_connect(self, addr) except orig_socket.error, exc: if get_errno(exc) in CONNECT_ERR: trampoline(self, write = True, timeout = end - time.time(), timeout_exc = timeout_exc('timed out')) elif get_errno(exc) in CONNECT_SUCCESS: return else: raise if time.time() >= end: raise timeout_exc('timed out') def connect (self, addr): """ Connects to remote ADDR, and then wraps the connection in an SSL channel.""" # *NOTE: grrrrr copied this code from ssl.py because of the reference # to socket.connect which we don't want to call directly