a
    kxÛg†N  ã                   @   sf  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 eeeeeeehƒZze W n ey¢   i ZY n0 dd„ ZG dd„ deƒZeeefZdd	„ Zd
d„ Z dd„ Z!dd„ Z"d&dd„Z#d'dd„Z$e$Z%d(dd„Z&G dd„ dƒZ'G dd„ de'ƒZ(dd„ Z)d)dd „Z*ej+d!krbG d"d#„ d#ƒZ,G d$d%„ d%e'ƒZ-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Ú	errorcodec              
   C   sF   zt  | ¡W S  tttfy@   | tv r4t|   Y S d|   Y S 0 d S )NzUnknown error %s)ÚosÚstrerrorÚ
ValueErrorÚOverflowErrorÚ	NameErrorr   )Úerr© r   ú/usr/lib/python3.9/asyncore.pyÚ	_strerrorD   s    r   c                   @   s   e Zd ZdS )ÚExitNowN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   L   s   r   c                 C   s8   z|   ¡  W n& ty    ‚ Y n   |  ¡  Y n0 d S ©N)Úhandle_read_eventÚ_reraised_exceptionsÚhandle_error©Úobjr   r   r   ÚreadQ   s    r"   c                 C   s8   z|   ¡  W n& ty    ‚ Y n   |  ¡  Y n0 d S r   )Úhandle_write_eventr   r   r    r   r   r   ÚwriteY   s    r$   c                 C   s8   z|   ¡  W n& ty    ‚ Y n   |  ¡  Y n0 d S r   )Úhandle_expt_eventr   r   r    r   r   r   Ú
_exceptiona   s    r&   c              
   C   sÆ   zX|t j@ r|  ¡  |t j@ r&|  ¡  |t j@ r8|  ¡  |t jt jB t j	B @ rV|  
¡  W nh tyœ } z,|jd tvr€|  ¡  n|  
¡  W Y d }~n.d }~0  ty®   ‚ Y n   |  ¡  Y n0 d S ©Nr   )ÚselectÚPOLLINr   ÚPOLLOUTr#   ÚPOLLPRIr%   ZPOLLHUPZPOLLERRZPOLLNVALÚhandle_closeÚOSErrorÚargsÚ_DISCONNECTEDr   r   )r!   ÚflagsÚer   r   r   Ú	readwritei   s"    



r2   ç        c           	      C   s<  |d u rt }|r8g }g }g }t| ¡ ƒD ]L\}}| ¡ }| ¡ }|rP| |¡ |rd|jsd| |¡ |sl|r*| |¡ q*g |  kr–|  kr–|kr¨n nt | ¡ d S t	 	|||| ¡\}}}|D ] }| 
|¡}|d u rÚqÂt|ƒ qÂ|D ]"}| 
|¡}|d u rqèt|ƒ qè|D ]&}| 
|¡}|d u r,qt|ƒ qd S r   )Ú
socket_mapÚlistÚitemsÚreadableÚwritableÚappendÚ	acceptingÚtimeÚsleepr(   Úgetr"   r$   r&   )	ÚtimeoutÚmapÚrÚwr1   Úfdr!   Zis_rZis_wr   r   r   Úpoll}   s@    


"







rC   c                 C   sÀ   |d u rt }| d ur t| d ƒ} t ¡ }|r¼t| ¡ ƒD ]L\}}d}| ¡ r\|tjtjB O }| 	¡ rt|j
st|tjO }|r8| ||¡ q8| | ¡}|D ]&\}}| |¡}|d u r°q”t||ƒ q”d S )Niè  r   )r4   Úintr(   rC   r5   r6   r7   r)   r+   r8   r:   r*   Úregisterr=   r2   )r>   r?   ZpollsterrB   r!   r0   r@   r   r   r   Úpoll2¤   s(    


rF   ç      >@Fc                 C   sb   |d u rt }|r ttdƒr t}nt}|d u r>|r^|| |ƒ q,n |r^|dkr^|| |ƒ |d }q>d S )NrC   r   é   )r4   Úhasattrr(   rF   rC   )r>   Zuse_pollr?   ÚcountZpoll_funr   r   r   ÚloopÀ   s    
rK   c                   @   s2  e Zd ZdZdZdZdZdZdZe	dhƒZ
dAdd„Zdd„ ZdBdd	„ZdCd
d„Zejejfdd„ZdDd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d"d#„ Zd$d%„ ZdEd'd(„Zd)d*„ Z d+d,„ Z!d-d.„ Z"d/d0„ Z#d1d2„ Z$d3d4„ Z%d5d6„ Z&d7d8„ Z'd9d:„ Z(d;d<„ Z)d=d>„ Z*d?d@„ Z+dS )FÚ
dispatcherFNÚwarningc              
   C   s¢   |d u rt | _n|| _d | _|r˜| d¡ |  ||¡ d| _z| ¡ | _W qž ty” } z2|j	d t
