o
    iD3                     @  s  U d dl mZ d dlZd dlZd dlmZ d dlmZmZm	Z	 d dl
mZ d dlmZ d dlmZmZ d dlmZ d d	lmZmZmZ d
dlmZ d
dlmZmZmZ ddlmZmZmZ ddl m!Z! e"eB eB Z#de$d< e%e"e&f Z'de$d< e'e"B Z(de$d< e%e)e'f Z*de$d< e%e)e"f Z+de$d< edZ,ej-fdddd8d$d%Z.G d&d' d'eZ/G d(d) d)eZ0G d*d+ d+ee0Z1G d,d- d-e1Z2G d.d/ d/ee1 e0Z3G d0d1 d1ee* e0Z4G d2d3 d3ee) e0Z5G d4d5 d5ee+ e0Z6G d6d7 d7ee) e0Z7dS )9    )annotationsN)abstractmethod)Callable
CollectionMapping)AsyncExitStack)IOBase)IPv4AddressIPv6Address)AddressFamily)Any	TypeAliasTypeVar   )get_async_backend)TypedAttributeProviderTypedAttributeSettyped_attribute   )
ByteStreamListenerUnreliableObjectStream)	TaskGroupr   IPAddressTypeIPSockAddrTypeSockAddrTypeUDPPacketTypeUNIXDatagramPacketTypeT_RetvalF)require_connectedrequire_bound
sock_or_fdsocket.socket | int	sock_typesocket.SocketKindaddr_familysocket.AddressFamilyr   boolr    returnsocket.socketc             
   C  s  t | tr4ztj| d}W n: ty3 } z|jtjkr td||r'td||r.td| d }~ww t | tjr=| }ntdt| j	 dzo|rcz|
  W n tyb } ztd|d }~ww |rz|jtjtjfv rv| d }n| }W n ty   d }Y nw |std|tjkr|j|krtd|j d	|jj |j|krtd
|j d	|jj W n ty   t | tr|   w |d |S )N)filenoz.the file descriptor does not refer to a socketzthe socket must be connectedz+the socket must be bound to a local addresszexpected an int or socket, got z insteadr   z"address family mismatch: expected z, got zsocket type mismatch: expected F)
isinstanceintsocketOSErrorerrnoENOTSOCK
ValueError	TypeErrortype__qualname__getpeernamefamilyAF_INETAF_INET6getsockname	AF_UNSPECnameBaseExceptiondetachsetblocking)r!   r#   r%   r   r    sockexcZ
bound_addr rA   L/home/kim/smarthome/.venv/lib/python3.10/site-packages/anyio/abc/_sockets.py_validate_socket   sv   







rC   c                   @  sf   e Zd ZU dZe Zded< e Zded< e Zded< e Z	ded	< e Z
ded
< e Zded< dS )SocketAttributea  
    .. attribute:: family
        :type: socket.AddressFamily

        the address family of the underlying socket

    .. attribute:: local_address
        :type: tuple[str, int] | str

        the local address the underlying socket is connected to

    .. attribute:: local_port
        :type: int

        for IP based sockets, the local port the underlying socket is bound to

    .. attribute:: raw_socket
        :type: socket.socket

        the underlying stdlib socket object

    .. attribute:: remote_address
        :type: tuple[str, int] | str

        the remote address the underlying socket is connected to

    .. attribute:: remote_port
        :type: int

        for IP based sockets, the remote port the underlying socket is connected to
    r   r6   r   local_addressr,   
local_portr)   
raw_socketremote_addressremote_portN)__name__
__module__r4   __doc__r   r6   __annotations__rE   rF   rG   rH   rI   rA   rA   rA   rB   rD   c   s   
  rD   c                   @  s,   e Zd Zed	ddZeed
