o
    >hA5                     @   sH  d dl Z d dlZd dlZd dlZd dlmZ d dlmZ e  Z	dej
v r2e	eeej
d d ejddZedurUeeejsTejdedd	 ejd= ee	 nee	 d dlZd dlZd d
lmZ d dlmZ d dlmZmZ d dlmZ d dlmZm Z  d dl!m"Z" ddl#m$Z$ ddl%m&Z& e'e(Z)G dd dZ*dS )    N)ThreadPoolExecutor)asyncioreactorASGI_THREADS)max_workersztwisted.internet.reactorzSomething has already installed a non-asyncio Twisted reactor. Attempting to uninstall it; you can fix this warning by importing daphne.server early in your codebase or finding the package that imports Twisted and importing it later on.   )
stacklevel)CancelledError)partial)deferreactor)serverFromString)STDLibLogObserverglobalLogBeginner)http   )HTTPFactory)WebSocketFactoryc                   @   s   e Zd Z																	
		d(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edd Zd d! Zd"d# Zd$d% Zd&d' ZdS ))ServerNT    Q        r      
   daphnec                 C   s   || _ |pg | _g | _g | _|| _|| _|| _|	| _|
| _|| _	|| _
|| _|| _|| _|| _|| _|| _|| _|| _d| _|| _|| _| jsStd td d S d S )NFz6No endpoints. This server will not listen on anything.r   )application	endpoints	listenerslistening_addressessignal_handlersaction_loggerhttp_timeoutping_intervalping_timeoutrequest_buffer_sizeproxy_forwarded_address_headerproxy_forwarded_port_headerproxy_forwarded_proto_headerwebsocket_timeoutwebsocket_connect_timeoutwebsocket_handshake_timeoutapplication_close_timeout	root_path	verbosityabort_startready_callableserver_nameloggererrorsysexit)selfr   r   r    r!   r"   r%   r)   r*   r#   r$   r-   r&   r'   r(   r.   r+   r,   r0   r1    r7   m/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/daphne/server.py__init__0   s4   

zServer.__init__c                 C   sT  i | _ t| | _t| | jd| _| jj| jd| jd | j	dkr,t
jdd gddd nt
ttg tjr=td	 ntd
 td| j td| j | jD ]'}td| ttt|}|| j}|| j || j | j| qSt !tj" | j	dkrt # $d t%dd| j& | j's| j(r| (  tj)| j*d d S d S )N)serverT)autoPingTimeoutallowNullOriginopenHandshakeTimeoutr   c                 S      d S Nr7   )_r7   r7   r8   <lambda>o       zServer.run.<locals>.<lambda>F)redirectStandardIOdiscardBufferzHTTP/2 support enabledzEHTTP/2 support not enabled (install the http2 and tls Twisted extras)r   zConfiguring endpoint %s   beforeshutdown)installSignalHandlers)+connectionsr   http_factoryr   r1   
ws_factorysetProtocolOptionsr$   r+   r.   r   beginLoggingTor   __name__r   
H2_ENABLEDr2   infor   	callLaterapplication_checkertimeout_checkerr   r   strlistenaddCallbacklisten_success
addErrbacklisten_errorr   appendasyncioset_event_loop_asyncioEventloopget_event_loop	set_debugaddSystemEventTriggerkill_all_applicationsr/   r0   runr    )r6   socket_descriptioneplistenerr7   r7   r8   rb   a   sH   



z
Server.runc                 C   sd   t |dr,| }t |dr.t |dr0| j|j|jf td| j| j dS dS dS dS )z_
        Called when a listen succeeds so we can store port details (if there are any)
        getHosthostportzListening on TCP address %s:%sN)hasattrrf   r   rZ   rg   rh   r2   rP   )r6   rh   rg   r7   r7   r8   rW      s   
zServer.listen_successc                 C   s   t d|  |   d S )NzListen failure: %s)r2   criticalgetErrorMessagestop)r6   failurer7   r7   r8   rY      s   zServer.listen_errorc                 C   s   t jr	t   dS d| _dS )z)
        Force-stops the server.
        TN)r   runningrl   r/   )r6   r7   r7   r8   rl      s   
zServer.stopc                 C   s,   || j v rtd| dt i| j |< dS )z:
        Adds a protocol as a current connection.
        z)Protocol %r was added to main list twice!	connectedN)rI   RuntimeErrortimer6   protocolr7   r7   r8   protocol_connected   s   
