
    hVO                        d 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m	Z	m
Z
mZmZmZmZmZmZmZmZmZmZ dZ ej        eed            eeeeeeeh          Z	 e n# e$ r i ZY nw xY wd Z G d d	e          Zeee fZ!d
 Z"d Z#d Z$d Z%ddZ&ddZ'e'Z(d dZ) G d d          Z* G d de*          Z+d Z,d!dZ-ej.        dk    r G d d          Z/ G d de*          Z0dS dS )"a  Basic infrastructure for asynchronous socket service clients and servers.

There are only two ways to have a program on a single processor do "more
than one thing at a time".  Multi-threaded programming is the simplest and
most popular way to do it, but there is another very different technique,
that lets you have nearly all the advantages of multi-threading, without
actually using multiple threads. it's really only practical if your program
is largely I/O bound. If your program is CPU bound, then pre-emptive
scheduled threads are probably what you really need. Network servers are
rarely CPU-bound, however.

If your operating system supports the select() system call in its I/O
library (and nearly all do), then you can use it to juggle multiple
communication channels at once; doing other work while your I/O is taking
place in the "background."  Although this strategy can seem strange and
complex, especially at first, it is in many ways easier to understand and
control than multi-threaded programming. The module documented here solves
many of the difficult problems for you, making the task of building
sophisticated high-performance network servers and clients a snap.
    N)EALREADYEINPROGRESSEWOULDBLOCK
ECONNRESETEINVALENOTCONN	ESHUTDOWNEISCONNEBADFECONNABORTEDEPIPEEAGAIN	errorcodeznThe {name} module is deprecated and will be removed in Python {remove}. The recommended replacement is asyncio)      )removec                     	 t          j        |           S # t          t          t          f$ r  | t
          v rt
          |          cY S d| z  cY S w xY w)NzUnknown error %s)osstrerror
ValueErrorOverflowError	NameErrorr   )errs    /usr/lib/python3.11/asyncore.py	_strerrorr   I   se    '{3y1 ' ' ')S>!!!!3&&&&'s    -AAAc                       e Zd ZdS )ExitNowN)__name__
__module____qualname__     r   r   r   Q   s        Dr"   r   c                 |    	 |                                   d S # t          $ r   |                                  Y d S xY wN)handle_read_event_reraised_exceptionshandle_errorobjs    r   readr*   V   X            ;c                 |    	 |                                   d S # t          $ r   |                                  Y d S xY wr$   )handle_write_eventr&   r'   r(   s    r   writer/   ^   sX            r,   c                 |    	 |                                   d S # t          $ r   |                                  Y d S xY wr$   )handle_expt_eventr&   r'   r(   s    r   
_exceptionr2   f   r+   r,   c                 D   	 |t           j        z  r|                                  |t           j        z  r|                                  |t           j        z  r|                                  |t           j        t           j        z  t           j	        z  z  r| 
                                 d S d S # t          $ rH}|j        t          vr|                                  n| 
                                 Y d }~d S Y d }~d S d }~wt          $ r   |                                  Y d S xY wr$   )selectPOLLINr%   POLLOUTr.   POLLPRIr1   POLLHUPPOLLERRPOLLNVALhandle_closeOSErrorerrno_DISCONNECTEDr'   r&   )r)   flagses      r   	readwriterA   n   sT   6=  	$!!###6>! 	%""$$$6>! 	$!!###FNV^3foEF 		 	   7-''      s   B&B, ,
D67C99#D        c                     |t           }|rog }g }g }t          |                                          D ]{\  }}|                                }|                                }|r|                    |           |r|j        s|                    |           |s|r|                    |           |g |cxk    r|cxk    r|k    rn nt          j        |            d S t          j	        ||||           \  }}}|D ])}|
                    |          }|t          |           *|D ])}|
                    |          }|t          |           *|D ]+}|
                    |          }|t          |           *d S d S r$   )
