a �DOg�N�@sfdZddlZddlZddlZddlZddlZddlZddlmZm Z m Z m Z m Z m Z mZmZmZmZmZmZmZee e eeeeh�ZzeWney�iZYn0dd�ZGdd�de�ZeeefZdd �Zd d �Z d d �Z!dd�Z"d&dd�Z#d'dd�Z$e$Z%d(dd�Z&Gdd�d�Z'Gdd�de'�Z(dd�Z)d)dd �Z*ej+d!k�rbGd"d#�d#�Z,Gd$d%�d%e'�Z-dS)*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 CsFz t�|�WStttfy@|tvr4t|YSd|YS0dS)NzUnknown error %s)�os�strerror� ValueError� OverflowError� NameErrorr)�err�r� /usr/lib64/python3.9/asyncore.py� _strerrorDs   rc@s eZdZdS)�ExitNowN)�__name__� __module__� __qualname__rrrrrLsrcCs8z |��Wn&ty �Yn|��Yn0dS�N)�handle_read_event�_reraised_exceptions� handle_error��objrrr�readQs   r"cCs8z |��Wn&ty �Yn|��Yn0dSr)�handle_write_eventrrr rrr�writeYs   r$cCs8z |��Wn&ty �Yn|��Yn0dSr)�handle_expt_eventrrr rrr� _exceptionas   r&c Cs�zX|tj@r|��|tj@r&|��|tj@r8|��|tjtjBtj B@rV|� �Wnht y�}z,|j dt vr�|��n|� �WYd}~n.d}~0ty��Yn|��Yn0dS�Nr)�select�POLLINr�POLLOUTr#�POLLPRIr%�POLLHUP�POLLERR�POLLNVAL� handle_close�OSError�args� _DISCONNECTEDrr)r!�flags�errr� readwriteis"      r5�c Cs<|dur t}|�r8g}g}g}t|���D]L\}}|��}|��}|rP|�|�|rd|jsd|�|�|sl|r*|�|�q*g|kr�|kr�|kr�nnt�|�dSt � ||||�\}}}|D] }|� |�}|dur�q�t |�q�|D]"}|� |�}|du�rq�t |�q�|D]&}|� |�}|du�r,�qt |��qdSr)� socket_map�list�items�readable�writable�append� accepting�time�sleepr(�getr"r$r&) �timeout�map�r�wr4�fdr!Zis_rZis_wrrr�poll}s@     "        rFcCs�|dur t}|dur t|d�}t��}|r�t|���D]L\}}d}|��r\|tjtjBO}|� �rt|j st|tj O}|r8|� ||�q8|�|�}|D]&\}}|� |�}|dur�q�t||�q�dS)Ni�r)r7�intr(rFr8r9r:r)r+r;r=r*�registerr@r5)rArBZpollsterrEr!r3rCrrr�poll2�s(     rI�>@FcCsb|dur t}|r ttd�r t}nt}|dur>|r^|||�q,n |r^|dkr^|||�|d}q>dS)NrFr�)r7�hasattrr(rIrF)rAZuse_pollrB�countZpoll_funrrr�loop�s  rNc@s2eZdZdZdZdZdZdZdZe dh�Z dAdd�Z dd�Z dBdd �Z dCd d �Zejejfd d �ZdDdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�ZdEd'd(�Zd)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+dS)F� dispatcherFN�warningc Cs�|durt|_n||_d|_|r�|�d�|�||�d|_z|��|_Wq�ty�}z2|j dt t fvrtd|_n |� |��WYd}~q�d}~00nd|_ dS)NFTr)r7�_map�_fileno� setblocking� set_socket� connected� getpeername�addrr0r1rr� del_channel�socket)�self�sockrBrrrr�__init__�s    zdispatcher.__init__cCs�|jjd|jjg}|jr.|jr.|�d�n|jr>|�d�|jdur�z|�d|j�Wn"ty~|�t|j��Yn0dd� |�t |�fS)N�.Z listeningrUz%s:%dz <%s at %#x>� ) � __class__rrr=rWr<rU� TypeError�repr�join�id)rZ�statusrrr�__repr__�s     zdispatcher.__repr__cCs|dur|j}|||j<dSr)rQrR)rZrBrrr� add_channel szdispatcher.add_channelcCs,|j}|dur|j}||vr"||=d|_dSr)rRrQ)rZrBrErrrrXs zdispatcher.del_channelcCs.||f|_t�||�}|�d�|�|�dS)NF)Zfamily_and_typerYrSrT)rZ�family�typer[rrr� create_sockets   zdispatcher.create_socketcCs||_|��|_|�|�dSr)rY�filenorRrf�rZr[rBrrrrTs zdispatcher.set_socketcCsBz*|j�tjtj|j�tjtj�dB�Wnty<Yn0dS)NrK)rY� setsockopt� SOL_SOCKET� SO_REUSEADDR� getsockoptr0�rZrrr�set_reuse_addr#s ��� zdispatcher.set_reuse_addrcCsdS�NTrrprrrr:4szdispatcher.readablecCsdSrrrrprrrr;7szdispatcher.writablecCs(d|_tjdkr|dkrd}|j�|�S)NT�nt�)r=r�namerY�listen)rZ�numrrrrv>szdispatcher.listencCs||_|j�|�Sr)rWrY�bind)rZrWrrrrxDszdispatcher.bindcCspd|_d|_|j�|�}|tttfvs8|tkrBtj dkrB||_ dS|dt fvr^||_ |� �nt |t|��dS)NFTrsr)rU� connectingrY� connect_exrrrrrrurWr �handle_connect_eventr0r)rZ�addressrrrr�connectHs ��  zdispatcher.connectc Cszz|j��\}}WnZty&YdStyl}z0|jdtttfvrVWYd}~dS�WYd}~nd}~00||fSdSr')rY�acceptr`r0r1rr r )rZ�connrW�whyrrrr~Vs zdispatcher.acceptc Cs~z|j�|�}|WStyx}zN|jdtkr>WYd}~dS|jdtvrb|��WYd}~dS�WYd}~n d}~00dSr')rY�sendr0r1rr2r/)rZ�data�resultr�rrrr�ds zdispatcher.sendc Csxz(|j�|�}|s |��WdS|WSWnJtyr}z2|jdtvr\|��WYd}~dS�WYd}~n d}~00dS)N�r)rY�recvr/r0r1r2)rZZ buffer_sizer�r�rrrr�qs  zdispatcher.recvc Cspd|_d|_d|_|��|jdurlz|j��Wn8tyj}z |jdtt fvrV�WYd}~n d}~00dS)NFr) rUr=ryrXrY�closer0r1rr )rZr�rrrr��s zdispatcher.closecCstj�dt|��dS)Nzlog: %s )�sys�stderrr$�str)rZ�messagerrr�log�szdispatcher.log�infocCs||jvrtd||f�dS)Nz%s: %s)�ignore_log_types�print)rZr�rhrrr�log_info�s zdispatcher.log_infocCs:|jr|��n&|js.|jr$|��|��n|��dSr)r=� handle_acceptrUryr{� handle_readrprrrr�s  zdispatcher.handle_read_eventcCs@|j�tjtj�}|dkr(t|t|���|��d|_d|_dS)NrTF) rYrorm�SO_ERRORr0r�handle_connectrUry�rZrrrrr{�s zdispatcher.handle_connect_eventcCs*|jr dS|js|jr|��|��dSr)r=rUryr{� handle_writerprrrr#�s zdispatcher.handle_write_eventcCs0|j�tjtj�}|dkr$|��n|��dSr')rYrormr�r/� handle_exptr�rrrr%�s zdispatcher.handle_expt_eventcCsXt�\}}}}z t|�}Wndt|�}Yn0|�d||||fd�|��dS)Nz)<__repr__(self) failed for object at %0x>z:uncaptured python exception, closing channel %s (%s:%s %s)�error)�compact_tracebackrarcr�r/)rZ�nil�t�v�tbinfoZ self_reprrrrr�s �� zdispatcher.handle_errorcCs|�dd�dS)Nz!unhandled incoming priority eventrP�r�rprrrr��szdispatcher.handle_exptcCs|�dd�dS)Nzunhandled read eventrPr�rprrrr��szdispatcher.handle_readcCs|�dd�dS)Nzunhandled write eventrPr�rprrrr��szdispatcher.handle_writecCs|�dd�dS)Nzunhandled connect eventrPr�rprrrr��szdispatcher.handle_connectcCs|��}|dur|j|�dSr)r~�handle_accepted)rZZpairrrrr��szdispatcher.handle_acceptcCs|��|�dd�dS)Nzunhandled accepted eventrP)r�r�)rZr[rWrrrr��szdispatcher.handle_acceptedcCs|�dd�|��dS)Nzunhandled close eventrP)r�r�rprrrr/�s zdispatcher.handle_close)NN)N)N)N)r�),rrr�debugrUr=ry�closingrW� frozensetr�r\rerfrXrY�AF_INET� SOCK_STREAMrirTrqr:r;rvrxr}r~r�r�r�r�r�rr{r#r%rr�r�r�r�r�r�r/rrrrrO�sJ         rOc@s6eZdZd dd�Zdd�Zdd�Zdd �Zd d �ZdS) �dispatcher_with_sendNcCst�|||�d|_dS)Nr�)rOr\� out_bufferrkrrrr\�szdispatcher_with_send.__init__cCs.d}t�||jdd��}|j|d�|_dS)Nri)rOr�r�)rZZnum_sentrrr� initiate_sendsz"dispatcher_with_send.initiate_sendcCs |��dSr)r�rprrrr�sz!dispatcher_with_send.handle_writecCs|j pt|j�Sr)rU�lenr�rprrrr;szdispatcher_with_send.writablecCs0|jr|�dt|��|j||_|��dS)Nz sending %s)r�r�rar�r�)rZr�rrrr� s zdispatcher_with_send.send)NN)rrrr\r�r�r;r�rrrrr��s  r�cCs�t��\}}}g}|std��|rL|�|jjj|jjjt|j �f�|j }q~|d\}}}d� dd�|D��}|||f|||fS)Nztraceback does not exist�����r^cSsg|] }d|�qS)z [%s|%s|%s]r)�.0�xrrr� &r�z%compact_traceback..) r��exc_info�AssertionErrorr<�tb_frame�f_code� co_filename�co_namer�� tb_lineno�tb_nextrb)r�r��tbr��file�function�liner�rrrr�s�r�c Cs�|dur t}t|���D]n}z |��Wqtyb}z"|jdtkrHn|sN�WYd}~qd}~0tyt�Yq|s��Yq0q|��dSr') r7r8�valuesr�r0r1r r�clear)rBZ ignore_allr�rrr� close_all)s    r��posixc@sNeZdZdd�Zdd�Zdd�Zdd�Zdd d �ZeZeZ d d �Z dd�Z d S)� file_wrappercCst�|�|_dSr)r�duprE�rZrErrrr\Nszfile_wrapper.__init__cCs*|jdkrtjd|t|d�|��dS)Nrzunclosed file %r)�source)rE�warnings�warn�ResourceWarningr�rprrr�__del__Qs   �zfile_wrapper.__del__cGstj|jg|�R�Sr)rr"rE�rZr1rrrr�Wszfile_wrapper.recvcGstj|jg|�R�Sr)rr$rEr�rrrr�Zszfile_wrapper.sendNcCs(|tjkr|tjkr|sdStd��dS)Nrz-Only asyncore specific behaviour implemented.)rYrmr��NotImplementedError)rZ�levelZoptname�buflenrrrro]s ��zfile_wrapper.getsockoptcCs(|jdkrdS|j}d|_t�|�dS)Nrr�)rErr�r�rrrr�hs  zfile_wrapper.closecCs|jSr)rErprrrrjoszfile_wrapper.fileno)N) rrrr\r�r�r�ror"r$r�rjrrrrr�Is r�c@seZdZddd�Zdd�ZdS)�file_dispatcherNcCsNt�|d|�d|_z |��}Wnty2Yn0|�|�t�|d�dS)NTF)rOr\rUrj�AttributeError�set_filer� set_blocking)rZrErBrrrr\ts   zfile_dispatcher.__init__cCs"t|�|_|j��|_|��dSr)r�rYrjrRrfr�rrrr�s  zfile_dispatcher.set_file)N)rrrr\r�rrrrr�rs r�)r6N)r6N)rJFNN)NF).�__doc__r(rYr�r>r�r�errnorrrrrrrr r r r r rr�r2r7rr� Exceptionr�KeyboardInterrupt� SystemExitrr"r$r&r5rFrIZpoll3rNrOr�r�r�rur�r�rrrr�sD< �    '  *  )