调用reactor.run(),就会调用到mainloop函数,从而调用到select或epoll,监控fd的读写。

posixbase.py:

    def listenTCP(self, port, factory, backlog=50, interface=''):
p = tcp.Port(port, factory, backlog, interface, self)
p.startListening()#会调用self.startReading(),再调用self.reactor.addReader(self)
#把自己加入epoll
return p def connectTCP(self, host, port, factory, timeout=30, bindAddress=None):
c = tcp.Connector(host, port, factory, timeout, bindAddress, self)
c.connect()
return c

factory类负责connect的管理,比如connect的建立、丢失、失败等,procotol是负责数据的接收。

 tcp.Connector(host, port, factory, timeout, bindAddress, self)

Connector类有factory,由factory可以找出procotol协议,协议主要是指tcp、process、ssl等。
class ClientCreator:
"""
Client connections that do not require a factory. The various connect* methods create a protocol instance using the given
protocol class and arguments, and connect it, returning a Deferred of the
resulting protocol instance. Useful for cases when we don't really need a factory. Mainly this
is when there is no shared state between protocol instances, and no need
to reconnect. The C{connectTCP}, C{connectUNIX}, and C{connectSSL} methods each return a
L{Deferred} which will fire with an instance of the protocol class passed to
L{ClientCreator.__init__}. These Deferred can be cancelled to abort the
connection attempt (in a very unlikely case, cancelling the Deferred may not
prevent the protocol from being instantiated and connected to a transport;
if this happens, it will be disconnected immediately afterwards and the
Deferred will still errback with L{CancelledError}).
""" def __init__(self, reactor, protocolClass, *args, **kwargs):
self.reactor = reactor
self.protocolClass = protocolClass
self.args = args
self.kwargs = kwargs def _connect(self, method, *args, **kwargs):
"""
Initiate a connection attempt. @param method: A callable which will actually start the connection
attempt. For example, C{reactor.connectTCP}. @param *args: Positional arguments to pass to C{method}, excluding the
factory. @param **kwargs: Keyword arguments to pass to C{method}. @return: A L{Deferred} which fires with an instance of the protocol
class passed to this L{ClientCreator}'s initializer or fails if the
connection cannot be set up for some reason.
"""
def cancelConnect(deferred):
connector.disconnect()
if f.pending is not None:
f.pending.cancel()
d = defer.Deferred(cancelConnect)#会生成一个延迟对象
f = _InstanceFactory(
self.reactor, self.protocolClass(*self.args, **self.kwargs), d)
connector = method(factory=f, *args, **kwargs)
return d def connectTCP(self, host, port, timeout=30, bindAddress=None):
"""
Connect to a TCP server. The parameters are all the same as to L{IReactorTCP.connectTCP} except
that the factory parameter is omitted. @return: A L{Deferred} which fires with an instance of the protocol
class passed to this L{ClientCreator}'s initializer or fails if the
connection cannot be set up for some reason.
"""
return self._connect(
self.reactor.connectTCP, host, port, timeout=timeout,
bindAddress=bindAddress)#返回一个延迟对象
class BaseConnector:
"""Basic implementation of connector. State can be: "connecting", "connected", "disconnected"
"""
timeoutID = None
factoryStarted = 0 def __init__(self, factory, timeout, reactor):
self.state = "disconnected"
self.reactor = reactor
self.factory = factory
self.timeout = timeout def disconnect(self):
"""Disconnect whatever our state is."""
if self.state == 'connecting':
self.stopConnecting()
elif self.state == 'connected':
self.transport.loseConnection() def connect(self):
"""Start connection to remote server."""
if self.state != "disconnected":
raise RuntimeError("can't connect in this state") self.state = "connecting"
if not self.factoryStarted:
self.factory.doStart()
self.factoryStarted = 1
self.transport = transport = self._makeTransport()#创建一个client端
if self.timeout is not None:
self.timeoutID = self.reactor.callLater(self.timeout, transport.failIfNotConnected, error.TimeoutError())
self.factory.startedConnecting(self)

重要结论:调用

最新文章

  1. PAT 1042. 字符统计(20)
  2. Request header is too large
  3. 打包上传成功, itunes connect 不出现上传的版本
  4. Android笔记——了解SDK,数据库sqlite的使用
  5. 用CSS box-shadow画画
  6. [原创]AHA大会回顾
  7. 刀哥多线程串行队列gcd-04-dispatch_queue_serial
  8. 不要随随便便的distinct和order by
  9. ASP.NET MVC之PagedList使用
  10. kafka rebalance 部分分区没有owner
  11. 常用排序算法总结(C#版)
  12. Java异常分类 转载
  13. 解决Flex4 发布后访问 初始化极其缓慢的问题
  14. JSP的学习(1)——基本知识与底层原理
  15. SharePoint备份文件
  16. ethtool确定网卡对应的物理网口
  17. Loadrunner录制https脚本
  18. Spring 缓存注解之@Cacheable,@CacheEvit
  19. 【C#复习总结】析构函数
  20. sublime package control INSTALLATION

热门文章

  1. adb调试android设备 说的比较清楚的一篇文章
  2. VS项目平台的x86,x64,Any CPU以及Debug和Release
  3. 查看app日志的方法
  4. [转][MVC]更新 dll 后版本不匹配的问题
  5. 1118 Birds in Forest (25 分)
  6. php中的各种http报错的报错的状态码的分析
  7. Socket传输简单的信息以及粘包问题的解决
  8. [UE4]工程设置:自动捕获鼠标、通过代码设置鼠标显示隐藏、输入模式、编译时自动保存
  9. [UE4]Format Text
  10. (转)C# WebApi 接口返回值不困惑:返回值类型详解