U
    •uÄcJ€  ã                   @   sÆ  d dl mZmZ d dlmZ d dlmZ d dlZe ¡  d dl	Z
d dlmZ d dlmZ d dlmZmZmZ d dlmZ d d	lmZ d d
l	mZ d dlmZmZmZ d dlmZmZ d dlmZ d dl m!Z! d dl"m#Z#m$Z$m%Z% d dl&m'Z' d dl(m)Z) d dl*m+Z+m,Z, d dl-m.Z.m/Z/m0Z0m1Z1 dZ2dd„ Z3dd„ Z4dd„ Z5dd„ Z6G dd„ de)ƒZ7G dd „ d e
j8j'j9ƒZ:eG d!d"„ d"e:e'j;ƒƒZ;eG d#d$„ d$e:e'j<ƒƒZ<G d%d&„ d&e=ƒZ>eG d'd(„ d(e>e'j?e
j8j'j@ƒƒZ?eG d)d*„ d*e>e'jAe
j8j'jBƒƒZAeeƒG d+d,„ d,e=ƒƒZCG d-d.„ d.eCe;ƒZDG d/d0„ d0eCe<ƒZEG d1d2„ d2e?ƒZFG d3d4„ d4eAƒZGedDd6d7„ƒZHedEd:d;„ƒZIeG d<d=„ d=ejJe;ƒƒZJeG d>d?„ d?ejKe?ƒƒZKeG d@dA„ dAejLe<ƒƒZLeG dBdC„ dCejMeAƒƒZMdS )Fé    )Ú	b64encodeÚ	b64decode)ÚOptional)ÚimplementerN)Ú	endpoints)Ú
ITransport)ÚConnectionDoneÚConnectionAbortedÚConnectionLost)ÚDeferred)ÚFailure)ÚconnectionDone)ÚpublicÚhltypeÚhlval)Ú_is_tls_errorÚ_maybe_tls_reason)Ú	websocket)ÚTransportDetails)ÚConnectionRequestÚConnectionResponseÚConnectionDeny)Úprotocol)ÚIWebSocketClientAgent)Úcreate_transport_detailsÚtransport_channel_id)ÚPerMessageDeflateOfferÚPerMessageDeflateOfferAcceptÚPerMessageDeflateResponseÚPerMessageDeflateResponseAccept)Úcreate_client_agentÚWebSocketAdapterProtocolÚWebSocketServerProtocolÚWebSocketClientProtocolÚWebSocketAdapterFactoryÚWebSocketServerFactoryÚWebSocketClientFactoryÚWrappingWebSocketAdapterÚWrappingWebSocketServerProtocolÚWrappingWebSocketClientProtocolÚWrappingWebSocketServerFactoryÚWrappingWebSocketClientFactoryÚlistenWSÚ	connectWSÚWampWebSocketServerProtocolÚWampWebSocketServerFactoryÚWampWebSocketClientProtocolÚWampWebSocketClientFactoryc                 C   s   t | ƒS )zB
    :returns: an instance implementing IWebSocketClientAgent
    )Ú_TwistedWebSocketClientAgent©Úreactor© r5   ú[/var/www/html/HOTSTAR/school/venv/lib/python3.8/site-packages/autobahn/twisted/websocket.pyr    V   s    r    c                 C   s<   t | tƒstd t| ƒ¡ƒ‚|  d¡s8|  d¡s8tdƒ‚dS )z>
    raises a ValueError if `transport_config` is invalid
    z-'transport_config' must be a string, found {}zws://zwss://z6'transport_config' must start with 'ws://' or 'wss://'N)Ú
isinstanceÚstrÚ
ValueErrorÚformatÚtypeÚ
startswith)Útransport_configr5   r5   r6   Úcheck_transport_config]   s    
ÿ
ÿÿr>   c                 C   sH   t | tƒstdƒ‚dddddg}|  ¡ D ]}||kr(td |¡ƒ‚q(dS )	z5
    raises a ValueError if `options` is invalid
    z'options' must be a dictÚoriginÚ	protocolsZ	useragentÚheadersÚproxyz'options' may not contain '{}'N)r7   Údictr9   Úkeysr:   )ÚoptionsÚ
