#TCP is a streaming protocol. After the client sends a piece of data to the server, it may not be completely received by the server at once. The client sends multiple pieces of data to the server, and the server may receive them all at once. (Recommended learning: swoole video tutorial )
In practical applications, we hope that the server can receive a complete piece of data at a time, no more and no less.
In traditional TCP servers, programmers often need to maintain a cache area, first write the read data into the cache area, and then distinguish the beginning of a complete piece of data through the preset protocol content. , end and length, and hand over a complete piece of data to the logical part for processing. This is the function of the custom protocol.
In Swoole, a data cache area has been implemented at the bottom layer, and several commonly used protocol types have been built in. The data has been split directly at the bottom layer to ensure that in the onReceive callback function, certain Able to receive one or several complete data segments.
The size of the data buffer can be controlled by configuring pakcage_max_length.
$configs = []; $configs["package_max_length"] = 8192; $server->set($configs);
swoole currently supports two communication protocols: EOF terminator protocol and fixed header plus body protocol
package_max_length
package_max_length is used to set the maximum packet size , when protocol parsing such as open_length_check or open_eof_check or open_http_protocol is turned on, the bottom layer of Swoole will process data packet splicing. At this time, when the data packet is not completely received, all data will be saved in the memory.
So you need to set package_max_length to the maximum allowed memory size of a data packet.
If there are 10,000 TCP connections sending data at the same time, each data packet is 2MB, which will occupy 20GB of memory space in the most extreme case. Therefore, this parameter should not be set too large, otherwise it will occupy a lot of memory.
Related configuration options
open_length_check
When it is found that the length of the data packet exceeds package_max_length, the data will be discarded directly and the connection will be closed, so it will not occupy any memory and is suitable for websocket, mqtt, http2 protocols.
open_eof_check
Since the length of the data packet cannot be known in advance, the received data will still be stored in the memory and continue to grow. When it is found that the memory usage has exceeded package_max_length, the data packet will be dropped directly and the connection will be closed.
open_http_protocol
The HTTP GET request allows a maximum of 8KB data and this configuration cannot be modified. The POST request will detect the Content-Type. If it is found that the package_max_length is exceeded, the data will be discarded directly, an HTTP 400 error will be sent and the connection will be closed.
EOF protocol
Uses a fixed set of strings /r/n that will not appear in normal data as the mark of the split protocol, called EOF protocol.
What is the EOF protocol?
EOF stands for End of File, and uses \r\n as the end tag.
When reading the data in the data stream one by one, if the EOF mark is found, it means that the end of the data has been read.
In the TCP data flow, the characteristics of the data flow using the EOF protocol are |data|EOF|data|EOF|.
The principle of EOF protocol processing is that at the end of each string of normal data, a predetermined string that will never appear in the data will be added as an end mark, so that the received data can be marked according to the EOF to split the data.
Typical memcached, ftp, and stmp all use /r/n as the terminator. When sending data just add /r/n at the end of the data packet.
When using EOF protocol processing, you must ensure that EOF does not occur in the middle of the data packet, otherwise it will cause subpackaging errors.
The above is the detailed content of What protocol does swoole use?. For more information, please follow other related articles on the PHP Chinese website!

workerman 对比 swoole 实际开发项目中,你会选择哪个?对于新手学哪个较好,有什么建议吗?

在现代的应用开发中,异步编程在高并发场景下变得越来越重要。Swoole和Go是两个非常流行的异步编程框架,它们都具有高效的异步能力,但是很多人在选择使用哪个框架时会陷入困境。本文将探讨如何选择Swoole和Go,以及它们的优缺点。

你学会 Swoole 需要多久呢?这个问题其实非常难回答,因为它涉及到很多因素,比如你的编程基础、学习动力、时间安排等等。不过,在这篇文章中,我将分享一些我自己学习 Swoole 的经验和建议,希望能够对你有所帮助。

Swoole是一个基于PHP的开源高性能网络通信框架,它提供了TCP/UDP服务器和客户端的实现,以及多种异步IO、协程等高级特性。随着Swoole日益流行,许多人开始关心Web服务器使用Swoole的问题。为什么当前的Web服务器(如Apache、Nginx、OpenLiteSpeed等)不使用Swoole呢?让我们探讨一下这个问题。

以下为大家整理了php异步通信框架Swoole的视频教程,不需要从迅雷、百度云之类的第三方平台下载,全部在线免费观看。教程由浅入深,有php基础的人就能学习,从安装到案例讲解,全面详细,帮助你更快更好的掌握Swoole框架!

怎么在docker中搭建swoole环境?下面本篇文章给大家介绍一下用docker搭建swoole环境的方法,希望对大家有所帮助!

php让Swoole|Pool进程池实现Redis持久连接进程池,基于Swoole\Server的Manager管理进程模块实现。可管理多个工作进程,相比Process实现多进程,Process\Pool更加简单,封装层次更高,开发者无需编写过多代码即可实现进程管理功能,配合Co\Server可以创建纯协程风格的,能利用多核CPU的服务端程序。Swoole进程池实现redis数据读取如下案例,通过WorkerStart启动Redis进程池,并持久读取Redis列表数据;当WorkerStop断开

Swoole是一种基于PHP语言的网络通信框架,它能够提供异步、并发、高性能的HTTP、WebSocket以及TCP/UDP协议服务器和客户端,在开发Web服务以及网络通信应用时都有很大的用途,广泛应用于一些互联网公司。本文将介绍如何使用Swoole调用。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
