
    h                    .   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	 d dl
mZ d dlmZmZmZ d dlmZ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mZ d	dlmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z' d	dl(m)Z) d	dl*m+Z+m,Z, ddl-m.Z. ddl/m0Z0 ddl1m2Z2 ddl3m4Z4m5Z5 erd dl6m7Z7 ne8Z7ejr                  dk  rd dl:m;Z; ejr                  dk\  rd dlm<Z< nd dl=m<Z< ejr                  dk  rd dl=m>Z> nd dl?m>Z>  e@edd      ZAeej                  ej                  ej                  f   ZEeej                  ej                  f   ZFeddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dJd       ZGeddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dKd        ZGedddddd!	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dLd"       ZGedddddd!	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dMd#       ZGeddd$	 	 	 	 	 	 	 	 	 dNd%       ZGdd&dd'dd(d)	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dOd*ZGdPd+ZHdd ej$                  j                  d,d&d-	 	 	 	 	 	 	 	 	 	 	 dQd.ZIdd,d/	 	 	 	 	 	 	 dRd0ZJej                  fdd d&d1	 	 	 	 	 	 	 	 	 dSd2ZKej                  dd d&d3	 	 	 	 	 	 	 	 	 	 	 	 	 dTd4ZLddd5	 	 	 	 	 dUd6ZMddd5	 	 	 	 	 	 	 dVd7ZNd d d d d8	 	 	 	 	 	 	 	 	 	 	 	 	 dWd9ZOdXdYd:ZP e>d;      dZd<       ZQ e>d=      dZd>       ZRd[d?ZSd[d@ZTd\dAZU	 	 	 	 d]dBZV	 	 	 	 	 	 	 	 d^dCZWe G dD dEe             ZXe G dF dGe             ZYd&ddd'dH	 	 	 	 	 	 	 	 	 	 	 d_dIZZy)`    )annotationsN)	Awaitable)	dataclass)IPv4AddressIPv6Address
ip_address)PathLikechmod)AddressFamily
SocketKind)TYPE_CHECKINGAnyLiteralcastoverload   )ConnectionFailed	to_thread)
ByteStreamConnectableConnectedUDPSocketConnectedUNIXDatagramSocketIPAddressTypeIPSockAddrTypeSocketListenerSocketStream	UDPSocketUNIXDatagramSocketUNIXSocketStream)MultiListener)TLSConnectable	TLSStream   )get_async_backend)aclose_forcefully)Event)create_task_groupmove_on_after)FileDescriptorLike)      )ExceptionGroup)r)      )override)r)      )
deprecatedIPPROTO_IPV6)   .)
local_hostssl_contexttls_standard_compatiblehappy_eyeballs_delayc                  K   y wN remote_hostremote_portr2   r3   r4   tls_hostnamer5   s          9D:\jyotish\venv\Lib\site-packages\anyio/_core/_sockets.pyconnect_tcpr>   ?            )r2   r4   r<   r5   c                  K   y wr7   r8   r9   s          r=   r>   r>   M   r?   r@   )r2   r3   r4   r<   r5   c                  K   y wr7   r8   r:   r;   r2   tlsr3   r4   r<   r5   s           r=   r>   r>   [   s      r@   c                  K   y wr7   r8   rC   s           r=   r>   r>   j   s      r@   )r2   r5   c                  K   y wr7   r8   )r:   r;   r2   r5   s       r=   r>   r>   y   s      r@   FTg      ?)r2   rD   r3   r4   r<   r5   c                 K   ddfd}t               dt        j                  }	|r&t        t	        |      d       d{   }
|
d   ^}	}t	        |       }	 t        |       }|Lt        |t              rt        j                  |j                  fg}nt        j                  |j                  fg}nt        ||	t        j                         d{   }
dx}}g }|
D ]|  ^}}}|t        j                  k(  r|sd}|j                  d||d   f       6|t        j                  k(  r|s|rd}|j                  d||d   f       g|j                  ||d   f       ~ g 	 t               4 d{   |D ]N  \  }}t!               }j#                  |||       t%        |      5  |j'                          d{    ddd       P ddd      d{    +t)              dk(  rd   nt+        d      }t-        d	      |	 j/                          |s|s|r0	 t1        j2                  d|xs t	        |       ||
       d{   S S 7 # t        $ r d}Y w xY w7 7 7 # 1 sw Y   xY w7 # 1 d{  7  sw Y   xY w# j/                          w xY w7 Z# t4        $ r t7               d{  7    w xY ww)a  
    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 ConnectionFailed: if the connection fails

    Nc                v  K   	 j                  |        d {   }|	j                  j                          n|j                          d {    	 |j                          y 7 M7 # t        $ r+}j                  |       Y d }~|j                          y d }~ww xY w# |j                          w xY wwr7   )r>   cancel_scopecancelacloseOSErrorappendset)
r:   eventstreamexcasynclibconnected_streamlocal_addressoserrorsr;   tgs
       r=   try_connectz connect_tcp.<locals>.try_connect   s     	#//[-XXF
  '#) &&(mmo%%IIK Y &  	OOC  IIK	 IIKsg   B9A- A)A- 2B$ A+B$ B9)A- +B$ -	B!6BB$ B9B!!B$ $B66B9r   familytypeFTr"   z#multiple connection attempts failedzAll connection attempts failed)server_sidehostnamer3   standard_compatible)r:   strrO   r%   returnNone)r#   socket	AF_UNSPECgetaddrinfor^   r   
ValueError
isinstancer   AF_INET6
compressedAF_INETSOCK_STREAMinsertrM   r&   r%   
start_soonr'   waitlenr+   rL   clearr!   wrapBaseExceptionr$   )r:   r;   r2   rD   r3   r4   r<   r5   rW   rY   gai_res_target_hostaddr_objtarget_addrsv6_foundv4_foundafsa_afaddrrO   causerR   rS   rT   rU   rV   s    `                     @@@@@r=   r>   r>      s    \ -1   !"H+/MF#C
OT::$+AJ!Mk"Kk* h,#__h.A.ABCL#^^X-@-@ABL $V&:L:L
 
 $#8!JBBV__$X##ABqE{3v~~%h8##ABqE{3##RAK0 " !H$&&")	Tk47"#78**,&& 98 * '& # x=A% #$I8T 
 :;F $ 	
lk
	" !%9[)9'$;   E ;  
( '
 ' 98	 '&&& 	  	#$4555	s  AKIK&I 1A.KI/ BK.J <I2=J  1J1I6I4
I6

JJ J 1J K)*J0 J.J0 KI,(K+I,,K2J 4I66I?;JJ J
JJJ J++K.J0 0KK
KKc                z   K   t        j                  |       } t               j                  |        d{   S 7 w)z
    Connect to the given UNIX socket.

    Not available on Windows.

    :param path: path to the socket
    :return: a socket stream object
    :raises ConnectionFailed: if the connection fails

    N)osfspathr#   connect_unix)paths    r=   r   r     s/      99T?D"$11$7777s   2;9;   )r2   
local_portrY   backlog
reuse_portc                <  K   t               }t        d      | t        |       nd} dd	 	 	 	 	 	 	 dfd}t        | ||t        j
                  dk(  rt        j                  ndt        j                  t        j                  z         d{   }t        |D ch c]  }|d	   t        j                  k(  s| c}      }	| q|t        j                  k(  r^t        j                         rJt        d
 |D              r8 |t        j                   d|fd      }
