
    YfkO                       d Z ddlmZ ddlZddlZddlZddl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 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( ddl)m*Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1 ddl2m3Z3m4Z4m5Z5 	 ddl6m7Z8 e8Z9n# e:$ r dZ9Y nw xY werddl;m<Z< e ej=        e"ej>        iZ? ee&ej@        ejA                   G d dejB        e1e,                      ZCe9 eeCejD                    G d de-e.eC          ZE G d deC          ZF G d de+          Z* eejG                   G d de1e                      ZHdS )z
TCP support for IOCP reactor
    )annotationsN)TYPE_CHECKINGOptionalUnion)classImplementsimplementer)addressdefererror
interfacesmain)	_LogOwnerisIPv6Address)IPv4AddressIPv6Address)	IProtocol)abstractiocpsupport)ERROR_CONNECTION_REFUSEDERROR_IO_PENDINGERROR_NETWORK_UNREACHABLESO_UPDATE_ACCEPT_CONTEXTSO_UPDATE_CONNECT_CONTEXT)IReadWriteHandle)Protocol)	Connector_AbortingMixin_BaseBaseClient_BaseTCPClient_getsockname_resolveIPv6_SocketCloser)failurelogreflect)startTLS)IOCPReactorc                      e Zd ZdZdZddZd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd ZeddZd Zd ZddZd Zd Zd Zd ZdS )
Connectionz
    @ivar TLS: C{False} to indicate the connection is in normal TCP mode,
        C{True} to indicate that TLS has been started and that operations must
        be routed through the L{TLSMemoryBIOProtocol} instance.
    FNc                z    t           j                            | |           || _        |j        | _        || _        d S N)r   
FileHandle__init__socketfilenogetFileHandleprotocol)selfsockprotoreactors       R/var/www/html/env/lib/python3.11/site-packages/twisted/internet/iocpreactor/tcp.pyr-   zConnection.__init__F   s7    $$T7333![    c                    | j         S r+   )r.   r2   s    r6   	getHandlezConnection.getHandleL   s
    {r7   c                    t          |t                    rnDt          |t                    rt          |          }nt          dt	          |          z             | j                            |           dS )z`
        @param rbuffer: Data received.
        @type rbuffer: L{bytes} or L{bytearray}
        z%data must be bytes or bytearray, not N)
isinstancebytes	bytearray	TypeErrortyper1   dataReceived)r2   rbuffers     r6   rA   zConnection.dataReceivedO   sp    
 gu%% 	U++ 	UGnnGGCd7mmSTTT""7+++++r7   c                R    t          j        |                                 ||          S r+   )_iocprecvr0   )r2   bufflistevts      r6   readFromHandlezConnection.readFromHandle^   s"    z$,,..#>>>r7   c                    t          |          }t          j        |                                 |d| j                                                 |          S )z
        Send C{buff} to current file handle using C{_iocp.send}. The buffer
        sent is limited to a size of C{self.SEND_LIMIT}.
        r   )
memoryviewrD   sendr0   
SEND_LIMITtobytes)r2   buffrG   	writeViews       r6   writeToHandlezConnection.writeToHandlea   sP    
 t$$	z  )A,?"@"H"H"J"JC
 
 	
r7   c                \   	 | j                             d           n# t          $ r Y nw xY wt          j        | j        d           }|rc	 |                                 d S # t          $ r? t          j	                    }t          j                     |                     |           Y d S w xY wd S )N   )r.   shutdownOSErrorr   IHalfCloseableProtocolr1   writeConnectionLostBaseExceptionr#   Failurer$   errconnectionLost)r2   pfs      r6   _closeWriteConnectionz Connection._closeWriteConnectionk   s    	K  #### 	 	 	D	-dmTBB 	''%%'''''  ' ' 'O%%			##A&&&&&&'	' 	's    
**
A   AB)(B)c                *   t          j        | j        d           }|ra	 |                                 d S # t          $ r= t          j                     |                     t          j	                               Y d S w xY w|                     |           d S r+   )
r   rU   r1   readConnectionLostrW   r$   rY   rZ   r#   rX   )r2   reasonr[   s      r6   r_   zConnection.readConnectionLosty   s    -dmTBB 	(7$$&&&&&  7 7 7			##GO$5$56666667 '''''s   4 AA;:A;c                   | j         rd S t          j                            | |           |d u p|                    t
          j                   }|                     |           | j        }| `| `	| `
