1
0

Luciana.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Two reactors spinning at once.
  2. # IOCP reactor for most things
  3. # selectreactor for SSL
  4. #
  5. # The contents of this file are subject to the Python Software Foundation
  6. # License Version 2.3 (the License). You may not copy or use this file, in
  7. # either source code or executable form, except in compliance with the License.
  8. # You may obtain a copy of the License at http://www.python.org/license.
  9. #
  10. # Software distributed under the License is distributed on an AS IS basis,
  11. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. # for the specific language governing rights and limitations under the
  13. # License.
  14. #
  15. # by Greg Hazel
  16. from twisted.internet import iocpreactor
  17. iocpreactor.proactor.install()
  18. from twisted.internet.selectreactor import SelectReactor
  19. selectreactor = SelectReactor()
  20. from twisted.internet import reactor
  21. selectreactor.spin_task = 0
  22. def selectrun():
  23. selectreactor.iterate(0)
  24. if selectreactor.spin_task > 0:
  25. reactor.callLater(0.01, selectrun)
  26. class HookedFactory(object):
  27. def __init__(self, factory):
  28. self.factory = factory
  29. def startedConnecting(self, connector):
  30. if selectreactor.spin_task == 0:
  31. reactor.callLater(0.01, selectrun)
  32. selectreactor.spin_task += 1
  33. return self.factory.startedConnecting(connector)
  34. def clientConnectionFailed(self, connector, reason):
  35. selectreactor.spin_task -= 1
  36. return self.factory.clientConnectionFailed(connector, reason)
  37. def clientConnectionLost(self, connector, reason):
  38. selectreactor.spin_task -= 1
  39. return self.factory.clientConnectionLost(connector, reason)
  40. def __getattr__(self, attr):
  41. return getattr(self.factory, attr)
  42. def spin_ssl(host, port, factory, contextFactory, timeout=30, bindAddress=None):
  43. factory = HookedFactory(factory)
  44. connector = selectreactor.connectSSL(host, port, factory, contextFactory,
  45. timeout, bindAddress)
  46. return connector
  47. reactor.connectSSL = spin_ssl