|j#                  |
      }t%        |g      S g }	 t'        t)        |	            D ]|  }g }|}	 |	D ]b  ^}}}|d   |g|dd } |||      }
|dk(  r!t)        |      d	kD  r|
j+                         d	   }|j-                  |j#                  |
             d 	 t%        |      c ~S  t5        dt)        |	       d      t=        d|      7 ac c}w # t.        $ r}|D ]  }|j1                          d{  7    t3        |t4              r[|j6                  t6        j8                  k(  r>|dk(  r9|r7|j-                  |       |	j-                  |	j;                  d             Y d}~> d}~ww xY w# ~w xY ww)a6  
    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 multi-listener object containing one or more socket listeners
    :raises OSError: if there's an error creating a socket, or binding to one or more
        interfaces failed

    r   NT)v6onlyc                  t        j                   |       }	 |j                  d       | t        j                  k(  r%|j	                  t
        t         j                  |       t        j                  dk(  r0|j	                  t         j                  t         j                  d       n/|j	                  t         j                  t         j                  d       r/|j	                  t         j                  t         j                  d       | t         j                  k(  r1d|d   v r*|d   j                  dd      \  }}||d   dt        |      f}|j                  |       |j!                         |S # t"        $ r |j%                           w xY w)NFwin32r"   %r   )ra   setblockingr   rf   