|                    |           d S r+   )disconnectedr   r,   rZ   checkr   ConnectionAborted_closeSocketr1   r.   r0   )r2   r`   isCleanr1   s       r6   rZ   zConnection.connectionLost   s     	F**4888D.MU5L(M(M$M'"""=MK'''''r7   c                    | j         S )zN
        Return the prefix to log with when I own the logging thread.
        )logstrr9   s    r6   	logPrefixzConnection.logPrefix        {r7   c                z    t          | j                            t          j        t          j                            S r+   )boolr.   
getsockoptIPPROTO_TCPTCP_NODELAYr9   s    r6   getTcpNoDelayzConnection.getTcpNoDelay   s(    DK**6+=v?QRRSSSr7   c                f    | j                             t           j        t           j        |           d S r+   )r.   
setsockoptrn   ro   r2   enableds     r6   setTcpNoDelayzConnection.setTcpNoDelay   s(    v163EwOOOOOr7   c                z    t          | j                            t          j        t          j                            S r+   )rl   r.   rm   
SOL_SOCKETSO_KEEPALIVEr9   s    r6   getTcpKeepAlivezConnection.getTcpKeepAlive   s(    DK**6+<f>QRRSSSr7   c                f    | j                             t           j        t           j        |           d S r+   )r.   rr   rw   rx   rs   s     r6   setTcpKeepAlivezConnection.setTcpKeepAlive   s(    v0&2EwOOOOOr7   Tc                >    t          | ||t          j                   dS )z9
            @see: L{ITLSTransport.startTLS}
            N)	_startTLSr   r,   )r2   contextFactorynormals      r6   r&   zConnection.startTLS   s!     dNFH4GHHHHHr7   c                    | j         rdS | j        r| j                            |           dS t          j                            | |           dS )z
        Write some data, either directly to the underlying handle or, if TLS
        has been started, to the L{TLSMemoryBIOProtocol} for it to encrypt and
        send.

        @see: L{twisted.internet.interfaces.ITransport.write}
        N)rb   TLSr1   writer   r,   )r2   datas     r6   r   zConnection.write   sY      	F8 	2M%%%%%%%dD11111r7   c                    | j         rdS | j        r| j                            |           dS t          j                            | |           dS )z
        Write some data, either directly to the underlying handle or, if TLS
        has been started, to the L{TLSMemoryBIOProtocol} for it to encrypt and
        send.

        @see: L{twisted.internet.interfaces.ITransport.writeSequence}
        N)rb   r   r1   writeSequencer   r,   )r2   iovecs     r6   r   zConnection.writeSequence   sY      	F8 	;M''.....--dE:::::r7   c                    | j         r-| j        r"| j        s| j                                         dS dS dS t
          j                            | |           dS )z
        Close the underlying handle or, if TLS has been started, first shut it
        down.

        @see: L{twisted.internet.interfaces.ITransport.loseConnection}
        N)r   	connecteddisconnectingr1   loseConnectionr   r,   )r2   r`   s     r6   r   zConnection.loseConnection   sq     8 	=~ /d&8 /,,...../ / / / ..tV<<<<<r7   c                    | j         r| j                            ||           dS t          j                            | ||           dS )zc
        Register a producer.

        If TLS is enabled, the TLS connection handles this.
        N)r   r1   registerProducerr   r,   )r2   producer	streamings      r6   r   zConnection.registerProducer   sP     8 	L
 M**8Y?????00xKKKKKr7   c                    | j         r| j                                         dS t          j                            |            dS )ze
        Unregister a producer.

        If TLS is enabled, the TLS connection handles this.
        N)r   r1   unregisterProducerr   r,   r9   s    r6   r   zConnection.unregisterProducer   sE     8 	9M,,.....22488888r7   c                    d S r+    r9   s    r6   getHostzConnection.getHost       r7   c                    d S r+   r   r9   s    r6   getPeerzConnection.getPeer   r   r7   r+   )T)__name__