tfv rtd| _n|  |¡ ‚ W Y d }~qžd }~0 0 nd | _d S )NFTr   )r4   Ú_mapÚ_filenoÚsetblockingÚ
set_socketÚ	connectedZgetpeernameÚaddrr-   r.   r   r   Údel_channelÚsocket)ÚselfÚsockr?   r   r   r   r   Ú__init__Ü   s     

zdispatcher.__init__c                 C   s–   | j jd | j j g}| jr.| jr.| d¡ n| jr>| d¡ | jd ur€z| d| j ¡ W n" ty~   | t| jƒ¡ Y n0 dd 	|¡t
| ƒf S )NÚ.Z	listeningrR   z%s:%dz<%s at %#x>ú )Ú	__class__r   r   r:   rS   r9   rR   Ú	TypeErrorÚreprÚjoinÚid)rV   Zstatusr   r   r   Ú__repr__ü   s    

zdispatcher.__repr__c                 C   s   |d u r| j }| || j< d S r   )rN   rO   )rV   r?   r   r   r   Úadd_channel	  s    zdispatcher.add_channelc                 C   s,   | j }|d u r| j}||v r"||= d | _ d S r   )rO   rN   )rV   r?   rB   r   r   r   rT     s    zdispatcher.del_channelc                 C   s.   ||f| _ t ||¡}| d¡ |  |¡ d S )NF)Zfamily_and_typerU   rP   rQ   )rV   ZfamilyÚtyperW   r   r   r   Úcreate_socket  s    

zdispatcher.create_socketc                 C   s   || _ | ¡ | _|  |¡ d S r   )rU   ÚfilenorO   ra   ©rV   rW   r?   r   r   r   rQ     s    
zdispatcher.set_socketc                 C   sB   z*| j  t jt j| j  t jt j¡dB ¡ W n ty<   Y n0 d S )NrH   )rU   Z
setsockoptÚ
SOL_SOCKETZSO_REUSEADDRÚ
getsockoptr-   ©rV   r   r   r   Úset_reuse_addr#  s    
ÿÿþzdispatcher.set_reuse_addrc                 C   s   dS ©NTr   rh   r   r   r   r7   4  s    zdispatcher.readablec                 C   s   dS rj   r   rh   r   r   r   r8   7  s    zdispatcher.writablec                 C   s(   d| _ tjdkr|dkrd}| j |¡S )NTÚnté   )r:   r   ÚnamerU   Úlisten)rV   Znumr   r   r   rn   >  s    zdispatcher.listenc                 C   s   || _ | j |¡S r   )rS   rU   Úbind)rV   rS   r   r   r   ro   D  s    zdispatcher.bindc                 C   sp   d| _ d| _| j |¡}|tttfv s8|tkrBtj	dkrB|| _
d S |dtfv r^|| _
|  ¡  nt|t| ƒ‚d S )NFTrk   r   )rR   Ú
connectingrU   Z
connect_exr   r   r   r   r   rm   rS   r	   Úhandle_connect_eventr-   r   )rV   Zaddressr   r   r   r   ÚconnectH  s    ÿÿ
zdispatcher.connectc              
   C   sz   z| j  ¡ \}}W nZ ty&   Y d S  tyl } z0|jd tttfv rVW Y d }~d S ‚ W Y d }~nd }~0 0 ||fS d S r'   )rU   Úacceptr\   r-   r.   r   r   r   )rV   ZconnrS   Úwhyr   r   r   rs   V  s    zdispatcher.acceptc              
   C   s~   z| j  |¡}|W S  tyx } zN|jd tkr>W Y d }~dS |jd tv rb|  ¡  W Y d }~dS ‚ W Y d }~n
d }~0 0 d S r'   )rU   Úsendr-   r.   r   r/   r,   )rV   ÚdataÚresultrt   r   r   r   ru   d  s    zdispatcher.sendc              
   C   sx   z(| j  |¡}|s |  ¡  W dS |W S W nJ tyr } z2|jd tv r\|  ¡  W Y d }~dS ‚ W Y d }~n
d }~0 0 d S )Nó    r   )rU   Úrecvr,   r-   r.   r/   )rV   Zbuffer_sizerv   rt   r   r   r   ry   q  s    
zdispatcher.recvc              
   C   sp   d| _ d| _d| _|  ¡  | jd urlz| j ¡  W n8 tyj } z |jd tt	fvrV‚ W Y d }~n