setsockoptr0   IPV6_V6ONLYsysplatform
SOL_SOCKETSO_EXCLUSIVEADDRUSESO_REUSEADDRSO_REUSEPORTsplitintbindlistenrp   close)fam	bind_addrr   sockr{   scope_idr   r   s         r=   setup_raw_socketz-create_tcp_listener.<locals>.setup_raw_socket8  s4    }}S!	U#m,,,f.@.@&I ||w& 1 163M3MqQ 1 163F3FJ 1 163F3FJ foo%#1*=!*1!3!3C!;h!9Q<CMB	IIi KK 
 	  	JJL	s   EE, ,Fr   r   rY   rZ   flagsr"   c              3  F   K   | ]  ^}}|t         j                  k(    y wr7   )r   rf   ).0r   rr   s      r=   	<genexpr>z&create_tcp_listener.<locals>.<genexpr>q  s      EW'#}---Ws   !::Fr   zCould not create z! listeners with a consistent portzSeveral bind attempts failed)r   r   r   z+tuple[str, int] | tuple[str, int, int, int]r   boolr_   socket.socket)r#   minr^   rc   r   r   ra   ri   
AI_PASSIVEAI_ADDRCONFIGsortedr   r   rb   has_dualstack_ipv6anyrf   create_tcp_listenerr   rangerm   getsocknamerM   rp   rK   re   rL   errno
EADDRINUSEpopr+   )r2   r   rY   r   r   rR   r   rq   res	sockaddrs
raw_socketlistenererrorsrr   	listenersbound_ephemeral_portr   sockaddrrQ   s      ``              r=   r   r     s    2 !"H'5!G$.$:ZJ 	"">" 	"
 
"P  #&<<7#:V&"6"66 G wSw#a&J<R<R2RwSTI 	m---%%'EWEE%""T:$6u

 //
;hZ((F's9~&A.0I#- )2%C!X'{,@O8AB<OH!1#x!@J "Q3w<!+;/9/E/E/G/J,$$X%A%A*%MN *38 !++ M 'D I//PQ
<fE	F{ T> !  )H"//+++ !* sG,		U%5%55"a,MM#&$$Y]]1%56!0 s   BJG1JG4:G4>A<J;J A'G9>J J$J 1J4J9	JJH
A,JJ JJJ JJ)moder   c                  K   t        |d      }t        | |t        j                         d{   }	 |j	                  |       t               j                  |      S 7 /# t        $ r |j                           w xY w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.

    r   N)	r   setup_unix_local_socketra   ri   r   r#   create_unix_listenerrp   r   )r   r   r   r   s       r=   r   r     sw     , '5!G.tT6;M;MNNJ'" "77
CC O  s&   +A=AA=)A A=A::A=)r2   r   r   c                 K   | t         j                  u r|st        d      |rmt        t	        |      || t
        j                  t
        j                  t
        j                  z         d{   }t        t        |d   d         } |d   d   }n| t         j                  u rd}nd}t               j                  | |d|       d{   }t        t        |      S 7 n7 w)uY  
    Create a UDP socket.

    If ``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 givenr   Nr   )r   r   )z0.0.0.0r   )r   rb   rd   rc   r^   ra   
