o
    `jC                     @   st   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
 eeZG dd dZG dd deZdS )    )dequeNc                   @   s4  e Zd ZdHddZedd Zdd Zdd	 Zd
d Zdd Z		 dd Z
dd Zdd Zdd Zdd Z	 dIddZdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) ZdHd*d+ZdHd,d-Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Z d<d= Z!d>d? Z"d@dA Z#dBdC Z$dDdE Z%dFdG Z&dS )JKafkaTCPTransportNc                 C   s\   || _ || _|| _d| _t | _d| _d | _d | _d | _	d| _
d| _t | _t | _d S )NFT)_net_sockhost_closedr   _write_buffer_writing
_read_task_write_task	_protocol_read_writetime	monotonic
last_write	last_read)selfnetsockr    r   Q/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/kafka/net/transport.py__init__   s   
zKafkaTCPTransport.__init__c                 C   s   t | j| jS N)maxr   r   r   r   r   r   last_activity   s   zKafkaTCPTransport.last_activityc                 C      | j S )z2Return True if the transport is closing or closed.)r   r   r   r   r   
is_closing$      zKafkaTCPTransport.is_closingc                 C   s8   | j std|  d| _ d| _| js|   dS dS dS )a  Close the transport.

        Buffered data will be flushed asynchronously.  No more data
        will be received.  After all buffered data is flushed, the
        protocol's connection_lost() method will (eventually) be
        called with None as its argument.
        z%s: Closing transportTFN)r   loginfor   r   _closer   r   r   r   close(   s   zKafkaTCPTransport.closec                 C   s   || _ td| | dS )zSet a new protocol.z%s: Set protocol %sN)r   r    debug)r   protocolr   r   r   set_protocol7   s   zKafkaTCPTransport.set_protocolc                 C   r   )zReturn the current protocol.)r   r   r   r   r   get_protocol<   r   zKafkaTCPTransport.get_protocolc                 C   r   )z*Return True if the transport is receiving.)r   r   r   r   r   
is_readingB   r   zKafkaTCPTransport.is_readingc                 C   s   d| _ td|  dS )zPause the receiving end.

        No data will be passed to the protocol's data_received()
        method until resume_reading() is called.
        Fz%s: Paused readingN)r   r    r$   r   r   r   r   pause_readingF   s   zKafkaTCPTransport.pause_readingc                 C   s,   | j s| j| j| _d| _ td|  dS )zResume the receiving end.

        Data received will once again be passed to the protocol's
        data_received() method.
        Tz%s: Resumed readingN)r   r   	call_soon_read_from_sockr
   r    r$   r   r   r   r   resume_readingO   s   z KafkaTCPTransport.resume_readingc              
      s   | j rh| jsj| j| jI d H  |  \}}|r| j|dS td| t	| t
 | _| jr>| jjr>| jjjt	| z| j| W n tjy_ } z| j|dW  Y d }~S d }~ww | j rl| jrd S d S d S d S )Nerrorz%s: received %d bytes)r   r   r   	wait_readr   
_sock_recvabortr    r$   lenr   r   r   r   _sensorsbytes_receivedrecorddata_receivedErrorsKafkaProtocolError)r   
recvd_dataerrer   r   r   r+   Z   s"   
z!KafkaTCPTransport._read_from_sockc              
   C   s   g }d }	 z| j d}|std|  td}W n/|| W n' ttfy,   Y n t	yH } zt
d|  t|}W Y d }~nd }~ww qd|}||fS NTi   z%s: socket disconnectedzsocket disconnectedz/%s: Error receiving network data closing socket    )r   recvr    r.   r7   KafkaConnectionErrorappendBlockingIOErrorInterruptedErrorBaseException	exceptionjoinr   recvdr:   datar;   r9   r   r   r   r0   i   s.   


zKafkaTCPTransport._sock_recvc                 C      t )a  Set the high- and low-water limits for write flow control.

        These two values control when to call the protocol's
        pause_writing() and resume_writing() methods.  If specified,
        the low-water limit must be less than or equal to the
        high-water limit.  Neither value can be negative.

        The defaults are implementation-specific.  If only the
        high-water limit is given, the low-water limit defaults to an
        implementation-specific value less than or equal to the
        high-water limit.  Setting high to zero forces low to zero as
        well, and causes pause_writing() to be called whenever the
        buffer becomes non-empty.  Setting low to zero causes
        resume_writing() to be called only once the buffer is empty.
        Use of zero for either limit is generally sub-optimal as it
        reduces opportunities for doing I/O and computation
        concurrently.
        NotImplementedError)r   highlowr   r   r   set_write_buffer_limits   s   z)KafkaTCPTransport.set_write_buffer_limitsc                 C   rI   )z,Return the current size of the write buffer.rJ   r   r   r   r   get_write_buffer_size      z'KafkaTCPTransport.get_write_buffer_sizec                 C   rI   )zGet the high and low watermarks for write flow control.
        Return a tuple (low, high) where low and high are
        positive number of bytes.rJ   r   r   r   r   get_write_buffer_limits   s   z)KafkaTCPTransport.get_write_buffer_limitsc                 C   sP   | j r| jr
td|std| j| | js&d| _| j| j	| _
dS dS )zWrite some data bytes to the transport.

        This does not block; it buffers the data and arranges for it
        to be sent out asynchronously.
        Transport closed for writeszCant write empty dataTN)r   r   RuntimeError
ValueErrorr   r@   r	   r   r*   _write_to_sockr   r   rH   r   r   r   write   s   zKafkaTCPTransport.writec                 C   sD   | j r| jr
td| j| | js d| _| j| j| _	dS dS )z>Write a list (or any iterable) of data bytes to the transport.rR   TN)
r   r   rS   r   extendr	   r   r*   rU   r   )r   list_of_datar   r   r   
writelines   s   zKafkaTCPTransport.writelinesc                    s   zB| j r?| j| jI d H  |  \}}|r!| j|dW d| _S td| | t	
 | _| jr<| jjr<| jjj| | j sW d| _nd| _w | jrQ|   d S | jsiz
| jtj W d S  tyh   Y d S w d S )Nr-   Fz%s: sent %d bytes)r   r   
wait_writer   
_sock_sendr1   r	   r    r$   r   r   r   r   r3   
bytes_sentr5   r   r"   r   shutdownsocketSHUT_WROSError)r   total_bytesr:   r   r   r   rU      s0   

z KafkaTCPTransport._write_to_sockc              
   C   s   d}| j d u r|tdfS | jrm| j }t|tst|}|rjz| j |}||7 }||d  }W n4 tt	fyH   | j
| |d f Y S  tyg } ztd| | |t|fW  Y d }~S d }~ww |s!| js|d fS Nr   zConnection closed during sendz"%s: Error sending request data: %s)r   r7   r?   r   popleft
isinstance
memoryviewsendrA   rB   
appendleftrC   r    rD   )r   rb   
next_chunk
sent_bytesr;   r   r   r   r\      s.   


zKafkaTCPTransport._sock_sendc                 C   s.   t d|  d| _| js| jtj dS dS )zClose the write end after flushing buffered data.

        (This is like typing ^D into a UNIX program reading from stdin.)

        Data may still be received.
        z%s: write_eofFN)r    r$   r   r   r   r^   r_   r`   r   r   r   r   	write_eof   s
   zKafkaTCPTransport.write_eofc                 C   s   dS )zAReturn True if this transport supports write_eof(), False if not.Tr   r   r   r   r   can_write_eof   rP   zKafkaTCPTransport.can_write_eofc                 C   sB   | j std| | d| _ | j  d | _| _| | dS dS )zClose the transport immediately.

        Buffered data will be lost.  No more data will be received.
        The protocol's connection_lost() method will (eventually) be
        called with None as its argument.
        z%s: Abort (%s)TFN)r   r    r.   r   clearr   r   r"   )r   r.   r   r   r   r1      s   
