<s> """<STR_LIT>""" <EOL> import time <EOL> import sys <EOL> from threading import Thread <EOL> from weakref import WeakKeyDictionary <EOL> from zope . interface import implementer <EOL> from win32file import FD_READ , FD_CLOSE , FD_ACCEPT , FD_CONNECT , WSAEventSelect <EOL> try : <EOL> from win32file import WSAEnumNetworkEvents <EOL> except ImportError : <EOL> import warnings <EOL> warnings . warn ( <EOL> '<STR_LIT>' , <EOL> category = UserWarning ) <EOL> def WSAEnumNetworkEvents ( fd , event ) : <EOL> return set ( [ FD_READ ] ) <EOL> from win32event import CreateEvent , MsgWaitForMultipleObjects <EOL> from win32event import WAIT_OBJECT_0 , WAIT_TIMEOUT , QS_ALLINPUT <EOL> import win32gui <EOL> from twisted . internet import posixbase <EOL> from twisted . python import log , threadable , failure <EOL> from twisted . internet . interfaces import IReactorFDSet <EOL> from twisted . internet . interfaces import IReactorWin32Events <EOL> from twisted . internet . threads import blockingCallFromThread <EOL> @ implementer ( IReactorFDSet , IReactorWin32Events ) <EOL> class Win32Reactor ( posixbase . PosixReactorBase ) : <EOL> """<STR_LIT>""" <EOL> dummyEvent = CreateEvent ( None , <NUM_LIT:0> , <NUM_LIT:0> , None ) <EOL> def __init__ ( self ) : <EOL> self . _reads = { } <EOL> self . _writes = { } <EOL> self . _events = { } <EOL> self . _closedAndReading = { } <EOL> self . _closedAndNotReading = WeakKeyDictionary ( ) <EOL> posixbase . PosixReactorBase . __init__ ( self ) <EOL> def _makeSocketEvent ( self , fd , action , why ) : <EOL> """<STR_LIT>""" <EOL> event = CreateEvent ( None , <NUM_LIT:0> , <NUM_LIT:0> , None ) <EOL> WSAEventSelect ( fd , event , why ) <EOL> self . _events [ event ] = ( fd , action ) <EOL> return event <EOL> def addEvent ( self , event , fd , action ) : <EOL> """<STR_LIT>""" <EOL> self . _events [ event ] = ( fd , action ) <EOL> def removeEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> del self . _events [ event ] <EOL> def addReader ( self , reader ) : <EOL> """<STR_LIT>""" <EOL> if reader not in self . _reads : <EOL> self . _reads [ reader ] = self . _makeSocketEvent ( <EOL> reader , '<STR_LIT>' , FD_READ | FD_ACCEPT | FD_CONNECT | FD_CLOSE ) <EOL> if reader in self . _closedAndNotReading : <EOL> self . _closedAndReading [ reader ] = True <EOL> del self . _closedAndNotReading [ reader ] <EOL> def addWriter ( self , writer ) : <EOL> """<STR_LIT>""" <EOL> if writer not in self . _writes : <EOL> self . _writes [ writer ] = <NUM_LIT:1> <EOL> def removeReader ( self , reader ) : <EOL> """<STR_LIT>""" <EOL> if reader in self . _reads : <EOL> del self . _events [ self . _reads [ reader ] ] <EOL> del self . _reads [ reader ] <EOL> if reader in self . _closedAndReading : <EOL> self . _closedAndNotReading [ reader ] = True <EOL> del self . _closedAndReading [ reader ] <EOL> def removeWriter ( self , writer ) : <EOL> """<STR_LIT>""" <EOL> if writer in self . _writes : <EOL> del self . _writes [ writer ] <EOL> def removeAll ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _removeAll ( self . _reads , self . _writes ) <EOL> def getReaders ( self ) : <EOL> return self . _reads . keys ( ) <EOL> def getWriters ( self ) : <EOL> return self . _writes . keys ( ) <EOL> def doWaitForMultipleEvents ( self , timeout ) : <EOL> log . msg ( channel = '<STR_LIT>' , event = '<STR_LIT>' , reactor = self ) <EOL> if timeout is None : <EOL> timeout = <NUM_LIT:100> <EOL> ranUserCode = False <EOL> for reader in self . _closedAndReading . keys ( ) : <EOL> ranUserCode = True <EOL> self . _runAction ( '<STR_LIT>' , reader ) <EOL> for fd in self . _writes . keys ( ) : <EOL> ranUserCode = True <EOL> log . callWithLogger ( fd , self . _runWrite , fd ) <EOL> if ranUserCode : <EOL> timeout = <NUM_LIT:0> <EOL> if not ( self . _events or self . _writes ) : <EOL> time . sleep ( timeout ) <EOL> return <EOL> handles = self . _events . keys ( ) or [ self . dummyEvent ] <EOL> timeout = int ( timeout * <NUM_LIT:1000> ) <EOL> val = MsgWaitForMultipleObjects ( handles , <NUM_LIT:0> , timeout , QS_ALLINPUT ) <EOL> if val == WAIT_TIMEOUT : <EOL> return <EOL> elif val == WAIT_OBJECT_0 + len ( handles ) : <EOL> exit = win32gui . PumpWaitingMessages ( ) <EOL> if exit : <EOL> self . callLater ( <NUM_LIT:0> , self . stop ) <EOL> return <EOL> elif val >= WAIT_OBJECT_0 and val < WAIT_OBJECT_0 + len ( handles ) : <EOL> event = handles [ val - WAIT_OBJECT_0 ] <EOL> fd , action = self . _events [ event ] <EOL> if fd in self . _reads : <EOL> fileno = fd . fileno ( ) <EOL> if fileno == - <NUM_LIT:1> : <EOL> self . _disconnectSelectable ( fd , posixbase . _NO_FILEDESC , False ) <EOL> return <EOL> events = WSAEnumNetworkEvents ( fileno , event ) <EOL> if FD_CLOSE in events : <EOL> self . _closedAndReading [ fd ] = True <EOL> log . callWithLogger ( fd , self . _runAction , action , fd ) <EOL> def _runWrite ( self , fd ) : <EOL> closed = <NUM_LIT:0> <EOL> try : <EOL> closed = fd . doWrite ( ) <EOL> except : <EOL> closed = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> log . deferr ( ) <EOL> if closed : <EOL> self . removeReader ( fd ) <EOL> self . removeWriter ( fd ) <EOL> try : <EOL> fd . connectionLost ( failure . Failure ( closed ) ) <EOL> except : <EOL> log . deferr ( ) <EOL> elif closed is None : <EOL> return <NUM_LIT:1> <EOL> def _runAction ( self , action , fd ) : <EOL> try : <EOL> closed = getattr ( fd , action ) ( ) <EOL> except : <EOL> closed = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> log . deferr ( ) <EOL> if closed : <EOL> self . _disconnectSelectable ( fd , closed , action == '<STR_LIT>' ) <EOL> doIteration = doWaitForMultipleEvents <EOL> class _ThreadFDWrapper ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , reactor , fd , action , logPrefix ) : <EOL> self . _reactor = reactor <EOL> self . _fd = fd <EOL> self . _action = action <EOL> self . _logPrefix = logPrefix <EOL> def logPrefix ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _logPrefix <EOL> def _execute ( self ) : <EOL> """<STR_LIT>""" <EOL> return blockingCallFromThread ( <EOL> self . _reactor , lambda : getattr ( self . _fd , self . _action ) ( ) ) <EOL> def connectionLost ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> self . _reactor . callFromThread ( self . _fd . connectionLost , reason ) <EOL> @ implementer ( IReactorWin32Events ) <EOL> class _ThreadedWin32EventsMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> _reactor = None <EOL> _reactorThread = None <EOL> def _unmakeHelperReactor ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _reactor . callFromThread ( self . _reactor . stop ) <EOL> self . _reactor = None <EOL> def _makeHelperReactor ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _reactor = Win32Reactor ( ) <EOL> self . _reactor . _registerAsIOThread = False <EOL> self . _reactorThread = Thread ( <EOL> target = self . _reactor . run , args = ( False , ) ) <EOL> self . addSystemEventTrigger ( <EOL> '<STR_LIT>' , '<STR_LIT>' , self . _unmakeHelperReactor ) <EOL> self . _reactorThread . start ( ) <EOL> def addEvent ( self , event , fd , action ) : <EOL> """<STR_LIT>""" <EOL> if self . _reactor is None : <EOL> self . _makeHelperReactor ( ) <EOL> self . _reactor . callFromThread ( <EOL> self . _reactor . addEvent , <EOL> event , _ThreadFDWrapper ( self , fd , action , fd . logPrefix ( ) ) , <EOL> "<STR_LIT>" ) <EOL> def removeEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> self . _reactor . callFromThread ( self . _reactor . removeEvent , event ) <EOL> def install ( ) : <EOL> threadable . init ( <NUM_LIT:1> ) <EOL> r = Win32Reactor ( ) <EOL> import main <EOL> main . installReactor ( r ) <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" ] </s>