SOCK_DGRAMr   r   r   AnyIPAddressFamilyrf   r#   create_udp_socketr   )rY   r2   r   r   rq   rT   r   s          r=   r   r     s     , (((HII#
O""##f&:&::
 
 ('!*Q-8
2	=))	)!&"$66tZ D 	4  #
s%   A+C -C.AC CC C )rY   r2   r   r   c                 K   d}|rlt        t        |      ||t        j                  t        j                  t        j
                  z         d{   }t        t        |d   d         }|d   d   }t        t        |       ||t        j                         d{   }t        t        |d   d         }|d   d   }t               j                  ||||       d{   }	t        t        |	      S 7 7 X7 w)u  
    Create a connected UDP socket.

    Connected UDP sockets can only communicate with the specified remote host/port, an
    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

    Nr   r   r   rX   )rc   r^   ra   r   r   r   r   r   r#   r   r   )
r:   r;   rY   r2   r   r   rT   rq   remote_addressr   s
             r=   create_connected_udp_socketr     s     4 M#
O""##f&:&::
 
 ('!*Q-8
2K+f6;L;L G $gajm4FQZ^N"$66~z D "D))'
s8   AC9C3AC9C5AC9C7C95C97C9)
local_path
local_modec                   K   t        | |t        j                         d{   }t               j	                  |d       d{   S 7 &7 w)a8  
    Create a UNIX datagram socket.

    Not available on Windows.

    If ``local_path`` has been given, the socket will be bound to this path, making this
    socket suitable for receiving datagrams from other processes. Other processes can
    send datagrams to this socket only if ``local_path`` is set.

    If a socket already exists on the file system in the ``local_path``, it will be
    removed first.

    :param local_path: the path on which to bind to
    :param local_mode: permissions to set on the local socket
    :return: a UNIX datagram socket

    N)r   ra   r   r#   create_unix_datagram_socket)r   r   r   s      r=   r   r   $  sK     , /J 1 1 J #$@@TRRR Ss!   AA!AA
A
Ac                  K   t        j                  |       } t        ||t        j                         d{   }t               j                  ||        d{   S 7 &7 w)a  
    Create a connected UNIX datagram socket.

    Connected datagram sockets can only communicate with the specified remote path.

    If ``local_path`` has been given, the socket will be bound to this path, making
    this socket suitable for receiving datagrams from other processes. Other processes
    can send datagrams to this socket only if ``local_path`` is set.

    If a socket already exists on the file system in the ``local_path``, it will be
    removed first.

    :param remote_path: the path to set as the default target
    :param local_path: the path on which to bind to
    :param local_mode: permissions to set on the local socket
    :return: a connected UNIX datagram socket

    N)r~   r   r   ra   r   r#   r   )remote_pathr   r   r   s       r=   %create_connected_unix_datagram_socketr   @  s_     0 ))K(K.J 1 1 J #$@@K  s!   4A!A!A!AA!A!rY   rZ   protor   c                 K   t        | t              r	 | j                  d      }n| }t               j                  ||||||       d{   }|D 	
