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.

_win32stdio.py 3.1KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # -*- test-case-name: twisted.test.test_stdio -*-
  2. """
  3. Windows-specific implementation of the L{twisted.internet.stdio} interface.
  4. """
  5. import msvcrt
  6. import os
  7. from zope.interface import implementer
  8. import win32api # type: ignore[import]
  9. from twisted.internet import _pollingfile, main
  10. from twisted.internet.interfaces import (
  11. IAddress,
  12. IConsumer,
  13. IHalfCloseableProtocol,
  14. IPushProducer,
  15. ITransport,
  16. )
  17. from twisted.python.failure import Failure
  18. @implementer(IAddress)
  19. class Win32PipeAddress:
  20. pass
  21. @implementer(ITransport, IConsumer, IPushProducer)
  22. class StandardIO(_pollingfile._PollingTimer):
  23. disconnecting = False
  24. disconnected = False
  25. def __init__(self, proto, reactor=None):
  26. """
  27. Start talking to standard IO with the given protocol.
  28. Also, put it stdin/stdout/stderr into binary mode.
  29. """
  30. if reactor is None:
  31. from twisted.internet import reactor
  32. for stdfd in range(0, 1, 2):
  33. msvcrt.setmode(stdfd, os.O_BINARY)
  34. _pollingfile._PollingTimer.__init__(self, reactor)
  35. self.proto = proto
  36. hstdin = win32api.GetStdHandle(win32api.STD_INPUT_HANDLE)
  37. hstdout = win32api.GetStdHandle(win32api.STD_OUTPUT_HANDLE)
  38. self.stdin = _pollingfile._PollableReadPipe(
  39. hstdin, self.dataReceived, self.readConnectionLost
  40. )
  41. self.stdout = _pollingfile._PollableWritePipe(hstdout, self.writeConnectionLost)
  42. self._addPollableResource(self.stdin)
  43. self._addPollableResource(self.stdout)
  44. self.proto.makeConnection(self)
  45. def dataReceived(self, data):
  46. self.proto.dataReceived(data)
  47. def readConnectionLost(self):
  48. if IHalfCloseableProtocol.providedBy(self.proto):
  49. self.proto.readConnectionLost()
  50. self.checkConnLost()
  51. def writeConnectionLost(self):
  52. if IHalfCloseableProtocol.providedBy(self.proto):
  53. self.proto.writeConnectionLost()
  54. self.checkConnLost()
  55. connsLost = 0
  56. def checkConnLost(self):
  57. self.connsLost += 1
  58. if self.connsLost >= 2:
  59. self.disconnecting = True
  60. self.disconnected = True
  61. self.proto.connectionLost(Failure(main.CONNECTION_DONE))
  62. # ITransport
  63. def write(self, data):
  64. self.stdout.write(data)
  65. def writeSequence(self, seq):
  66. self.stdout.write(b"".join(seq))
  67. def loseConnection(self):
  68. self.disconnecting = True
  69. self.stdin.close()
  70. self.stdout.close()
  71. def getPeer(self):
  72. return Win32PipeAddress()
  73. def getHost(self):
  74. return Win32PipeAddress()
  75. # IConsumer
  76. def registerProducer(self, producer, streaming):
  77. return self.stdout.registerProducer(producer, streaming)
  78. def unregisterProducer(self):
  79. return self.stdout.unregisterProducer()
  80. # def write() above
  81. # IProducer
  82. def stopProducing(self):
  83. self.stdin.stopProducing()
  84. # IPushProducer
  85. def pauseProducing(self):
  86. self.stdin.pauseProducing()
  87. def resumeProducing(self):
  88. self.stdin.resumeProducing()