zKafkaTCPTransport.abortc              	   C   s   | j }d | _ |d ur9z| j|tjtjB  W n ttfy"   Y nw z|t	j
 W n	 ty4   Y nw |  | j| jfD ]}|d urK| j| q?d  | _| _| j}d | _|d urc|| d S d S r   )r   r   unregister_event	selectors
EVENT_READEVENT_WRITEKeyErrorrT   r^   r_   	SHUT_RDWRra   r#   r
   r   cancelr   connection_lost)r   r.   r   taskprotor   r   r   r"     s0   zKafkaTCPTransport._closec                 C      |   S )zClose the connection abruptly.)r1   r   r   r   r   abortConnection      z!KafkaTCPTransport.abortConnectionc                 C   
   | j  S )zSimilar to getPeer, but returns an address describing this side of the connection.

        Returns IPv4Address or IPv6Address.
        )r   getsocknamer   r   r   r   getHost$  s   
zKafkaTCPTransport.getHostc                 C   r{   )aX  Get the remote address of this connection.

        Treat this method with caution. It is the unfortunate result of the CGI and Jabber standards,
        but should not be considered reliable for the usual host of reasons;
        port forwarding, proxying, firewalls, IP masquerading, etc.

        Returns IPv4Address or IPv6Address.
        )r   getpeernamer   r   r   r   getPeer+  s   
	zKafkaTCPTransport.getPeerc                 C      | j tjtjS )z"Return if SO_KEEPALIVE is enabled.)r   