cg c]+  \  }}}}	}
t        |
d   t              s||||	t        |
      f- c}
}	}}}S # t        $ r ddl}|j                  | d      }Y w xY w7 ic c}
}	}}}w w)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   )	re   r^   encodeUnicodeEncodeErroridnar#   rc   r   convert_ipv6_sockaddr)hostportrY   rZ   r   r   encoded_hostr   rq   	canonnamer   s              r=   rc   rc   a  s     < $	9)-W)=L %'33d6E 4  G
 9@8?4FD%H(1+s+ 
ui)>x)HI8?  " 	9;;t4;8L	9sE   B?B %B?
B4B?0B6B? B1.B?0B11B?6	B?c                6    t               j                  | |      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   r   s     r=   r   r     s     **8U;;    z8This function is deprecated; use `wait_readable` insteadc                P    t               j                  | j                               S )a  
    .. deprecated:: 4.7.0
       Use :func:`wait_readable` instead.

    Wait until the given socket has data to be read.

    .. 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_readablefilenor   s    r=   wait_socket_readabler     s    $ ,,T[[];;r   z8This function is deprecated; use `wait_writable` insteadc                P    t               j                  | j                               S )a  
    .. deprecated:: 4.7.0
       Use :func:`wait_writable` instead.

    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_writabler   r   s    r=   wait_socket_writabler     s    * ,,T[[];;r   c                4    t               j                  |       S )aJ  
    Wait until the given object has data to be read.

    On Unix systems, ``obj`` must either be an integer file descriptor, or else an
    object with a ``.fileno()`` method which returns an integer file descriptor. Any
    kind of file descriptor can be passed, though the exact semantics will depend on
    your kernel. For example, this probably won't do anything useful for on-disk files.

    On Windows systems, ``obj`` must either be an integer ``SOCKET`` handle, or else an
    object with a ``.fileno()`` method which returns an integer ``SOCKET`` handle. File
    descriptors aren't supported, and neither are handles that refer to anything besides
    a ``SOCKET``.

    On backends where this functionality is not natively provided (asyncio
    ``ProactorEventLoop`` on Windows), it is provided using a separate selector thread
    which is set to shut down when the interpreter shuts down.

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

    :param obj: an object with a ``.fileno()`` method or an integer handle
    :raises ~anyio.ClosedResourceError: if the object was closed while waiting for the
        object to become readable
    :raises ~anyio.BusyResourceError: if another task is already waiting for the object
        to become readable

    )r#   r   objs    r=   r   r     s    8 ,,S11r   c                4    t               j                  |       S )a~  
    Wait until the given object can be written to.

    :param obj: an object with a ``.fileno()`` method or an integer handle
    :raises ~anyio.ClosedResourceError: if the object was closed while waiting for the
        object to become writable
    :raises ~anyio.BusyResourceError: if another task is already waiting for the object
        to become writable

    .. seealso:: See the documentation of :func:`wait_readable` for the definition of
       ``obj`` and notes on backend compatibility.

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

    )r#   r   r   s    r=   r   r     s    " ,,S11r   c                6    t               j                  |        y)u  
    Call this before closing a file descriptor (on Unix) or socket (on
    Windows). This will cause any `wait_readable` or `wait_writable`
    calls on the given object to immediately wake up and raise
    `~anyio.ClosedResourceError`.

    This doesn't actually close the object – you still have to do that
    yourself afterwards. Also, you want to be careful to make sure no
    new tasks start waiting on the object in between when you call this
    and when it's actually closed. So to close something properly, you
    usually want to do these steps in order:

    1. Explicitly mark the object as closed, so that any new attempts
       to use it will abort before they start.
    2. Call `notify_closing` to wake up any already-existing users.
    3. Actually close the object.

    It's also possible to do them in a different order if that's more
    convenient, *but only if* you make sure not to have any checkpoints in
    between the steps. This way they all happen in a single atomic
    step, so other tasks won't be able to tell what order they happened
    in anyway.

    :param obj: an object with a ``.fileno()`` method or an integer handle

    N)r#   notify_closingr   s    r=   r   r     s    6 &&s+r   c                    t        | t              r8t        |       dk(  r*| \  }}}}|r|j                  d      d   }| d| |fS ||fS | 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

       r   r   )re   tuplerm   r   )r   r   r   flowinfor   s        r=   r   r   %  se     (E"s8}'9)1&dHh ::c?1%D V1XJ'--:r   c                  K   | qt        j                  |       }|j                  d      sM	 t        j                  |       }t        j                  |j
                        rt        j                  |        nd}t        j                  t        j                  |      }|j                  d       |T	 t!        j"                  |j$                  |d       d{    |%t!        j"                  t&        ||d       d{    |S |S # t        $ rU}|j                  t        j                  t        j                  t        j                  t        j                  fvr Y d}~d}~ww xY w7 7 k# t(        $ r |j+                           w xY ww)a  
    Create a UNIX local socket object, deleting the socket at the given path if it
    exists.

    Not available on Windows.

    :param path: path of the socket
    :param mode: permissions to set on the socket
    :param socktype: socket.SOCK_STREAM or socket.SOCK_DGRAM

    N FT)abandon_on_cancel)r~   fsdecode
