o
    f,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rG 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 }|rg }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 rqt| qd 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 existrg   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   <


'
  *

)