ddZdS )_SocketProviderr(   Mapping[Any, Callable[[], Any]]c                   s   ddl m  tjfddtj fddtjfddi}z	 j W n ty2   d Y nw d ur@fdd|tj	< jjt
jt
jfv rdfdd|tj< d urdd	 fd
d|tj< |S )Nr   )convert_ipv6_sockaddrc                     s    j jS N)_raw_socketr6   rA   selfrA   rB   <lambda>   s    z2_SocketProvider.extra_attributes.<locals>.<lambda>c                     s    j  S rQ   rR   r9   rA   )convertrT   rA   rB   rU      s    c                     s    j S rQ   )rR   rA   rS   rA   rB   rU          c                         S rQ   rA   rA   )peernamerA   rB   rU          c                     s    j  d S )Nr   rV   rA   rS   rA   rB   rU      s   r   c                     rY   rQ   rA   rA   )rI   rA   rB   rU      r[   )Z_core._socketsrP   rD   r6   rE   rG   rR   r5   r.   rH   r   r7   r8   rF   rI   )rT   
attributesrA   )rW   rZ   rI   rT   rB   extra_attributes   s$   z _SocketProvider.extra_attributesr)   c                 C  s   d S rQ   rA   rS   rA   rA   rB   rR      s   z_SocketProvider._raw_socketN)r(   rO   )r(   r)   )rJ   rK   r4   propertyr]   r   rR   rA   rA   rA   rB   rN      s    rN   c                   @     e Zd ZdZedddZdS )	SocketStreamzu
    Transports bytes over a socket.

    Supports all relevant extra attributes from :class:`~SocketAttribute`.
    r!   r"   r(   c                   $   t |tjdd}t |I dH S )aH  
        Wrap an existing socket object or file descriptor as a socket stream.

        The newly created socket wrapper takes ownership of the socket being passed in.
        The existing socket must already be connected.

        :param sock_or_fd: a socket object or file descriptor
        :return: a socket stream

        Tr   N)rC   r-   SOCK_STREAMr   Zwrap_stream_socketclsr!   r?   rA   rA   rB   from_socket      zSocketStream.from_socketN)r!   r"   r(   r`   rJ   rK   r4   rL   classmethodrf   rA   rA   rA   rB   r`          r`   c                   @  s6   e Zd ZedddZedddZedddZdS )UNIXSocketStreamr!   r"   r(   c                   (   t |tjtjdd}t |I dH S )aR  
        Wrap an existing socket object or file descriptor as a UNIX socket stream.

        The newly created socket wrapper takes ownership of the socket being passed in.
        The existing socket must already be connected.

        :param sock_or_fd: a socket object or file descriptor
        :return: a UNIX socket stream

        Trb   N)rC   r-   rc   AF_UNIXr   Zwrap_unix_stream_socketrd   rA   rA   rB   rf      s
   zUNIXSocketStream.from_socketmessagebytesfdsCollection[int | IOBase]Nonec                      dS )z
        Send file descriptors along with a message to the peer.

        :param message: a non-empty bytestring
        :param fds: a collection of files (either numeric file descriptors or open file
            or socket objects)
        NrA   )rT   rn   rp   rA   rA   rB   send_fds   rX   zUNIXSocketStream.send_fdsmsglenr,   maxfdstuple[bytes, list[int]]c                   rs   )a  
        Receive file descriptors along with a message from the peer.

        :param msglen: length of the message to expect from the peer
        :param maxfds: maximum number of file descriptors to expect from the peer
        :return: a tuple of (message, file descriptors)
        NrA   )rT   ru   rv   rA   rA   rB   receive_fds   rX   zUNIXSocketStream.receive_fdsN)r!   r"   r(   rk   )rn   ro   rp   rq   r(   rr   )ru   r,   rv   r,   r(   rw   )rJ   rK   r4   ri   rf   r   rt   rx   rA   rA   rA   rB   rk      s    	rk   c                   @  s:   e Zd ZdZedddZeddd	Z	
