o
    #6dP                  	   @  s  d dl m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	m
Z
 d dlmZ d dlmZmZ d dlmZmZmZmZmZ dd	lmZ dd
lmZmZmZmZmZmZmZ ddlm Z  ddl!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z( ddl)m*Z*m+Z+ ej,dkrd dlm-Z- nd dl.m-Z- e/eddZ0eeeee1e2ee2e1f f  Z3e-ej4ej5ej6f Z7e-ej5ej6f Z8eddddddwd(d)Z9eddddd*dxd-d)Z9edddddd.dyd1d)Z9edddddd.dzd4d)Z9eddd5d{d6d)Z9dd7dd8dd9d:d|d<d)Z9d}d@dAZ:dd ejj4dBd7dCd~dJdKZ;ddBdLddPdQZ<ej4fdd d7dRddTdUZ=ej4dd d7dVddXdYZ>d d d d dZddedfZ?dddjdkZ@ddodpZAddqdrZBddudvZCdS )    )annotationsN)IPv6Address
ip_address)PathLikechmod)Path)AddressFamily
SocketKind)	AwaitableListTuplecastoverload   )	to_thread)ConnectedUDPSocketIPAddressTypeIPSockAddrTypeSocketListenerSocketStream	UDPSocketUNIXSocketStream)MultiListener)	TLSStream   )get_asynclib)aclose_forcefully)Event)create_task_groupmove_on_after)      )LiteralIPPROTO_IPV6)   .)
local_hostssl_contexttls_standard_compatiblehappy_eyeballs_delayremote_hostr   remote_portintr%   IPAddressType | Noner&   ssl.SSLContext | Noner'   booltls_hostnamestrr(   floatreturnr   c                     d S N r)   r*   r%   r&   r'   r/   r(   r5   r5   kC:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\site-packages\anyio/_core/_sockets.pyconnect_tcp.      r8   )r%   r'   r/   r(   ssl.SSLContext
str | Nonec                  r3   r4   r5   r6   r5   r5   r7   r8   =   r9   )r%   r&   r'   r/   r(   tlsLiteral[True]c                  r3   r4   r5   r)   r*   r%   r<   r&   r'   r/   r(   r5   r5   r7   r8   L      Literal[False]r   c                  r3   r4   r5   r>   r5   r5   r7   r8   \   r?   )r%   r(   c                  r3   r4   r5   )r)   r*   r%   r(   r5   r5   r7   r8   l   s   FTg      ?)r%   r<   r&   r'   r/   r(   SocketStream | TLSStreamc             
     sp  dd fdd	}t   dtj}	|r+tt|dI dH }
|
d
 ^}	}t| }zt| }W nW ty   t||	tjdI dH }
d }}g }|
D ]9^}}}|tjkrj|sjd}|	d
||d
 f qP|tj
kr|s|rd}|	d||d
 f qP|||d
 f qPY nw t|trtj|jfg}ntj
|jfg}g t 4 I dH ;t|D ]+\}\}}t }||| t| | I dH  W d   n1 sw   Y  qW d  I dH  n1 I dH sw   Y  du r	tdkrd
 n }td||s|s|r6ztjd|pt| ||dI dH W S  ty5   tI dH   w S )af  
    Connect to a host using the TCP protocol.

    This function implements the stateless version of the Happy Eyeballs algorithm (RFC
    6555). If ``remote_host`` is a host name that resolves to multiple IP addresses,
    each one is tried until one connection attempt succeeds. If the first attempt does
    not connected within 250 milliseconds, a second attempt is started using the next
    address in the list, and so on. On IPv6 enabled systems, an IPv6 address (if
    available) is tried first.

    When the connection has been established, a TLS handshake will be done if either
    ``ssl_context`` or ``tls_hostname`` is not ``None``, or if ``tls`` is ``True``.

    :param remote_host: the IP address or host name to connect to
    :param remote_port: port on the target host to connect to
    :param local_host: the interface address or name to bind the socket to before connecting
    :param tls: ``True`` to do a TLS handshake with the connected stream and return a
        :class:`~anyio.streams.tls.TLSStream` instead
    :param ssl_context: the SSL context object to use (if omitted, a default context is created)
    :param tls_standard_compatible: If ``True``, performs the TLS shutdown handshake before closing
        the stream and requires that the server does this as well. Otherwise,
        :exc:`~ssl.SSLEOFError` may be raised during reads from the stream.
        Some protocols, such as HTTP, require this option to be ``False``.
        See :meth:`~ssl.SSLContext.wrap_socket` for details.
    :param tls_hostname: host name to check the server certificate against (defaults to the value
        of ``remote_host``)
    :param happy_eyeballs_delay: delay (in seconds) before starting the next connection attempt
    :return: a socket stream object if no TLS handshake was done, otherwise a TLS stream
    :raises OSError: if the connection attempt fails

    Nr)   r0   eventr   r2   Nonec              
     s   zKz  | I d H }W n ty+ } z| W Y d }~W |  d S d }~ww d u r8|j  n| I d H  W |  d S W |  d S |  w r4   )r8   OSErrorappendsetZcancel_scopecancelaclose)r)   rB   streamexcasynclibZconnected_streamlocal_addressZoserrorsr*   tgr5   r7   try_connect   s    