__module____qualname____doc__r   r-   r:   rA   rH   rP   r]   r_   rZ   ri   rp   ru   ry   r{   r}   r&   r   r   r   r   r   r   r   r   r7   r6   r)   r)   <   s         C     , , ,? ? ?
 
 
' ' '	( 	( 	(
( 
( 
(  T T TP P PT T TP P P 	I 	I 	I 	I2 2 2; ; ;= = = =L L L	9 	9 	9      r7   r)   c                  Z    e Zd ZdZej        Zej        ZdZ	e
Zd Zd Zd Zd Zd Zd Zd	S )
Clientz
    @ivar _tlsClientDefault: Always C{True}, indicating that this is a client
        connection, and by default when TLS is negotiated this class will act as
        a TLS client.
    Tc                N    |d}|| _         t          j        | |||||           d S )N) r   )r5   r   r-   )r2   hostportbindAddress	connectorr5   s         r6   r-   zClient.__init__	  s5    !KdD+y'RRRRRr7   c                L    | j                             | j        | j                  S )zd
        Create a socket registered with the IOCP reactor.

        @see: L{_BaseTCPClient}
        )r5   createSocketaddressFamily
socketTyper9   s    r6   createInternetSocketzClient.createInternetSocket  s!     |(();T_MMMr7   c                    | ` | `dS )z
        Clean up potentially circular references to the socket and to its
        C{getFileHandle} method.

        @see: L{_BaseBaseClient}
        N)r.   r0   r9   s    r6   _collectSocketDetailszClient._collectSocketDetails  s     K+++r7   c                :    | j                             |            dS )z^
        Remove the active handle from the reactor.

        @see: L{_BaseBaseClient}
        N)r5   removeActiveHandler9   s    r6   _stopReadingAndWritingzClient._stopReadingAndWriting!  s      	''-----r7   c           	        |rdt                               ||          }|                     t          j        |t
          j                            |d          f                     d S | j                            t          j	        t          t          j        d| j                                                             | j                            |                                           | _        d| _        |                     | j                  }|dz   | _        | j        )t+                      | _        |                                  d S | j                            |            |                                  d S )NzUnknown errorPTz,client)connectExErrorsgetfailIfNotConnectedr   getConnectErrorerrno	errorcoder.   rr   rw   r   structpackr/   r   buildProtocolr   r1   r   _getLogPrefixrh   r   r   makeConnectionstartReading)r2   rcr   rG   ri   s        r6   	cbConnectzClient.cbConnect)  sL    	$ $$R,,B##%r5?+>+>r?+S+S&TUU     K""!)C!3!3!5!566  
 !N88HHDM!DN**4=99I#i/DK}$ !)

##%%%%%,,T222!!#####r7   c                b   t          | d          sd S t          j        sJ | j                            |            t          j        | j        |           }t          j        | j        	                                | j
        |          }|r$|t          k    r|                     |d|           d S d S d S )Nr   r   )hasattrrD   have_connectexr5   addActiveHandleEventr   connectr.   r/   realAddressr   )r2   rG   r   s      r6   	doConnectzClient.doConnectG  s    t[)) 	 F####$$T***k$.$//]4;--//1A3GG 	'"(((NN2q#&&&&&	' 	'((r7   N)r   r   r   r   r.   AF_INETr   SOCK_STREAMr   _tlsClientDefaultr)   _commonConnectionr-   r   r   r   r   r   r   r7   r6   r   r      s          NM#J"S S SN N N, , ,. . .$ $ $<' ' ' ' 'r7   r   c                  2    e Zd ZdZdZddZddZd Zd ZdS )ServeraV  
    Serverside socket-stream connection class.

    I am a serverside network connection transport; a socket which came from an
    accept() on a server.

    @ivar _tlsClientDefault: Always C{False}, indicating that this is a server
        connection, and by default when TLS is negotiated this class will act as
        a TLS server.
    Fr3   socket.socketr1   r   
clientAddrUnion[IPv4Address, IPv6Address]
serverAddr	sessionnointr5   r'   c                z   t                               | |||           || _        || _        || _        |                     | j                  }| d| d| j        j         | _        d	                    | j        j
        j        | j        | j        j                  | _        d| _        |                                  dS )a  
        Server(sock, protocol, client, server, sessionno)

        Initialize me with a socket, a protocol, a descriptor for my peer (a
        tuple of host, port describing the other end of the connection), an
        instance of Port, and a session number.
        ,z<{} #{} on {}>TN)r)   r-   r   r   r   r   r1   r   rh   format	__class__r   r   repstrr   r   )r2   r3   r1   r   r   r   r5   ri   s           r6   r-   zServer.__init__c  s      	D$':::$$"&&t}55	"GGYGG1EGG+22M#,NO 
 

 r7   returnstrc                    | j         S )z=
        A string representation of this connection.
        )r   r9   s    r6   __repr__zServer.__repr__  rj   r7   c                    | j         S )zW
        Returns an IPv4Address.

        This indicates the server's address.
        )r   r9   s    r6   r   zServer.getHost       r7   c                    | j         S )zW
        Returns an IPv4Address.

        This indicates the client's address.
        )r   r9   s    r6   r   zServer.getPeer  r   r7   N)r3   r   r1   r   r   r   r   r   r   r   r5   r'   r   r   )	r   r   r   r   r   r-   r   r   r   r   r7   r6   r   r   U  sl        	 	    <         r7   r   c                      e Zd Zd ZdS )r   c                P    t          | j        | j        | j        | | j                  S r+   )r   r   r   r   r5   r9   s    r6   _makeTransportzConnector._makeTransport  s!    diD,<dDLQQQr7   N)r   r   r   r   r   r7   r6   r   r     s(        R R R R Rr7   r   c                      e Zd ZU dZdZdZej        Zej	        Z
