o
    2.a[                     @   sd   d dl Z d dlZd dlZd dlZd dlZdZdZd ZdZdd Z	G dd dZ
G d	d
 d
ejZdS )    N   i   
   c                    s   t   fdd}|S )Nc                    s$   | j rtd | g|R i |S )Nz1Can not reuse socket after connection was closed.)_closedRuntimeError)selfargskwargsf >/usr/lib/python3/dist-packages/docker/transport/npipesocket.pywrapped   s
   zcheck_closed.<locals>.wrapped)	functoolswraps)r
   r   r   r	   r   check_closed   s   r   c                   @   s,  e Zd ZdZd:ddZdd Zdd Zd	d
 Zed;ddZ	edd Z
edd Zedd Zdd Zdd Zd:ddZdd Zdd Zd<ddZed;d d!Zed;d"d#Zed=d$d%Zed;d&d'Zd(d) Zed;d*d+Zed;d,d-Zed.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zed8d9 ZdS )>NpipeSocketz Partial implementation of the socket API over windows named pipes.
        This implementation is only designed to be used as a client socket,
        and server-specific methods (bind, listen, accept...) are not
        implemented.
    Nc                 C   s   t j| _|| _d| _d S NF)	win32pipeZNMPWAIT_USE_DEFAULT_WAIT_timeout_handler   )r   handler   r   r   __init__!   s   
zNpipeSocket.__init__c                 C      t  NNotImplementedErrorr   r   r   r   accept&      zNpipeSocket.acceptc                 C   r   r   r   r   addressr   r   r   bind)   r   zNpipeSocket.bindc                 C   s   | j   d| _d S NT)r   ZCloser   r   r   r   r   close,      

zNpipeSocket.closer   c              
   C   s   zt |t jt jB dd t jttB d}W n- tjyA } z |j	t
kr;|d }|tk r;td | ||W  Y d }~S |d }~ww t|d | _|| _|| _d S )Nr      )	win32fileZ
CreateFileZGENERIC_READZGENERIC_WRITEZOPEN_EXISTINGcSECURITY_ANONYMOUScSECURITY_SQOS_PRESENTr   errorwinerrorcERROR_PIPE_BUSYMAXIMUM_RETRY_COUNTtimesleepconnectZGetNamedPipeInfoflagsr   _address)r   r    Zretry_countr   er   r   r   r/   0   s,   
	


zNpipeSocket.connectc                 C   s
   |  |S r   )r/   r   r   r   r   
connect_exN      
zNpipeSocket.connect_exc                 C   s   d| _ | jS r"   )r   r   r   r   r   r   detachR   s   zNpipeSocket.detachc                 C   s
   t | jS r   )r   r   r   r   r   r   dupW   r4   zNpipeSocket.dupc                 C      | j S r   r1   r   r   r   r   getpeername[   r   zNpipeSocket.getpeernamec                 C   r7   r   r8   r   r   r   r   getsockname^   r   zNpipeSocket.getsocknamec                 C   r   r   r   )r   leveloptnamebuflenr   r   r   
getsockopta   r   zNpipeSocket.getsockoptc                 C   r   r   r   )r   Zcontroloptionr   r   r   ioctld   r   zNpipeSocket.ioctlc                 C   r   r   r   )r   Zbacklogr   r   r   listeng   r   zNpipeSocket.listenc                 C   s@   | ddkr
t t| }|d u s|dkrtj}tj||dS )Nbrr   )Zbuffer_size)stripr   NpipeFileIOBaseioDEFAULT_BUFFER_SIZEBufferedReader)r   modebufsizeZrawior   r   r   makefilej   s   zNpipeSocket.makefilec                 C      t | j|\}}|S r   )r&   ReadFiler   )r   rJ   r0   errdatar   r   r   recvr      zNpipeSocket.recvc                 C   s   |  ||}|| jfS r   )rP   r1   )r   rJ   r0   rO   r   r   r   recvfromw   s   
zNpipeSocket.recvfromc                 C   s   |  |||| jfS r   )	recv_intor1   )r   bufnbytesr0   r   r   r   recvfrom_into|   s   zNpipeSocket.recvfrom_intoc                 C   s@   |}t |tst|}t| j|r|d | n|\}}t|S r   )
isinstance
memoryviewr&   rM   r   len)r   rT   rU   ZreadbufrN   rO   r   r   r   rS      s   
zNpipeSocket.recv_intoc                 C   s2   t | j|p	t|\}}t|}||d |< |S r   )r&   rM   r   rY   )r   rT   rU   rN   rO   nr   r   r   _recv_into_py2   s   zNpipeSocket._recv_into_py2c                 C   rL   r   )r&   Z	WriteFiler   )r   stringr0   rN   rU   r   r   r   send   rQ   zNpipeSocket.sendc                 C   s   |  ||S r   )r]   )r   r\   r0   r   r   r   sendall   s   zNpipeSocket.sendallc                 C   s   |  | | |S r   )r/   r]   )r   r\   r    r   r   r   sendto   s   

zNpipeSocket.sendtoc                 C   s   |r|  d S |  dS )Nr   )
settimeout)r   flagr   r   r   setblocking   s   

zNpipeSocket.setblockingc                 C   sT   |d u r
t j| _d S t|ttfr|dk rtd|dkr#t j| _d S |d | _d S )Nr   zTimeout value out of rangei  )r   ZNMPWAIT_WAIT_FOREVERr   rW   floatint
ValueErrorZNMPWAIT_NO_WAIT)r   valuer   r   r   r`      s   zNpipeSocket.settimeoutc                 C   r7   r   )r   r   r   r   r   
gettimeout   r   zNpipeSocket.gettimeoutc                 C   r   r   r   )r   r;   r<   rf   r   r   r   
setsockopt   r   zNpipeSocket.setsockoptc                 C   s   |   S r   )r#   )r   Zhowr   r   r   shutdown   s   zNpipeSocket.shutdownr   )r   )NN)r   r   ) __name__
__module____qualname____doc__r   r   r!   r#   r   r/   r3   r5   r6   r9   r:   r>   r@   rA   rK   rP   rR   rV   rS   r[   r]   r^   r_   rb   r`   rg   rh   ri   r   r   r   r   r      sR    






r   c                       sT   e Zd Zdd Z fddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
  ZS )rE   c                 C   s
   || _ d S r   )sock)r   Znpipe_socketr   r   r   r         
zNpipeFileIOBase.__init__c                    s   t    d | _d S r   )superr#   rn   r   	__class__r   r   r#      r$   zNpipeFileIOBase.closec                 C   s
   | j  S r   )rn   filenor   r   r   r   rs      ro   zNpipeFileIOBase.filenoc                 C      dS r   r   r   r   r   r   isatty      zNpipeFileIOBase.isattyc                 C   rt   r"   r   r   r   r   r   readable   rv   zNpipeFileIOBase.readablec                 C   s   | j |S r   )rn   rS   )r   rT   r   r   r   readinto   s   zNpipeFileIOBase.readintoc                 C   rt   r   r   r   r   r   r   seekable   rv   zNpipeFileIOBase.seekablec                 C   rt   r   r   r   r   r   r   writable   rv   zNpipeFileIOBase.writable)rj   rk   rl   r   r#   rs   ru   rw   rx   ry   rz   __classcell__r   r   rq   r   rE      s    rE   )r   r-   rF   r&   r   r+   r(   r'   r,   r   r   	RawIOBaserE   r   r   r   r   <module>   s     $