d }~0 0 d S )NFr   )
rR   r:   rp   rT   rU   Úcloser-   r.   r   r
   )rV   rt   r   r   r   rz   ƒ  s    
zdispatcher.closec                 C   s   t j dt|ƒ ¡ d S )Nzlog: %s
)ÚsysÚstderrr$   Ústr)rV   Úmessager   r   r   Úlog“  s    zdispatcher.logÚinfoc                 C   s   || j vrtd||f ƒ d S )Nz%s: %s)Úignore_log_typesÚprint)rV   r~   rb   r   r   r   Úlog_info–  s    
zdispatcher.log_infoc                 C   s:   | j r|  ¡  n&| js.| jr$|  ¡  |  ¡  n|  ¡  d S r   )r:   Úhandle_acceptrR   rp   rq   Úhandle_readrh   r   r   r   r   š  s    

zdispatcher.handle_read_eventc                 C   s@   | j  t jt j¡}|dkr(t|t|ƒƒ‚|  ¡  d| _d| _d S )Nr   TF)	rU   rg   rf   ÚSO_ERRORr-   r   Úhandle_connectrR   rp   ©rV   r   r   r   r   rq   ¦  s    zdispatcher.handle_connect_eventc                 C   s*   | j r
d S | js| jr|  ¡  |  ¡  d S r   )r:   rR   rp   rq   Úhandle_writerh   r   r   r   r#   ®  s    zdispatcher.handle_write_eventc                 C   s0   | j  t jt j¡}|dkr$|  ¡  n|  ¡  d S r'   )rU   rg   rf   r†   r,   Úhandle_exptrˆ   r   r   r   r%   ¹  s    
zdispatcher.handle_expt_eventc                 C   sX   t ƒ \}}}}zt| ƒ}W n   dt| ƒ }Y n0 |  d||||f d¡ |  ¡  d S )Nz)<__repr__(self) failed for object at %0x>z:uncaptured python exception, closing channel %s (%s:%s %s)Úerror)Úcompact_tracebackr]   r_   rƒ   r,   )rV   ZnilÚtÚvÚtbinfoZ	self_reprr   r   r   r   È  s    üù	zdispatcher.handle_errorc                 C   s   |   dd¡ d S )Nz!unhandled incoming priority eventrM   ©rƒ   rh   r   r   r   rŠ   Ü  s    zdispatcher.handle_exptc                 C   s   |   dd¡ d S )Nzunhandled read eventrM   r   rh   r   r   r   r…   ß  s    zdispatcher.handle_readc                 C   s   |   dd¡ d S )Nzunhandled write eventrM   r   rh   r   r   r   r‰   â  s    zdispatcher.handle_writec                 C   s   |   dd¡ d S )Nzunhandled connect eventrM   r   rh   r   r   r   r‡   å  s    zdispatcher.handle_connectc                 C   s   |   ¡ }|d ur| j|Ž  d S r   )rs   Úhandle_accepted)rV   Zpairr   r   r   r„   è  s    zdispatcher.handle_acceptc                 C   s   |  ¡  |  dd¡ d S )Nzunhandled accepted eventrM   )rz   rƒ   )rV   rW   rS   r   r   r   r‘   í  s    zdispatcher.handle_acceptedc                 C   s   |   dd¡ |  ¡  d S )Nzunhandled close eventrM   )rƒ   rz   rh   r   r   r   r,   ñ  s    zdispatcher.handle_close)NN)N)N)N)r€   ),r   r   r   ÚdebugrR   r:   rp   ÚclosingrS   Ú	frozensetr   rX   r`   ra   rT   rU   ZAF_INETZSOCK_STREAMrc   rQ   ri   r7   r8   rn   ro   rr   rs   ru   ry   rz   r   rƒ   r   rq   r#   r%   r   rŠ   r…   r‰   r‡   r„   r‘   r,   r   r   r   r   rL   Ò   sJ   

 

	

rL   c                   @   s6   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )Údispatcher_with_sendNc                 C   s   t  | ||¡ d| _d S )Nrx   )rL   rX   Ú
out_bufferre   r   r   r   rX   ü  s    zdispatcher_with_send.__init__c                 C   s.   d}t  | | jd d… ¡}| j|d … | _d S )Nr   i   )rL   ru   r–   )rV   Znum_sentr   r   r   Úinitiate_send   s    z"dispatcher_with_send.initiate_sendc                 C   s   |   ¡  d S r   )r—   rh   r   r   r   r‰     s    z!dispatcher_with_send.handle_writec                 C   s   | j  pt| jƒS r   )rR   Úlenr–   rh   r   r   r   r8     s    zdispatcher_with_send.writablec                 C   s0   | j r|  dt|ƒ ¡ | j| | _|  ¡  d S )Nz
sending %s)r’   rƒ   r]   r–   r—   )rV   rv   r   r   r   ru     s    zdispatcher_with_send.send)NN)r   r   r   rX   r—   r‰   r8   ru   r   r   r   r   r•   ú  s
   
r•   c                  C   s‚   t  ¡ \} }}g }|stdƒ‚|rL| |jjj|jjjt|j	ƒf¡ |j
}q~|d \}}}d dd„ |D ƒ¡}|||f| ||fS )Nztraceback does not existéÿÿÿÿrZ   c                 S   s   g | ]}d | ‘qS )z
[%s|%s|%s]r   )Ú.0Úxr   r   r   Ú
<listcomp>&  rx   z%compact_traceback.<locals>.<listcomp>)r{   Úexc_infoÚAssertionErrorr9   Útb_frameÚf_codeÚco_filenameÚco_namer}   Ú	tb_linenoÚtb_nextr^   )r   rŽ   Útbr   ÚfileZfunctionÚliner€   r   r   r   rŒ     s    ýrŒ   c                 C   s”   | d u rt } t|  ¡ ƒD ]n}z| ¡  W q tyb } z"|jd tkrHn|sN‚ W Y d }~qd }~0  tyt   ‚ Y q   |s€‚ Y q0 q|  ¡  d S r'   )	r4   r5   Úvaluesrz   r-   r.   r
   r   Úclear)r?   Z
ignore_allr›   r   r   r   Ú	close_all)  s     
rª   Úposixc                   @   sN   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zdd
d„ZeZeZ	dd„ Z
dd„ Zd	S )Úfile_wrapperc                 C   s   t  |¡| _d S r   )r   ÚduprB   ©rV   rB   r   r   r   rX   N  s    zfile_wrapper.__init__c                 C   s*   | j dkrtjd|  t| d |  ¡  d S )Nr   zunclosed file %r)Úsource)rB   ÚwarningsÚwarnÚResourceWarningrz   rh   r   r   r   Ú__del__Q  s
    
ÿzfile_wrapper.__del__c                 G   s   t j| jg|¢R Ž S r   )r   r"   rB   ©rV   r.   r   r   r   ry   W  s    zfile_wrapper.recvc                 G   s   t j| jg|¢R Ž S r   )r   r$   rB   r´   r   r   r   ru   Z  s    zfile_wrapper.sendNc                 C   s(   |t jkr|t jkr|sdS tdƒ‚d S )Nr   z-Only asyncore specific behaviour implemented.)rU   rf   r†   ÚNotImplementedError)rV   ÚlevelZoptnameZbuflenr   r   r   rg   ]  s    
ÿþzfile_wrapper.getsockoptc                 C   s(   | j dk rd S | j }d| _ t |¡ d S )Nr   r™   )rB   r   rz   r®   r   r   r   rz   h  s
    
zfile_wrapper.closec                 C   s   | j S r   )rB   rh   r   r   r   rd   o  s    zfile_wrapper.fileno)N)r   r   r   rX   r³   ry   ru   rg   r"   r$   rz   rd   r   r   r   r   r¬   I  s   
r¬   c                   @   s   e Zd Zddd„Zdd„ ZdS )Úfile_dispatcherNc                 C   sN   t  | d |¡ d| _z| ¡ }W n ty2   Y n0 |  |¡ t |d¡ d S )NTF)rL   rX   rR   rd   ÚAttributeErrorÚset_filer   Úset_blocking)rV   rB   r?   r   r   r   rX   t  s    
zfile_dispatcher.__init__c                 C   s"   t |ƒ| _| j ¡ | _|  ¡  d S r   )r¬   rU   rd   rO   ra   r®   r   r   r   r¹     s    
zfile_dispatcher.set_file)N)r   r   r   rX   r¹   r   r   r   r   r·   r  s   
r·   )r3   N)r3   N)rG   FNN)NF).Ú__doc__r(   rU   r{   r;   r°   r   Úerrnor   r   r   r   r   r   r   r	   r
   r   r   r   r   r”   r/   r4   r   r   Ú	Exceptionr   ÚKeyboardInterruptÚ
SystemExitr   r"   r$   r&   r2   rC   rF   Zpoll3rK   rL   r•   rŒ   rª   rm   r¬   r·   r   r   r   r   Ú<module>   sD   <ÿ


'

  *
)