valid_keysZactual_kr5   r5   r6   Úcheck_client_optionsq   s    
ÿûÿrG   c                 C   sˆ   |j rddlm} | |j¡}|jd k	rJ||_t | |jd |jd ¡}n:|j rrddlm} t 	| |j|j
|¡}nt | |j|j
¡}|S )Nr   ©ÚsslÚhostÚport)ÚisSecureÚtwisted.internetrI   ÚoptionsForClientTLSrJ   rB   ÚcontextFactoryr   ZHostnameEndpointZSSL4ClientEndpointrK   )r4   Úfactoryr=   rE   rI   Zcontext_factoryÚendpointr5   r5   r6   Ú_endpoint_from_configŠ   s0    
ýüýrR   c                   @   s"   e Zd ZdZdd„ Zddd„ZdS )r2   z6
    This agent creates connections using Twisted
    c                 C   s
   || _ d S ©N)Ú_reactor)Úselfr4   r5   r5   r6   Ú__init__¸   s    z%_TwistedWebSocketClientAgent.__init__Nc                    s‚   t |ƒ t|ƒ tf || jdœ|—Ž}|dkr2tn||_t| j|||ƒ}tƒ ‰| |¡}‡fdd„‰ ‡ ‡fdd„}| 	|ˆ ¡ ˆS )a-  
        Open a new connection.

        :param dict transport_config: valid transport configuration

        :param dict options: additional options for the factory

        :param protocol_class: a callable that returns an instance of
            the protocol (WebSocketClientProtocol if the default None
            is passed in)

        :returns: a Deferred that fires with an instance of
            `protocol_class` (or WebSocketClientProtocol by default)
            that has successfully shaken hands (completed the
            handshake).
        )Úurlr4   Nc                    s   ˆ   | ¡ d S rS   )Zerrback)Úf)Úrtn_dr5   r6   ÚfailedÜ   s    z1_TwistedWebSocketClientAgent.open.<locals>.failedc                    s    ‡ ‡fdd„}ˆ j  |ˆ¡ ˆ S )Nc                    s   ˆ  ˆ ¡ | S rS   )Úcallback)Úarg)ÚprotorY   r5   r6   Úhandshake_completedá   s    
zQ_TwistedWebSocketClientAgent.open.<locals>.got_proto.<locals>.handshake_completed)Zis_openÚaddCallbacks)r]   r^   ©rZ   rY   )r]   r6   Ú	got_protoß   s    z4_TwistedWebSocketClientAgent.open.<locals>.got_proto)
r>   rG   r&   rT   r#   r   rR   r   Úconnectr_   )rU   r=   rE   Zprotocol_classrP   rQ   Zproto_dra   r5   r`   r6   Úopen»   s     þý
z!_TwistedWebSocketClientAgent.open)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rV   rc   r5   r5   r5   r6   r2   ³   s   r2   c                   @   sÔ   e Zd ZU dZe ¡ ZdZee	 e
d< dZee e
d< dd„ Zefedœdd	„Zed
œd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d d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Z dS )+r!   aÄ  
    Adapter class for Twisted WebSocket client and server protocols.

    Called from Twisted:

    * :meth:`autobahn.twisted.websocket.WebSocketAdapterProtocol.connectionMade`
    * :meth:`autobahn.twisted.websocket.WebSocketAdapterProtocol.connectionLost`
    * :meth:`autobahn.twisted.websocket.WebSocketAdapterProtocol.dataReceived`

    Called from Network-independent Code (WebSocket implementation):

    * :meth:`autobahn.twisted.websocket.WebSocketAdapterProtocol._onOpen`
    * :meth:`autobahn.twisted.websocket.WebSocketAdapterProtocol._onMessageBegin`
    * :meth:`autobahn.twisted.websocket.WebSocketAdapterProtocol._onMessageFrameData`
    * :meth:`autobahn.twisted.websocket.WebSocketAdapterProtocol._onMessageFrameEnd`
    * :meth:`autobahn.twisted.websocket.WebSocketAdapterProtocol._onMessageEnd`
    * :meth:`autobahn.twisted.websocket.WebSocketAdapterProtocol._onMessage`
    * :meth:`autobahn.twisted.websocket.WebSocketAdapterProtocol._onPing`
    * :meth:`autobahn.twisted.websocket.WebSocketAdapterProtocol._onPong`
    * :meth:`autobahn.twisted.websocket.WebSocketAdapterProtocol._onClose`

    FIXME:

    * :meth:`autobahn.twisted.websocket.WebSocketAdapterProtocol._closeConnection`
    * :meth:`autobahn.twisted.websocket.WebSocketAdapterProtocol._create_transport_details`
    * :meth:`autobahn.twisted.websocket.WebSocketAdapterProtocol.registerProducer`
    * :meth:`autobahn.twisted.websocket.WebSocketAdapterProtocol.unregisterProducer`
    NÚpeerÚ	is_serverc                 C   sn   t | j| jƒ| _tj| j_| jj| _z| j | j	¡ W n   Y nX |  