z connect_tcp.<locals>.try_connectr   familytypeFTr   zAll connection attempts failed)server_sidehostnamer&   Zstandard_compatible)r)   r0   rB   r   r2   rC   )r   socket	AF_UNSPECgetaddrinfor0   r   
ValueErrorSOCK_STREAMAF_INET6insertAF_INETrE   
isinstancer   
compressedr   	enumerater   Z
start_soonr   waitlenZExceptionGrouprD   r   wrapBaseExceptionr   )r)   r*   r%   r<   r&   r'   r/   r(   rO   rQ   gai_res_Ztarget_hostZaddr_objZv6_foundZv4_foundZtarget_addrsafrestsaiaddrrB   causer5   rK   r7   r8   w   sv   +


(

pathstr | PathLike[str]r   c                   s    t t| } t | I dH S )z
    Connect to the given UNIX socket.

    Not available on Windows.

    :param path: path to the socket
    :return: a socket stream object

    N)r0   r   r   connect_unix)rl   r5   r5   r7   rn      s   
rn      )r%   
local_portrQ   backlog
reuse_portrp   rQ   AnyIPAddressFamilyrq   rr   MultiListener[SocketStream]c                   s  t  }t|d}| durt| nd} t| ||tjdkr tjjndtj	tj
B dI dH }g }ztt|D ]z^}}	}
}tjdkrG|	tjurGq5t|}|d tjdkr`|tjtjd n	|tjtjd |rt|tjtjd |tjkr|ttjd d|d v r|d dd\}}||d dt|f}|| || ||}|| q5W t|S  ty   |D ]	}| I dH  q w )	a  
    Create a TCP socket listener.

    :param local_port: port number to listen on
    :param local_host: IP address of the interface to listen on. If omitted, listen on
        all IPv4 and IPv6 interfaces. To listen on all interfaces on a specific address
        family, use ``0.0.0.0`` for IPv4 or ``::`` for IPv6.
    :param family: address family (used if ``local_host`` was omitted)
    :param backlog: maximum number of queued incoming connections (up to a maximum of
        2**16, or 65536)
    :param reuse_port: ``True`` to allow multiple sockets to bind to the same
        address/port (not supported on Windows)
    :return: a list of listener objects

    ro   Nwin32r   rQ   rR   flagsFr   %)r   minr0   rW   sysplatformrU   r	   rY   
AI_PASSIVEAI_ADDRCONFIGsortedrF   setblocking
setsockopt
SOL_SOCKETSO_EXCLUSIVEADDRUSESO_REUSEADDRSO_REUSEPORTrZ   r#   IPV6_V6ONLYsplitr+   bindlistenZTCPSocketListenerrE   rc   rH   r   )r%   rp   rQ   rq   rr   rL   rd   Z	listenersfamkindre   sockaddr
raw_socketrj   scope_idlistenerr5   r5   r7   create_tcp_listener  sN   








&r   )moderq   r   
int | Noner   c                  s   t | }t| } |  r|   t|d}ttj}|d z(tj	|j
|ddI dH  |dur>tj	t||ddI dH  || t |W S  tyU   |   w )a  
    Create a UNIX socket listener.

    Not available on Windows.

    :param path: path of the socket
    :param mode: permissions to set on the socket
    :param backlog: maximum number of queued incoming connections (up to a maximum of 2**16, or
        65536)
    :return: a listener object

    .. versionchanged:: 3.0
        If a socket already exists on the file system in the given path, it will be removed first.

    ro   FT)ZcancellableN)r0   r   	is_socketunlinkry   rU   AF_UNIXr   r   Zrun_syncr   r   r   r   ZUNIXSocketListenerrc   close)rl   r   rq   Zpath_strr   r5   r5   r7   create_unix_listenerP  s$   


r   )r%   rp   rr   r   c                  s   | t ju r|std|r1tt||| tjtjtjB dI dH }t	t
|d d } |d d }n
| t ju r9d}nd}t | |d|I dH S )u_  
    Create a UDP socket.

    If ``local_port`` has been given, the socket will be bound to this port on the local
    machine, making this socket suitable for providing UDP based services.

    :param family: address family (``AF_INET`` or ``AF_INET6``) – automatically determined from
        ``local_host`` if omitted
    :param local_host: IP address or host name of the local interface to bind to
    :param local_port: local port to bind to
    :param reuse_port: ``True`` to allow multiple sockets to bind to the same address/port
        (not supported on Windows)
    :return: a UDP socket

    z-Either "family" or "local_host" must be givenrv   Nr   )z::r   )z0.0.0.0r   )r   rV   rX   rW   r0   rU   
SOCK_DGRAMr|   r}   r   rs   rZ   r   create_udp_socket)rQ   r%   rp   rr   rd   rM   r5   r5   r7   r   y  s&   


r   )rQ   r%   rp   rr   r   c          	        s   d}|r't t|||tjtjtjB dI dH }tt|d d }|d d }t t| ||tjdI dH }tt|d d }|d d }t 	||||I dH S )u  
    Create a connected UDP socket.

    Connected UDP sockets can only communicate with the specified remote host/port, and any packets
    sent from other sources are dropped.

    :param remote_host: remote host to set as the default target
    :param remote_port: port on the remote host to set as the default target
    :param family: address family (``AF_INET`` or ``AF_INET6``) – automatically determined from
        ``local_host`` or ``remote_host`` if omitted
    :param local_host: IP address or host name of the local interface to bind to
    :param local_port: local port to bind to
    :param reuse_port: ``True`` to allow multiple sockets to bind to the same address/port
        (not supported on Windows)
    :return: a connected UDP socket

    Nrv   r   r   rP   )
rW   r0   rU   r   r|   r}   r   rs   r   r   )	r)   r*   rQ   r%   rp   rr   rM   rd   remote_addressr5   r5   r7   create_connected_udp_socket  s(   

r   rQ   rR   protorw   hostbytearray | bytes | strportstr | int | Noneint | AddressFamilyrR   int | SocketKindr   rw   GetAddrInfoReturnTypec          	        st   t | tr"z| d}W n ty!   ddl}|j| dd}Y nw | }t j||||||dI dH }dd |D S )	a  
    Look up a numeric IP address given a host name.

    Internationalized domain names are translated according to the (non-transitional) IDNA 2008
    standard.

    .. note:: 4-tuple IPv6 socket addresses are automatically converted to 2-tuples of
        (host, port), unlike what :func:`socket.getaddrinfo` does.

    :param host: host name
    :param port: port number
    :param family: socket family (`'AF_INET``, ...)
    :param type: socket type (``SOCK_STREAM``, ...)
    :param proto: protocol number
    :param flags: flags to pass to upstream ``getaddrinfo()``
    :return: list of tuples containing (family, type, proto, canonname, sockaddr)

    .. seealso:: :func:`socket.getaddrinfo`

    asciir   NT)uts46r   c                 S  s(   g | ]\}}}}}||||t |fqS r5   )convert_ipv6_sockaddr).0rQ   rR   r   	canonnamer   r5   r5   r7   
