reactor.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. # -*- test-case-name: twisted.internet.test.test_iocp -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Reactor that uses IO completion ports
  6. """
  7. import warnings, socket, sys
  8. from zope.interface import implementer
  9. from twisted.internet import base, interfaces, main, error
  10. from twisted.python import log, failure
  11. from twisted.internet._dumbwin32proc import Process
  12. from twisted.internet.win32eventreactor import _ThreadedWin32EventsMixin
  13. from twisted.internet.iocpreactor import iocpsupport as _iocp
  14. from twisted.internet.iocpreactor.const import WAIT_TIMEOUT
  15. from twisted.internet.iocpreactor import tcp, udp
  16. try:
  17. from twisted.protocols.tls import TLSMemoryBIOFactory
  18. except ImportError:
  19. # Either pyOpenSSL isn't installed, or it is too old for this code to work.
  20. # The reactor won't provide IReactorSSL.
  21. TLSMemoryBIOFactory = None
  22. _extraInterfaces = ()
  23. warnings.warn(
  24. "pyOpenSSL 0.10 or newer is required for SSL support in iocpreactor. "
  25. "It is missing, so the reactor will not support SSL APIs.")
  26. else:
  27. _extraInterfaces = (interfaces.IReactorSSL,)
  28. MAX_TIMEOUT = 2000 # 2 seconds, see doIteration for explanation
  29. EVENTS_PER_LOOP = 1000 # XXX: what's a good value here?
  30. # keys to associate with normal and waker events
  31. KEY_NORMAL, KEY_WAKEUP = range(2)
  32. _NO_GETHANDLE = error.ConnectionFdescWentAway(
  33. 'Handler has no getFileHandle method')
  34. _NO_FILEDESC = error.ConnectionFdescWentAway('Filedescriptor went away')
  35. @implementer(interfaces.IReactorTCP, interfaces.IReactorUDP,
  36. interfaces.IReactorMulticast, interfaces.IReactorProcess,
  37. *_extraInterfaces)
  38. class IOCPReactor(base._SignalReactorMixin, base.ReactorBase,
  39. _ThreadedWin32EventsMixin):
  40. port = None
  41. def __init__(self):
  42. base.ReactorBase.__init__(self)
  43. self.port = _iocp.CompletionPort()
  44. self.handles = set()
  45. def addActiveHandle(self, handle):
  46. self.handles.add(handle)
  47. def removeActiveHandle(self, handle):
  48. self.handles.discard(handle)
  49. def doIteration(self, timeout):
  50. """
  51. Poll the IO completion port for new events.
  52. """
  53. # This function sits and waits for an IO completion event.
  54. #
  55. # There are two requirements: process IO events as soon as they arrive
  56. # and process ctrl-break from the user in a reasonable amount of time.
  57. #
  58. # There are three kinds of waiting.
  59. # 1) GetQueuedCompletionStatus (self.port.getEvent) to wait for IO
  60. # events only.
  61. # 2) Msg* family of wait functions that can stop waiting when
  62. # ctrl-break is detected (then, I think, Python converts it into a
  63. # KeyboardInterrupt)
  64. # 3) *Ex family of wait functions that put the thread into an
  65. # "alertable" wait state which is supposedly triggered by IO completion
  66. #
  67. # 2) and 3) can be combined. Trouble is, my IO completion is not
  68. # causing 3) to trigger, possibly because I do not use an IO completion
  69. # callback. Windows is weird.
  70. # There are two ways to handle this. I could use MsgWaitForSingleObject
  71. # here and GetQueuedCompletionStatus in a thread. Or I could poll with
  72. # a reasonable interval. Guess what! Threads are hard.
  73. processed_events = 0
  74. if timeout is None:
  75. timeout = MAX_TIMEOUT
  76. else:
  77. timeout = min(MAX_TIMEOUT, int(1000*timeout))
  78. rc, numBytes, key, evt = self.port.getEvent(timeout)
  79. while 1:
  80. if rc == WAIT_TIMEOUT:
  81. break
  82. if key != KEY_WAKEUP:
  83. assert key == KEY_NORMAL
  84. log.callWithLogger(evt.owner, self._callEventCallback,
  85. rc, numBytes, evt)
  86. processed_events += 1
  87. if processed_events >= EVENTS_PER_LOOP:
  88. break
  89. rc, numBytes, key, evt = self.port.getEvent(0)
  90. def _callEventCallback(self, rc, numBytes, evt):
  91. owner = evt.owner
  92. why = None
  93. try:
  94. evt.callback(rc, numBytes, evt)
  95. handfn = getattr(owner, 'getFileHandle', None)
  96. if not handfn:
  97. why = _NO_GETHANDLE
  98. elif handfn() == -1:
  99. why = _NO_FILEDESC
  100. if why:
  101. return # ignore handles that were closed
  102. except:
  103. why = sys.exc_info()[1]
  104. log.err()
  105. if why:
  106. owner.loseConnection(failure.Failure(why))
  107. def installWaker(self):
  108. pass
  109. def wakeUp(self):
  110. self.port.postEvent(0, KEY_WAKEUP, None)
  111. def registerHandle(self, handle):
  112. self.port.addHandle(handle, KEY_NORMAL)
  113. def createSocket(self, af, stype):
  114. skt = socket.socket(af, stype)
  115. self.registerHandle(skt.fileno())
  116. return skt
  117. def listenTCP(self, port, factory, backlog=50, interface=''):
  118. """
  119. @see: twisted.internet.interfaces.IReactorTCP.listenTCP
  120. """
  121. p = tcp.Port(port, factory, backlog, interface, self)
  122. p.startListening()
  123. return p
  124. def connectTCP(self, host, port, factory, timeout=30, bindAddress=None):
  125. """
  126. @see: twisted.internet.interfaces.IReactorTCP.connectTCP
  127. """
  128. c = tcp.Connector(host, port, factory, timeout, bindAddress, self)
  129. c.connect()
  130. return c
  131. if TLSMemoryBIOFactory is not None:
  132. def listenSSL(self, port, factory, contextFactory, backlog=50, interface=''):
  133. """
  134. @see: twisted.internet.interfaces.IReactorSSL.listenSSL
  135. """
  136. port = self.listenTCP(
  137. port,
  138. TLSMemoryBIOFactory(contextFactory, False, factory),
  139. backlog, interface)
  140. port._type = 'TLS'
  141. return port
  142. def connectSSL(self, host, port, factory, contextFactory, timeout=30, bindAddress=None):
  143. """
  144. @see: twisted.internet.interfaces.IReactorSSL.connectSSL
  145. """
  146. return self.connectTCP(
  147. host, port,
  148. TLSMemoryBIOFactory(contextFactory, True, factory),
  149. timeout, bindAddress)
  150. else:
  151. def listenSSL(self, port, factory, contextFactory, backlog=50, interface=''):
  152. """
  153. Non-implementation of L{IReactorSSL.listenSSL}. Some dependency
  154. is not satisfied. This implementation always raises
  155. L{NotImplementedError}.
  156. """
  157. raise NotImplementedError(
  158. "pyOpenSSL 0.10 or newer is required for SSL support in "
  159. "iocpreactor. It is missing, so the reactor does not support "
  160. "SSL APIs.")
  161. def connectSSL(self, host, port, factory, contextFactory, timeout=30, bindAddress=None):
  162. """
  163. Non-implementation of L{IReactorSSL.connectSSL}. Some dependency
  164. is not satisfied. This implementation always raises
  165. L{NotImplementedError}.
  166. """
  167. raise NotImplementedError(
  168. "pyOpenSSL 0.10 or newer is required for SSL support in "
  169. "iocpreactor. It is missing, so the reactor does not support "
  170. "SSL APIs.")
  171. def listenUDP(self, port, protocol, interface='', maxPacketSize=8192):
  172. """
  173. Connects a given L{DatagramProtocol} to the given numeric UDP port.
  174. @returns: object conforming to L{IListeningPort}.
  175. """
  176. p = udp.Port(port, protocol, interface, maxPacketSize, self)
  177. p.startListening()
  178. return p
  179. def listenMulticast(self, port, protocol, interface='', maxPacketSize=8192,
  180. listenMultiple=False):
  181. """
  182. Connects a given DatagramProtocol to the given numeric UDP port.
  183. EXPERIMENTAL.
  184. @returns: object conforming to IListeningPort.
  185. """
  186. p = udp.MulticastPort(port, protocol, interface, maxPacketSize, self,
  187. listenMultiple)
  188. p.startListening()
  189. return p
  190. def spawnProcess(self, processProtocol, executable, args=(), env={},
  191. path=None, uid=None, gid=None, usePTY=0, childFDs=None):
  192. """
  193. Spawn a process.
  194. """
  195. if uid is not None:
  196. raise ValueError("Setting UID is unsupported on this platform.")
  197. if gid is not None:
  198. raise ValueError("Setting GID is unsupported on this platform.")
  199. if usePTY:
  200. raise ValueError("PTYs are unsupported on this platform.")
  201. if childFDs is not None:
  202. raise ValueError(
  203. "Custom child file descriptor mappings are unsupported on "
  204. "this platform.")
  205. args, env = self._checkProcessArgs(args, env)
  206. return Process(self, processProtocol, executable, args, env, path)
  207. def removeAll(self):
  208. res = list(self.handles)
  209. self.handles.clear()
  210. return res
  211. def install():
  212. r = IOCPReactor()
  213. main.installReactor(r)
  214. __all__ = ['IOCPReactor', 'install']