startswithstatS_ISSOCKst_modeunlinkrL   r   ENOENTENOTDIREBADFELOOPra   AF_UNIXr   r   run_syncr   r
   rp   r   )r   r   socktypepath_strstat_resulter   s          r=   r   r   D  sJ    " ;;t$ ""4($ ggdm ==!4!45IIdOv~~x8J5!	$$Z__hRVWWW(($RVWWW
 :5  77LLMMKKKK	#  & XW 		sl   )FD A.F0%E* E&&E* <E(=E* F	E#AEFE##F&E* (E* *FFc                  @    e Zd ZU dZded<   ded<   d	dZed
d       Zy)TCPConnectablez
    Connects to a TCP server at the given host and port.

    :param host: host name or IP address of the server
    :param port: TCP port number of the server
    zstr | IPv4Address | IPv6Addressr   r   r   c                V    | j                   dk  s| j                   dkD  rt        d      y )Nr"   i  zTCP port number out of range)r   rd   )selfs    r=   __post_init__zTCPConnectable.__post_init__  s)    99q=DII-;<< .r   c           	        K   	 t        | j                  | j                         d {   S 7 # t        $ r.}t	        d| j                   d| j                   d|       |d }~ww xY ww)Nerror connecting to :: )r>   r   r   rL   r   r  rQ   s     r=   connectzTCPConnectable.connect  sc     	$TYY		:::: 	"&tyyk499+RuE	s0   A(#. ,. A(. 	A%)A  A%%A(N)r_   r`   )r_   r   )__name__
__module____qualname____doc____annotations__r  r-   r  r8   r   r=   r  r  z  s-     *)
I=  r   r  c                  .    e Zd ZU dZded<   edd       Zy)UNIXConnectablezr
    Connects to a UNIX domain socket at the given path.

    :param path: the file system path of the socket
    z-str | bytes | PathLike[str] | PathLike[bytes]r   c                   K   	 t        | j                         d {   S 7 # t        $ r!}t        d| j                  d|       |d }~ww xY ww)Nr  r
  )r   r   rL   r   r  s     r=   r  zUNIXConnectable.connect  sR     	Y%dii0000 	Y"%9$))b#NOUXX	Ys0   A# !# A# 	AAAAN)r_   r   )r  r  r  r  r  r-   r  r8   r   r=   r  r    s#     87Y Yr   r  )rD   r3   r<   r4   c              T   t        | t              r| S t        | t              rt        |       dk(  r	t	        |  }n6t        | t
        t        t        f      rt        |       }nt        d| d      |r6|s%t        |t              rt        |j                        }t        ||||      }|S )a  
    Return a byte stream connectable from the given object.

    If a bytestream connectable is given, it is returned unchanged.
    If a tuple of (host, port) is given, a TCP connectable is returned.
    If a string or bytes path is given, a UNIX connectable is returned.

    If ``tls=True``, the connectable will be wrapped in a
    :class:`~.streams.tls.TLSConnectable`.

    :param remote: a connectable, a tuple of (host, port) or a path to a UNIX socket
    :param tls: if ``True``, wrap the plaintext connectable in a
        :class:`~.streams.tls.TLSConnectable`, using the provided TLS settings)
    :param ssl_context: if ``tls=True``, the SSLContext object to use  (if not provided,
        a secure default will be created)
    :param tls_hostname: if ``tls=True``, host name of the server to use for checking
        the server certificate (defaults to the host portion of the address for TCP
        connectables)
    :param tls_standard_compatible: if ``False`` and ``tls=True``, makes the TLS stream
        skip the closing handshake when closing the connection, so it won't raise an
        exception if the server does the same

    r   zcannot convert z to a connectable)r3   r\   r]   )re   r   r   rm   r  r^   bytesr	   r  	TypeErrorr   r    )remoterD   r3   r<   r4   connectables         r=   as_connectabler    s    J &/0	FE	"s6{a'7$f-	FS%2	3%f-/&3DEFF