zServer.protocol_connectedc                 C   s(   d| j | vrt | j | d< d S d S )Ndisconnected)rI   rq   rr   r7   r7   r8   protocol_disconnected   s   zServer.protocol_disconnectedc                 C   sr   d| j | vs	J t }|dddi | j||jt| j|d}|| j vr)dS tj|t	 d| j | d< |S )a  
        Creates a new application instance that fronts a Protocol instance
        for one of our supported protocols. Pass it the protocol,
        and it will work out the type, supply appropriate callables, and
        return you the application's input queue
        application_instanceasgiversionz3.0)scopereceivesendN)loop)
rI   r[   Queue
setdefaultr   getr	   handle_replyensure_futurer^   )r6   rs   rz   input_queuerw   r7   r7   r8   create_application   s   

zServer.create_applicationc                    s^   || j vs| j | ddrdS z| | W n ty'   |ddd  w || dS )zP
        Coroutine that jumps the reply message from asyncio to Twisted
        ru   Ni  s   Server ErrorzServer Error)rI   r   check_headers_type
ValueErrorbasic_errorr   )r6   rs   messager7   r7   r8   r      s   zServer.handle_replyc                 C   sf   | d dksd S |  dg D ]"\}}t|ts!td|t|t|ts0td|t|qd S )Ntypezhttp.response.startheadersz5Header name '{}' expected to be `bytes`, but got `{}`z6Header value '{}' expected to be `bytes`, but got `{}`)r   
isinstancebytesr   formatr   )r   kvr7   r7   r8   r      s"   

zServer.check_headers_typec              
   C   s  t | j D ]r\}}|dd}|dd}|r5t | | jkr5|r5| s5td|t	| |
  |rq| rqz| }W n ttjfyM   Y nw |rit|trZ|   ntjd||d |si|| | j| d= d}|sy|ry| j|= qtd| j dS )z
        Goes through the set of current application Futures and cleans up
        any that are done/prints exceptions for any that errored.
        ru   Nrw   zTApplication instance %r for connection %s took too long to shut down and was killed.z Exception inside application: %s)exc_infor   )listrI   itemsr   rq   r,   doner2   warningreprcancel	exceptionr   r[   r   KeyboardInterruptrl   r3   handle_exceptionr   rQ   rR   )r6   rs   detailsru   rw   r   r7   r7   r8   rR     sF   


zServer.application_checkerc                 C   sj   g }| j  D ]}|d }| s|  || qtdt| tj	
tj| }|dd  |S )zG
        Kills all application coroutines before reactor exit.
        rw   z'Killed %i pending application instancesc                 S   r>   r?   r7   )xr7   r7   r8   rA   E  rB   z.Server.kill_all_applications.<locals>.<lambda>)rI   valuesr   r   rZ   r2   rP   lenr
   Deferred
fromFuturer[   gatherrX   )r6   wait_forr   rw   wait_deferredr7   r7   r8   ra   7  s   
zServer.kill_all_applicationsc                 C   s.   t | j D ]}|  qtd| j dS )z~
        Called periodically to enforce timeout rules on all connections.
        Also checks pings at the same time.
        r   N)r   rI   keyscheck_timeoutsr   rQ   rS   rr   r7   r7   r8   rS   H  s   
zServer.timeout_checkerc                 C   s   | j r|  ||| dS dS )zN
        Dispatches to any registered action logger, if there is one.
        N)r!   )r6   rs   actionr   r7   r7   r8   
log_actionQ  s   zServer.log_action)NTNNr   r   r   r   r   r   NNNr   r   r   Nr   )rN   
__module____qualname__r9   rb   rW   rY   rl   rt   rv   r   r   staticmethodr   rR   ra   rS   r   r7   r7   r7   r8   r   /   sD    
17

0	r   )+r[   osr4   warningsconcurrent.futuresr   twisted.internetr   new_event_looptwisted_loopenvironset_default_executorintmodulesr   current_reactorr   AsyncioSelectorReactorwarnUserWarninginstallloggingrq   r   	functoolsr	   r
   r   twisted.internet.endpointsr   twisted.loggerr   r   twisted.webr   http_protocolr   ws_protocolr   	getLoggerrN   r2   r   r7   r7   r7   r8   <module>   sF   