¡  | jjdt| jƒt| jƒd� d S )Nz/{func} connection established for peer="{peer}"©Úfuncrh   )r   Ú	transportri   Ú_transport_detailsr   ZCHANNEL_FRAMING_WEBSOCKETZchannel_framingrh   ZsetTcpNoDelayZ
tcpNoDelayZ_connectionMadeÚlogÚdebugr   ÚconnectionMader   ©rU   r5   r5   r6   rp     s    

þz'WebSocketAdapterProtocol.connectionMade)Úreasonc                 C   s  d}t |jtƒr(| jjd| jd� d}nœt|jƒrF| j t|jƒ¡ n~t |jt	ƒrf| jjd| jd� n^t |jt
ƒr¦t|jƒ}t|jdƒr�|jj}| jjd| j|d� n| jjd	| jt|jƒ|jd
� |  |¡ |rò| jjdt| jƒt| jƒd� n | jjdt| jƒt| jƒ|d� d S )NFz,Connection to/from {peer} was closed cleanly)rh   Tz-Connection to/from {peer} was aborted locallyÚmessagezDConnection to/from {peer} was lost in a non-clean fashion: {message})rh   rs   z7Connection to/from {peer} lost ({error_type}): {error}))rh   Z
error_typeÚerrorz8{func} connection lost for peer="{peer}", closed cleanlyrj   zD{func} connection lost for peer="{peer}", closed with error {reason})rk   rh   rr   )r7   Úvaluer   rn   ro   rh   r   rt   r   r	   r
   r8   Úhasattrrs   r;   Z_connectionLostr   ÚconnectionLostr   )rU   rr   Z	was_cleanrs   r5   r5   r6   rw   '  sJ    ÿ
