a
    ö£Be€  ã                   @   s~   d dl Z d dlZd dlZd dlmZ d dlmZ d e jdfdd„Zdd„ Z	dd	„ Z
e jfd
d„Zdd„ Zd e jdfdd„ZdS )é    N)Úlog)Úhide_thread_from_debuggerc              	   C   sÎ   |dksJ ‚| du rd} |du r$d}zˆt ƒ }|dkr€tjdkrR| tjtjd¡ n.z| tjtjd¡ W n tt	fy~   Y n0 | 
| |f¡ |dur | |¡ | |¡ W n tyÈ   | ¡  ‚ Y n0 |S )z9Return a local server socket listening on the given port.r   Nz	127.0.0.1Úwin32é   )Ú	_new_sockÚsysÚplatformÚ
setsockoptÚsocketÚ
SOL_SOCKETZSO_EXCLUSIVEADDRUSEÚSO_REUSEADDRÚAttributeErrorÚOSErrorÚbindÚ
settimeoutÚlistenÚ	ExceptionÚclose)ÚhostÚportÚbacklogÚtimeoutÚserver© r   úY/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/debugpy/common/sockets.pyÚcreate_server   s,    

r   c                   C   s   t ƒ S )zAReturn a client socket that may be connected to a remote address.)r   r   r   r   r   Úcreate_client-   s    r   c               	   C   sÐ   t   t jt jt j¡} z|  t jt jd¡ W n ttfy@   Y n0 z|  t jt j	d¡ W n ttfyn   Y n0 z|  t jt j
d¡ W n ttfyœ   Y n0 z|  t jt jd¡ W n ttfyÊ   Y n0 | S )Nr   é   é   )r
   ÚAF_INETÚSOCK_STREAMÚIPPROTO_TCPr	   r   ÚSO_KEEPALIVEr   r   ÚTCP_KEEPIDLEÚTCP_KEEPINTVLÚTCP_KEEPCNT©Úsockr   r   r   r   2   s$    r   c                 C   s   |   |¡ dS )zShut down the given socket.N)Úshutdown)r'   Úhowr   r   r   Ú	shut_downL   s    r*   c                 C   s,   zt | ƒ W n ty   Y n0 |  ¡  dS )zShutdown and close the socket.N)r*   r   r   r&   r   r   r   Úclose_socketQ   s
    r+   c              	      s”   |dksJ ‚zt ||||ƒ‰W n" ty@   t dˆ||¡ Y n0 ˆ ¡ \}}t dˆ||¡ ‡ ‡‡fdd„}tj|d�}d|_t	|ƒ | 
¡  ˆS )z¬Accepts TCP connections on the specified host and port, and invokes the
    provided handler function for every new connection.

    Returns the created server socket.
    r   z8Error listening for incoming {0} connections on {1}:{2}:z4Listening for incoming {0} connections on {1}:{2}...c               	      sP   zˆ  ¡ \} \}}W n ttjfy0   Y qLY n0 t dˆ||¡ ˆ | ƒ q d S )Nz.Accepted incoming {0} connection from {1}:{2}.)Úacceptr   r
   Úerrorr   Úinfo)r'   Z
other_hostZ
other_port©ÚhandlerZlistenerÚnamer   r   Úaccept_workerl   s    
üzserve.<locals>.accept_worker)ÚtargetT)r   r   r   Zreraise_exceptionÚgetsocknamer.   Ú	threadingÚThreadÚdaemonr   Ústart)r1   r0   r   r   r   r   r2   Úthreadr   r/   r   ÚserveZ   s    ÿ
r:   )r
   r   r5   Zdebugpy.commonr   Zdebugpy.common.utilr   Ú	SOMAXCONNr   r   r   Ú	SHUT_RDWRr*   r+   r:   r   r   r   r   Ú<module>   s    	