o
    éT•j,O  ã                   @   sx  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jdedd� eeeeeeehƒZze W n eyY   i ZY nw 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rºG d%d&„ d&ƒZ.G d'd(„ d(e)ƒZ/d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Ú	errorcodezlThe asyncore module is deprecated and will be removed in Python 3.12. The recommended replacement is asyncioé   )Ú
stacklevelc              
   C   sB   zt  | ¡W S  tttfy    | tv rt|   Y S d|   Y S w )NzUnknown error %s)ÚosÚstrerrorÚ
ValueErrorÚOverflowErrorÚ	NameErrorr   )Úerr© r   ú/usr/lib/python3.10/asyncore.pyÚ	_strerrorK   s   ýr   c                   @   s   e Zd ZdS )ÚExitNowN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   S   s    r   c                 C   ó2   z|   ¡  W d S  ty   ‚    |  ¡  Y d S ©N)Úhandle_read_eventÚ_reraised_exceptionsÚhandle_error©Úobjr   r   r   ÚreadX   ó   r%   c                 C   r   r   )Úhandle_write_eventr!   r"   r#   r   r   r   Úwrite`   r&   r(   c                 C   r   r   )Úhandle_expt_eventr!   r"   r#   r   r   r   Ú
_exceptionh   r&   r*   c              
   C   sÒ   z0|t j@ r
|  ¡  |t j@ r|  ¡  |t j@ r|  ¡  |t jt jB t j	B @ r.|  
¡  W d S W d S  tyX } z|jtvrB|  ¡  n|  
¡  W Y d }~d S W Y d }~d S d }~w ty_   ‚    |  ¡  Y d S r   )ÚselectÚPOLLINr    ÚPOLLOUTr'   ÚPOLLPRIr)   ÚPOLLHUPÚPOLLERRÚPOLLNVALÚhandle_closeÚOSErrorÚerrnoÚ_DISCONNECTEDr"   r!   )r$   ÚflagsÚer   r   r   Ú	readwritep   s(   


ÿ

þ€r8   ç        c           	      C   s6  |d u rt }|r—g }g }g }t| ¡ ƒD ]&\}}| ¡ }| ¡ }|r'| |¡ |r1|js1| |¡ |s5|r:| |¡ qg |  krJ|  krJ|krSn nt | ¡ d S t	 	|||| ¡\}}}|D ]}| 
|¡}|d u rlq`t|ƒ q`|D ]}| 
|¡}|d u rqst|ƒ qs|D ]}| 
|¡}|d u r’q†t|ƒ q†d S d S r   )Ú
socket_mapÚlistÚitemsÚreadableÚwritableÚappendÚ	acceptingÚtimeÚsleepr+   Úgetr%   r(   r*   )	ÚtimeoutÚmapÚrÚwr7   Úfdr$   Úis_rÚis_wr   r   r   Úpoll„   sF   



€"






ÞrK   c                 C   sÄ   |d u rt }| d urt| d ƒ} t ¡ }|r^t| ¡ ƒD ]&\}}d}| ¡ r.|tjtjB O }| 	¡ r:|j
s:|tjO }|rB| ||¡ q| | ¡}|D ]\}}| |¡}|d u rXqJt||ƒ qJd S d S )Niè  r   )r:   Úintr+   rK   r;   r<   r=   r,   r.   r>   r@   r-   ÚregisterrC   r8   )rD   rE   ÚpollsterrH   r$   r6   rF   r   r   r   Úpoll2«   s.   
€

ðrO   ç      >@Fc                 C   s€   |d u rt }|rttdƒrt}nt}|d u r#|r!|| |ƒ |sd S d S |r:|dkr>|| |ƒ |d }|r<|dks)d S d S d S d S )NrK   r   é   )r:   Úhasattrr+   rO   rK   )rD   Úuse_pollrE   ÚcountÚpoll_funr   r   r   ÚloopÇ   s   
ÿ
þrV   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dBd
d„Zejejfdd„ZdBd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dCd'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 )DÚ
dispatcherFNÚwarningc              
   C   s    |d u rt | _n|| _d | _|rK| d¡ |  ||¡ d| _z| ¡ | _W d S  tyJ } z|j	t
