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,拥塞窗口).队首阻塞等方面看下 ...
随机推荐
- ALGO-43_蓝桥杯_算法训练_A+B Problem
问题描述 输入A,B. 输出A+B. 输入格式 输入包含两个整数A,B,用一个空格分隔. 输出格式 输出一个整数,表示A+B的值. 样例输入 样例输出 数据规模和约定 -,,,<=A,B< ...
- C++进阶--模板及关键字typename
//############################################################################ /* * 模板介绍 */ //函数模板 t ...
- C++进阶--静态多态
//############################################################################ /* * 多态 */ // 常见的动态多态 ...
- vue之v-show与v-if的区别
为什么data必须返回函数? v-show和v-if的区别 v-if只有在满足条件时才渲染元素,v-show无论是否满足条件都会渲染元素,都会对元素样式的display属性进行控制. v-if可以绑定 ...
- 关于java前端入门的一些简单的看法
html:是网页的骨架,静态网页初步的轮廓,简单粗糙,僵硬又没有美感.表单的标签<form>,里面的<input>很常用,里面有type属性等css:为了更加灵活,常与div一 ...
- 解决python中路径中包含中文无法找到文件的问题
a="C:\Users\Dell\Desktop\ATOU\公共测试用例" (带中文的路径) a=a.decode("utf-8").encode(" ...
- NIO文件锁FileLock
目录 <linux文件锁flock> <NIO文件锁FileLock> <java程序怎么在一个电脑上只启动一次,只开一个进程> 文件锁可以是shared(共享锁) ...
- 廖雪峰Java5集合-3Map-Properties的使用
Properties用于读取配置 properties文件只能使用ASCII码 #表示注释 可以从文件系统读取.properties文件 Properties props = new Properti ...
- html标签SEO规范
原文地址:http://blog.sina.com.cn/s/blog_6c3898dd0100whr7.html 1.<!--页面注解--> 2.<html> 3.<h ...
- HDOJ 2019 数列有序!
#include<vector> #include<iostream> #include<algorithm> #include<cstdio> usi ...