gpt4 book ai didi

python - Python 如何处理您未放入变量的 TCP 响应?

转载 作者:可可西里 更新时间:2023-11-01 02:53:27 25 4
gpt4 key购买 nike

我正在开发一个 domotica 程序 (Extron),但我遇到了内存问题。因此,我想知道 Python 对我没有放入变量的 TCP 响应做了什么。

我有以下代码行:

Matrix.SendAndWait(str(States.MatrixStatus[States.RecorderStatus['Recorder 2']]['Fullscreen Input'])+'*'+\
str(Collegerama['Recorder 2 Output'])+'%', 0.3, deliTag=b'\x0A')

SendAndWait 命令通常用于将响应放入变量中,但我用它来减慢编程速度并确保设备已准备好接收下一个命令。

但这并不是说我有内存泄漏,我想知道响应是否放在某个地方并且永远不会从内存中清除。

最佳答案

只要不溢出,信息就会存储在接收缓冲区中。通过设置 TCP 接收窗口大小 ( https://en.wikipedia.org/wiki/TCP_tuning#Window_size ) 可以防止溢出。如果数据包具有最大大小,则接收缓冲区只能容纳一个数据包。

maximum packet size for a TCP connection

因此数据包最初存储在接收缓冲区中(这是 OSI 模型中的网络层)从该接收缓冲区中,它们被 OSI 模型中的更高层提取,即 TCP(OSI 模型中的传输层),然后删除 header 以获取数据/有效负载...

如果当前在接收缓冲区中的数据包未被提取,它们将被新传入的数据包覆盖。因此,如果数据处理速度不够快,信息就会丢失,任何新传入的数据包都会覆盖接收缓冲区中的旧数据包。

https://en.wikipedia.org/wiki/OSI_model

https://www.cubrid.org/blog/understanding-tcp-ip-network-stack这个有详细说明

Data Receiving

Now, let's take a look at how data is received. Data receiving is a procedure for how the network stack handles a packet coming in. Figure 3 shows how the network stack handles a packet received.

First, the NIC writes the packet onto its memory. It checks whether the packet is valid by performing the CRC check and then sends the packet to the memory buffer of the host. This buffer is a memory that has already been requested by the driver to the kernel and allocated for receiving packets. After the buffer has been allocated, the driver tells the memory address and size to the NIC. When there is no host memory buffer allocated by the driver even though the NIC receives a packet, the NIC may drop the packet.

After sending the packet to the host memory buffer, the NIC sends an interrupt to the host OS.

Then, the driver checks whether it can handle the new packet or not. So far, the driver-NIC communication protocol defined by the manufacturer is used.

When the driver should send a packet to the upper layer, the packet must be wrapped in a packet structure that the OS uses for the OS to understand the packet. For example, sk_buff of Linux, mbuf of BSD-series kernel, and NET_BUFFER_LIST of Microsoft Windows are the packet structures of the corresponding OS. The driver sends the wrapped packets to the upper layer.

The Ethernet layer checks whether the packet is valid and then de-multiplexes the upper protocol (network protocol). At this time, it uses the ethertype value of the Ethernet header. The IPv4 ethertype value is 0x0800. It removes the Ethernet header and then sends the packet to the IP layer.

The IP layer also checks whether the packet is valid. In other words, it checks the IP header checksum. It logically determines whether it should perform IP routing and make the local system handle the packet, or send the packet to the other system. If the packet must be handled by the local system, the IP layer de-multiplexes the upper protocol (transport protocol) by referring to the proto value of the IP header. The TCP proto value is 6. It removes the IP header and then sends the packet to the TCP layer.

Like the lower layer, the TCP layer checks whether the packet is valid. It also checks the TCP checksum. As mentioned before, since the current network stack uses the checksum offload, the TCP checksum is computed by NIC, not by the kernel.

Then it searches the TCP control block where the packet is connected. At this time, <source IP, source port, target IP, target port> of the packet is used as an identifier. After searching the connection, it performs the protocol to handle the packet. If it has received new data, it adds the data to the receive socket buffer. According to the TCP state, it can send a new TCP packet (for example, an ACK packet). Now TCP/IP receiving packet handling has completed.

The size of the receive socket buffer is the TCP receive window. To a certain point, the TCP throughput increases when the receive window is large. In the past, the socket buffer size had been adjusted on the application or the OS configuration. The latest network stack has a function to adjust the receive socket buffer size, i.e., the receive window, automatically.

When the application calls the read system call, the area is changed to the kernel area and the data in the socket buffer is copied to the memory in the user area. The copied data is removed from the socket buffer. And then the TCP is called. The TCP increases the receive window because there is new space in the socket buffer. And it sends a packet according to the protocol status. If no packet is transferred, the system call is terminated.

关于python - Python 如何处理您未放入变量的 TCP 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46601308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com