ej        ZdZdZded<   dZdd	ZddZd Z ej        ej                  fdZeZd Zd Zd Zd Zd Zd Z d Z!dS )PortFr   NzOptional[int]_realPortNumberTCP2   r   c                    || _         || _        || _        || _        || _        t          |          r$t          j        | _        t          j
        | _        d S d S r+   )r   factorybacklog	interfacer5   r   r.   AF_INET6r   r	   r   _addressType)r2   r   r   r   r   r5   s         r6   r-   zPort.__init__  sZ    	"## 	4!'D ' 3D	4 	4r7   r   r   c                    | j         +d                    | j        | j        j        | j                   S d                    | j        | j        j                  S )Nz<{} of {} on {}>z<{} of {} (not listening)>)r   r   r   r   r9   s    r6   r   zPort.__repr__  s]    +%,,&$   066&  r7   c                l   	 | j                             | j        | j                  }| j        t          j        k    rt          | j        | j                  }n| j        | j        f}|	                    |           n2# t          $ r%}t          j        | j        | j        |          d }~ww xY wt          j        |                                          | _        |                                d         | _        t'          j        |                     | j                  d| j                   | j                                         |                    | j                   d| _        d| _        | j                             |            || _        | j        j        | _        |                                  d S )NrR   z starting on TF)r5   r   r   r   r.   r   r!   r   r   bindrT   r   CannotListenErrorrD   
maxAddrLenr/   addrLengetsocknamer   r$   msgr   r   doStartlistenr   r   rb   r   r0   doAccept)r2   sktaddrles       r6   startListeningzPort.startListening  s   		I,++D,>PPC!V_44#DNDI>>	2HHTNNNN 	I 	I 	I)$.$)RHHH	I '

55  #003!!$,////1E1EG	
 	
 	

 	

4<   !$$T***![/s   A8A; ;
B* B%%B*c                    d| _         | j        r@t          j                    | _        | j                            d| j        |           | j        S dS )z
        Stop accepting connections on this port.

        This will shut down my socket and call self.connectionLost().
        It returns a deferred which will fire successfully when the
        port is actually closed.
        Tr   N)r   r   r
   Deferreddeferredr5   	callLaterrZ   )r2   connDones     r6   r   zPort.loseConnection  sT     "> 	!!N,,DML""1d&98DDD= 	! 	!r7   c                P    t          j        d| j         d| j         d           dS )z.
        Log message for closing port
        (z Port z Closed)N)r$   r   _typer   r9   s    r6   _logConnectionLostMsgzPort._logConnectionLostMsg  s3     	DDJDDd&:DDDEEEEEr7   c                   |                                   d| _        d}t          | d          r	| j        }| `d| _        | j                            |            d| _        |                     d           | `	| `
	 | j                                         d| _        ||                    d           dS dS # t          $ r4 d| _        |)|                    t#          j                               Y dS  w xY w)z'
        Cleans up the socket.
        Nr   TF)r  r   r   r   rb   r5   r   r   re   r.   r0   r   doStopr   callbackrW   errbackr#   rX   )r2   r`   ds      r6   rZ   zPort.connectionLost  s    	""$$$#4$$ 	A ''---$K	!L!!! "'D}