socket_maplistitemsreadablewritableappend	acceptingtimesleepr4   getr*   r/   r2   )	timeoutmaprwr@   fdr)   is_ris_ws	            r   pollrU      s   
{
 "BBCIIKK(( 		 		GB<<>>D<<>>D  CM  t a1JwF-1a111a 	 	B''"++C{IIII 	 	B''"++C{#JJJJ 	 	B''"++C{sOOOOE" "<	 	r"   c                 .   |t           }| t          | dz            } t          j                    }|rt	          |                                          D ]y\  }}d}|                                r|t          j        t          j        z  z  }|	                                r|j
        s|t          j        z  }|r|                    ||           z|                    |           }|D ]/\  }}|                    |          }|t          ||           .d S d S )Ni  r   )rD   intr4   rU   rE   rF   rG   r5   r7   rH   rJ   r6   registerrM   rA   )rN   rO   pollsterrR   r)   r?   rP   s          r   poll2rZ      s'   
{gdl##{}}H
 "CIIKK(( 	- 	-GBE||~~ 877||~~ (cm (' -!!"e,,,MM'"" 	" 	"IB''"++C{c5!!!!!" "	" 	"r"         >@Fc                     |t           }|rt          t          d          rt          }nt          }||r || |           |d S d S |r!|dk    r || |           |dz
  }|r
|dk    d S d S d S d S )NrU   r      )rD   hasattrr4   rZ   rU   )rN   use_pollrO   countpoll_funs        r   looprb      s    
{ GFF++ } 	#HWc"""  	# 	# 	# 	# 	#  	eaiiHWc"""AIE  	eaiiii 	 	 	 	iir"   c                      e Zd ZdZdZdZdZdZdZ e	dh          Z
d#dZd Zd$dZd$dZej        ej        f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%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+dS )&
dispatcherFNwarningc                    |t           | _        n|| _        d | _        |r|                    d           |                     ||           d| _        	 |                                | _        d S # t          $ r>}|j	        t          t          fv rd| _        n|                     |            Y d }~d S d }~ww xY wd | _        d S )NFT)rD   _map_filenosetblocking
set_socket	connectedgetpeernameaddrr<   r=   r   r   del_channelsocket)selfsockrO   r   s       r   __init__zdispatcher.__init__   s    ;"DIIDI 	 U###OOD#&&&!DN ,,..			 
 
 
96 222 &+DNN
 $$S))) #NNNNN	
 DKKKs   A. .
B683B11B6c                    | j         j        dz   | j         j        z   g}| j        r| j        r|                    d           n| j        r|                    d           | j        V	 |                    d| j        z             n7# t          $ r* |                    t          | j                             Y nw xY wdd	                    |          t          |           fz  S )N.	listeningrk   z%s:%dz<%s at %#x> )	__class__r   r    rJ   rm   rI   rk   	TypeErrorreprjoinid)rp   statuss     r   __repr__zdispatcher.__repr__  s    .+C/0KKL> 	'di 	'MM+&&&&^ 	'MM+&&&9 /g	12222 / / /d49oo...../ 0 0"T((;;;s   &B 1B87B8c                 ,    || j         }| || j        <   d S r$   )rg   rh   )rp   rO   s     r   add_channelzdispatcher.add_channel  s     ;)C DLr"   c                 B    | j         }|| j        }||v r||= d | _         d S r$   )rh   rg   )rp   rO   rR   s      r   rn   zdispatcher.del_channel  s0    \;)C99Br"   c                     ||f| _         t          j        ||          }|                    d           |                     |           d S NF)family_and_typero   ri   rj   )rp   familytyperq   s       r   create_socketzdispatcher.create_socket  sK    %t|}VT**r"   c                 p    || _         |                                | _        |                     |           d S r$   )ro   filenorh   r   rp   rq   rO   s      r   rj   zdispatcher.set_socket#  s2    {{}}r"   c                     	 | j                             t           j        t           j        | j                             t           j        t           j                  dz             d S # t
          $ r Y d S w xY w)Nr]   )ro   
