o
    èó·iJ€  ã                   @   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
    N)Ú_TwistedWebSocketClientAgent©Úreactor© r5   ú`C:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\autobahn/twisted/websocket.pyr    V   s   r    c                 C   s<   t | tƒstd t| ƒ¡ƒ‚|  d¡s|  d¡st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   sB   t | tƒs	tdƒ‚g d¢}|  ¡ D ]}||v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:   )ÚoptionsZ
valid_keysZactual_kr5   r5   r6   Úcheck_client_optionsq   s   
ÿÿÿÿrF   c                 C   sŒ   |j rddlm} | |j¡}|jd ur&||_t | |jd |jd ¡}|S |j r;ddlm} t 	| |j|j
|¡}|S t | |j|j
¡}|S )Nr   ©ÚsslÚhostÚport)ÚisSecureÚtwisted.internetrH   ÚoptionsForClientTLSrI   rB   ÚcontextFactoryr   ZHostnameEndpointZSSL4ClientEndpointrJ   )r4   Úfactoryr=   rE   rH   Zcontext_factoryÚendpointr5   r5   r6   Ú_endpoint_from_configŠ   s4   
ýïüùýrQ   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d|| jdœ|¤Ž}|du rt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 rR   )Zerrback)Úf)Úrtn_dr5   r6   ÚfailedÜ   ó   z1_TwistedWebSocketClientAgent.open.<locals>.failedc                    s    ‡ ‡fdd„}ˆ j  |ˆ¡ ˆ S )Nc                    s   ˆ  ˆ ¡ | S rR   )Úcallback)Úarg)ÚprotorX   r5   r6   Úhandshake_completedá   s   
zQ_TwistedWebSocketClientAgent.open.<locals>.got_proto.<locals>.handshake_completed)Zis_openÚaddCallbacks)r]   r^   ©rY   rX   )r]   r6   Ú	got_protoß   s   z4_TwistedWebSocketClientAgent.open.<locals>.got_protor5   )
r>   rF   r&   rS   r#   r   rQ   r   Úconnectr_   )rT   r=   rE   Zprotocol_classrO   rP   Zproto_dra   r5   r`   r6   Úopen»   s    þý
z!_TwistedWebSocketClientAgent.openrR   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__rU   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defdd	„Zd
ef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   sj   t | j| jƒ| _tj| j_| jj| _z	| j | j	¡ W n   Y |  
¡  | 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   ©rT   r5   r5   r6   rp     s   


þz'WebSocketAdapterProtocol.connectionMadeÚreasonc                 C   s  d}t |jtƒr| jjd| jd� d}nNt|jƒr#| j t|jƒ¡ n?t |jt	ƒr3| jjd| jd� n/t |jt
ƒrSt|jƒ}t|jdƒrH|jj}| jjd| j|d� n| jjd	| jt|jƒ|jd
� |  |¡ |rz| jjdt| jƒt| jƒd� d S | 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   )rT   rr   Z	was_cleanrs   r5   r5   r6   rw   '  sF   ÿ
ÿ
ýÿ

þ
ý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©rT   rx   r5   r5   r6   rz   U  s   
ýz%WebSocketAdapterProtocol.dataReceivedFc                 C   s,   |rt | jdƒr| j ¡  d S | j ¡  d S )NÚabortConnection)rv   rl   r}   ÚloseConnection)rT   Ú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)rT   r�   r5   r5   r6   Ú_onOpene  s
   ÿz WebSocketAdapterProtocol._onOpenc                 C   ó   |   |¡ d S rR   )ZonMessageBegin)rT   ÚisBinaryr5   r5   r6   Ú_onMessageBeginp  rZ   z(WebSocketAdapterProtocol._onMessageBeginc                 C   r„   rR   )ZonMessageFrameBegin)rT   Úlengthr5   r5   r6   Ú_onMessageFrameBegins  rZ   z-WebSocketAdapterProtocol._onMessageFrameBeginc                 C   r„   rR   )ZonMessageFrameData©rT   Úpayloadr5   r5   r6   Ú_onMessageFrameDatav  rZ   z,WebSocketAdapterProtocol._onMessageFrameDatac                 C   ó   |   ¡  d S rR   )ZonMessageFrameEndrq   r5   r5   r6   Ú_onMessageFrameEndy  ó   z+WebSocketAdapterProtocol._onMessageFrameEndc                 C   r„   rR   )ZonMessageFramer‰   r5   r5   r6   Ú_onMessageFrame|  rZ   z(WebSocketAdapterProtocol._onMessageFramec                 C   rŒ   rR   )ZonMessageEndrq   r5   r5   r6   Ú_onMessageEnd  rŽ   z&WebSocketAdapterProtocol._onMessageEndc                 C   s   |   ||¡ d S rR   )Ú	onMessage)rT   rŠ   r…   r5   r5   r6   Ú
