U
    uë]jA5  ã                   @   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
krde	 eeej
d ƒd�¡ ej dd¡Zedk	rªeeejƒs´e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)ÚasyncioreactorZASGI_THREADS)Úmax_workersztwisted.internet.reactorzìSomething 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s¢t d¡ t d¡ 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+   r/   r0   © r6   úVc:\users\psant\downloads\hotstarbackend\school\venv\Lib\site-packages\daphne/server.pyÚ__init__0   s2    

zServer.__init__c                 C   sV  i | _ t| ƒ| _t| | jd�| _| jj| jd| jd� | j	dkrXt
jdd„ gddd� nt
 ttƒg¡ tjrzt d	¡ n
t d
¡ t d| j¡ t d| j¡ | jD ]N}t d|¡ ttt|ƒƒ}| | j¡}| | j¡ | | j¡ | j |¡ q¦t  !tj"¡ | j	dk�rt  #¡  $d¡ t %dd| j&¡ | j'�sR| j(�rD|  (¡  tj)| j*d� d S )N)ÚserverT)ZautoPingTimeoutZallowNullOriginZopenHandshakeTimeoutr   c                 S   s   d S ©Nr6   )Ú_r6   r6   r7   Ú<lambda>o   ó    zServer.run.<locals>.<lambda>F)ZredirectStandardIOZdiscardBufferzHTTP/2 support enabledzEHTTP/2 support not enabled (install the http2 and tls Twisted extras)r   zConfiguring endpoint %sé   ÚbeforeÚshutdown)ZinstallSignalHandlers)+Úconnectionsr   Zhttp_factoryr   r0   Z
ws_factoryZsetProtocolOptionsr#   r*   r-   r   ZbeginLoggingTor   Ú__name__r   Z
H2_ENABLEDr1   Úinfor
   Ú	callLaterÚapplication_checkerÚtimeout_checkerr   r   ÚstrÚlistenZaddCallbackÚlisten_successÚ
addErrbackÚlisten_errorr   ÚappendÚasyncioÚset_event_loopZ_asyncioEventloopÚget_event_loopÚ	set_debugZaddSystemEventTriggerÚkill_all_applicationsr.   r/   Úrunr   )r5   Zsocket_descriptionÚepÚlistenerr6   r6   r7   rR   a   sJ    
ý
  ÿÿ
z
Server.runc                 C   sX   t |dƒrT| ¡ }t |dƒrTt |dƒrT| j |j|jf¡ t d| ¡ j| ¡ j¡ 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)ÚhasattrrU   r   rL   rV   rW   r1   rC   )r5   rW   rV   r6   r6   r7   rI   ˜   s    
ýzServer.listen_successc                 C   s   t  d| ¡ ¡ |  ¡  d S )NzListen failure: %s)r1   ÚcriticalZgetErrorMessageÚstop)r5   Zfailurer6   r6   r7   rK   ¦   s    zServer.listen_errorc                 C   s   t jrt  ¡  nd| _dS )z)
        Force-stops the server.
        TN)r
   ÚrunningrZ   r.   )r5   r6   r6   r7   rZ   ª   s    
zServer.stopc                 C   s,   || j k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)rA   ÚRuntimeErrorÚtime©r5   Úprotocolr6   r6   r7   Úprotocol_connectedµ   s    
zServer.protocol_connectedc                 C   s$   d| j | kr t ¡ | j | d< d S )NÚdisconnected)rA   r^   r_   r6   r6   r7   Úprotocol_disconnected½   s    zServer.protocol_disconnectedc                 C   sr   d| j | kst‚t ¡ }| dddi¡ | j||jt| j|ƒd�}|| j krRd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_instanceZasgiÚversionz3.0)ÚscopeZreceiveÚsendN)Úloop)rA   ÚAssertionErrorrM   ÚQueueÚ
setdefaultr   Úgetr   Úhandle_replyÚensure_futurerO   )r5   r`   rf   Zinput_queuerd   r6   r6   r7   Úcreate_applicationÇ   s    
ý
þzServer.create_applicationc                 Ã   sb   || j ks| j |  dd¡r dS z|  |¡ W n$ tk
rR   | ddd¡ ‚ Y nX | |¡ dS )zP
        Coroutine that jumps the reply message from asyncio to Twisted
        rb   Niô  s   Server ErrorzServer Error)rA   rl   Úcheck_headers_typeÚ
ValueErrorZbasic_errorrm   )r5   r`   Úmessager6   r6   r7   rm   á   s     ÿzServer.handle_replyc                 C   sf   | d dksd S |   dg ¡D ]D\}}t|tƒsBtd |t|ƒ¡ƒ‚t|tƒst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 `{}`)rl   Ú
isinstanceÚbytesrq   Úformatrs   )rr   ÚkÚvr6   r6   r7   rp   ó   s"    
 ÿÿ
 ÿÿzServer.check_headers_typec              
   C   s  t | j ¡ ƒD ]æ\}}| dd¡}| dd¡}|rjt ¡ | | jkrj|rj| ¡ sjt d|t	|ƒ¡ | 
¡  |rä| ¡ räz| ¡ }W n ttjfk
rœ   Y n8X |rÔt|tƒr¶|  ¡  ntjd||d� |sÔ| |¡ | j| d= d}|s|r| 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.
        rb   Nrd   zTApplication instance %r for connection %s took too long to shut down and was killed.z Exception inside application: %s)Úexc_infor   )ÚlistrA   Úitemsrl   r^   r+   Údoner1   ÚwarningÚreprÚcancelÚ	exceptionr   rM   ru   ÚKeyboardInterruptrZ   r2   Zhandle_exceptionr
   rD   rE   )r5   r`   Údetailsrb   rd   r�   r6   r6   r7   rE     sD    ÿþý

ý

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.
        rd   z'Killed %i pending application instancesc                 S   s   d S r:   r6   )Úxr6   r6   r7   r<   E  r=   z.Server.kill_all_applications.<locals>.<lambda>)rA   Úvaluesr}   r€   rL   r1   rC   Úlenr	   ZDeferredZ
fromFuturerM   ÚgatherrJ   )r5   Úwait_forrƒ   rd   Zwait_deferredr6   r6   r7   rQ   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{   rA   ÚkeysZcheck_timeoutsr
   rD   rF   r_   r6   r6   r7   rF   H  s    
zServer.timeout_checkerc                 C   s   | j r|   |||¡ dS )zN
        Dispatches to any registered action logger, if there is one.
        N)r    )r5   r`   Úactionrƒ   r6   r6   r7   Ú
log_actionQ  s    zServer.log_action)NTNNr   r   r   r   r   r   NNNr   r   r   Nr   )rB   Ú
__module__Ú__qualname__r8   rR   rI   rK   rZ   ra   rc   ro   rm   Ústaticmethodrp   rE   rQ   rF   r‹   r6   r6   r6   r7   r   /   sB                     ì
17

0	r   )+rM   Úosr3   ÚwarningsÚconcurrent.futuresr   Ztwisted.internetr   Únew_event_loopZtwisted_loopÚenvironÚset_default_executorÚintÚmodulesrl   Zcurrent_reactorru   ZAsyncioSelectorReactorÚwarnÚUserWarningÚinstallÚloggingr^   r   Ú	functoolsr   r	   r
   Ztwisted.internet.endpointsr   Ztwisted.loggerr   r   Ztwisted.webr   Zhttp_protocolr   Zws_protocolr   Ú	getLoggerrB   r1   r   r6   r6   r6   r7   Ú<module>   sB   
ÿû