tfv r9d| _n|  |¡ ‚ W Y d }~d S d }~ww d | _d S )NFT)r:   Ú_mapÚ_filenoÚsetblockingÚ
set_socketÚ	connectedÚgetpeernameÚaddrr3   r4   r   r   Údel_channelÚsocket)ÚselfÚsockrE   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w dd 	|¡t
| ƒf S )NÚ.Ú	listeningr]   z%s:%dz<%s at %#x>ú )Ú	__class__r   r   r@   r_   r?   r]   Ú	TypeErrorÚreprÚjoinÚid)rb   Ústatusr   r   r   Ú__repr__  s   

ÿzdispatcher.__repr__c                 C   s   |d u r| j }| || j< d S r   )rY   rZ   )rb   rE   r   r   r   Úadd_channel  s   zdispatcher.add_channelc                 C   s,   | j }|d u r
| j}||v r||= d | _ d S r   )rZ   rY   )rb   rE   rH   r   r   r   r`     s   
zdispatcher.del_channelc                 C   s.   ||f| _ t ||¡}| d¡ |  |¡ d S ©NF)Úfamily_and_typera   r[   r\   )rb   ÚfamilyÚtyperc   r   r   r   Úcreate_socket  s   

zdispatcher.create_socketc                 C   s   || _ | ¡ | _|  |¡ d S r   )ra   ÚfilenorZ   ro   ©rb   rc   rE   r   r   r   r\   %  s   
zdispatcher.set_socketc                 C   sB   z| j  t jt j| j  t jt j¡dB ¡ W d S  ty    Y d S w )NrQ   )ra   Ú
setsockoptÚ
SOL_SOCKETÚSO_REUSEADDRÚ
getsockoptr3   ©rb   r   r   r   Úset_reuse_addr*  s   
ÿÿ
þÿzdispatcher.set_reuse_addrc                 C   ó   dS ©NTr   r{   r   r   r   r=   ;  ó   zdispatcher.readablec                 C   r}   r~   r   r{   r   r   r   r>   >  r   zdispatcher.writablec                 C   s(   d| _ tjdkr|dkrd}| j |¡S )NTÚnté   )r@   r   Únamera   Úlisten)rb   Únumr   r   r   rƒ   E  s   zdispatcher.listenc                 C   s   || _ | j |¡S r   )r_   ra   Úbind)rb   r_   r   r   r   r…   K  s   zdispatcher.bindc                 C   sn   d| _ d| _| j |¡}|tttfv s|tkr!tj	dkr!|| _
d S |dtfv r0|| _
|  ¡  d S t|t| ƒ‚)NFTr€   r   )r]   Ú
connectingra   Ú
connect_exr   r   r   r   r   r‚   r_   r	   Úhandle_connect_eventr3   r   )rb   Úaddressr   r   r   r   ÚconnectO  s   zdispatcher.connectc              
   C   sd   z| j  ¡ \}}W ||fS  ty   Y d S  ty1 } z|jtttfv r,W Y d }~d S ‚ d }~ww r   )ra   Úacceptri   r3   r4   r   r   r   )rb   Úconnr_   Úwhyr   r   r   r‹   ]  s   	ø€üzdispatcher.acceptc              
   C   sf   z	| j  |¡}|W S  ty2 } z|jtkrW Y d }~dS |jtv r-|  ¡  W Y d }~dS ‚ d }~ww ©Nr   )ra   Úsendr3   r4   r   r5   r2   )rb   ÚdataÚresultr�   r   r   r   r�   k  s   

€ùzdispatcher.sendc              
   C   s`   z| j  |¡}|s|  ¡  W dS |W S  ty/ } z|jtv r*|  ¡  W Y d }~dS ‚ d }~ww ©Nó    )ra   Úrecvr2   r3   r4   r5   )rb   Úbuffer_sizer�   r�   r   r   r   r”   x  s   
€úzdispatcher.recvc              
   C   sp   d| _ d| _d| _|  ¡  | jd ur6z| j ¡  W d S  ty5 } z|jtt	fvr*‚ W Y d }~d S d }~ww d S rp   )
