<s> """<STR_LIT>""" <EOL> import warnings , socket , sys <EOL> from zope . interface import implements <EOL> from twisted . internet import base , interfaces , main , error <EOL> from twisted . python import log , failure <EOL> from twisted . internet . _dumbwin32proc import Process <EOL> from twisted . internet . iocpreactor import iocpsupport as _iocp <EOL> from twisted . internet . iocpreactor . const import WAIT_TIMEOUT <EOL> from twisted . internet . iocpreactor import tcp , udp <EOL> try : <EOL> from twisted . protocols . tls import TLSMemoryBIOFactory <EOL> except ImportError : <EOL> TLSMemoryBIOFactory = None <EOL> _extraInterfaces = ( ) <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> _extraInterfaces = ( interfaces . IReactorSSL , ) <EOL> from twisted . python . compat import set <EOL> MAX_TIMEOUT = <NUM_LIT> <EOL> EVENTS_PER_LOOP = <NUM_LIT:1000> <EOL> KEY_NORMAL , KEY_WAKEUP = range ( <NUM_LIT:2> ) <EOL> _NO_GETHANDLE = error . ConnectionFdescWentAway ( <EOL> '<STR_LIT>' ) <EOL> _NO_FILEDESC = error . ConnectionFdescWentAway ( '<STR_LIT>' ) <EOL> class IOCPReactor ( base . _SignalReactorMixin , base . ReactorBase ) : <EOL> implements ( interfaces . IReactorTCP , interfaces . IReactorUDP , <EOL> interfaces . IReactorMulticast , interfaces . IReactorProcess , <EOL> * _extraInterfaces ) <EOL> port = None <EOL> def __init__ ( self ) : <EOL> base . ReactorBase . __init__ ( self ) <EOL> self . port = _iocp . CompletionPort ( ) <EOL> self . handles = set ( ) <EOL> def addActiveHandle ( self , handle ) : <EOL> self . handles . add ( handle ) <EOL> def removeActiveHandle ( self , handle ) : <EOL> self . handles . discard ( handle ) <EOL> def doIteration ( self , timeout ) : <EOL> processed_events = <NUM_LIT:0> <EOL> if timeout is None : <EOL> timeout = MAX_TIMEOUT <EOL> else : <EOL> timeout = min ( MAX_TIMEOUT , int ( <NUM_LIT:1000> * timeout ) ) <EOL> rc , bytes , key , evt = self . port . getEvent ( timeout ) <EOL> while processed_events < EVENTS_PER_LOOP : <EOL> if rc == WAIT_TIMEOUT : <EOL> break <EOL> if key != KEY_WAKEUP : <EOL> assert key == KEY_NORMAL <EOL> if not evt . ignore : <EOL> log . callWithLogger ( evt . owner , self . _callEventCallback , <EOL> rc , bytes , evt ) <EOL> processed_events += <NUM_LIT:1> <EOL> rc , bytes , key , evt = self . port . getEvent ( <NUM_LIT:0> ) <EOL> def _callEventCallback ( self , rc , bytes , evt ) : <EOL> owner = evt . owner <EOL> why = None <EOL> try : <EOL> evt . callback ( rc , bytes , evt ) <EOL> handfn = getattr ( owner , '<STR_LIT>' , None ) <EOL> if not handfn : <EOL> why = _NO_GETHANDLE <EOL> elif handfn ( ) == - <NUM_LIT:1> : <EOL> why = _NO_FILEDESC <EOL> if why : <EOL> return <EOL> except : <EOL> why = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> log . err ( ) <EOL> if why : <EOL> owner . loseConnection ( failure . Failure ( why ) ) <EOL> def installWaker ( self ) : <EOL> pass <EOL> def wakeUp ( self ) : <EOL> self . port . postEvent ( <NUM_LIT:0> , KEY_WAKEUP , None ) <EOL> def registerHandle ( self , handle ) : <EOL> self . port . addHandle ( handle , KEY_NORMAL ) <EOL> def createSocket ( self , af , stype ) : <EOL> skt = socket . socket ( af , stype ) <EOL> self . registerHandle ( skt . fileno ( ) ) <EOL> return skt <EOL> def listenTCP ( self , port , factory , backlog = <NUM_LIT:50> , interface = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> p = tcp . Port ( port , factory , backlog , interface , self ) <EOL> p . startListening ( ) <EOL> return p <EOL> def connectTCP ( self , host , port , factory , timeout = <NUM_LIT:30> , bindAddress = None ) : <EOL> """<STR_LIT>""" <EOL> c = tcp . Connector ( host , port , factory , timeout , bindAddress , self ) <EOL> c . connect ( ) <EOL> return c <EOL> if TLSMemoryBIOFactory is not None : <EOL> def listenSSL ( self , port , factory , contextFactory , backlog = <NUM_LIT:50> , interface = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return self . listenTCP ( <EOL> port , <EOL> TLSMemoryBIOFactory ( contextFactory , False , factory ) , <EOL> backlog , interface ) <EOL> def connectSSL ( self , host , port , factory , contextFactory , timeout = <NUM_LIT:30> , bindAddress = None ) : <EOL> """<STR_LIT>""" <EOL> return self . connectTCP ( <EOL> host , port , <EOL> TLSMemoryBIOFactory ( contextFactory , True , factory ) , <EOL> timeout , bindAddress ) <EOL> else : <EOL> def listenSSL ( self , port , factory , contextFactory , backlog = <NUM_LIT:50> , interface = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def connectSSL ( self , host , port , factory , contextFactory , timeout = <NUM_LIT:30> , bindAddress = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def listenUDP ( self , port , protocol , interface = '<STR_LIT>' , maxPacketSize = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> p = udp . Port ( port , protocol , interface , maxPacketSize , self ) <EOL> p . startListening ( ) <EOL> return p <EOL> def listenMulticast ( self , port , protocol , interface = '<STR_LIT>' , maxPacketSize = <NUM_LIT> , <EOL> listenMultiple = False ) : <EOL> """<STR_LIT>""" <EOL> p = udp . MulticastPort ( port , protocol , interface , maxPacketSize , self , <EOL> listenMultiple ) <EOL> p . startListening ( ) <EOL> return p <EOL> def spawnProcess ( self , processProtocol , executable , args = ( ) , env = { } , <EOL> path = None , uid = None , gid = None , usePTY = <NUM_LIT:0> , childFDs = None ) : <EOL> """<STR_LIT>""" <EOL> if uid is not None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if gid is not None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if usePTY : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if childFDs is not None : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> args , env = self . _checkProcessArgs ( args , env ) <EOL> return Process ( self , processProtocol , executable , args , env , path ) <EOL> def removeAll ( self ) : <EOL> res = list ( self . handles ) <EOL> self . handles . clear ( ) <EOL> return res <EOL> def install ( ) : <EOL> r = IOCPReactor ( ) <EOL> main . installReactor ( r ) <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] </s>
