2
from twisted.internet import reactor
5
"""Mixin for protocols which wish to timeout connections
7
@cvar timeOut: The number of seconds after which to timeout the connection.
13
def callLater(self, period, func):
14
return reactor.callLater(period, func)
17
def resetTimeout(self):
18
"""Reset the timeout count down"""
19
if self.__timeoutCall is not None and self.timeOut is not None:
20
self.__timeoutCall.reset(self.timeOut)
22
def setTimeout(self, period):
23
"""Change the timeout period
25
@type period: C{int} or C{NoneType}
26
@param period: The period, in seconds, to change the timeout to, or
27
C{None} to disable the timeout.
32
if self.__timeoutCall is not None:
34
self.__timeoutCall.cancel()
35
self.__timeoutCall = None
37
self.__timeoutCall.reset(period)
38
elif period is not None:
39
self.__timeoutCall = self.callLater(period, self.__timedOut)
44
self.__timeoutCall = None
45
self.timeoutConnection()
47
def timeoutConnection(self):
48
"""Called when the connection times out.
49
Override to define behavior other than dropping the connection.
51
self.transport.loseConnection()
57
from twisted.protocols import policies
58
policies.TimeoutMixin.__dict__ = TimeoutMixin.__dict__
59
policies.TimeoutMixin.__dict__['module'] = 'twisted.protocols.policies'
60
TimeoutMixin = policies.TimeoutMixin