4      }  	 	 	!&D}		'/++,,,,,,	s   9B4 49C20C2c                >    t          j        | j        j                  S )zK
        Returns the name of my class, to prefix log entries with.
        )r%   qualr   r   r9   s    r6   ri   zPort.logPrefix  s     |DL2333r7   c                B     | j         dgt          | j                  R  S )zf
        Returns an IPv4Address or IPv6Address.

        This indicates the server's address.
        r   )r   r    r.   r9   s    r6   r   zPort.getHost#  s)     !t Cdk)B)BCCCCr7   c                ~    |                      ||           | j        s| j        s|                                  d S d S d S r+   )handleAcceptr   rb   r   )r2   r   r   rG   s       r6   cbAcceptzPort.cbAccept+  sR    "c"""" 	d&7 	MMOOOOO	 	 	 	r7   c           	     2   | j         s| j        rdS |r;t          j        dt          j                            |d          d|d           dS |j                            t          j
        t          t          j        d| j	                                                             t          j        |j                                        |j                  \  }}}|| j        k    sJ d|d         v r@t'          |d                             d          d	                   }|d         |d	         d|f}d|d         v r@t'          |d                             d          d	                   }|d         |d	         d|f}| j                             | j        d
g|R            }||j                                         n\| j        }|d	z   | _        t5          |j        | | j        d
g|R   | j        d
g|R  || j                  }	|                    |	           dS )NFz#Could not accept new connection -- zunknown errorz ()r   %r   rR   r   T)r   rb   r$   r   r   r   r   newsktrr   r.   rw   r   r   r   r/   rD   get_accept_addrsrN   r   r   splitr   r   r   closer   r   r5   r   )
r2   r   rG   familylAddrrAddrscoper1   s	transports
             r6   r  zPort.handleAccept0  s;    	!2 	5  +	GG?&&r?;;;;RRRA   5 J!!!(C!3!3!5!566  
 $)#9#*:K:K:M:Msx#X#X FE5T///// eAhE!HNN3//233q58Q6eAhE!HNN3//233q58Q6|112C$2CE2RE2R2R2RSSH
  """"N!"Q"J%D%e4e444%D%e4e444L 	 ''	2224r7   c                   t          j        | j        |           }t          d| j        dz   z            x|_        }| j                            | j        | j	                  x|_
        }t          j        | j                                        |                                ||          }|r#|t          k    r|                     ||           d S d S d S )N      )rD   r   r  r>   r   rN   r5   r   r   r   r  acceptr.   r/   r   r  )r2   rG   rN   r  r   s        r6   r   zPort.doAcceptc  s    k$-.. $A):$;<<<4"l77
 
 	

V \$+,,..sKK 	'"(((b#&&&&&	' 	'((r7   )r   r   Nr   )"r   r   r   r   rb   r   r.   r   r   r   r   r	   r   r   r   r   __annotations__r  r-   r   r   r#   rX   r   CONNECTION_DONEr   stopListeningr  rZ   ri   r   r  r  r   r   r7   r6   r   r     s2        ILMNM#J&LI &*O)))) E4 4 4 4     @ '6god6J&K&K ! ! ! ! #MF F F! ! !>4 4 4D D D  
1 1 1f' ' ' ' 'r7   r   )Ir   
__future__r   r   r.   r   typingr   r   r   zope.interfacer   r   twisted.internetr	   r
   r   r   r   twisted.internet.abstractr   r   twisted.internet.addressr   r   twisted.internet.interfacesr   twisted.internet.iocpreactorr   r   rD   "twisted.internet.iocpreactor.constr   r   r   r   r   'twisted.internet.iocpreactor.interfacesr   twisted.internet.protocolr   twisted.internet.tcpr   TCPConnectorr   r   r   r    r!   r"   twisted.pythonr#   r$   r%   twisted.internet._newtlsr&   
__startTLSr}   ImportError$twisted.internet.iocpreactor.reactorr'   WSAECONNREFUSEDWSAENETUNREACHr   ITCPTransportISystemHandler,   r)   ITLSTransportr   r   IListeningPortr   r   r7   r6   <module>r;     s    # " " " " "    1 1 1 1 1 1 1 1 1 1 7 7 7 7 7 7 7 7 D D D D D D D D D D D D D D > > > > > > > > = = = = = = = = 1 1 1 1 1 1 G G G G G G G G              E D D D D D . . . . . .                  1 0 0 0 0 0 0 0 0 0?????? II    III
  A@@@@@@ e3u3 z79QRRx x x x x$m^ x x SRxv OJ
 8999V' V' V' V' V'_nj V' V' V'r@ @ @ @ @Z @ @ @FR R R R R R R R
 Z&''Q' Q' Q' Q' Q'=) Q' Q' ('Q' Q' Q's   
B BB