<listcomp>  s    zgetaddrinfo.<locals>.<listcomp>)r]   r0   encodeUnicodeEncodeErroridnar   rW   )	r   r   rQ   rR   r   rw   Zencoded_hostr   rd   r5   r5   r7   rW     s   
rW   r   r   Awaitable[tuple[str, str]]c                 C  s   t  | |S )a  
    Look up the host name of an IP address.

    :param sockaddr: socket address (e.g. (ipaddress, port) for IPv4)
    :param flags: flags to pass to upstream ``getnameinfo()``
    :return: a tuple of (host name, service name)

    .. seealso:: :func:`socket.getnameinfo`

    )r   getnameinfo)r   rw   r5   r5   r7   r     s   r   socksocket.socketAwaitable[None]c                 C     t  | S )aI  
    Wait until the given socket has data to be read.

    This does **NOT** work on Windows when using the asyncio backend with a proactor event loop
    (default on py3.8+).

    .. warning:: Only use this on raw sockets that have not been wrapped by any higher level
        constructs like socket streams!

    :param sock: a socket object
    :raises ~anyio.ClosedResourceError: if the socket was closed while waiting for the
        socket to become readable
    :raises ~anyio.BusyResourceError: if another task is already waiting for the socket
        to become readable

    )r   wait_socket_readabler   r5   r5   r7   r        r   c                 C  r   )aG  
    Wait until the given socket can be written to.

    This does **NOT** work on Windows when using the asyncio backend with a proactor event loop
    (default on py3.8+).

    .. warning:: Only use this on raw sockets that have not been wrapped by any higher level
        constructs like socket streams!

    :param sock: a socket object
    :raises ~anyio.ClosedResourceError: if the socket was closed while waiting for the
        socket to become writable
    :raises ~anyio.BusyResourceError: if another task is already waiting for the socket
        to become writable

    )r   wait_socket_writabler   r5   r5   r7   r   *  r   r   +tuple[str, int, int, int] | tuple[str, int]tuple[str, int]c                 C  sr   t | tr0t| dkr0ttttttf | \}}}}|r,|dd }| d| |fS ||fS ttttf | S )a  
    Convert a 4-tuple IPv6 socket address to a 2-tuple (address, port) format.

    If the scope ID is nonzero, it is added to the address, separated with ``%``.
    Otherwise the flow id and scope id are simply cut off from the tuple.
    Any other kinds of socket addresses are returned as-is.

    :param sockaddr: the result of :meth:`~socket.socket.getsockname`
    :return: the converted socket address

       rx   r   )r]   tuplera   r   r   r0   r+   r   )r   r   r   flowinfor   r5   r5   r7   r   C  s   r   )r)   r   r*   r+   r%   r,   r&   r-   r'   r.   r/   r0   r(   r1   r2   r   )r)   r   r*   r+   r%   r,   r&   r:   r'   r.   r/   r;   r(   r1   r2   r   )r)   r   r*   r+   r%   r,   r<   r=   r&   r-   r'   r.   r/   r;   r(   r1   r2   r   )r)   r   r*   r+   r%   r,   r<   r@   r&   r-   r'   r.   r/   r;   r(   r1   r2   r   )
