3
Z_ð]µ  ã               @   s`   d dl mZ d dlT d dlmZ d dlmZ d dlm	Z	 d dl
Z
G dd„ dejƒZe ZZ[dS )	é    )ÚSSL)Ú*)Ú	get_errno)Úgreenio)Ú
trampolineNc                   sd   e Zd ZdZd‡ fdd„	Zdd„ Zdd„ Zddd„Zdd„ ZeZ	dd„ Z
e
Zdd„ Zdd„ Z‡  ZS )ÚGreenConnectionz5 Nonblocking wrapper for SSL.Connection objects.
    Nc                s.   |d k	rt j||ƒ}n|}tt| ƒj|ƒ d S )N)Úorig_SSLÚ
ConnectionÚsuperÚConnectionTypeÚ__init__)ÚselfÚctxÚsockÚfd)Ú	__class__© ú>/tmp/pip-build-yz_zf6az/eventlet/eventlet/green/OpenSSL/SSL.pyr      s    zGreenConnection.__init__c             C   s„   | j r| jjƒ S xny
| jjƒ S  tk
rL   t| jjƒ d| jƒ tjd� Y q t	k
rz   t| jjƒ d| jƒ tjd� Y qX qW dS )z² 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. T)ÚreadÚtimeoutÚtimeout_exc)Úwriter   r   N)
Úact_non_blockingr   Údo_handshakeÚWantReadErrorr   ÚfilenoÚ
gettimeoutÚsocketr   ÚWantWriteError)r   r   r   r   r      s    



zGreenConnection.do_handshakec             C   s   t dƒ‚d S )Nz Dup not supported on SSL sockets)ÚNotImplementedError)r   r   r   r   Údup*   s    zGreenConnection.dupÚré   c             C   s   t dƒ‚d S )Nz%Makefile not supported on SSL sockets)r   )r   ÚmodeÚbufsizer   r   r   Úmakefile-   s    zGreenConnection.makefilec             C   sÄ   | j r| jj|ƒS x¬y| jj|ƒS  tk
rP   t| jjƒ d| jƒ tjd� Y q t	k
r~   t| jjƒ d| jƒ tjd� Y q t
k
rº } z"t|ƒdks¦t|ƒdkrªdS W Y dd}~X qX qW dS )	z‚Works like a blocking call to SSL_read(), whose behavior is
        described here:  http://www.openssl.org/docs/ssl/SSL_read.htmlT)r   r   r   )r   r   r   r"   r   Ú Néÿÿÿÿ)r   r   r   r   r   r   r   r   r   r   ZSysCallErrorr   )r   ÚsizeÚer   r   r   r   0   s$    

zGreenConnection.readc             C   s�   |sdS | j r| jj|ƒS xpy| jj|ƒS  tk
rX   t| jjƒ d| jƒ tjd� Y q t	k
r†   t| jjƒ d| jƒ tjd� Y qX qW dS )z„Works like a blocking call to SSL_write(), whose behavior is
        described here:  http://www.openssl.org/docs/ssl/SSL_write.htmlr   T)r   r   r   )r   r   r   N)
r   r   r   r   r   r   r   r   r   r   )r   Údatar   r   r   r   H   s"    

zGreenConnection.writec             C   s6   | j |ƒ}x&|t|ƒk r0|| j ||d… ƒ7 }qW dS )zÊSend "all" data on the connection. This calls send() repeatedly until
        all data is sent. If an error occurs, it's impossible to tell how much data
        has been sent.

        No return value.N)ÚsendÚlen)r   r*   Útailr   r   r   Úsendall_   s    
zGreenConnection.sendallc             C   s„   | j r| jjƒ S xny
| jjƒ S  tk
rL   t| jjƒ d| jƒ tjd� Y q t	k
rz   t| jjƒ d| jƒ tjd� Y qX qW d S )NT)r   r   r   )r   r   r   )
r   r   Úshutdownr   r   r   r   r   r   r   )r   r   r   r   r/   i   s    



zGreenConnection.shutdown)Nr'   )r!   r'   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r    r%   r   Úrecvr   r+   r.   r/   Ú__classcell__r   r   )r   r   r   	   s   	

r   )ZOpenSSLr   r   ZOpenSSL.SSLZeventlet.supportr   Zeventletr   Zeventlet.hubsr   r   ZGreenSocketr   r	   r   r   r   r   r   Ú<module>   s   q