setsockopt
SOL_SOCKETSO_REUSEADDR
getsockoptr<   rp   s    r   set_reuse_addrzdispatcher.set_reuse_addr(  s    	K""!6#6&&v'8'-':< <>?@    
  	 	 	DD	s   A A$ $
A21A2c                     dS NTr!   r   s    r   rG   zdispatcher.readable9      tr"   c                     dS r   r!   r   s    r   rH   zdispatcher.writable<  r   r"   c                 t    d| _         t          j        dk    r|dk    rd}| j                            |          S )NTnt   )rJ   r   namero   listen)rp   nums     r   r   zdispatcher.listenC  s7    7d??sQwwC{!!#&&&r"   c                 D    || _         | j                            |          S r$   )rm   ro   bind)rp   rm   s     r   r   zdispatcher.bindI  s    	{%%%r"   c                 L   d| _         d| _        | j                            |          }|t          t
          t          fv s|t          k    rt          j	        dk    r	|| _
        d S |dt          fv r|| _
        |                                  d S t          |t          |                   )NFTr   r   )rk   
connectingro   
connect_exr   r   r   r   r   r   rm   r
   handle_connect_eventr<   r   )rp   addressr   s      r   connectzdispatcher.connectM  s    k$$W--;+666&==RW__DIF1g,DI%%'''''#y~...r"   c                     	 | j                                         \  }}||fS # t          $ r Y d S t          $ r'}|j        t
          t          t          fv rY d }~d S  d }~ww xY wr$   )ro   acceptrx   r<   r=   r   r   r   )rp   connrm   whys       r   r   zdispatcher.accept[  s    
	++--JD$ :  	 	 	44 	 	 	y[,???ttttt		s   " 
A	AAAAc                     	 | j                             |          }|S # t          $ rD}|j        t          k    rY d }~dS |j        t
          v r|                                  Y d }~dS  d }~ww xY wNr   )ro   sendr<   r=   r   r>   r;   )rp   dataresultr   s       r   r   zdispatcher.sendi  s    
	[%%d++FM 	 	 	yK''qqqqqm++!!###qqqqq	s    
A,A'"A'&A''A,c                     	 | j                             |          }|s|                                  dS |S # t          $ r.}|j        t
          v r|                                  Y d }~dS  d }~ww xY wNr"   )ro   recvr;   r<   r=   r>   )rp   buffer_sizer   r   s       r   r   zdispatcher.recvv  s    	;##K00D  !!###s 	 	 	yM))!!###sssss	s   06 6 
A. "A)(A))A.c                     d| _         d| _        d| _        |                                  | j        J	 | j                                         d S # t          $ r!}|j        t          t          fvr Y d }~d S d }~ww xY wd S r   )
rk   rJ   r   rn   ro   closer<   r=   r   r   )rp   r   s     r   r   zdispatcher.close  s    ;"!!#####   9Xu$555 655555 #"s   A 
A8A33A8c                 d    t           j                            dt          |          z             d S )Nzlog: %s
)sysstderrr/   str)rp   messages     r   logzdispatcher.log  s)    
s7||344444r"   infoc                 D    || j         vrt          |d|           d S d S )Nz: )ignore_log_typesprint)rp   r   r   s      r   log_infozdispatcher.log_info  s7    t,,,dddGG,----- -,r"   c                     | j         r|                                  d S | j        s1| j        r|                                  |                                  d S |                                  d S r$   )rJ   handle_acceptrk   r   r   handle_readr   s    r   r%   zdispatcher.handle_read_event  s|    > 		       	 ,))+++r"   c                     | j                             t           j        t           j                  }|dk    rt	          |t          |                    |                                  d| _        d| _        d S )Nr   TF)	ro   r   r   SO_ERRORr<   r   handle_connectrk   r   rp   r   s     r   r   zdispatcher.handle_connect_event  sa    k$$V%6HH!88#y~~...r"   c                     | j         rd S | j        s| j        r|                                  |                                  d S r$   )rJ   rk   r   r   handle_writer   s    r   r.   zdispatcher.handle_write_event  sQ    > 	 F~ 	, ,))+++r"   c                     | j                             t           j        t           j                  }|dk    r|                                  d S |                                  d S r   )ro   r   r   r   r;   handle_exptr   s     r   r1   zdispatcher.handle_expt_event  s[     k$$V%6HH!88 r"   c                     t                      \  }}}}	 t          |           }n#  dt          |           z  }Y nxY w|                     d|d|d|d|d	d           |                                  d S )Nz)<__repr__(self) failed for object at %0x>z-uncaptured python exception, closing channel z (:rv   )error)compact_tracebackry   r{   r   r;   )rp   niltvtbinfo	self_reprs         r   r'   zdispatcher.handle_error  s    -//Q6	OT

II	OCbhhNIII			 	 	 	 	s   % ;c                 2    |                      dd           d S )Nz!unhandled incoming priority eventre   r   r   s    r   r   zdispatcher.handle_expt  s    99EEEEEr"   c                 2    |                      dd           d S )Nzunhandled read eventre   r   r   s    r   r   zdispatcher.handle_read  s    ,i88888r"   c                 2    |                      dd           d S )Nzunhandled write eventre   r   r   s    r   r   zdispatcher.handle_write  s    -y99999r"   c                 2    |                      dd           d S )Nzunhandled connect eventre   r   r   s    r   r   zdispatcher.handle_connect  s    /;;;;;r"   c                 J    |                                  }| | j        |  d S d S r$   )r   handle_accepted)rp   pairs     r   r   zdispatcher.handle_accept  s3    {{}} D $'''' r"   c                 Z    |                                  |                     dd           d S )Nzunhandled accepted eventre   )r   r   )rp   rq   rm   s      r   r   zdispatcher.handle_accepted  s*    

