
>Mc           @   s  d  Z  d d l Z d d l Z d d l Z d d l m Z d d l m Z 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 d d	 l m Z m Z y d d
 l m Z Wn* e k
 r d Z d Z e j d  n Xe j f Z d d l m Z d Z  d Z! e" d  \ Z# Z$ e
 j% d  Z& e
 j% d  Z' d e j( e j) f d     YZ* d   Z+ d d g Z, d S(   s'   
Reactor that uses IO completion ports
iN(   t
   implements(   t   baset
   interfacest   maint   error(   t   logt   failure(   t   Process(   t   iocpsupport(   t   WAIT_TIMEOUT(   t   tcpt   udp(   t   TLSMemoryBIOFactorys|   pyOpenSSL 0.10 or newer is required for SSL support in iocpreactor. It is missing, so the reactor will not support SSL APIs.(   t   seti  i  i   s#   Handler has no getFileHandle methods   Filedescriptor went awayt   IOCPReactorc           B   s2  e  Z e e j e j e j e j e  d Z
 d    Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d	 d
 d  Z d d d  Z e d k	 r d	 d
 d  Z d d d  Z n d	 d
 d  Z d d d  Z d
 d d  Z d
 d e d  Z d i  d d d d d d  Z d   Z RS(   c         C   s/   t  j j |   t j   |  _ t   |  _ d  S(   N(   R   t   ReactorBaset   __init__t   _iocpt   CompletionPortt   portR   t   handles(   t   self(    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/iocpreactor/reactor.pyR   8   s    c         C   s   |  j  j |  d  S(   N(   R   t   add(   R   t   handle(    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/iocpreactor/reactor.pyt   addActiveHandle>   s    c         C   s   |  j  j |  d  S(   N(   R   t   discard(   R   R   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/iocpreactor/reactor.pyt   removeActiveHandleB   s    c         C   s   d } | d k r t } n t t t d |   } |  j j |  \ } } } } x | t k re Pn  | t k r | t k s t	  t
 j | j |  j | | |  | d 7} n  | t k r Pn  |  j j d  \ } } } } qU d S(   s=   
        Poll the IO completion port for new events.
        i    i  i   N(   t   Nonet   MAX_TIMEOUTt   mint   intR   t   getEventR	   t
   KEY_WAKEUPt
   KEY_NORMALt   AssertionErrorR   t   callWithLoggert   ownert   _callEventCallbackt   EVENTS_PER_LOOP(   R   t   timeoutt   processed_eventst   rct   bytest   keyt   evt(    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/iocpreactor/reactor.pyt   doIterationF   s     	c         C   s   | j  } d  } yZ | j | | |  t | d d   } | sF t } n |   d k r^ t } n  | rh d  SWn! t j   d } t j	   n X| r | j
 t j |   n  d  S(   Nt   getFileHandleii   (   R$   R   t   callbackt   getattrt   _NO_GETHANDLEt   _NO_FILEDESCt   syst   exc_infoR   t   errt   loseConnectionR   t   Failure(   R   R)   R*   R,   R$   t   whyt   handfn(    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/iocpreactor/reactor.pyR%   r   s     			c         C   s   d  S(   N(    (   R   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/iocpreactor/reactor.pyt   installWaker   s    c         C   s   |  j  j d t d   d  S(   Ni    (   R   t	   postEventR    R   (   R   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/iocpreactor/reactor.pyt   wakeUp   s    c         C   s   |  j  j | t  d  S(   N(   R   t	   addHandleR!   (   R   R   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/iocpreactor/reactor.pyt   registerHandle   s    c         C   s)   t  j  | |  } |  j | j    | S(   N(   t   socketR>   t   fileno(   R   t   aft   stypet   skt(    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/iocpreactor/reactor.pyt   createSocket   s    i2   t    c         C   s)   t  j | | | | |   } | j   | S(   sI   
        @see: twisted.internet.interfaces.IReactorTCP.listenTCP
        (   R
   t   Portt   startListening(   R   R   t   factoryt   backlogt	   interfacet   p(    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/iocpreactor/reactor.pyt	   listenTCP   s    
i   c         C   s,   t  j | | | | | |   } | j   | S(   sJ   
        @see: twisted.internet.interfaces.IReactorTCP.connectTCP
        (   R
   t	   Connectort   connect(   R   t   hostR   RH   R'   t   bindAddresst   c(    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/iocpreactor/reactor.pyt
   connectTCP   s    
c         C   s"   |  j  | t | t |  | |  S(   sQ   
            @see: twisted.internet.interfaces.IReactorSSL.listenSSL
            (   RL   R   t   False(   R   R   RH   t   contextFactoryRI   RJ   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/iocpreactor/reactor.pyt	   listenSSL   s    c         C   s%   |  j  | | t | t |  | |  S(   sR   
            @see: twisted.internet.interfaces.IReactorSSL.connectSSL
            (   RR   R   t   True(   R   RO   R   RH   RT   R'   RP   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/iocpreactor/reactor.pyt
   connectSSL   s    c         C   s   t  d   d S(   s   
            Non-implementation of L{IReactorSSL.listenSSL}.  Some dependency
            is not satisfied.  This implementation always raises
            L{NotImplementedError}.
            s|   pyOpenSSL 0.10 or newer is required for SSL support in iocpreactor. It is missing, so the reactor does not support SSL APIs.N(   t   NotImplementedError(   R   R   RH   RT   RI   RJ   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/iocpreactor/reactor.pyRU      s    c         C   s   t  d   d S(   s   
            Non-implementation of L{IReactorSSL.connectSSL}.  Some dependency
            is not satisfied.  This implementation always raises
            L{NotImplementedError}.
            s|   pyOpenSSL 0.10 or newer is required for SSL support in iocpreactor. It is missing, so the reactor does not support SSL APIs.N(   RX   (   R   RO   R   RH   RT   R'   RP   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/iocpreactor/reactor.pyRW      s    i    c         C   s)   t  j | | | | |   } | j   | S(   s   
        Connects a given L{DatagramProtocol} to the given numeric UDP port.

        @returns: object conforming to L{IListeningPort}.
        (   R   RF   RG   (   R   R   t   protocolRJ   t   maxPacketSizeRK   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/iocpreactor/reactor.pyt	   listenUDP   s    
c         C   s,   t  j | | | | |  |  } | j   | S(   s   
        Connects a given DatagramProtocol to the given numeric UDP port.

        EXPERIMENTAL.

        @returns: object conforming to IListeningPort.
        (   R   t   MulticastPortRG   (   R   R   RY   RJ   RZ   t   listenMultipleRK   (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/iocpreactor/reactor.pyt   listenMulticast   s    		
i    c
   
      C   s   | d k	 r t d   n  | d k	 r6 t d   n  | rK t d   n  |	 d k	 rf t d   n  |  j | |  \ } } t |  | | | | |  S(   s"   
        Spawn a process.
        s,   Setting UID is unsupported on this platform.s,   Setting GID is unsupported on this platform.s&   PTYs are unsupported on this platform.sG   Custom child file descriptor mappings are unsupported on this platform.N(   R   t
   ValueErrort   _checkProcessArgsR   (
   R   t   processProtocolt
   executablet   argst   envt   patht   uidt   gidt   usePTYt   childFDs(    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/iocpreactor/reactor.pyt   spawnProcess   s    c         C   s    t  |  j  } |  j j   | S(   N(   t   listR   t   clear(   R   t   res(    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/iocpreactor/reactor.pyt	   removeAll  s    N(    (   t   __name__t
   __module__R    R   t   IReactorTCPt   IReactorUDPt   IReactorMulticastt   IReactorProcesst   _extraInterfacesR   R   R   R   R   R-   R%   R:   R<   R>   RD   RL   RR   R   RU   RW   R[   RS   R^   Rj   Rn   (    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/iocpreactor/reactor.pyR   1   s4   				,							
	c          C   s   t    }  t j |   d  S(   N(   R   R   t   installReactor(   t   r(    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/iocpreactor/reactor.pyt   install	  s    	Rx   (    (-   t   __doc__t   warningsR?   R3   t   zope.interfaceR    t   twisted.internetR   R   R   R   t   twisted.pythonR   R   t   twisted.internet._dumbwin32procR   t   twisted.internet.iocpreactorR   R   t"   twisted.internet.iocpreactor.constR	   R
   R   t   twisted.protocols.tlsR   t   ImportErrorR   Ru   t   warnt   IReactorSSLt   twisted.python.compatR   R   R&   t   rangeR!   R    t   ConnectionFdescWentAwayR1   R2   t   _SignalReactorMixinR   R   Rx   t   __all__(    (    (    sH   /usr/lib/python2.7/dist-packages/twisted/internet/iocpreactor/reactor.pyt   <module>   s4   $"		