Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cfreactor.py 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. # -*- test-case-name: twisted.internet.test.test_core -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. A reactor for integrating with U{CFRunLoop<http://bit.ly/cfrunloop>}, the
  6. CoreFoundation main loop used by macOS.
  7. This is useful for integrating Twisted with U{PyObjC<http://pyobjc.sf.net/>}
  8. applications.
  9. """
  10. __all__ = ["install", "CFReactor"]
  11. import sys
  12. from zope.interface import implementer
  13. from CFNetwork import ( # type: ignore[import]
  14. CFSocketCreateRunLoopSource,
  15. CFSocketCreateWithNative,
  16. CFSocketDisableCallBacks,
  17. CFSocketEnableCallBacks,
  18. CFSocketInvalidate,
  19. CFSocketSetSocketFlags,
  20. kCFSocketAutomaticallyReenableReadCallBack,
  21. kCFSocketAutomaticallyReenableWriteCallBack,
  22. kCFSocketConnectCallBack,
  23. kCFSocketReadCallBack,
  24. kCFSocketWriteCallBack,
  25. )
  26. from CoreFoundation import ( # type: ignore[import]
  27. CFAbsoluteTimeGetCurrent,
  28. CFRunLoopAddSource,
  29. CFRunLoopAddTimer,
  30. CFRunLoopGetMain,
  31. CFRunLoopRemoveSource,
  32. CFRunLoopRun,
  33. CFRunLoopStop,
  34. CFRunLoopTimerCreate,
  35. CFRunLoopTimerInvalidate,
  36. kCFAllocatorDefault,
  37. kCFRunLoopCommonModes,
  38. )
  39. from twisted.internet.interfaces import IReactorFDSet
  40. from twisted.internet.posixbase import _NO_FILEDESC, PosixReactorBase
  41. from twisted.python import log
  42. # We know that we're going to run on macOS so we can just pick the
  43. # POSIX-appropriate waker. This also avoids having a dynamic base class and
  44. # so lets more things get type checked.
  45. from ._signals import _UnixWaker
  46. _READ = 0
  47. _WRITE = 1
  48. _preserveSOError = 1 << 6
  49. class _WakerPlus(_UnixWaker):
  50. """
  51. The normal Twisted waker will simply wake up the main loop, which causes an
  52. iteration to run, which in turn causes L{ReactorBase.runUntilCurrent}
  53. to get invoked.
  54. L{CFReactor} has a slightly different model of iteration, though: rather
  55. than have each iteration process the thread queue, then timed calls, then
  56. file descriptors, each callback is run as it is dispatched by the CFRunLoop
  57. observer which triggered it.
  58. So this waker needs to not only unblock the loop, but also make sure the
  59. work gets done; so, it reschedules the invocation of C{runUntilCurrent} to
  60. be immediate (0 seconds from now) even if there is no timed call work to
  61. do.
  62. """
  63. def doRead(self):
  64. """
  65. Wake up the loop and force C{runUntilCurrent} to run immediately in the
  66. next timed iteration.
  67. """
  68. result = super().doRead()
  69. self.reactor._scheduleSimulate(True)
  70. return result
  71. @implementer(IReactorFDSet)
  72. class CFReactor(PosixReactorBase):
  73. """
  74. The CoreFoundation reactor.
  75. You probably want to use this via the L{install} API.
  76. @ivar _fdmap: a dictionary, mapping an integer (a file descriptor) to a
  77. 4-tuple of:
  78. - source: a C{CFRunLoopSource}; the source associated with this
  79. socket.
  80. - socket: a C{CFSocket} wrapping the file descriptor.
  81. - descriptor: an L{IReadDescriptor} and/or L{IWriteDescriptor}
  82. provider.
  83. - read-write: a 2-C{list} of booleans: respectively, whether this
  84. descriptor is currently registered for reading or registered for
  85. writing.
  86. @ivar _idmap: a dictionary, mapping the id() of an L{IReadDescriptor} or
  87. L{IWriteDescriptor} to a C{fd} in L{_fdmap}. Implemented in this
  88. manner so that we don't have to rely (even more) on the hashability of
  89. L{IReadDescriptor} providers, and we know that they won't be collected
  90. since these are kept in sync with C{_fdmap}. Necessary because the
  91. .fileno() of a file descriptor may change at will, so we need to be
  92. able to look up what its file descriptor I{used} to be, so that we can
  93. look it up in C{_fdmap}
  94. @ivar _cfrunloop: the C{CFRunLoop} pyobjc object wrapped
  95. by this reactor.
  96. @ivar _inCFLoop: Is C{CFRunLoopRun} currently running?
  97. @type _inCFLoop: L{bool}
  98. @ivar _currentSimulator: if a CFTimer is currently scheduled with the CF
  99. run loop to run Twisted callLater calls, this is a reference to it.
  100. Otherwise, it is L{None}
  101. """
  102. def __init__(self, runLoop=None, runner=None):
  103. self._fdmap = {}
  104. self._idmap = {}
  105. if runner is None:
  106. runner = CFRunLoopRun
  107. self._runner = runner
  108. if runLoop is None:
  109. runLoop = CFRunLoopGetMain()
  110. self._cfrunloop = runLoop
  111. PosixReactorBase.__init__(self)
  112. def installWaker(self):
  113. """
  114. Override C{installWaker} in order to use L{_WakerPlus}; otherwise this
  115. should be exactly the same as the parent implementation.
  116. """
  117. if not self.waker:
  118. self.waker = _WakerPlus(self)
  119. self._internalReaders.add(self.waker)
  120. self.addReader(self.waker)
  121. def _socketCallback(
  122. self, cfSocket, callbackType, ignoredAddress, ignoredData, context
  123. ):
  124. """
  125. The socket callback issued by CFRunLoop. This will issue C{doRead} or
  126. C{doWrite} calls to the L{IReadDescriptor} and L{IWriteDescriptor}
  127. registered with the file descriptor that we are being notified of.
  128. @param cfSocket: The C{CFSocket} which has got some activity.
  129. @param callbackType: The type of activity that we are being notified
  130. of. Either C{kCFSocketReadCallBack} or C{kCFSocketWriteCallBack}.
  131. @param ignoredAddress: Unused, because this is not used for either of
  132. the callback types we register for.
  133. @param ignoredData: Unused, because this is not used for either of the
  134. callback types we register for.
  135. @param context: The data associated with this callback by
  136. C{CFSocketCreateWithNative} (in C{CFReactor._watchFD}). A 2-tuple
  137. of C{(int, CFRunLoopSource)}.
  138. """
  139. (fd, smugglesrc) = context
  140. if fd not in self._fdmap:
  141. # Spurious notifications seem to be generated sometimes if you
  142. # CFSocketDisableCallBacks in the middle of an event. I don't know
  143. # about this FD, any more, so let's get rid of it.
  144. CFRunLoopRemoveSource(self._cfrunloop, smugglesrc, kCFRunLoopCommonModes)
  145. return
  146. src, skt, readWriteDescriptor, rw = self._fdmap[fd]
  147. def _drdw():
  148. why = None
  149. isRead = False
  150. try:
  151. if readWriteDescriptor.fileno() == -1:
  152. why = _NO_FILEDESC
  153. else:
  154. isRead = callbackType == kCFSocketReadCallBack
  155. # CFSocket seems to deliver duplicate read/write
  156. # notifications sometimes, especially a duplicate
  157. # writability notification when first registering the
  158. # socket. This bears further investigation, since I may
  159. # have been mis-interpreting the behavior I was seeing.
  160. # (Running the full Twisted test suite, while thorough, is
  161. # not always entirely clear.) Until this has been more
  162. # thoroughly investigated , we consult our own
  163. # reading/writing state flags to determine whether we
  164. # should actually attempt a doRead/doWrite first. -glyph
  165. if isRead:
  166. if rw[_READ]:
  167. why = readWriteDescriptor.doRead()
  168. else:
  169. if rw[_WRITE]:
  170. why = readWriteDescriptor.doWrite()
  171. except BaseException:
  172. why = sys.exc_info()[1]
  173. log.err()
  174. if why:
  175. self._disconnectSelectable(readWriteDescriptor, why, isRead)
  176. log.callWithLogger(readWriteDescriptor, _drdw)
  177. def _watchFD(self, fd, descr, flag):
  178. """
  179. Register a file descriptor with the C{CFRunLoop}, or modify its state
  180. so that it's listening for both notifications (read and write) rather
  181. than just one; used to implement C{addReader} and C{addWriter}.
  182. @param fd: The file descriptor.
  183. @type fd: L{int}
  184. @param descr: the L{IReadDescriptor} or L{IWriteDescriptor}
  185. @param flag: the flag to register for callbacks on, either
  186. C{kCFSocketReadCallBack} or C{kCFSocketWriteCallBack}
  187. """
  188. if fd == -1:
  189. raise RuntimeError("Invalid file descriptor.")
  190. if fd in self._fdmap:
  191. src, cfs, gotdescr, rw = self._fdmap[fd]
  192. # do I need to verify that it's the same descr?
  193. else:
  194. ctx = []
  195. ctx.append(fd)
  196. cfs = CFSocketCreateWithNative(
  197. kCFAllocatorDefault,
  198. fd,
  199. kCFSocketReadCallBack
  200. | kCFSocketWriteCallBack
  201. | kCFSocketConnectCallBack,
  202. self._socketCallback,
  203. ctx,
  204. )
  205. CFSocketSetSocketFlags(
  206. cfs,
  207. kCFSocketAutomaticallyReenableReadCallBack
  208. | kCFSocketAutomaticallyReenableWriteCallBack
  209. |
  210. # This extra flag is to ensure that CF doesn't (destructively,
  211. # because destructively is the only way to do it) retrieve
  212. # SO_ERROR and thereby break twisted.internet.tcp.BaseClient,
  213. # which needs SO_ERROR to tell it whether or not it needs to
  214. # call connect_ex a second time.
  215. _preserveSOError,
  216. )
  217. src = CFSocketCreateRunLoopSource(kCFAllocatorDefault, cfs, 0)
  218. ctx.append(src)
  219. CFRunLoopAddSource(self._cfrunloop, src, kCFRunLoopCommonModes)
  220. CFSocketDisableCallBacks(
  221. cfs,
  222. kCFSocketReadCallBack
  223. | kCFSocketWriteCallBack
  224. | kCFSocketConnectCallBack,
  225. )
  226. rw = [False, False]
  227. self._idmap[id(descr)] = fd
  228. self._fdmap[fd] = src, cfs, descr, rw
  229. rw[self._flag2idx(flag)] = True
  230. CFSocketEnableCallBacks(cfs, flag)
  231. def _flag2idx(self, flag):
  232. """
  233. Convert a C{kCFSocket...} constant to an index into the read/write
  234. state list (C{_READ} or C{_WRITE}) (the 4th element of the value of
  235. C{self._fdmap}).
  236. @param flag: C{kCFSocketReadCallBack} or C{kCFSocketWriteCallBack}
  237. @return: C{_READ} or C{_WRITE}
  238. """
  239. return {kCFSocketReadCallBack: _READ, kCFSocketWriteCallBack: _WRITE}[flag]
  240. def _unwatchFD(self, fd, descr, flag):
  241. """
  242. Unregister a file descriptor with the C{CFRunLoop}, or modify its state
  243. so that it's listening for only one notification (read or write) as
  244. opposed to both; used to implement C{removeReader} and C{removeWriter}.
  245. @param fd: a file descriptor
  246. @type fd: C{int}
  247. @param descr: an L{IReadDescriptor} or L{IWriteDescriptor}
  248. @param flag: C{kCFSocketWriteCallBack} C{kCFSocketReadCallBack}
  249. """
  250. if id(descr) not in self._idmap:
  251. return
  252. if fd == -1:
  253. # need to deal with it in this case, I think.
  254. realfd = self._idmap[id(descr)]
  255. else:
  256. realfd = fd
  257. src, cfs, descr, rw = self._fdmap[realfd]
  258. CFSocketDisableCallBacks(cfs, flag)
  259. rw[self._flag2idx(flag)] = False
  260. if not rw[_READ] and not rw[_WRITE]:
  261. del self._idmap[id(descr)]
  262. del self._fdmap[realfd]
  263. CFRunLoopRemoveSource(self._cfrunloop, src, kCFRunLoopCommonModes)
  264. CFSocketInvalidate(cfs)
  265. def addReader(self, reader):
  266. """
  267. Implement L{IReactorFDSet.addReader}.
  268. """
  269. self._watchFD(reader.fileno(), reader, kCFSocketReadCallBack)
  270. def addWriter(self, writer):
  271. """
  272. Implement L{IReactorFDSet.addWriter}.
  273. """
  274. self._watchFD(writer.fileno(), writer, kCFSocketWriteCallBack)
  275. def removeReader(self, reader):
  276. """
  277. Implement L{IReactorFDSet.removeReader}.
  278. """
  279. self._unwatchFD(reader.fileno(), reader, kCFSocketReadCallBack)
  280. def removeWriter(self, writer):
  281. """
  282. Implement L{IReactorFDSet.removeWriter}.
  283. """
  284. self._unwatchFD(writer.fileno(), writer, kCFSocketWriteCallBack)
  285. def removeAll(self):
  286. """
  287. Implement L{IReactorFDSet.removeAll}.
  288. """
  289. allDesc = {descr for src, cfs, descr, rw in self._fdmap.values()}
  290. allDesc -= set(self._internalReaders)
  291. for desc in allDesc:
  292. self.removeReader(desc)
  293. self.removeWriter(desc)
  294. return list(allDesc)
  295. def getReaders(self):
  296. """
  297. Implement L{IReactorFDSet.getReaders}.
  298. """
  299. return [descr for src, cfs, descr, rw in self._fdmap.values() if rw[_READ]]
  300. def getWriters(self):
  301. """
  302. Implement L{IReactorFDSet.getWriters}.
  303. """
  304. return [descr for src, cfs, descr, rw in self._fdmap.values() if rw[_WRITE]]
  305. def _moveCallLaterSooner(self, tple):
  306. """
  307. Override L{PosixReactorBase}'s implementation of L{IDelayedCall.reset}
  308. so that it will immediately reschedule. Normally
  309. C{_moveCallLaterSooner} depends on the fact that C{runUntilCurrent} is
  310. always run before the mainloop goes back to sleep, so this forces it to
  311. immediately recompute how long the loop needs to stay asleep.
  312. """
  313. result = PosixReactorBase._moveCallLaterSooner(self, tple)
  314. self._scheduleSimulate()
  315. return result
  316. _inCFLoop = False
  317. def mainLoop(self):
  318. """
  319. Run the runner (C{CFRunLoopRun} or something that calls it), which runs
  320. the run loop until C{crash()} is called.
  321. """
  322. self._inCFLoop = True
  323. try:
  324. self._runner()
  325. finally:
  326. self._inCFLoop = False
  327. _currentSimulator = None
  328. def _scheduleSimulate(self, force=False):
  329. """
  330. Schedule a call to C{self.runUntilCurrent}. This will cancel the
  331. currently scheduled call if it is already scheduled.
  332. @param force: Even if there are no timed calls, make sure that
  333. C{runUntilCurrent} runs immediately (in a 0-seconds-from-now
  334. C{CFRunLoopTimer}). This is necessary for calls which need to
  335. trigger behavior of C{runUntilCurrent} other than running timed
  336. calls, such as draining the thread call queue or calling C{crash()}
  337. when the appropriate flags are set.
  338. @type force: C{bool}
  339. """
  340. if self._currentSimulator is not None:
  341. CFRunLoopTimerInvalidate(self._currentSimulator)
  342. self._currentSimulator = None
  343. timeout = self.timeout()
  344. if force:
  345. timeout = 0.0
  346. if timeout is not None:
  347. fireDate = CFAbsoluteTimeGetCurrent() + timeout
  348. def simulate(cftimer, extra):
  349. self._currentSimulator = None
  350. self.runUntilCurrent()
  351. self._scheduleSimulate()
  352. c = self._currentSimulator = CFRunLoopTimerCreate(
  353. kCFAllocatorDefault, fireDate, 0, 0, 0, simulate, None
  354. )
  355. CFRunLoopAddTimer(self._cfrunloop, c, kCFRunLoopCommonModes)
  356. def callLater(self, _seconds, _f, *args, **kw):
  357. """
  358. Implement L{IReactorTime.callLater}.
  359. """
  360. delayedCall = PosixReactorBase.callLater(self, _seconds, _f, *args, **kw)
  361. self._scheduleSimulate()
  362. return delayedCall
  363. def stop(self):
  364. """
  365. Implement L{IReactorCore.stop}.
  366. """
  367. PosixReactorBase.stop(self)
  368. self._scheduleSimulate(True)
  369. def crash(self):
  370. """
  371. Implement L{IReactorCore.crash}
  372. """
  373. wasStarted = self._started
  374. PosixReactorBase.crash(self)
  375. if self._inCFLoop:
  376. self._stopNow()
  377. else:
  378. if wasStarted:
  379. self.callLater(0, self._stopNow)
  380. def _stopNow(self):
  381. """
  382. Immediately stop the CFRunLoop (which must be running!).
  383. """
  384. CFRunLoopStop(self._cfrunloop)
  385. def iterate(self, delay=0):
  386. """
  387. Emulate the behavior of C{iterate()} for things that want to call it,
  388. by letting the loop run for a little while and then scheduling a timed
  389. call to exit it.
  390. """
  391. self.callLater(delay, self._stopNow)
  392. self.mainLoop()
  393. def install(runLoop=None, runner=None):
  394. """
  395. Configure the twisted mainloop to be run inside CFRunLoop.
  396. @param runLoop: the run loop to use.
  397. @param runner: the function to call in order to actually invoke the main
  398. loop. This will default to C{CFRunLoopRun} if not specified. However,
  399. this is not an appropriate choice for GUI applications, as you need to
  400. run NSApplicationMain (or something like it). For example, to run the
  401. Twisted mainloop in a PyObjC application, your C{main.py} should look
  402. something like this::
  403. from PyObjCTools import AppHelper
  404. from twisted.internet.cfreactor import install
  405. install(runner=AppHelper.runEventLoop)
  406. # initialize your application
  407. reactor.run()
  408. @return: The installed reactor.
  409. @rtype: C{CFReactor}
  410. """
  411. reactor = CFReactor(runLoop=runLoop, runner=runner)
  412. from twisted.internet.main import installReactor
  413. installReactor(reactor)
  414. return reactor