0)<<<<<r"   c                 Z    |                      dd           |                                  d S )Nzunhandled close eventre   )r   r   r   s    r   r;   zdispatcher.handle_close  s(    -y999

r"   NNr$   )r   ),r   r   r    debugrk   rJ   r   closingrm   	frozensetr   rr   r}   r   rn   ro   AF_INETSOCK_STREAMr   rj   r   rG   rH   r   r   r   r   r   r   r   r   r   r%   r   r.   r1   r'   r   r   r   r   r   r   r;   r!   r"   r   rd   rd      s4       EIIJGD y)--   @< < <! ! ! !    $*>8J       
	 	 	"    ' ' '& & &/ / /      $
 
 
 5 5 5. . . .
 
 
     	 	 	    (F F F9 9 9: : :< < <( ( (
= = =    r"   rd   c                   .    e Zd ZddZd Zd Zd Zd ZdS )dispatcher_with_sendNc                 L    t                               | ||           d| _        d S r   )rd   rr   
out_bufferr   s      r   rr   zdispatcher_with_send.__init__  s%    D$,,,r"   c                     d}t                               | | j        d d                   }| j        |d          | _        d S )Nr   i   )rd   r   r   )rp   num_sents     r   initiate_sendz"dispatcher_with_send.initiate_send  s;    ??4%)@AA/())4r"   c                 .    |                                   d S r$   )r   r   s    r   r   z!dispatcher_with_send.handle_write
  s    r"   c                 :    | j          pt          | j                  S r$   )rk   lenr   r   s    r   rH   zdispatcher_with_send.writable  s    N";s4?';';;r"   c                     | j         r%|                     dt          |          z             | j        |z   | _        |                                  d S )Nz
sending %s)r   r   ry   r   r   )rp   r   s     r   r   zdispatcher_with_send.send  sO    : 	5MM,d3444/D0r"   r   )r   r   r    rr   r   r   rH   r   r!   r"   r   r   r     sd           5 5 5
  < < <    r"   r   c                  f   t          j                    \  } }}g }|st          d          |rQ|                    |j        j        j        |j        j        j        t          |j	                  f           |j
        }|Q~|d         \  }}}d                    d |D                       }|||f| ||fS )Nztraceback does not existrv   c                     g | ]}d |z  S )z