ddddZd
S )SocketListenerz}
    Listens to incoming socket connections.

    Supports all relevant extra attributes from :class:`~SocketAttribute`.
    r!   r"   r(   c                   ra   )a  
        Wrap an existing socket object or file descriptor as a socket listener.

        The newly created listener takes ownership of the socket being passed in.

        :param sock_or_fd: a socket object or file descriptor
        :return: a socket listener

        Tr    N)rC   r-   rc   r   Zwrap_listener_socketrd   rA   rA   rB   rf      s   zSocketListener.from_socketr`   c                   rs   )zAccept an incoming connection.NrA   rS   rA   rA   rB   accept  rX   zSocketListener.acceptNhandlerCallable[[SocketStream], Any]
task_groupTaskGroup | Nonerr   c              	     sp   ddl m} t 4 I d H }|d u r|| I d H }	 |  I d H }||| q1 I d H s1w   Y  d S )Nr   )create_task_group) r   r   enter_async_contextr{   Z
start_soon)rT   r|   r~   r   stackstreamrA   rA   rB   serve  s   zSocketListener.serve)r!   r"   r(   ry   )r(   r`   rQ   )r|   r}   r~   r   r(   rr   )	rJ   rK   r4   rL   ri   rf   r   r{   r   rA   rA   rA   rB   ry      s    ry   c                   @  s(   e Zd ZdZedddZdddZdS )	UDPSocketz{
    Represents an unconnected UDP socket.

    Supports all relevant extra attributes from :class:`~SocketAttribute`.
    r!   r"   r(   c                   ra   )aI  
        Wrap an existing socket object or file descriptor as a UDP socket.

        The newly created socket wrapper takes ownership of the socket being passed in.
        The existing socket must be bound to a local address.

        :param sock_or_fd: a socket object or file descriptor
        :return: a UDP socket

        Trz   N)rC   r-   
SOCK_DGRAMr   Zwrap_udp_socketrd   rA   rA   rB   rf   #  rg   zUDPSocket.from_socketdataro   hoststrportr,   rr   c                   s   |  |||ffI dH S )z^
        Alias for :meth:`~.UnreliableObjectSendStream.send` ((data, (host, port))).

        Nsend)rT   r   r   r   rA   rA   rB   sendto2  s   zUDPSocket.sendtoN)r!   r"   r(   r   )r   ro   r   r   r   r,   r(   rr   rJ   rK   r4   rL   ri   rf   r   rA   rA   rA   rB   r     s
    r   c                   @  r_   )	ConnectedUDPSocketzy
    Represents an connected UDP socket.

    Supports all relevant extra attributes from :class:`~SocketAttribute`.
    r!   r"   r(   c                   ra   )aV  
        Wrap an existing socket object or file descriptor as a connected UDP socket.

        The newly created socket wrapper takes ownership of the socket being passed in.
        The existing socket must already be connected.

        :param sock_or_fd: a socket object or file descriptor
        :return: a connected UDP socket

        Trb   N)rC   r-   r   r   Zwrap_connected_udp_socketrd   rA   rA   rB   rf   A  s   zConnectedUDPSocket.from_socketN)r!   r"   r(   r   rh   rA   rA   rA   rB   r   :  rj   r   c                   @  s(   e Zd ZdZedddZdddZdS )UNIXDatagramSocketz
    Represents an unconnected Unix datagram socket.

    Supports all relevant extra attributes from :class:`~SocketAttribute`.
    r!   r"   r(   c                   s$   t |tjtj}t |I dH S )a'  
        Wrap an existing socket object or file descriptor as a UNIX datagram
        socket.

        The newly created socket wrapper takes ownership of the socket being passed in.

        :param sock_or_fd: a socket object or file descriptor
        :return: a UNIX datagram socket

        N)rC   r-   r   rm   r   Zwrap_unix_datagram_socketrd   rA   rA   rB   rf   ^  s   zUNIXDatagramSocket.from_socketr   ro   pathr   rr   c                   s   |  ||fI dH S )zCAlias for :meth:`~.UnreliableObjectSendStream.send` ((data, path)).Nr   )rT   r   r   rA   rA   rB   r   p  s   zUNIXDatagramSocket.sendtoN)r!   r"   r(   r   )r   ro   r   r   r(   rr   r   rA   rA   rA   rB   r   U  s
    r   c                   @  r_   )	ConnectedUNIXDatagramSocketz
    Represents a connected Unix datagram socket.

    Supports all relevant extra attributes from :class:`~SocketAttribute`.
    r!   r"   r(   c                   rl   )ar  
        Wrap an existing socket object or file descriptor as a connected UNIX datagram
        socket.

        The newly created socket wrapper takes ownership of the socket being passed in.
        The existing socket must already be connected.

        :param sock_or_fd: a socket object or file descriptor
        :return: a connected UNIX datagram socket

        Trb   N)rC   r-   r   rm   r   Z#wrap_connected_unix_datagram_socketrd   rA   rA   rB   rf   |  s
   z'ConnectedUNIXDatagramSocket.from_socketN)r!   r"   r(   r   rh   rA   rA   rA   rB   r   u  rj   r   )r!   r"   r#   r$   r%   r&   r   r'   r    r'   r(   r)   )8
__future__r   r/   r-   abcr   collections.abcr   r   r   
contextlibr   ior   	ipaddressr	   r
   r   typingr   r   r   Z_core._eventloopr   Z_core._typedattrr   r   r   Z_streamsr   r   r   _tasksr   r   r   rM   tupler,   r   r   ro   r   r   r   r:   rC   rD   rN   r`   rk   ry   r   r   r   r   rA   rA   rA   rB   <module>   sH    E)&',
 