twisted的tcp.py分析
#每个connector都有一个 Connection对象
@implementer(interfaces.ITCPTransport, interfaces.ISystemHandle)
class Connection(_TLSConnectionMixin, abstract.FileDescriptor, _SocketCloser,
_AbortingMixin):
"""
Superclass of all socket-based FileDescriptors. This is an abstract superclass of all objects which represent a TCP/IP
connection based socket. @ivar logstr: prefix used when logging events related to this connection.
@type logstr: C{str}
""" def __init__(self, skt, protocol, reactor=None):
abstract.FileDescriptor.__init__(self, reactor=reactor)
self.socket = skt
self.socket.setblocking(0)
self.fileno = skt.fileno
self.protocol = protocol def getHandle(self):
"""Return the socket for this connection."""
return self.socket def doRead(self):
"""Calls self.protocol.dataReceived with all available data. This reads up to self.bufferSize bytes of data from its socket, then
calls self.dataReceived(data) to process it. If the connection is not
lost through an error in the physical recv(), this function will return
the result of the dataReceived call.
"""
try:
data = self.socket.recv(self.bufferSize)#recv读取数据
except socket.error as se:
if se.args[0] == EWOULDBLOCK:
return
else:
return main.CONNECTION_LOST return self._dataReceived(data)#调用内部方法 def _dataReceived(self, data):
if not data:
return main.CONNECTION_DONE
rval = self.protocol.dataReceived(data)#调用-协议提供的数据接收方法
if rval is not None:
offender = self.protocol.dataReceived
warningFormat = (
'Returning a value other than None from %(fqpn)s is '
'deprecated since %(version)s.')
warningString = deprecate.getDeprecationWarningString(
offender, versions.Version('Twisted', 11, 0, 0),
format=warningFormat)
deprecate.warnAboutFunction(offender, warningString)
return rval def writeSomeData(self, data):
"""
Write as much as possible of the given data to this TCP connection. This sends up to C{self.SEND_LIMIT} bytes from C{data}. If the
connection is lost, an exception is returned. Otherwise, the number
of bytes successfully written is returned.
"""
# Limit length of buffer to try to send, because some OSes are too
# stupid to do so themselves (ahem windows)
limitedData = lazyByteSlice(data, 0, self.SEND_LIMIT) try:
return untilConcludes(self.socket.send, limitedData)
except socket.error as se:
if se.args[0] in (EWOULDBLOCK, ENOBUFS):
return 0
else:
return main.CONNECTION_LOST def _closeWriteConnection(self):
try:
self.socket.shutdown(1)
except socket.error:
pass
p = interfaces.IHalfCloseableProtocol(self.protocol, None)
if p:
try:
p.writeConnectionLost()
except:
f = failure.Failure()
log.err()
self.connectionLost(f) def readConnectionLost(self, reason):
p = interfaces.IHalfCloseableProtocol(self.protocol, None)
if p:
try:
p.readConnectionLost()
except:
log.err()
self.connectionLost(failure.Failure())
else:
self.connectionLost(reason) def connectionLost(self, reason):
"""See abstract.FileDescriptor.connectionLost().
"""
# Make sure we're not called twice, which can happen e.g. if
# abortConnection() is called from protocol's dataReceived and then
# code immediately after throws an exception that reaches the
# reactor. We can't rely on "disconnected" attribute for this check
# since twisted.internet._oldtls does evil things to it:
if not hasattr(self, "socket"):
return
abstract.FileDescriptor.connectionLost(self, reason)
self._closeSocket(not reason.check(error.ConnectionAborted))
protocol = self.protocol
del self.protocol
del self.socket
del self.fileno
protocol.connectionLost(reason) logstr = "Uninitialized" def logPrefix(self):
"""Return the prefix to log with when I own the logging thread.
"""
return self.logstr def getTcpNoDelay(self):
return operator.truth(self.socket.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY)) def setTcpNoDelay(self, enabled):
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, enabled) def getTcpKeepAlive(self):
return operator.truth(self.socket.getsockopt(socket.SOL_SOCKET,
socket.SO_KEEPALIVE)) def setTcpKeepAlive(self, enabled):
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, enabled)
twisted的tcp.py分析的更多相关文章
- 使用wireshark抓取TCP包分析1
使用wireshark抓取TCP包分析1 前言 介绍 目的 准备工作 传输 创建连接 握手 生成密钥 发送数据 断开连接 结论 前言 介绍 本篇文章是使用wireshrak对某个https请求的tcp ...
- wireshark tcp 协议分析 z
虽然知道wireshark是抓包神器,只会大概大概用一下,还用一下下tcpdump,略懂一点BPF过滤器,也知道一点怎么用 wirkshark过滤相关的报文,但是对于详细的字段的含义,如何查看TCP的 ...
- wireshark tcp 协议分析
虽然知道wireshark是抓包神器,只会大概大概用一下,还用一下下tcpdump,略懂一点BPF过滤器,也知道一点怎么用wirkshark过滤相关的报文,但是对于详细的字段的含义,如何查看TCP ...
- 一次http完整的请求tcp报文分析
一次http请求的报文分析 数据包如下: 第一个包113.31的主机(下边称之为客户端)给114.80的主机(下边称之为服务器)发送一个syn包请求建立连接 第二个包服务器回复客户端syn+ack表示 ...
- TCP头部分析与确认号的理解
1.TCP的特点: 基于字节流面向连接可靠传输缓冲传输全双工流量控制 2.头部格式和说明 图源百度.如下图示,就是TCP包的头部结构.可以看到这个头部最少有4x5=20个字节. 另外还需要理解TCP协 ...
- Wireshark抓取TCP包分析
介绍 本篇文章是使用wireshrak对某个https请求的tcp包进行分析. 目的 通过抓包实际分析了解tcp包. 准备工作 在我自己机子上安装的是wireshark2.2.6版本,随机查找了某个T ...
- TCP协议分析(包结构)---转
TCP首部格式 tcp数据是被封装在IP数据包中的,和udp类似,在IP数据包的数据部分.tcp数据包的格式如下: 源端口号和目的端口号(寻址)与udp中类似,用于寻找发端和收端应用进程.这两个值加上 ...
- [转载] HTTP请求的TCP瓶颈分析
原文: http://bhsc881114.github.io/2015/06/23/HTTP%E8%AF%B7%E6%B1%82%E7%9A%84TCP%E7%93%B6%E9%A2%88%E5%8 ...
- HTTP请求的TCP瓶颈分析
这篇文章基本是对<Web性能权威指南>第一章和第二章的读书笔记,另外加一些扩展内容,这本书确实赞,推荐 针对三次握手.流量控制(接收窗口).慢启动(cwnd,拥塞窗口).队首阻塞等方面看下 ...
随机推荐
- Spring mvc Json 的正确返回姿势
我们经常都需要封装统一JSON格式 例如以下形式 { “data”:“输出的数据”, “code”:“响应代码”, “msg”:“响应信息” } /** * Created by linli on 2 ...
- 关于dubbo通信协议之对比
对dubbo的协议的学习,可以知道目前主流RPC通信大概是什么情况, dubbo共支持如下几种通信协议: dubbo:// rmi:// hessian:// http:// webservice:/ ...
- LeetCode——10. Regular Expression Matching
一.题目链接:https://leetcode.com/problems/regular-expression-matching/ 二.题目大意: 实现一个正则表达式,该正则表达式只有两种特殊的字符— ...
- Ubuntu 14.10 下Eclipse安装Hadoop插件
准备环境 1 安装好了Hadoop,之前安装了Hadoop 2.5.0,安装参考http://www.cnblogs.com/liuchangchun/p/4097286.html 2 安装Eclip ...
- 基于folly的AtomicIntrusiveLinkedList无锁队列进行简单封装的多生产多消费模型
1.基于folly的AtomicIntrusiveLinkedList略微修改的无锁队列代码: #ifndef FOLLY_REVISE_H #define FOLLY_REVISE_H namesp ...
- Oracle 官方文档地址
官方文档地址: https://docs.oracle.com/cd/E11882_01/index.htm
- [UE4]捡枪
一.给角色添加碰撞胶囊体并添加On Component Begin Overlap事件 二.检查碰到的东西是不是武器 三.要捡的武器是否已经有主人.检查身上是否有同样类型的枪 五.把枪放到背包数组.关 ...
- AD中常用的命令
1:查看客户机使用哪台DC进行登录的:在客户端cmd下运行 set命令可以查看使用哪台DC登录. 2:使用命令把客户端加入到域 netdom join fs01 /domain:contoso.co ...
- 图片Alpha预乘的作用[转]
Premultiplied Alpha 这个概念做游戏开发的人都不会不知道.Xcode 的工程选项里有一项 Compress PNG Files,会对 PNG 进行 Premultiplied Alp ...
- ElasticSearch 核心概念
ElasticSearch核心概念-Cluster ElasticSearch核心概念-shards ElasticSearch核心概念-replicas ElasticSearch核心概念-reco ...