
'Qc           @`  s  d  Z  d d l m Z m Z 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 d k rd d l m Z d d l m Z m Z m Z d d	 l m Z m Z m Z d d
 l m Z e e e e g Z e e e e g Z e Z e Z e Z e Z n@ d d l m Z m Z m Z m Z m Z e e e g Z e g Z d d l m Z m Z m Z d d l  m! Z! m" Z" d d l m# Z# m$ Z$ m% Z% e	 e% j& e% j' e% j(  d e j) f d     Y Z* d f  d     YZ+ e	 e% j,  d e+ e* f d     Y Z- d S(   s  
Various asynchronous UDP classes.

Please do not use this module directly.

@var _sockErrReadIgnore: list of symbolic error constants (from the C{errno}
    module) representing socket errors where the error is temporary and can be
    ignored.

@var _sockErrReadRefuse: list of symbolic error constants (from the C{errno}
    module) representing socket errors that indicate connection refused.
i    (   t   divisiont   absolute_importN(   t   implementer(   t   platformTypet   win32(   t   WSAEWOULDBLOCK(   t   WSAEINTRt   WSAEMSGSIZEt   WSAETIMEDOUT(   t   WSAECONNREFUSEDt   WSAECONNRESETt   WSAENETRESET(   t   WSAEINPROGRESS(   t   EWOULDBLOCKt   EINTRt   EMSGSIZEt   ECONNREFUSEDt   EAGAIN(   t   baset   defert   address(   t   logt   failure(   t   abstractt   errort
   interfacest   Portc           B`  s   e  Z d  Z e j Z e j Z d Z d Z
 d Z d d d d  Z e d    Z d   Z d   Z d	   Z d
   Z d   Z d   Z d d  Z d   Z d   Z d   Z d   Z d   Z d d  Z d   Z d   Z d   Z RS(   s  
    UDP port, listening for packets.

    @ivar maxThroughput: Maximum number of bytes read in one event
        loop iteration.

    @ivar _realPortNumber: Actual port number being listened on. The
        value will be C{None} until this L{Port} is listening.

    @ivar _preexistingSocket: If not C{None}, a L{socket.socket} instance which
        was created and initialized outside of the reactor and will be used to
        listen for connections (instead of a new socket being created by this
        L{Port}).
    i   i   t    i    c         C`  sN   t  j j |  |  | |  _ | |  _ | |  _ | |  _ |  j   d |  _	 d S(   s  
        @param port: A port number on which to listen.
        @type port: C{int}

        @param proto: A C{DatagramProtocol} instance which will be
            connected to the given C{port}.
        @type proto: L{twisted.internet.protocol.DatagramProtocol}

        @param interface: The local IPv4 or IPv6 address to which to bind;
            defaults to '', ie all IPv4 addresses.
        @type interface: C{str}

        @param maxPacketSize: The maximum packet size to accept.
        @type maxPacketSize: C{int}

        @param reactor: A reactor which will notify this C{Port} when
            its socket is ready for reading or writing. Defaults to
            C{None}, ie the default global reactor.
        @type reactor: L{interfaces.IReactorFDSet}
        N(
   R   t   BasePortt   __init__t   portt   protocolt   maxPacketSizet	   interfacet	   setLogStrt   Nonet   _connectedAddr(   t   selfR   t   protoR!   R    t   reactor(    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyR   S   s    				
c   	   	   C`  sV   t  j | | |  j  } | j   d } |  d | d | d | d | } | | _ | S(   s  
        Create a new L{Port} based on an existing listening
        I{SOCK_DGRAM} socket.

        @param reactor: A reactor which will notify this L{Port} when
            its socket is ready for reading or writing. Defaults to
            C{None}, ie the default global reactor.
        @type reactor: L{interfaces.IReactorFDSet}

        @param fd: An integer file descriptor associated with a listening
            socket.  The socket must be in non-blocking mode.  Any additional
            attributes desired, such as I{FD_CLOEXEC}, must also be set already.
        @type fd: C{int}

        @param addressFamily: The address family (sometimes called I{domain}) of
            the existing socket.  For example, L{socket.AF_INET}.
        @param addressFamily: C{int}

        @param protocol: A C{DatagramProtocol} instance which will be
            connected to the C{port}.
        @type proto: L{twisted.internet.protocol.DatagramProtocol}

        @param maxPacketSize: The maximum packet size to accept.
        @type maxPacketSize: C{int}

        @return: A new instance of C{cls} wrapping the socket given by C{fd}.
        @rtype: L{Port}
        i    R!   R'   R    N(   t   sockett   fromfdt
   socketTypet   getsocknameR#   t   _preexistingSocket(	   t   clsR'   t   fdt   addressFamilyR   R    R   R!   R%   (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyt   _fromListeningDescriptorq   s    		c         C`  s;   |  j  d  k	 r& d |  j j |  j  f Sd |  j j f Sd  S(   Ns
   <%s on %s>s   <%s not connected>(   t   _realPortNumberR#   R   t	   __class__(   R%   (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyt   __repr__   s    c         C`  s   |  j  S(   s)   
        Return a socket object.
        (   R(   (   R%   (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyt	   getHandle   s    c         C`  s   |  j    |  j   d S(   s   
        Create and bind my socket, and begin listening on it.

        This is called on unserialization, and must be called after creating a
        server to begin listening on the specified port.
        N(   t   _bindSockett   _connectToProtocol(   R%   (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyt   startListening   s    
c         C`  s   |  j  d k ro y) |  j   } | j |  j |  j f  Wq t j k
 rk } t j |  j |  j |   q Xn |  j  } d |  _  | j	   d |  _
 t j d |  j |  j  |  j
 f  d |  _ | |  _ |  j j |  _ d S(   sJ  
        Prepare and assign a L{socket.socket} instance to
        C{self.socket}.

        Either creates a new SOCK_DGRAM L{socket.socket} bound to
        C{self.interface} and C{self.port} or takes an existing
        L{socket.socket} provided via the
        L{interfaces.IReactorSocket.adoptDatagramPort} interface.
        i   s   %s starting on %sN(   R,   R#   t   createInternetSockett   bindR!   R   R(   R   t   CannotListenErrorR+   R1   R   t   msgt   _getLogPrefixR   t	   connectedt   fileno(   R%   t   sktt   le(    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyR5      s    
"					c         C`  s   |  j  j |   |  j   d  S(   N(   R   t   makeConnectiont   startReading(   R%   (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyR6      s    c         C`  s   d } x | |  j  k  r y |  j j |  j  \ } } Wn_ t j k
 r } | j d } | t k ri d S| t k r |  j r |  j	 j
   n  d S  q	 X| t |  7} y |  j	 j | |  Wq	 t j   q	 Xq	 Wd S(   s=   
        Called when my socket is ready for reading.
        i    N(   t   maxThroughputR(   t   recvfromR    R   t   argst   _sockErrReadIgnoret   _sockErrReadRefuseR$   R   t   connectionRefusedt   lent   datagramReceivedR   t   err(   R%   t   readt   datat   addrt   set   no(    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyt   doRead   s$    	c         C`  s  |  j  r | d	 |  j  f k s$ t  y |  j j |  SWqt j k
 r } | j d } | t k rs |  j |  S| t	 k r t j
 d   q | t k r |  j j   q   qXn | d	 k s t  | d j d d  j   r| d d k rt j d t d d n  y |  j j | |  SWnp t j k
 r} | j d } | t k rd|  j | |  S| t	 k rt j
 d   q| t k rd	 S  n Xd	 S(
   sz  
        Write a datagram.

        @type datagram: C{str}
        @param datagram: The datagram to be sent.

        @type addr: C{tuple} containing C{str} as first element and C{int} as
            second element, or C{None}
        @param addr: A tuple of (I{stringified dotted-quad IP address},
            I{integer port number}); can be C{None} in connected mode.
        i    s   message too longt   .R   s   <broadcast>s.   Please only pass IPs to write(), not hostnamest
   stackleveli   N(   R$   R#   t   AssertionErrorR(   t   sendR   RE   R   t   writeR   t   MessageLengthErrorR   R   RH   t   replacet   isdigitt   warningst   warnt   DeprecationWarningt   sendto(   R%   t   datagramRN   RO   RP   (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyRV      s8    	
-	c         C`  s   |  j  d j |  |  d  S(   NR   (   RV   t   join(   R%   t   seqRN   (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyt   writeSequence  s    c         C`  s_   |  j  r t d   n  t j |  s6 t d   n  | | f |  _  |  j j | | f  d S(   s-   
        'Connect' to remote server.
        s:   already connected, reconnecting is not currently supporteds/   please pass only IP addresses, not domain namesN(   R$   t   RuntimeErrorR   t   isIPAddresst
   ValueErrorR(   t   connect(   R%   t   hostR   (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyRe   !  s    	c         C`  s0   |  j    |  j r, |  j j d |  j  n  d  S(   Ni    (   t   stopReadingR=   R'   t	   callLatert   connectionLost(   R%   (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyt   _loseConnection,  s    
	c         C`  s3   |  j  r t j   } |  _ n d  } |  j   | S(   N(   R=   R   t   Deferredt   dR#   Rj   (   R%   t   result(    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyt   stopListening1  s
    	
c         C`  s$   t  j d t d d |  j   d  S(   Ns-   Please use stopListening() to disconnect portRS   i   (   RZ   R[   R\   Rn   (   R%   (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyt   loseConnection9  s    c         C`  s   t  j d |  j  d |  _ t j j |  |  |  j j   |  j	 j
   |  `	 |  ` t |  d  r~ |  j j d  |  ` n  d S(   s&   
        Cleans up my socket.
        s   (UDP Port %s Closed)Rl   N(   R   R;   R1   R#   R   R   Ri   R   t   doStopR(   t   closeR>   t   hasattrRl   t   callback(   R%   t   reason(    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyRi   =  s    	c         C`  s#   |  j  |  j  } d | |  _ d S(   sP   
        Initialize the C{logstr} attribute to be used by C{logPrefix}.
        s   %s (UDP)N(   R<   R   t   logstr(   R%   t	   logPrefix(    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyR"   M  s    c         C`  s   |  j  S(   s0   
        Return the prefix to log with.
        (   Ru   (   R%   (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyRv   U  s    c         C`  s   t  j d |  j j    S(   si   
        Returns an IPv4Address.

        This indicates the address from which I am connecting.
        t   UDP(   R   t   IPv4AddressR(   R+   (   R%   (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyt   getHost\  s    i   N(   t   __name__t
   __module__t   __doc__R(   t   AF_INETR/   t
   SOCK_DGRAMR*   RC   R#   R1   R,   R   t   classmethodR0   R3   R4   R7   R5   R6   RQ   RV   Ra   Re   Rj   Rn   Ro   Ri   R"   Rv   Ry   (    (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyR   9   s0   		'				"		/							t   MulticastMixinc           B`  sw   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d d	  Z
 d
   Z d   Z d d  Z RS(   s,   
    Implement multicast functionality.
    c         C`  s4   |  j  j t  j t  j  } t  j t j d |   S(   Ns   @i(   R(   t
   getsockoptt
   IPPROTO_IPt   IP_MULTICAST_IFt	   inet_ntoat   structt   pack(   R%   t   i(    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyt   getOutgoingInterfacek  s    c         C`  s   |  j  j |  j |  j  S(   s   Returns Deferred of success.(   R'   t   resolvet   addCallbackt   _setInterface(   R%   RN   (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyt   setOutgoingInterfaceo  s    c         C`  s/   t  j |  } |  j  j t  j t  j |  d S(   Ni   (   R(   t	   inet_atont
   setsockoptR   R   (   R%   RN   R   (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyR   s  s    c         C`  s   |  j  j t  j t  j  S(   N(   R(   R   R   t   IP_MULTICAST_LOOP(   R%   (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyt   getLoopbackModex  s    c         C`  s;   t  j d t j |   } |  j j t j t j |  d  S(   Nt   b(   R   R   t   operatort   truthR(   R   R   R   (   R%   t   mode(    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyt   setLoopbackMode{  s    c         C`  s   |  j  j t  j t  j  S(   N(   R(   R   R   t   IP_MULTICAST_TTL(   R%   (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyt   getTTL  s    c         C`  s2   t  j d |  } |  j j t j t j |  d  S(   Nt   B(   R   R   R(   R   R   R   (   R%   t   ttl(    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyt   setTTL  s    R   c         C`  s"   |  j  j |  j |  j | d  S(   s4   Join a multicast group. Returns Deferred of success.i   (   R'   R   R   t
   _joinAddr1(   R%   RN   R!   (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyt	   joinGroup  s    c         C`  s"   |  j  j |  j |  j | |  S(   N(   R'   R   R   t
   _joinAddr2(   R%   RN   R!   R_   (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyR     s    c         C`  s   t  j |  } t  j |  } | r0 t  j } n	 t  j } y! |  j  j t  j | | |  Wn2 t  j k
 r } t j t j	 | | | j
   SXd  S(   N(   R(   R   t   IP_ADD_MEMBERSHIPt   IP_DROP_MEMBERSHIPR   R   R   R   t   Failuret   MulticastJoinErrorRE   (   R%   R!   RN   R_   t   cmdt   e(    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyR     s    	!c         C`  s"   |  j  j |  j |  j | d  S(   s2   Leave multicast group, return Deferred of success.i    (   R'   R   R   R   (   R%   RN   R!   (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyt
   leaveGroup  s    (   Rz   R{   R|   R   R   R   R   R   R   R   R   R   R   R   (    (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyR   f  s   									t   MulticastPortc           B`  s,   e  Z d  Z d d d e d  Z d   Z RS(   s.   
    UDP Port that supports multicasting.
    R   i    c         C`  s)   t  j |  | | | | |  | |  _ d S(   sX   
        @see: L{twisted.internet.interfaces.IReactorMulticast.listenMulticast}
        N(   R   R   t   listenMultiple(   R%   R   R&   R!   R    R'   R   (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyR     s    c         C`  sc   t  j |   } |  j r_ | j t j t j d  t t d  r_ | j t j t j d  q_ n  | S(   Ni   t   SO_REUSEPORT(	   R   R8   R   R   R(   t
   SOL_SOCKETt   SO_REUSEADDRRr   R   (   R%   R?   (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyR8     s    	N(   Rz   R{   R|   R#   t   FalseR   R8   (    (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyR     s   (.   R|   t
   __future__R    R   R(   R   R   RZ   t   zope.interfaceR   t   twisted.python.runtimeR   t   errnoR   R   R   R   R	   R
   R   R   RF   RG   R   R   R   R   R   t   twisted.internetR   R   R   t   twisted.pythonR   R   R   R   R   t   IListeningPortt   IUDPTransportt   ISystemHandleR   R   R   t   IMulticastTransportR   (    (    (    sF   /var/www/html/hlstest/hls-proxy/Twisted-13.2.0/twisted/internet/udp.pyt   <module>   sB   			(	 +8