r]   r@   r†   r`   ra   Úcloser3   r4   r   r
   )rb   r�   r   r   r   r–   Š  s   
ÿ€ÿýzdispatcher.closec                 C   s   t j dt|ƒ ¡ d S )Nzlog: %s
)ÚsysÚstderrr(   Ústr)rb   Úmessager   r   r   Úlogš  s   zdispatcher.logÚinfoc                 C   s"   || j vrtd||f ƒ d S d S )Nz%s: %s)Úignore_log_typesÚprint)rb   rš   rs   r   r   r   Úlog_info�  s   
ÿzdispatcher.log_infoc                 C   s>   | j r	|  ¡  d S | js| jr|  ¡  |  ¡  d S |  ¡  d S r   )r@   Úhandle_acceptr]   r†   rˆ   Úhandle_readr{   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)	ra   rz   rx   ÚSO_ERRORr3   r   Úhandle_connectr]   r†   ©rb   r   r   r   r   rˆ   ­  s   
zdispatcher.handle_connect_eventc                 C   s*   | j rd S | js| jr|  ¡  |  ¡  d S r   )r@   r]   r†   rˆ   Úhandle_writer{   r   r   r   r'   µ  s   zdispatcher.handle_write_eventc                 C   s2   | j  t jt j¡}|dkr|  ¡  d S |  ¡  d S rŽ   )ra   rz   rx   r¢   r2   Úhandle_exptr¤   r   r   r   r)   À  s   zdispatcher.handle_expt_eventc                 C   sT   t ƒ \}}}}zt| ƒ}W n
   dt| ƒ }Y |  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_tracebackrj   rl   rŸ   r2   )rb   ÚnilÚtÚvÚtbinfoÚ	self_reprr   r   r   r"   Ï  s   üù	zdispatcher.handle_errorc                 C   ó   |   dd¡ d S )Nz!unhandled incoming priority eventrX   ©rŸ   r{   r   r   r   r¦   ã  ó   zdispatcher.handle_exptc                 C   r®   )Nzunhandled read eventrX   r¯   r{   r   r   r   r¡   æ  r°   zdispatcher.handle_readc                 C   r®   )Nzunhandled write eventrX   r¯   r{   r   r   r   r¥   é  r°   zdispatcher.handle_writec                 C   r®   )Nzunhandled connect eventrX   r¯   r{   r   r   r   r£   ì  r°   zdispatcher.handle_connectc                 C   s"   |   ¡ }|d ur| j|Ž  d S d S r   )r‹   Úhandle_accepted)rb   Úpairr   r   r   r    ï  s   ÿzdispatcher.handle_acceptc                 C   s   |  ¡  |  dd¡ d S )Nzunhandled accepted eventrX   )r–   rŸ   )rb   rc   r_   r   r   r   r±   ô  s   zdispatcher.handle_acceptedc                 C   s   |   dd¡ |  ¡  d S )Nzunhandled close eventrX   )rŸ   r–   r{   r   r   r   r2   ø  s   zdispatcher.handle_close©NNr   )rœ   ),r   r   r   Údebugr]   r@   r†   Úclosingr_   Ú	frozensetr�   rd   rn   ro   r`   ra   ÚAF_INETÚSOCK_STREAMrt   r\   r|   r=   r>   rƒ   r…   rŠ   r‹   r�   r”   r–   r›   rŸ   r    rˆ   r'   r)   r"   r¦   r¡   r¥   r£   r    r±   r2   r   r   r   r   rW   Ù   sL    

 

	