_onMessage‚  ó   z#WebSocketAdapterProtocol._onMessagec                 C   r„   rR   )ZonPingr‰   r5   r5   r6   Ú_onPing…  rZ   z WebSocketAdapterProtocol._onPingc                 C   r„   rR   )ZonPongr‰   r5   r5   r6   Ú_onPongˆ  rZ   z WebSocketAdapterProtocol._onPongc                 C   s   |   |||¡ d S rR   )ÚonClose©rT   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)rT   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def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)rT   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   ÚstartTLSrO   rN   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                   @   ó   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$   Ã  ó    r$   c                   @   ó    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   sD   |  dd¡}|du rddlm} || _tjj| g|¢R i |¤Ž 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   )ÚpoprL   r4   r   r%   rU   )rT   ÚargsÚkwargsr4   r5   r5   r6   rU   Ó  s
   
zWebSocketServerFactory.__init__N©rd   re   rf   rg   rœ   r�   rn   rU   r5   r5   r5   r6   r%   É  ó    r%   c                   @   r©   )r&   z˜
    Base class for Twisted-based WebSocket client factories.

    Implements :class:`autobahn.websocket.interfaces.IWebSocketClientChannelFactory`
    c                 O   sv   |  dd¡}|du rddlm} || _tjj| g|¢R i |¤Ž | jr7| jdur9ddlm} | 	| j
¡| _dS dS 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   rG   )rª   rL   r4   r   r&   rU   rK   rB   rH   rM   rI   rN   )rT   r«   r¬   r4   rH   r5   r5   r6   rU   ï  s   
ûzWebSocketClientFactory.__init__Nr­   r5   r5   r5   r6   r&   å  r®   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ƒr'|}|jD ]}|| jjv r|dk| _|  S q
ttjd | jj¡ƒ‚t |t	ƒrJ|}|j
| jjvrB|  t
jjd | jj¡¡ |j
dk| _d S tdƒ‚)NÚbase64z2this server only speaks {0} WebSocket subprotocolsz2this client only speaks {0} WebSocket subprotocolszlogic error)r7   r   r@   rO   Ú_subprotocolsÚ_binaryModer   ÚNOT_ACCEPTABLEr:   r   r   Ú_fail_connectionÚWebSocketProtocolZ CLOSE_STATUS_CODE_PROTOCOL_ERRORÚ	Exception)rT   ZrequestOrResponseÚrequestÚpr¡   r5   r5   r6   r¤   $  s   


þ
z"WrappingWebSocketAdapter.onConnectc                 C   s   | j  ¡  d S rR   )Ú_protorp   rq   r5   r5   r6   r‚   6  rZ   zWrappingWebSocketAdapter.onOpenc              
   C   sz   || j kr|  tjjd¡ d S |s5zt|ƒ}W n ty4 } z|  tjjd |¡¡ W Y d }~nd }~ww | 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   )rT   rŠ   r…   Úer5   r5   r6   r‘   9  s   
"€ÿz"WrappingWebSocketAdapter.onMessagec                 C   s   | j  d ¡ d S rR   )r¸   rw   r—   r5   r5   r6   r–   E  r“   z WrappingWebSocketAdapter.onClosec                 C   sB   t |ƒtksJ ‚| jr| j|dd� d S t|ƒ}| j|dd� d S )NT)r…   F)r;   r    r±   ZsendMessager   r|   r5   r5   r6   ÚwriteH  s
   zWrappingWebSocketAdapter.writec                 C   s   |D ]}|   |¡ qd S rR   )rº   )rT   rx   Údr5   r5   r6   ÚwriteSequenceQ  s   ÿz&WrappingWebSocketAdapter.writeSequencec                 C   rŒ   rR   )Z	sendCloserq   r5   r5   r6   r~   V  s   z'WrappingWebSocketAdapter.loseConnectionc                 C   ó
   | j  ¡ S rR   )rl   ÚgetPeerrq   r5   r5   r6   r¾   Z  ó   
z WrappingWebSocketAdapter.getPeerc                 C   r½   rR   )rl   ÚgetHostrq   r5   r5   r6   rÀ   ^  r¿   z WrappingWebSocketAdapter.getHostN)rd   re   rf   rg   r¤   r‚   r‘   r–   rº   r¼   r~   r¾   rÀ   r5   r5   r5   r6   r'     s    	r'   c                   @   r¦   )r(   zD
    Server protocol for stream-based transport over WebSocket.
    Nr§   r5   r5   r5   r6   r(   c  r¨   r(   c                   @   r¦   )r)   zD
    Client protocol for stream-based transport over WebSocket.
    Nr§   r5   r5   r5   r6   r)   i  r¨   r)   c                   @   s:   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   sl   || _ ddg| _|r| j |¡ tj| ||| jd� | j|d� | jdd� |r4dd„ }| j|d	� d