ÿ
ý  ÿ
þýz'WebSocketAdapterProtocol.connectionLost)Údatac                 C   s6   | j jdt| jƒt| jƒtt|ƒƒd� |  |¡ d S )Nz2{func} received {data_len} bytes for peer="{peer}")rk   rh   Údata_len)rn   ro   r   ÚdataReceivedr   rh   ÚlenZ_dataReceived©rU   rx   r5   r5   r6   rz   U  s    
ýz%WebSocketAdapterProtocol.dataReceivedFc                 C   s*   |rt | jdƒr| j ¡  n
| j ¡  d S )NÚabortConnection)rv   rl   r}   ÚloseConnection)rU   Úabortr5   r5   r6   Ú_closeConnection^  s    z)WebSocketAdapterProtocol._closeConnectionc                 C   s2   | j jr&dt| j| j jdƒi}|| j _|  ¡  d S )Nz
tls-unique)rm   Z	is_securer   rl   ri   Ú
channel_idÚonOpen)rU   r�   r5   r5   r6   Ú_onOpene  s     ÿz WebSocketAdapterProtocol._onOpenc                 C   s   |   |¡ d S rS   )ZonMessageBegin)rU   ÚisBinaryr5   r5   r6   Ú_onMessageBeginp  s    z(WebSocketAdapterProtocol._onMessageBeginc                 C   s   |   |¡ d S rS   )ZonMessageFrameBegin)rU   Úlengthr5   r5   r6   Ú_onMessageFrameBegins  s    z-WebSocketAdapterProtocol._onMessageFrameBeginc                 C   s   |   |¡ d S rS   )ZonMessageFrameData©rU   Úpayloadr5   r5   r6   Ú_onMessageFrameDatav  s    z,WebSocketAdapterProtocol._onMessageFrameDatac                 C   s   |   ¡  d S rS   )ZonMessageFrameEndrq   r5   r5   r6   Ú_onMessageFrameEndy  s    z+WebSocketAdapterProtocol._onMessageFrameEndc                 C   s   |   |¡ d S rS   )ZonMessageFramerˆ   r5   r5   r6   Ú_onMessageFrame|  s    z(WebSocketAdapterProtocol._onMessageFramec                 C   s   |   ¡  d S rS   )ZonMessageEndrq   r5   r5   r6   Ú_onMessageEnd  s    z&WebSocketAdapterProtocol._onMessageEndc                 C   s   |   ||¡ d S rS   )Ú	onMessage)rU   r‰   r„   r5   r5   r6   Ú
_onMessage‚  s    z#WebSocketAdapterProtocol._onMessagec                 C   s   |   |¡ d S rS   )ZonPingrˆ   r5   r5   r6   Ú_onPing…  s    z WebSocketAdapterProtocol._onPingc                 C   s   |   |¡ d S rS   )ZonPongrˆ   r5   r5   r6   Ú_onPongˆ  s    z WebSocketAdapterProtocol._onPongc                 C   s   |   |||¡ d S rS   )ÚonClose©rU   ZwasCleanÚcoderr   r5   r5   r6   Ú_onClose‹  s    z!WebSocketAdapterProtocol._onClosec                 C   s   | j  ||¡ dS )zâ
        Register a Twisted producer with this protocol.

        :param producer: A Twisted push or pull producer.
        :type producer: object
        :param streaming: Producer type.
        :type streaming: bool
        N)rl   ÚregisterProducer)rU   ZproducerZ	streamingr5   r5   r6   r–   Ž  s    	z)WebSocketAdapterProtocol.registerProducerc                 C   s   | j  ¡  dS )zA
        Unregister Twisted producer with this protocol.
        N)rl   ÚunregisterProducerrq   r5   r5   r6   r—   ™  s    z+WebSocketAdapterProtocol.unregisterProducer)F)!rd   re   rf   rg   ÚtxaioÚmake_loggerrn   rh   r   r8   Ú__annotations__ri   Úboolrp   r   r   rw   Úbytesrz   r€   rƒ   r…   r‡   rŠ   r‹   rŒ   r�   r�   r�   r‘   r•   r–   r—   r5   r5   r5   r6   r!   ê   s*   
.	
r!   c                   @   s   e Zd ZdZe ¡ ZdZdS )r"   zŒ
    Base class for Twisted-based WebSocket server protocols.

    Implements :class:`autobahn.websocket.interfaces.IWebSocketChannel`.
    TN)rd   re   rf   rg   r˜   r™   rn   ri   r5   r5   r5   r6   r"      s   r"   c                   @   s2   e Zd ZdZe ¡ ZdZedœdd„Z	dd„ Z
dS )	r#   zŒ
    Base class for Twisted-based WebSocket client protocols.

    Implements :class:`autobahn.websocket.interfaces.IWebSocketChannel`.
    F©Úresponsec                 C   s"   | j jdt| jƒ|d� |  |¡S )Nz{meth}(response={response}))Úmethrž   )rn   ro   r   Ú
