reactor.py 9.2 KB

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