rW   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 r’   )rW   rd   Ú
out_bufferrv   r   r   r   rd     s   
zdispatcher_with_send.__init__c                 C   s.   d}t  | | jd d… ¡}| j|d … | _d S )Nr   i   )rW   r�   rº   )rb   Únum_sentr   r   r   Úinitiate_send  s   z"dispatcher_with_send.initiate_sendc                 C   s   |   ¡  d S r   )r¼   r{   r   r   r   r¥     s   z!dispatcher_with_send.handle_writec                 C   s   | j  pt| jƒS r   )r]   Úlenrº   r{   r   r   r   r>     s   zdispatcher_with_send.writablec                 C   s0   | j r|  dt|ƒ ¡ | j| | _|  ¡  d S )Nz
sending %s)r´   rŸ   rj   rº   r¼   )rb   r�   r   r   r   r�     s   zdispatcher_with_send.sendr³   )r   r   r   rd   r¼   r¥   r>   r�   r   r   r   r   r¹     s    
r¹   c                  C   s„   t  ¡ \} }}g }|stdƒ‚|r'| |jjj|jjjt|j	ƒf¡ |j
}|s~|d \}}}d dd„ |D ƒ¡}|||f| ||fS )Nztraceback does not existéÿÿÿÿrg   c                 S   s   g | ]}d | ‘qS )z
[%s|%s|%s]r   )Ú.0Úxr   r   r   Ú
<listcomp>-  s    z%compact_traceback.<locals>.<listcomp>)r—   Úexc_infoÚAssertionErrorr?   Útb_frameÚf_codeÚco_filenameÚco_namer™   Ú	tb_linenoÚtb_nextrk   )rª   r«   Útbr¬   ÚfileÚfunctionÚlinerœ   r   r   r   r¨     s    ýú	r¨   c                 C   sˆ   | d u rt } t|  ¡ ƒD ]1}z| ¡  W q ty/ } z|jtkr"n|s%‚ W Y d }~qd }~w ty6   ‚    |s<‚ Y q|  ¡  d S r   )	r:   r;   Úvaluesr–   r3   r4   r
   r!   Úclear)rE   Ú
ignore_allrÀ   r   r   r   Ú	close_all0  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   ÚduprH   ©rb   rH   r   r   r   rd   U  r°   zfile_wrapper.__init__c                 C   s*   | j dkrtjd|  t| d� |  ¡  d S )Nr   zunclosed file %r)Úsource)rH   ÚwarningsÚwarnÚResourceWarningr–   r{   r   r   r   Ú__del__X  s
   
ÿzfile_wrapper.__del__c                 G   ó   t j| jg|¢R Ž S r   )r   r%   rH   ©rb   Úargsr   r   r   r”   ^  ó   zfile_wrapper.recvc                 G   rÛ   r   )r   r(   rH   rÜ   r   r   r   r�   a  rÞ   zfile_wrapper.sendNc                 C   s$   |t jkr|t jkr|sdS tdƒ‚)Nr   z-Only asyncore specific behaviour implemented.)ra   rx   r¢   ÚNotImplementedError)rb   ÚlevelÚoptnameÚbuflenr   r   r   rz   d  s   

ÿzfile_wrapper.getsockoptc                 C   s(   | j dk rd S | j }d| _ t |¡ d S )Nr   r¾   )rH   r   r–   rÕ   r   r   r   r–   o  s
   
zfile_wrapper.closec                 C   s   | j S r   )rH   r{   r   r   r   ru   v  s   zfile_wrapper.filenor   )r   r   r   rd   rÚ   r”   r�   rz   r%   r(   r–   ru   r   r   r   r   rÓ   P  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y   Y nw |  |¡ t |d¡ d S )NTF)rW   rd   r]   ru   ÚAttributeErrorÚset_filer   Úset_blocking)rb   rH   rE   r   r   r   rd   {  s   ÿ
zfile_dispatcher.__init__c                 C   s"   t |ƒ| _| j ¡ | _|  ¡  d S r   )rÓ   ra   ru   rZ   ro   rÕ   r   r   r   rå   †  s   
zfile_dispatcher.set_filer   )r   r   r   rd   rå   r   r   r   r   rã   y  s    
rã   )r9   N)rP   FNNrp   )0Ú__doc__r+   ra   r—   rA   r×   r   r4   r   r   r   r   r   r   r   r	   r
   r   r   r   r   rØ   ÚDeprecationWarningr¶   r5   r:   r   r   Ú	Exceptionr   ÚKeyboardInterruptÚ
SystemExitr!   r%   r(   r*   r8   rK   rO   Úpoll3rV   rW   r¹   r¨   rÑ   r‚   rÓ   rã   r   r   r   r   Ú<module>   sT   <üÿÿ


'
  *

)Ö