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

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