B
    pjÆ`
  ã               @   sZ   d dl Z d dlmZ ye W n ek
r4   eZY nX G dd„ dƒZd	dd„Zdd„ ZdS )
é    N)ÚHostNotFoundc               @   sD   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )ÚSocketIOc          	   C   sf   || _ || _|j}y(| |j|jd¡ | |j|jd¡ W n& t|j	fk
r`   t
j d¡ Y nX d S )Né   é   z WARNING: cannot set socketoption)ÚsockÚ	execmodelÚsocketZ
setsockoptZSOL_IPZIP_TOSZSOL_TCPZTCP_NODELAYÚAttributeErrorÚerrorÚsysÚstderrÚwrite)Úselfr   r   r   © r   ú5lib/python3.7/site-packages/execnet/gateway_socket.pyÚ__init__   s    zSocketIO.__init__c             C   s@   t ƒ }x4t|ƒ|k r:| j |t|ƒ ¡}|s0t‚||7 }qW |S )z+Read exactly 'bytes' bytes from the socket.)ÚbytesÚlenr   ZrecvÚEOFError)r   ZnumbytesÚbufÚtr   r   r   Úread   s    zSocketIO.readc             C   s   | j  |¡ d S )N)r   Zsendall)r   Údatar   r   r   r   "   s    zSocketIO.writec             C   s0   y| j  d¡ W n | jjjk
r*   Y nX d S )Nr   )r   Úshutdownr   r   r
   )r   r   r   r   Ú
close_read%   s    zSocketIO.close_readc             C   s0   y| j  d¡ W n | jjjk
r*   Y nX d S )Nr   )r   r   r   r   r
   )r   r   r   r   Úclose_write+   s    zSocketIO.close_writec             C   s   d S )Nr   )r   r   r   r   Úwait1   s    zSocketIO.waitc             C   s   d S )Nr   )r   r   r   r   Úkill4   s    zSocketIO.killN)
Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   r   r   r   r   r      s   
r   c             C   sb   |dkrd\}}n|\}}ddl m} |  |¡}| ||f¡ | ¡ \}}|rV|dkrZd}||fS )z[return a host, port tuple,
    after instanciating a socketserver on the given gateway
    N)Ú	localhostr   r   )Úsocketserverz0.0.0.0r!   )Zexecnet.scriptr"   Zremote_execÚsendZreceive)ZgatewayZhostportÚhostÚportr"   ZchannelZrealhostZrealportr   r   r   Ú	start_via8   s    

r&   c       	      C   s´   | j rtdƒ‚| j}|r*t|| ƒ\}}n| j d¡\}}t|ƒ}|j}| |j|j¡}t	||ƒ}d||f |_
y| ||f¡ W n, |jjk
r®   ttt ¡ d ƒƒ‚Y nX |S )Nz7socket: specifying python executables not yet supportedú:z%s:%dr   )ZpythonÚAssertionErrorZ
installviar&   r   ÚsplitÚintZAF_INETZSOCK_STREAMr   ZremoteaddressZconnectZgaierrorr   Ústrr   Úexc_info)	ÚspecÚgroupr   Z
gateway_idr$   r%   r   r   Úior   r   r   Ú	create_ioN   s    
r0   )N)	r   Zexecnet.gateway_bootstrapr   r   Ú	NameErrorr+   r   r&   r0   r   r   r   r   Ú<module>   s   
,