_onConnectÚ	onConnect)rU   rž   r5   r5   r6   r    º  s    z"WebSocketClientProtocol._onConnectc                 C   s    | j  d¡ | j | jj¡ d S )NzStarting TLS upgrade)rn   ro   rl   ÚstartTLSrP   rO   rq   r5   r5   r6   r¢   ¾  s    z WebSocketClientProtocol.startTLSN)rd   re   rf   rg   r˜   r™   rn   ri   r   r    r¢   r5   r5   r5   r6   r#   ¯  s
   r#   c                   @   s   e Zd ZdZdS )r$   zP
    Adapter class for Twisted-based WebSocket client and server factories.
    N©rd   re   rf   rg   r5   r5   r5   r6   r$   Ã  s   r$   c                   @   s    e Zd ZdZe ¡ Zdd„ ZdS )r%   z˜
    Base class for Twisted-based WebSocket server factories.

    Implements :class:`autobahn.websocket.interfaces.IWebSocketServerChannelFactory`
    c                 O   s>   |  dd¡}|dkr ddlm} || _tjj| f|ž|Ž dS )a  

        .. note::
            In addition to all arguments to the constructor of
            :meth:`autobahn.websocket.interfaces.IWebSocketServerChannelFactory`,
            you can supply a ``reactor`` keyword argument to specify the
            Twisted reactor to be used.
        r4   Nr   r3   )ÚpoprM   r4   r   r%   rV   )rU   ÚargsÚkwargsr4   r5   r5   r6   rV   Ó  s
    
zWebSocketServerFactory.__init__N©rd   re   rf   rg   r˜   r™   rn   rV   r5   r5   r5   r6   r%   É  s   r%   c                   @   s    e Zd ZdZe ¡ Zdd„ ZdS )r&   z˜
    Base class for Twisted-based WebSocket client factories.

    Implements :class:`autobahn.websocket.interfaces.IWebSocketClientChannelFactory`
    c                 O   sh   |  dd¡}|dkr ddlm} || _tjj| f|ž|Ž | jrd| jdk	rdddlm} | 	| j
¡| _dS )a  

        .. note::
            In addition to all arguments to the constructor of
            :func:`autobahn.websocket.interfaces.IWebSocketClientChannelFactory`,
            you can supply a ``reactor`` keyword argument to specify the
            Twisted reactor to be used.
        r4   Nr   r3   rH   )r¤   rM   r4   r   r&   rV   rL   rB   rI   rN   rJ   rO   )rU   r¥   r¦   r4   rI   r5   r5   r6   rV   ï  s    
zWebSocketClientFactory.__init__Nr§   r5   r5   r5   r6   r&   å  s   r&   c                   @   sX   e Zd Z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S )r'   aŽ  
    An adapter for stream-based transport over WebSocket.

    This follows `websockify <https://github.com/kanaka/websockify>`_
    and should be compatible with that.

    It uses WebSocket subprotocol negotiation and supports the
    following WebSocket subprotocols:

      - ``binary`` (or a compatible subprotocol)
      - ``base64``

    Octets are either transmitted as the payload of WebSocket binary
    messages when using the ``binary`` subprotocol (or an alternative
    binary compatible subprotocol), or encoded with Base64 and then
    transmitted as the payload of WebSocket text messages when using
    the ``base64`` subprotocol.
    c                 C   s    t |tƒrP|}|jD ]"}|| jjkr|dk| _|  S qttjd | jj¡ƒ‚nLt |t	ƒr”|}|j
| jjkr†|  t
jjd | jj¡¡ |j
dk| _ntdƒ‚d S )NÚbase64z2this server only speaks {0} WebSocket subprotocolsz2this client only speaks {0} WebSocket subprotocolszlogic error)r7   r   r@   rP   Ú_subprotocolsÚ_binaryModer   ZNOT_ACCEPTABLEr:   r   r   Ú_fail_connectionÚWebSocketProtocolZ CLOSE_STATUS_CODE_PROTOCOL_ERRORÚ	Exception)rU   ZrequestOrResponseÚrequestÚprž   r5   r5   r6   r¡   $  s    




z"WrappingWebSocketAdapter.onConnectc                 C   s   | j  ¡  d S rS   )Ú_protorp   rq   r5   r5   r6   r‚   6  s    zWrappingWebSocketAdapter.onOpenc              
   C   sv   || j kr|  tjjd¡ nV|sfzt|ƒ}W n8 tk
