o
    Ikc                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 zd dl
mZmZmZmZmZ W n	 ey=   Y nw eeZe Zd aG dd de jZG dd deZG d	d
 d
eZe	sheZdS eZdS )    N)Path)is_win32)windllcastc_ulongc_void_pbyrefc                   @   sb   e Zd ZU eed< dd ZejdddZejddd	Z	ejde
fd
dZejdddZdS )NamedPipeBasepathc              
   C   sn   t  td7 adt  dt dtdd | _W d    n1 s#w   Y  td| j  | 	  d S )N   zstreamlinkpipe--r   i'  zCreating pipe )
_lock_idosgetpidrandomrandintnameloginfo_createself r   =/usr/lib/python3/dist-packages/streamlink/utils/named_pipe.py__init__   s   &zNamedPipeBase.__init__returnNc                 C      t NNotImplementedErrorr   r   r   r   r   "      zNamedPipeBase._createc                 C   r   r   r   r   r   r   r   open&   r!   zNamedPipeBase.openc                 C   r   r   r   r   datar   r   r   write*   r!   zNamedPipeBase.writec                 C   r   r   r   r   r   r   r   close.   r!   zNamedPipeBase.close)r   N)__name__
__module____qualname__r   __annotations__r   abcabstractmethodr   r"   intr%   r&   r   r   r   r   r	      s   
 r	   c                   @   s8   e Zd ZdZdZdZdd Zdd Zdd	 Zd
d Z	dS )NamedPipePosixwbi  Nc                 C   s&   t t | j| _t| j| j d S r   )r   tempfile
gettempdirr   r
   r   mkfifopermissionsr   r   r   r   r   8   s   zNamedPipePosix._createc                 C   s   t | j| j| _d S r   )r"   r
   modefifor   r   r   r   r"   <      zNamedPipePosix.openc                 C   s   | j |S r   )r5   r%   r#   r   r   r   r%   ?   s   zNamedPipePosix.writec                 C   s4   | j r| j   d | _ | j rt| j d S d S r   )r5   r&   r
   is_fifor   unlinkr   r   r   r   r&   B   s   

zNamedPipePosix.close)
r'   r(   r)   r4   r3   r5   r   r"   r%   r&   r   r   r   r   r.   3   s    r.   c                   @   sX   e Zd ZdZdZdZdZdZdZdZ	dZ
edd Zd	d
 Zdd Zdd Zdd ZdS )NamedPipeWindowsi    N   r      c                  C   s   t j } td| d)NzNamed pipe error code 0x08X)r   kernel32GetLastErrorOSError)
error_coder   r   r   _get_last_errorU   s   
z NamedPipeWindows._get_last_errorc              
   C   sb   t d| j| _tjt| j| j| j| j	B | j
B | j| j| jdd | _| j| jkr/|   d S d S )Nz\\.\piper   )r   r   r
   r   r>   CreateNamedPipeWstrPIPE_ACCESS_OUTBOUNDPIPE_TYPE_BYTEPIPE_READMODE_BYTE	PIPE_WAITPIPE_UNLIMITED_INSTANCESbufsizepipeINVALID_HANDLE_VALUErB   r   r   r   r   r   Z   s   
zNamedPipeWindows._createc                 C   s   t j| jd  d S r   )r   r>   ConnectNamedPiperK   r   r   r   r   r"   i   r6   zNamedPipeWindows.openc                 C   s2   t d}tj| jt|tt|t|d  |j	S )Nr   )
r   r   r>   	WriteFilerK   r   r   lenr   value)r   r$   writtenr   r   r   r%   l   s   zNamedPipeWindows.writec                 C   s4   | j d urtj| j  tj| j  d | _ d S d S r   )rK   r   r>   DisconnectNamedPipeCloseHandler   r   r   r   r&   w   s
   

zNamedPipeWindows.close)r'   r(   r)   rJ   rK   rE   rF   rG   rH   rI   rL   staticmethodrB   r   r"   r%   r&   r   r   r   r   r9   J   s    
r9   )r+   loggingr   r   r0   	threadingpathlibr   streamlink.compatr   ctypesr   r   r   r   r   ImportError	getLoggerr'   r   Lockr   r   ABCr	   r.   r9   	NamedPiper   r   r   r   <module>   s(     
4