; G{//0L$#! 7	
 r   )r:   r   r;   r   r2   IPAddressType | Noner3   ssl.SSLContext | Noner4   r   r<   r^   r5   floatr_   r!   )r:   r   r;   r   r2   r  r3   zssl.SSLContextr4   r   r<   
str | Noner5   r  r_   r!   )r:   r   r;   r   r2   r  rD   zLiteral[True]r3   r  r4   r   r<   r  r5   r  r_   r!   )r:   r   r;   r   r2   r  rD   zLiteral[False]r3   r  r4   r   r<   r  r5   r  r_   r   )
r:   r   r;   r   r2   r  r5   r  r_   r   )r:   r   r;   r   r2   r  rD   r   r3   r  r4   r   r<   r  r5   r  r_   zSocketStream | TLSStream)r   str | bytes | PathLike[Any]r_   r   )r2   r  r   r   rY   r   r   r   r   r   r_   zMultiListener[SocketStream])r   r  r   
int | Noner   r   r_   r   )
rY   r   r2   r  r   r   r   r   r_   r   )r:   r   r;   r   rY   r   r2   r  r   r   r   r   r_   r   )r   "None | str | bytes | PathLike[Any]r   r   r_   r   )r   r  r   r!  r   r   r_   r   )r   zbytes | str | Noner   zstr | int | NonerY   zint | AddressFamilyrZ   zint | SocketKindr   r   r   r   r_   zAlist[tuple[AddressFamily, SocketKind, int, str, tuple[str, int]]])r   )r   r   r   r   r_   zAwaitable[tuple[str, str]])r   r   r_   Awaitable[None])r   r(   r_   r"  )r   r(   r_   r`   )r   z+tuple[str, int, int, int] | tuple[str, int]r_   ztuple[str, int])r   r!  r   r   r   r   r_   r   )r  zaByteStreamConnectable | tuple[str | IPv4Address | IPv6Address, int] | str | bytes | PathLike[str]rD   r   r3   r  r<   r  r4   r   r_   r   )[
__future__r   r   r~   ra   sslr   r   collections.abcr   dataclassesr   	ipaddressr   r   r   r	   r
   r   r   typingr   r   r   r   r    r   r   abcr   r   r   r   r   r   r   r   r   r   streams.stapledr   streams.tlsr    r!   
_eventloopr#   
_resourcesr$   _synchronizationr%   _tasksr&   r'   	_typeshedr(   objectversion_infoexceptiongroupr+   r-   typing_extensionsr/   warningsgetattrr0   rb   rh   rf   r   IPAddressFamilyr>   r   r   r   r   r   r   r   rc   r   r   r   r   r   r   r   r   r  r  r  r8   r   r=   <module>r9     s   "  	  
  
 % ! : :  , > > *   , 3 ) ) # 4,g-w*g,#v~r2]22M4J4JJ  -//1G1GGH 

 (+),$'"%			 %		
 '	 "	 	  	 	 
	 

 (+$'""%			 %		
  	 "	 	  	 	 
	 

 (+),$'""%


 %	

 

 '
 "
 
  
 
 

 

 (+),$'""%


 %	

 

 '
 "
 
  
 
 

 

 (+"% %	
    
 (,)-$(#"&FFF %	F
 
F 'F "F F  F FR8" (,!'!5!5!?!?F$F F 	F
 F F !FX 	
%  	
 B "/!8!8+! (,+!+! %+! 	+!
 +! +!d "/!8!8'+/*/*/* 	/*
 %/* /* /* /*h 6:!S2S S 	S> 6:!	, 3 	
 !J #$0
0
0  	0
 0 0 0 G0f< FG< H<( FG< H<.2>2(,F9>3
,3
3 3 	3l *  2 Y+ Y Y2 )-#$(99 
9 '9 9 "9 9r   