o
    _cW                     @  s   d dl mZ 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	m
Z
 ddlmZ ddlmZ ddlmZ ddlmZ ejd	krHd"ddZd#ddZd$ddZd$ddZd%ddZd"dd Zed!krje  dS dS )&    )annotationsN)AnySet   )ConnectionClosed)Close)connect)versionwin32returnNonec                  C  s   ddl } | d}| d}d}| jj|}||krtd|  }| jj|| |dkr4tdt	|t
j}| ||B }| jj||dkrPtddS )	zu
        Enable VT-100 for console output on Windows.

        See also https://bugs.python.org/issue29059.

        r   Ni   zunable to obtain stdout handlez$unable to query current console modezunable to set console mode)ctypesc_uintwindllZkernel32ZGetStdHandleRuntimeErrorZGetConsoleModebyrefint
from_bytessys	byteorderZSetConsoleMode)r   ZSTD_OUTPUT_HANDLEZINVALID_HANDLE_VALUEZ"ENABLE_VIRTUAL_TERMINAL_PROCESSINGhandleZcur_modeZpy_int_modeZnew_mode r   >/usr/local/lib/python3.10/dist-packages/websockets/__main__.pywin_enable_vt100   s   

r   loopasyncio.AbstractEventLoopstopasyncio.Future[None]c                 C  s@   |    | stjdkrtj}ntj}tt	 | d S d S )Nr
   )
r   doner   platformsignalZCTRL_C_EVENTSIGINToskillgetpid)r   r   Zctrl_cr   r   r   exit_from_event_loop_thread1   s   
r'   stringstrc                 C  "   t jd|  d t j  d S )Nz	7
[A[Lz
8[Br   stdoutwriteflushr(   r   r   r   print_during_input@   s   r0   c                 C  r*   )Nz[K
r+   r/   r   r   r   print_over_inputT   s   r2   uriinputsasyncio.Queue[str]c              
     s  z	t | I d H }W n# ty- } ztd|  d| d t|| W Y d }~d S d }~ww td|  d z	 t| }t| }tj	|||gtj
dI d H \}}	||	v r^|  ||	v rf|  ||v rz| }
W n	 tyy   Y n+w t|
trtd|
  ntd|
   ||v r| }
||
I d H  ||v rnq8W | I d H  |jd ur|jd usJ t|j|j}td	| d t|| d S | I d H  |jd ur|jd usJ t|j|j}td	| d t|| w )
NzFailed to connect to z: .zConnected to T)return_whenz< z< (binary) zConnection closed: )r   	Exceptionr2   r'   r0   asynciocreate_taskrecvgetwaitFIRST_COMPLETEDcancelresultr   
isinstancer)   hexsendcloseZ
close_codeZclose_reasonr   )r3   r   r4   r   Z	websocketexcincomingoutgoingr    pendingmessageZclose_statusr   r   r   
run_client`   s^   

"rJ   c               
   C  s  t jdddd} |  }|jddd |jdd	d
d |  }|jr+tdt  d S |jd u r5| 	d t
jdkrazt  W n! ty` } zt
jd| d t
j  W Y d }~nd }~ww zdd l}W n	 typ   Y nw t }ddd}|| }| }|t|j||| tj|jd}	|	  z	 td}
||j|
 q t t!fy   ||j"d  Y nw |	#  |$  d S )Nzpython -m websocketszInteractive WebSocket client.F)progdescriptionadd_helpz	--version
store_true)actionr3   z<uri>?)metavarnargszwebsockets z+the following arguments are required: <uri>r
   zUnable to set terminal to VT100 mode. This is only supported since Win10 anniversary update. Expect weird symbols on the terminal.
Error: r1   r   r   r5   c                     s
   t  S )N)r9   Queuer   r   r   r   queue_factory   s   zmain.<locals>.queue_factory)targetTz> )r   r5   )%argparseArgumentParseradd_mutually_exclusive_groupadd_argument
parse_argsr	   printwebsockets_versionr3   errorr   r!   r   r   stderrr-   r.   readlineImportErrorr9   new_event_looprun_until_completecreate_futurer:   rJ   	threadingThreadrun_foreverstartinputcall_soon_threadsafe
put_nowaitKeyboardInterruptEOFError
set_resultjoinrD   )parsergroupargsrE   r_   r   rT   r4   r   threadrI   r   r   r   main   s`   




rs   __main__)r   r   )r   r   r   r   r   r   )r(   r)   r   r   )
r3   r)   r   r   r4   r5   r   r   r   r   )
__future__r   rV   r9   r$   r"   r   rd   typingr   r   
exceptionsr   framesr   Zlegacy.clientr   r	   r\   r!   r   r'   r0   r2   rJ   rs   __name__r   r   r   r   <module>   s,    






;J