rd } z|  tjjd |¡¡ W 5 d }~X Y nX | j	 
|¡ d S )Nz>message payload type does not match the negotiated subprotocolz*message payload base64 decoding error: {0})rª   r«   r   r¬   Z"CLOSE_STATUS_CODE_UNSUPPORTED_DATAr   r­   Z!CLOSE_STATUS_CODE_INVALID_PAYLOADr:   r°   rz   )rU   r‰   r„   Úer5   r5   r6   rŽ   9  s    
(z"WrappingWebSocketAdapter.onMessagec                 C   s   | j  d ¡ d S rS   )r°   rw   r“   r5   r5   r6   r’   E  s    z WrappingWebSocketAdapter.onClosec                 C   s@   t |ƒtkst‚| jr&| j|dd� nt|ƒ}| j|dd� d S )NT)r„   F)r;   rœ   ÚAssertionErrorrª   ZsendMessager   r|   r5   r5   r6   ÚwriteH  s
    zWrappingWebSocketAdapter.writec                 C   s   |D ]}|   |¡ qd S rS   )r³   )rU   rx   Údr5   r5   r6   ÚwriteSequenceQ  s    z&WrappingWebSocketAdapter.writeSequencec                 C   s   |   ¡  d S rS   )Z	sendCloserq   r5   r5   r6   r~   V  s    z'WrappingWebSocketAdapter.loseConnectionc                 C   s
   | j  ¡ S rS   )rl   ÚgetPeerrq   r5   r5   r6   r¶   Z  s    z WrappingWebSocketAdapter.getPeerc                 C   s
   | j  ¡ S rS   )rl   ÚgetHostrq   r5   r5   r6   r·   ^  s    z WrappingWebSocketAdapter.getHostN)rd   re   rf   rg   r¡   r‚   rŽ   r’   r³   rµ   r~   r¶   r·   r5   r5   r5   r6   r'     s   	r'   c                   @   s   e Zd ZdZdS )r(   zD
    Server protocol for stream-based transport over WebSocket.
    Nr£   r5   r5   r5   r6   r(   c  s   r(   c                   @   s   e Zd ZdZdS )r)   zD
    Client protocol for stream-based transport over WebSocket.
    Nr£   r5   r5   r5   r6   r)   i  s   r)   c                   @   s2   e Zd ZdZddd„Zdd„ Zd	d
„ Zdd„ ZdS )r*   zL
    Wrapping server factory for stream-based transport over WebSocket.
    NTr   c                 C   sh   || _ ddg| _|r | j |¡ tj| ||| jd� | j|d� | jdd� |rddd„ }| j|d	� d
S )zÿ

        :param factory: Stream-based factory to be wrapped.
        :type factory: A subclass of ``twisted.internet.protocol.Factory``
        :param url: WebSocket URL of the server this server factory will work for.
        :type url: unicode
        Úbinaryr¨   ©rW   r4   r@   ©ÚautoFragmentSizeF©Z
failByDropc                 S   s$   | D ]}t |tƒrt|ƒ  S qd S rS   )r7   r   r   )ÚoffersZofferr5   r5   r6   Úaccept—  s    
z7WrappingWebSocketServerFactory.__init__.<locals>.accept©ZperMessageCompressionAcceptN)Ú_factoryr©   Úappendr%   rV   ÚsetProtocolOptions)rU   rP   rW   r4   ÚenableCompressionr»   Úsubprotocolr¾   r5   r5   r6   rV   t  s    
ýz'WrappingWebSocketServerFactory.__init__c                 C   s&   t ƒ }| |_| j |¡|_||j_|S rS   )r(   rP   rÀ   ÚbuildProtocolr°   rl   ©rU   Úaddrr]   r5   r5   r6   rÅ   ž  s
    z,WrappingWebSocketServerFactory.buildProtocolc                 C   s   | j  ¡  t | ¡ d S rS   )rÀ   ÚstartFactoryr%   rq   r5   r5   r6   rÈ   ¥  s    