S 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¯   ©rV   r4   r@   ©ÚautoFragmentSizeF©Z
failByDropc                 S   s$   | D ]}t |tƒrt|ƒ  S qd S rR   )r7   r   r   )ÚoffersZofferr5   r5   r6   Úaccept—  s
   
ÿÿz7WrappingWebSocketServerFactory.__init__.<locals>.accept©ZperMessageCompressionAcceptN)Ú_factoryr°   Úappendr%   rU   ÚsetProtocolOptions)rT   rO   rV   r4   ÚenableCompressionrÄ   ÚsubprotocolrÇ   r5   r5   r6   rU   t  s   
ý÷z'WrappingWebSocketServerFactory.__init__c                 C   ó&   t ƒ }| |_| j |¡|_||j_|S rR   )r(   rO   rÉ   ÚbuildProtocolr¸   rl   ©rT   Úaddrr]   r5   r5   r6   rÏ   ž  ó
   z,WrappingWebSocketServerFactory.buildProtocolc                 C   ó   | j  ¡  t | ¡ d S rR   )rÉ   ÚstartFactoryr%   rq   r5   r5   r6   rÔ   ¥  ó   
z+WrappingWebSocketServerFactory.startFactoryc                 C   rÓ   rR   )rÉ   ÚstopFactoryr%   rq   r5   r5   r6   rÖ   ©  rÕ   z*WrappingWebSocketServerFactory.stopFactory©NTr   N)rd   re   rf   rg   rU   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� |r>tƒ g}| j|d� dd	„ }| j|d
� dS 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 rR   )r7   r   r   )r¡   r5   r5   r6   rÇ   Ú  s   
ÿz7WrappingWebSocketClientFactory.__init__.<locals>.acceptrÈ   N)rÉ   r°   rÊ   r&   rU   rË   r   )	rT   rO   rV   r4   rÌ   rÄ   rÍ   rÆ   rÇ   r5   r5   r6   rU   ³  s"   
ýôz'WrappingWebSocketClientFactory.__init__c                 C   rÎ   rR   )r)   rO   rÉ   rÏ   r¸   rl   rÐ   r5   r5   r6   rÏ   à  rÒ   z,WrappingWebSocketClientFactory.buildProtocolr×   )rd   re   rf   rg   rU   rÏ   r5   r5   r5   r6   r+   ®  s    
ú-r+   é   c                 C   s®   t | dƒr	| j}nddlm} | jr |du r ddlm} | ¡ }| jdur9|| _| | jd | jd | ||¡}|S | jrJ| 	| j
| j| |||¡}|S | | 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   NrG   rI   rJ   )rv   r4   rL   rK   rH   ZClientContextFactoryrB   rN   Z
connectTCPZ
connectSSLrI   rJ   )rO   rN   ÚtimeoutZbindAddressr4   rH   Úconnr5   r5   r6   r-   è  s    

üÿr-   é2   Ú c                 C   sb   t | dƒr	| j}nddlm} | jr&|du rtdƒ‚| | j| |||¡}|S | | 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   rL   rK   rµ   Z	listenSSLrJ   Z	listenTCP)rO   rN   ÚbacklogZ	interfacer4   Úlistenerr5   r5   r6   r,     s   
ÿr,   c                   @   r¦   )r.   z�
    Twisted-based WAMP-over-WebSocket server protocol.

    Implements:

    * :class:`autobahn.wamp.interfaces.ITransport`
    Nr§   r5   r5   r5   r6   r.   8  ó    r.   c                   @   ó   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   sB   |  dd¡}tj | ||¡ | j|d< tj| g|¢R i |¤Ž 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/   rU   Ú
_protocolsr%   ©rT   rO   r«   r¬   rá   r5   r5   r6   rU   K  s   
z#WampWebSocketServerFactory.__init__N)rd   re   rf   rg   r.   r   rU   r5   r5   r5   r6   r/   C  ó    r/   c                   @   r¦   )r0   z�
    Twisted-based WAMP-over-WebSocket client protocol.

    Implements:

    * :class:`autobahn.wamp.interfaces.ITransport`
    Nr§   r5   r5   r5   r6   r0   b  rß   r0   c                   @   rà   )r1   zD
    Twisted-based WAMP-over-WebSocket client protocol factory.
    c                 O   sH   |  dd¡}tj | ||¡ | j|d< tj| g|¢R i |¤Ž 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   rU   râ   r&   Znoisyrã   r5   r5   r6   rU   u  s
   

z#WampWebSocketClientFactory.__init__N)rd   re   rf   rg   r0   r   rU   r5   r5   r5   r6   r1   m  rä   r1   )NrØ   N)NrÛ   rÜ   )Nr¯   r   r   Útypingr   Zzope.interfacer   rœ   Zuse_twistedZtwisted.internet.protocolZtwistedrL   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>   rF   rQ   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>   sp   )7 7)S?:+#