r)   r   r*   r+   r%   r,   r(   r1   r2   r   )r)   r   r*   r+   r%   r,   r<   r.   r&   r-   r'   r.   r/   r;   r(   r1   r2   rA   )rl   rm   r2   r   )r%   r,   rp   r+   rQ   rs   rq   r+   rr   r.   r2   rt   )rl   rm   r   r   rq   r+   r2   r   )
rQ   rs   r%   r,   rp   r+   rr   r.   r2   r   )r)   r   r*   r+   rQ   rs   r%   r,   rp   r+   rr   r.   r2   r   )r   r   r   r   rQ   r   rR   r   r   r+   rw   r+   r2   r   )r   )r   r   rw   r+   r2   r   )r   r   r2   r   )r   r   r2   r   )D
__future__r   rU   sslrz   	ipaddressr   r   osr   r   pathlibr   r   r	   typingr
   r   r   r   r    r   abcr   r   r   r   r   r   r   Zstreams.stapledr   Zstreams.tlsr   Z
_eventloopr   Z
_resourcesr   Z_synchronizationr   _tasksr   r   version_infor"   Ztyping_extensionsgetattrr#   r+   r0   r   rV   r\   rZ   rs   ZIPAddressFamilyr8   rn   r   r   r   r   rW   r   r   r   r   r5   r5   r5   r7   <module>   s    $	

|R*151