[%s|%s|%s]r!   ).0xs     r   
<listcomp>z%compact_traceback.<locals>.<listcomp>+  s    666!\A%666r"   )r   exc_infoAssertionErrorrI   tb_framef_codeco_filenameco_namer   	tb_linenotb_nextrz   )r   r   tbr   filefunctionliner   s           r   r   r     s    |~~HAq"F 97888
 K*K& 	 	 	
 Z   	!":D(D8866v66677D(D!1a--r"   c                 &   | t           } t          |                                           D ]Q}	 |                                 # t          $ r}|j        t          k    rn|s Y d }~;d }~wt          $ r   |s Y OxY w|                                  d S r$   )	rD   rE   valuesr   r<   r=   r   r&   clear)rO   
ignore_allr   s      r   	close_allr  .  s    
{#**,,  	GGIIII 	 	 	w% # 	 	 		  IIKKKKKs   A
A:A&&A:posixc                   B    e Zd Zd Zd Zd Zd Zd	dZeZeZ	d Z
d ZdS )
file_wrapperc                 8    t          j        |          | _        d S r$   )r   duprR   rp   rR   s     r   rr   zfile_wrapper.__init__S  s    fRjjDGGGr"   c                     | j         dk    rt          j        d| z  t          |            |                                  d S )Nr   zunclosed file %r)source)rR   warningswarnResourceWarningr   r   s    r   __del__zfile_wrapper.__del__V  sD    w!||047%)+ + + +JJLLLLLr"   c                 0    t          j        | j        g|R  S r$   )r   r*   rR   rp   argss     r   r   zfile_wrapper.recv\  s    747*T****r"   c                 0    t          j        | j        g|R  S r$   )r   r/   rR   r  s     r   r   zfile_wrapper.send_  s    8DG+d++++r"   Nc                 h    |t           j        k    r|t           j        k    r|sdS t          d          )Nr   z-Only asyncore specific behaviour implemented.)ro   r   r   NotImplementedError)rp   leveloptnamebuflens       r   r   zfile_wrapper.getsockoptb  sA    ***6?** +q% '5 6 6 6r"   c                 d    | j         dk     rd S | j         }d| _         t          j        |           d S )Nr   r   )rR   r   r   r	  s     r   r   zfile_wrapper.closem  s1    w{{BDGHRLLLLLr"   c                     | j         S r$   )rR   r   s    r   r   zfile_wrapper.filenot  s	    7Nr"   r$   )r   r   r    rr   r  r   r   r   r*   r/   r   r   r!   r"   r   r  r  N  s        
	! 	! 	!	 	 		+ 	+ 	+	, 	, 	,	6 	6 	6 	6 	 	 		 	 	 	 	r"   r  c                       e Zd ZddZd ZdS )file_dispatcherNc                     t                               | d |           d| _        	 |                                }n# t          $ r Y nw xY w|                     |           t          j        |d           d S )NTF)rd   rr   rk   r   AttributeErrorset_filer   set_blocking)rp   rR   rO   s      r   rr   zfile_dispatcher.__init__y  s    dC000!DNYY[[!   MM"OB&&&&&s   : 
AAc                     t          |          | _        | j                                        | _        |                                  d S r$   )r  ro   r   rh   r   r	  s     r   r  zfile_dispatcher.set_file  s>    &r**DK;--//DLr"   r$   )r   r   r    rr   r  r!   r"   r   r  r  w  s7        		' 		' 		' 		'	 	 	 	 	r"   r  )rB   N)r[   FNNr   )1__doc__r4   ro   r   rK   r  r   r=   r   r   r   r   r   r   r	   r
   r   r   r   r   r   _DEPRECATION_MSG_deprecatedr   r   r>   rD   r   r   	Exceptionr   KeyboardInterrupt
SystemExitr&   r*   r/   r2   rA   rU   rZ   poll3rb   rd   r   r   r  r   r  r  r!   r"   r   <module>r)     s  8 *   



   				                             N   X/ @ @ @ @ 	:xL% " # #JJ   JJJ' ' '	 	 	 	 	i 	 	 	  !2J?         (% % % %N" " " "4 	   $a a a a a a a aP	    :   6. . .(   > 7g' ' ' ' ' ' ' 'R    *     U s    A# #A-,A-