z+WrappingWebSocketServerFactory.startFactoryc                 C   s   | j  ¡  t | ¡ d S rS   )rÀ   ÚstopFactoryr%   rq   r5   r5   r6   rÉ   ©  s    
z*WrappingWebSocketServerFactory.stopFactory)NTr   N)rd   re   rf   rg   rV   rÅ   rÈ   rÉ   r5   r5   r5   r6   r*   o  s       ú
*r*   c                   @   s"   e Zd ZdZd	dd„Zdd„ ZdS )
r+   zL
    Wrapping client factory for stream-based transport over WebSocket.
    NTr   c           	      C   s|   || _ ddg| _|r | j |¡ tj| ||| jd� | j|d� | jdd� |rxtƒ g}| j|d� dd	„ }| j|d
� dS )a  

        :param factory: Stream-based factory to be wrapped.
        :type factory: A subclass of ``twisted.internet.protocol.Factory``
        :param url: WebSocket URL of the server this client factory will connect to.
        :type url: unicode
        r¸   r¨   r¹   rº   Fr¼   )ZperMessageCompressionOffersc                 S   s   t | tƒrt| ƒS d S rS   )r7   r   r   r�   r5   r5   r6   r¾   Ú  s    
z7WrappingWebSocketClientFactory.__init__.<locals>.acceptr¿   N)rÀ   r©   rÁ   r&   rV   rÂ   r   )	rU   rP   rW   r4   rÃ   r»   rÄ   r½   r¾   r5   r5   r6   rV   ³  s     
ýz'WrappingWebSocketClientFactory.__init__c                 C   s&   t ƒ }| |_| j |¡|_||j_|S rS   )r)   rP   rÀ   rÅ   r°   rl   rÆ   r5   r5   r6   rÅ   à  s
    z,WrappingWebSocketClientFactory.buildProtocol)NTr   N)rd   re   rf   rg   rV   rÅ   r5   r5   r5   r6   r+   ®  s       ú
-r+   é   c                 C   sª   t | dƒr| j}nddlm} | jr@|dkr@ddlm} | ¡ }| jdk	rp|| _| | jd | jd | ||¡}n6| jr�| 	| j
| j| |||¡}n| | j
| j| ||¡}|S )a  
    Establish WebSocket connection to a server. The connection parameters like target
    host, port, resource and others are provided via the factory.

    :param factory: The WebSocket protocol factory to be used for creating client protocol instances.
    :type factory: An :class:`autobahn.websocket.WebSocketClientFactory` instance.

    :param contextFactory: SSL context factory, required for secure WebSocket connections ("wss").
    :type contextFactory: A `twisted.internet.ssl.ClientContextFactory <http://twistedmatrix.com/documents/current/api/twisted.internet.ssl.ClientContextFactory.html>`_ instance.

    :param timeout: Number of seconds to wait before assuming the connection has failed.
    :type timeout: int

    :param bindAddress: A (host, port) tuple of local address to bind to, or None.
    :type bindAddress: tuple

    :returns: The connector.
    :rtype: An object which implements `twisted.interface.IConnector <http://twistedmatrix.com/documents/current/api/twisted.internet.interfaces.IConnector.html>`_.
    r4   r   r3   NrH   rJ   rK   )rv   r4   rM   rL   rI   ZClientContextFactoryrB   rO   Z
connectTCPZ
connectSSLrJ   rK   )rP   rO   ÚtimeoutZbindAddressr4   rI   Úconnr5   r5   r6   r-   è  s    

 r-   é2   Ú c                 C   s`   t | dƒr| j}nddlm} | jrJ|dkr4tdƒ‚| | j| |||¡}n| | j| ||¡}|S )aŽ  
    Listen for incoming WebSocket connections from clients. The connection parameters like
    listening port and others are provided via the factory.

    :param factory: The WebSocket protocol factory to be used for creating server protocol instances.
    :type factory: An :class:`autobahn.websocket.WebSocketServerFactory` instance.

    :param contextFactory: SSL context factory, required for secure WebSocket connections ("wss").
    :type contextFactory: A twisted.internet.ssl.ContextFactory.

    :param backlog: Size of the listen queue.
    :type backlog: int

    :param interface: The interface (derived from hostname given) to bind to, defaults to '' (all).
    :type interface: str

    :returns: The listening port.
    :rtype: An object that implements `twisted.interface.IListeningPort <http://twistedmatrix.com/documents/current/api/twisted.internet.interfaces.IListeningPort.html>`_.
    r4   r   r3   NzCSecure WebSocket listen requested, but no SSL context factory given)rv   r4   rM   rL   r­   Z	listenSSLrK   Z	listenTCP)rP   rO   ÚbacklogZ	interfacer4   Zlistenerr5   r5   r6   r,     s    