getsockoptr_   
SOL_SOCKETSO_KEEPALIVEr   r   r   r   getTcpKeepAlive6     z!KafkaTCPTransport.getTcpKeepAlivec                 C   r   )z!Return if TCP_NODELAY is enabled.)r   r   r_   IPPROTO_TCPTCP_NODELAYr   r   r   r   getTcpNoDelay:  r   zKafkaTCPTransport.getTcpNoDelayc                 C   rx   )z.Half-close the write side of a TCP connection.)rk   r   r   r   r   loseWriteConnection>  rz   z%KafkaTCPTransport.loseWriteConnectionc                 C      | j tjtj|S )zEnable/disable SO_KEEPALIVE.)r   
setsockoptr_   r   r   r   enabledr   r   r   setTcpKeepAliveB     z!KafkaTCPTransport.setTcpKeepAlivec                 C   r   )zEnable/disable TCP_NODELAY.)r   r   r_   r   r   r   r   r   r   setTcpNoDelayF  r   zKafkaTCPTransport.setTcpNoDelayc                 C   rx   )zClose my connection, after writing all pending data.

        Note that if there is a registered producer on a transport it will not be closed until the producer has been unregistered.
        )r#   r   r   r   r   loseConnectionJ  s   z KafkaTCPTransport.loseConnectionc                 C   s
   |  |S )zWrite an iterable of byte strings to the physical connection.

        If possible, make sure that all of the data is written to the socket at once,
        without first copying it all into a single byte string.
        )rZ   rV   r   r   r   writeSequenceY  s   
zKafkaTCPTransport.writeSequencec                    s   t d| | j d S )N%s: connected to %s)r    r!   r   r   r   r   r   	handshakea  s   zKafkaTCPTransport.handshakec              	   C   s   | j d u rdS z| j  dd \}}W n ttfy    Y dS w z	| j  d }W n ttfy<   | d|  Y S w | d| d| S )Nnoner         :z<-)r   r~   ra   rT   r|   )r   r   port
local_portr   r   r   	host_portd  s   
zKafkaTCPTransport.host_portc                 C   s.   | j rdnd}d| jj d|   d| dS )Nz	 (closed) <z []>)r   	__class____name__r   )r   stater   r   r   __str__q  s    zKafkaTCPTransport.__str__r   )NN)'r   
__module____qualname__r   propertyr   r   r#   r&   r'   r(   r)   r,   r+   r0   rN   rO   rQ   rW   rZ   rU   r\   rk   rl   r1   r"   ry   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      sN    

	
	

r   c                       sV   e Zd ZddddddddZd fdd	Zedd Zdd	 Zd
d Zdd Z	  Z
S )KafkaSSLTransportNT)ssl_contextssl_check_hostname
ssl_cafilessl_certfilessl_keyfilessl_passwordssl_crlfilec                    s|   t  | j| _| jD ]}||v r|| | j|< q
| | j| _|d ur(|dnd }| jj||dd}t j|||d d S )N.F)server_hostnamedo_handshake_on_connect)r   )	copyDEFAULT_CONFIG
ssl_config_build_ssl_context_ssl_contextrstripwrap_socketsuperr   )r   r   r   r   configskeyr   r   r   r   r     s   
zKafkaSSLTransport.__init__c                 C   s   | d d ur
| d S t t j}t jj|_| d |_| d r&|| d  n|  | d r<|j	| d | d | d d | d rP|j| d d	 | j
t jO  _
|S )
Nr   r   r   r   r   r   )certfilekeyfilepasswordr   )crl)ssl
SSLContextPROTOCOL_TLS_CLIENT
TLSVersionTLSv1_2minimum_versioncheck_hostnameload_verify_locationsload_default_certsload_cert_chainverify_flagsVERIFY_CRL_CHECK_LEAF)configctxr   r   r   r     s$   

z$KafkaSSLTransport._build_ssl_contextc                    sv   	 z| j   td| | j  W d S  tjy&   | j| j I d H  Y n tjy9   | j	| j I d H  Y nw q)NTr   )
r   do_handshaker    r!   r   SSLWantReadErrorr   r/   SSLWantWriteErrorr[   r   r   r   r   r     s   
zKafkaSSLTransport.handshakec                 C   s   g }d }	 z| j d}|std|  td}W n3|| W n+ ttt	j
t	jfy0   Y n tyL } ztd|  t|}W Y d }~nd }~ww qd|}||fS r<   )r   r>   r    r.   r7   r?   r@   rA   rB   r   r   r   rC   rD   rE   rF   r   r   r   r0     s2   



zKafkaSSLTransport._sock_recvc                 C   s   d}d }| j d u r|tdfS | jrl| j }|riz| j |}||7 }||d  }W n: tttj	tj
fyE   | j| ||f Y S  tyf } ztd| | t|}||fW  Y d }~S d }~ww |s| js||fS rc   )r   r7   r?   r   rd   rg   rA   rB   r   r   r   rh   rC   r    rD   )r   rb   r:   ri   rj   r;   r   r   r   r\     s2   



zKafkaSSLTransport._sock_sendr   )r   r   r   r   r   staticmethodr   r   r0   r\   __classcell__r   r   r   r   r   v  s    	
r   )collectionsr   r   loggingro   r_   r   r   kafka.errorserrorsr7   	getLoggerr   r    r   r   r   r   r   r   <module>   s    
  i