@implementer(interfaces.IStreamClientEndpoint)
class TCP4ClientEndpoint(object):
"""
TCP client endpoint with an IPv4 configuration.
"""
def __init__(self, reactor, host, port, timeout=30, bindAddress=None):
self._reactor = reactor
self._host = host
self._port = port
self._timeout = timeout
self._bindAddress = bindAddress
def connect(self, protocolFactory):
"""
Implement L{IStreamClientEndpoint.connect} to connect via TCP.
"""
try:
wf = _WrappingFactory(protocolFactory)
self._reactor.connectTCP(
self._host, self._port, wf,
timeout=self._timeout, bindAddress=self._bindAddress)
return wf._onConnection
except:
return defer.fail()
class _WrappingFactory(ClientFactory):
protocol = _WrappingProtocol
def __init__(self, wrappedFactory):
self._wrappedFactory = wrappedFactory
self._onConnection = defer.Deferred(canceller=self._canceller)#返回了延迟对象
def startedConnecting(self, connector):
"""
A connection attempt was started. Remember the connector which started
said attempt, for use later.
"""
self._connector = connector
def _canceller(self, deferred):
推论:调用reactor.connectTCP()时会生成一个connetor,同时connector会加入到reactor的selectable中,当相应的fd有数据时,会通过connector调用doread,doread函数就会调用到protocol中的datarecieved函数,当协议的connectionLost时就是deferred对象回调时。connetor的回调对象在哪里呢?如果是通过TCP4ClientEndpoint作为tranport的话,deferred对象是保存在factory中的。又因为connetor中有factory属性,所以通过factory属性可以找到deferred对象,并调用callback。从而完成延迟对象的回调。