r,   c                   @   s   e Zd ZdZdS )r.   z�
    Twisted-based WAMP-over-WebSocket server protocol.

    Implements:

    * :class:`autobahn.wamp.interfaces.ITransport`
    Nr£   r5   r5   r5   r6   r.   8  s   r.   c                   @   s   e Zd ZdZeZdd„ ZdS )r/   zD
    Twisted-based WAMP-over-WebSocket server protocol factory.
    c                 O   s<   |  dd¡}tj | ||¡ | j|d< tj| f|ž|Ž dS )až  

        :param factory: A callable that produces instances that implement
            :class:`autobahn.wamp.interfaces.ITransportHandler`
        :type factory: callable

        :param serializers: A list of WAMP serializers to use (or ``None``
            for all available serializers).
        :type serializers: list of objects implementing
            :class:`autobahn.wamp.interfaces.ISerializer`
        ÚserializersNr@   )r¤   r   r/   rV   Ú
_protocolsr%   ©rU   rP   r¥   r¦   rÐ   r5   r5   r6   rV   K  s    
z#WampWebSocketServerFactory.__init__N)rd   re   rf   rg   r.   r   rV   r5   r5   r5   r6   r/   C  s   r/   c                   @   s   e Zd ZdZdS )r0   z�
    Twisted-based WAMP-over-WebSocket client protocol.

    Implements:

    * :class:`autobahn.wamp.interfaces.ITransport`
    Nr£   r5   r5   r5   r6   r0   b  s   r0   c                   @   s   e Zd ZdZeZdd„ ZdS )r1   zD
    Twisted-based WAMP-over-WebSocket client protocol factory.
    c                 O   sB   |  dd¡}tj | ||¡ | j|d< tj| f|ž|Ž d| _dS )aÜ  

        :param factory: A callable that produces instances that implement
            :class:`autobahn.wamp.interfaces.ITransportHandler`
        :type factory: callable

        :param serializer: The WAMP serializer to use (or ``None`` for
           "best" serializer, chosen as the first serializer available from
           this list: CBOR, MessagePack, UBJSON, JSON).
        :type serializer: object implementing :class:`autobahn.wamp.interfaces.ISerializer`
        rÐ   Nr@   F)r¤   r   r1   rV   rÑ   r&   ZnoisyrÒ   r5   r5   r6   rV   u  s
    
z#WampWebSocketClientFactory.__init__N)rd   re   rf   rg   r0   r   rV   r5   r5   r5   r6   r1   m  s   r1   )NrÊ   N)NrÍ   rÎ   )Nr¨   r   r   Útypingr   Zzope.interfacer   r˜   Zuse_twistedZtwisted.internet.protocolÚtwistedrM   r   Ztwisted.internet.interfacesr   Ztwisted.internet.errorr   r	   r
   Ztwisted.internet.deferr   Ztwisted.python.failurer   r   Zautobahn.utilr   r   r   r   r   Zautobahn.wampr   Zautobahn.wamp.typesr   Zautobahn.websocket.typesr   r   r   Zautobahn.websocketr   Zautobahn.websocket.interfacesr   Zautobahn.twisted.utilr   r   Zautobahn.websocket.compressr   r   r   r   Ú__all__r    r>   rG   rR   r2   ZinternetÚProtocolr!   r"   r#   Úobjectr$   r%   ZServerFactoryr&   ZClientFactoryr'   r(   r)   r*   r+   r-   r,   r.   r/   r0   r1   r5   r5   r5   r6   Ú<module>   sn   )7 7)S?:+#

