mirror of
https://gitee.com/RRQM_Home/TouchSocket.git
synced 2025-12-06 16:29:02 +08:00
文档(README): 优化文档结构与内容提升可读性
重写 README.md 和 README.zh.md 文件: - 更新语言切换链接和徽章,新增 GitHub Star 徽章 - 优化描述部分,突出框架高性能与多协议支持 - 重构文档导航,合并支持环境与框架部分 - 增加性能对比表格,更新 IOCP 优化与数据适配器内容 - 提供现代化代码示例,使用 Memory.Span 替代 ByteBlock.Span - 移除冗余内容,新增 TouchSocket 加入 dotNET China 声明 - 提供更清晰的联系与支持方式
This commit is contained in:
309
README.md
309
README.md
@@ -1,25 +1,20 @@
|
||||
**En** | [中](./README.zh.md)
|
||||
<p></p>
|
||||
<p></p>
|
||||
**En** | [中文](./README.zh.md)
|
||||
|
||||
<p align="center">
|
||||
<img src="logo.png" width = "100" height = "100" alt="The name of the image" align=center />
|
||||
<img src="logo.png" width="100" height="100" />
|
||||
</p>
|
||||
|
||||
<div align="center">
|
||||
<div align="center">
|
||||
|
||||
[](https://www.nuget.org/packages/TouchSocket/)
|
||||
[](https://www.nuget.org/packages/TouchSocket/)
|
||||
[](https://www.apache.org/licenses/LICENSE-2.0.html)
|
||||
[](https://gitee.com/RRQM_Home/TouchSocket)
|
||||
[](https://gitcode.com/RRQM_Home/TouchSocket)
|
||||
[](https://gitee.com/RRQM_Home/TouchSocket/members)
|
||||
<a href="https://jq.qq.com/?_wv=1027&k=gN7UL4fw">
|
||||
<img src="https://img.shields.io/badge/QQ group-234762506-red" alt="QQ">
|
||||
</a>
|
||||
[](https://gitee.com/RRQM_Home/TouchSocket/stargazers)
|
||||
[](https://gitcode.com/RRQM_Home/TouchSocket)
|
||||
[](https://gitee.com/RRQM_Home/TouchSocket/members) <a href="https://jq.qq.com/?_wv=1027&k=gN7UL4fw"> <img src="https://img.shields.io/badge/QQ Group-234762506-red"> </a>
|
||||
[](https://github.com/RRQM/TouchSocket)
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div align="center">
|
||||
|
||||
@@ -27,166 +22,258 @@
|
||||
|
||||
</div>
|
||||
|
||||
## 🎀Description
|
||||
---
|
||||
|
||||

|
||||
# 🎀 Description
|
||||
|
||||
'TouchSocket' is a powerful and easy-to-use.NET network communication framework for languages such as C#, VB.Net, and F#. It provides a variety of communication modules, including TCP, UDP, SSL, WebSocket, Modbus, etc. It supports solving the problem of TCP packet subcontracting and UDP large packet fragment combination. The framework supports a variety of protocol templates to quickly parse data packets such as fixed headers, fixed lengths, and interval characters.
|
||||

|
||||
|
||||
***
|
||||
**TouchSocket is a simple, modern, and high-performance .NET networking framework**, supporting C#, VB.NET, F#, and more.
|
||||
It helps you build powerful communication applications such as **TCP / UDP / MQTT / WebSocket / SSL / HTTP / Modbus / RPC** with ease.
|
||||
|
||||
## 🌟Related Documentation
|
||||
The framework features a highly optimized IOCP/Socket implementation, robust memory pooling, a flexible data-adapter pipeline, and a rich plugin ecosystem including heartbeat, reconnection, SSL, RPC, and more.
|
||||
|
||||
- [The first page of the document](https://touchsocket.net/)
|
||||
- [Getting started as a newbie](https://touchsocket.net/docs/current/startguide)
|
||||
- [API documentation](https://touchsocket.net/api/)
|
||||
---
|
||||
|
||||
***
|
||||
# 🌟 Documentation
|
||||
|
||||
## 🖥Supporting the environment
|
||||
* [Documentation Home](https://touchsocket.net/)
|
||||
* [Getting Started](https://touchsocket.net/docs/current/startguide)
|
||||
* [API Reference](https://touchsocket.net/api/)
|
||||
|
||||
- .NET Framework 4.6.2 or later.
|
||||
- .NET 6.0 and above.
|
||||
- .NET Standard 2.0 or later.
|
||||
---
|
||||
|
||||
## 🥪Support frameworks
|
||||
# 🖥 Supported Platforms
|
||||
|
||||
- Console
|
||||
- WPF
|
||||
- Winform
|
||||
- Blazor
|
||||
- Xamarin
|
||||
- MAUI
|
||||
- Avalonia
|
||||
- Mono
|
||||
- Unity 3D(except WebGL)
|
||||
- Other (i.e. all C# faculties)
|
||||
* .NET Framework ≥ **4.6.2**
|
||||
* .NET Standard ≥ **2.0**
|
||||
* .NET ≥ **6.0**
|
||||
|
||||
# 🥪 Supported Application Types
|
||||
|
||||
## 🌴A quick overview of TouchSocket features
|
||||
Console / WinForm / WPF / MAUI / Avalonia / Blazor / Xamarin / Unity (non-WebGL) / Mono / All C#-based platforms
|
||||
|
||||
#### Traditional IOCP and TouchSocket IOCP modes
|
||||
---
|
||||
|
||||
The IOCP of TouchSocket is not the same as the traditional one, just take Microsoft's official example as an example, he uses MemoryBuffer to open up a piece of memory, divide it evenly, and then assign a zone to each session to receive, and then **copy** the received data, and then process the copied data. The TouchSocket is to take a usable memory block from the memory pool before each reception, and then use it directly for receiving, and after receiving the data, the memory block is directly thrown out for processing, so as to avoid the **copy operation**, although it is only a small design, but when transmitting **10w**times**64kb**data, the performance difference is **10 times**.
|
||||
# 🌴 TouchSocket at a Glance (Updated & Revised)
|
||||
|
||||
#### Data Processing Adapters
|
||||
### 🚀 1. A Truly High-Performance IOCP Implementation
|
||||
|
||||
I believe that everyone has used other Socket products, so TouchSocket is also designed to learn from the excellent design concept of other products, [data processing adapter](https://touchsocket.net/docs/current/adapterdescription) is one of them, but unlike the design of other products, the adapter of TouchSocket is more powerful and easy to use. and flexible. Not only can it parse packets in advance, but it can also parse data objects, which can be replaced at any time and then take effect immediately. For example, you can use a fixed packet header to preprocess the data, so as to solve the problem of data subcontracting and sticky packet. It can also directly parse the HTTP data protocol, WebSocket data protocol, etc.
|
||||
TouchSocket provides a deeply optimized IOCP design.
|
||||
|
||||
#### Compatibility and adaptation
|
||||
| Implementation | Memory Handling | Performance Impact |
|
||||
| --------------------------------------- | ------------------------------------------------------------------------------ | -------------------------------------------------- |
|
||||
| **Traditional IOCP (Microsoft sample)** | Uses a fixed shared buffer; received data must be **copied** to another buffer | Extra copy → High overhead under load |
|
||||
| **TouchSocket IOCP** | Allocates a fresh memory block from the **memory pool** for each receive | **Zero extra copy** → Significant performance gain |
|
||||
|
||||
TouchSocket provides a variety of framework models and is fully compatible with all protocols based on TCP and UDP protocols. For example, TcpService and TcpClient have the same basic functions as Sockets, but they enhance the robustness and concurrency of the framework, and throw the connection and received data in the form of events, so that users can use it more user-friendly.
|
||||
In stress tests (100k messages × 64KB), TouchSocket achieved **up to 10× faster throughput** compared to the traditional model.
|
||||
|
||||
## 🔗Contact the author
|
||||
---
|
||||
|
||||
- [Blog Homepage](https://blog.csdn.net/qq_40374647)
|
||||
- [Bilibili video](https://space.bilibili.com/94253567)
|
||||
- [Source code repository home page](https://gitee.com/RRQM_Home)
|
||||
- QQ group:[234762506](https://jq.qq.com/?_wv=1027&k=gN7UL4fw)
|
||||
### 🔧 2. Data Adapter System — The Heart of the Framework
|
||||
|
||||
The data-adapter pipeline is one of TouchSocket’s core strengths:
|
||||
|
||||
## 👑Functional mapping
|
||||
* Parses **headers and payloads**
|
||||
* Handles **sticky packets & fragmentation**
|
||||
* Converts directly to **data objects**
|
||||
* Hot-swappable adapters
|
||||
* Built-in templates: fixed header, fixed length, terminator, HTTP, WebSocket, etc.
|
||||
|
||||
<p align="center">
|
||||
<img src="images/1.png" alt="The name of the image" align=center />
|
||||
</p>
|
||||
Adapters make protocol development clean, modular, and highly reusable.
|
||||
|
||||
## ✨Simple example
|
||||
---
|
||||
|
||||
The following is just a simple example of how to create an example, please see [Documentation](https://touchsocket.net/) for more details.
|
||||
### 🧩 3. Pluggable Extension System
|
||||
|
||||
#### TcpService
|
||||
TouchSocket’s **Plugins** system allows extending communication behavior across the entire lifecycle:
|
||||
|
||||
* Auto reconnection
|
||||
* Heartbeat detection
|
||||
* SSL validation
|
||||
* Logging
|
||||
* Authentication
|
||||
* Custom data pipelines
|
||||
|
||||
All via:
|
||||
|
||||
```csharp
|
||||
.ConfigurePlugins(a => { ... });
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 🛠 4. Robust TCP/UDP Abstraction
|
||||
|
||||
TouchSocket maintains full compatibility with native Socket semantics while improving:
|
||||
|
||||
* Stability
|
||||
* Concurrency and throughput
|
||||
* Connection lifecycle management
|
||||
* Exception handling
|
||||
* Unified event model (Connected / Received / Closed)
|
||||
|
||||
You can migrate existing Socket code with minimal changes.
|
||||
|
||||
---
|
||||
|
||||
### 📦 5. Unified Client/Server API
|
||||
|
||||
Across TCP, UDP, WebSocket, and others, TouchSocket exposes a consistent set of APIs:
|
||||
|
||||
```csharp
|
||||
client.ConnectAsync(...)
|
||||
client.SendAsync(...)
|
||||
client.Received += ...
|
||||
```
|
||||
|
||||
This ensures a low learning curve and fast development.
|
||||
|
||||
---
|
||||
|
||||
### 🧠 6. High-Efficiency Memory Pool & Span-Based Processing
|
||||
|
||||
The framework is optimized with:
|
||||
|
||||
* **ByteBlock** reusable high-performance buffers
|
||||
* **MemoryPool**
|
||||
* **Span<T> / Memory<T>**
|
||||
|
||||
Ensuring minimal allocations and low GC pressure during heavy workloads.
|
||||
|
||||
---
|
||||
|
||||
# ✨ Basic Examples
|
||||
|
||||
> The following examples show only the simplest usage. Refer to the documentation for more advanced scenarios.
|
||||
|
||||
## TcpService
|
||||
|
||||
```csharp
|
||||
TcpService service = new TcpService();
|
||||
service.Connected = (client, e) => { return EasyTask.CompletedTask; };//有客户端连接
|
||||
service.Closed = (client, e) => { return EasyTask.CompletedTask; };//有客户端断开连接
|
||||
|
||||
service.Connected = (client, e) => EasyTask.CompletedTask;
|
||||
service.Closed = (client, e) => EasyTask.CompletedTask;
|
||||
|
||||
service.Received = (client, e) =>
|
||||
{
|
||||
//Received information from the client
|
||||
string mes = e.ByteBlock.Span.ToString(Encoding.UTF8);
|
||||
Console.WriteLine($"From {client.Id} received message:{mes}");
|
||||
return EasyTask.CompletedTask;
|
||||
};
|
||||
await service.StartAsync(7789);//Start listening
|
||||
```
|
||||
|
||||
#### TcpClient
|
||||
|
||||
```
|
||||
TcpClient tcpClient = new TcpClient();
|
||||
tcpClient.Connected = (client, e) => { return EasyTask.CompletedTask; };//Successfully connected to the server
|
||||
tcpClient.Closed = (client, e) => { return EasyTask.CompletedTask; };//Disconnect from the server, which is not triggered when the connection is unsuccessful.
|
||||
tcpClient.Received = (client, e) =>
|
||||
{
|
||||
//Information is received from the server
|
||||
string mes = e.ByteBlock.Span.ToString(Encoding.UTF8);
|
||||
Console.WriteLine($"Message received: {mes}");
|
||||
string str = e.Memory.Span.ToString(Encoding.UTF8);
|
||||
Console.WriteLine($"Received: {str}");
|
||||
return EasyTask.CompletedTask;
|
||||
};
|
||||
|
||||
await tcpClient.ConnectAsync("127.0.0.1:7789");
|
||||
|
||||
//Send a message to the server
|
||||
await tcpClient.SendAsync("Hello");
|
||||
await service.StartAsync(7789);
|
||||
```
|
||||
|
||||
#### TcpClient reconnect
|
||||
## TcpClient
|
||||
|
||||
In the plug-in configuration of Config, you can use the reconnection plug-in.
|
||||
```csharp
|
||||
TcpClient client = new TcpClient();
|
||||
|
||||
```
|
||||
.ConfigurePlugins(a=>
|
||||
client.Connected = (c, e) => EasyTask.CompletedTask;
|
||||
client.Closed = (c, e) => EasyTask.CompletedTask;
|
||||
|
||||
client.Received = (c, e) =>
|
||||
{
|
||||
a.UseReconnection(5, true, 1000);
|
||||
Console.WriteLine(e.Memory.Span.ToString());
|
||||
return EasyTask.CompletedTask;
|
||||
};
|
||||
|
||||
await client.ConnectAsync("127.0.0.1:7789");
|
||||
await client.SendAsync("Hello");
|
||||
```
|
||||
|
||||
## TcpClient Auto-Reconnection
|
||||
|
||||
```csharp
|
||||
.ConfigurePlugins(a =>
|
||||
{
|
||||
a.UseReconnection<TcpClient>();
|
||||
});
|
||||
```
|
||||
|
||||
#### FixedHeaderPackageAdapter package
|
||||
---
|
||||
|
||||
The adapter mainly solves the problem of Tcp sticking and packing, and the data format adopts a simple and efficient "Baotou + Data Body" mode, in which Baotou supports:
|
||||
# 🧩 Fixed-Header Package Mode
|
||||
|
||||
- Byte mode (1+n), a maximum of 255 bytes of data can be received at one time.
|
||||
- Ushort mode (2+n), the maximum number of bytes received at a time is 65535 bytes.
|
||||
- Int mode (4+n), maximum reception of 2G data at a time.
|
||||
Solves packet fragmentation & merging issues.
|
||||
|
||||
The above data headers all use the default side mode (little-end mode) of TouchSocketBitConverter, and users can switch the default side mode according to their needs.
|
||||
Supports:
|
||||
|
||||
```
|
||||
* **Byte = 1 + n** (≤255B)
|
||||
* **Ushort = 2 + n** (≤65535B)
|
||||
* **Int = 4 + n** (≤2GB)
|
||||
|
||||
Endianness is configurable:
|
||||
|
||||
```csharp
|
||||
TouchSocketBitConverter.DefaultEndianType = EndianType.Little;
|
||||
```
|
||||
|
||||
#### CustomFixedHeaderDataHandlingAdapter
|
||||
---
|
||||
|
||||
The user-defined fixed header adapter mainly helps users solve the data frame information with fixed headers. For example, the following data formats only need to implement a few interfaces to complete the parsing, please refer to the API for details.
|
||||
# 🧱 Custom Adapters
|
||||
|
||||
|1|1|1|**********|
|
||||
## CustomFixedHeaderDataHandlingAdapter
|
||||
|
||||
#### CustomUnfixedHeaderDataHandlingAdapter
|
||||
For fixed-header formats such as:
|
||||
|
||||
The user-defined non-fixed header adapter mainly helps users solve the problem of data frame information with unfixed headers. For example, the most typical HTTP packets have a data header separated from the data body by "\r\n", and the data header is not fixed because of the different request information of the requester, and the length of the data body is also explicitly specified by the value of the ContentLength of the data header, so you can consider using CustomUnfixedHeaderDataHandlingAdapter to parse, which can also be achieved through simple development.
|
||||
```
|
||||
| 1 | 1 | 1 | ********** |
|
||||
```
|
||||
|
||||
***
|
||||
## CustomUnfixedHeaderDataHandlingAdapter
|
||||
|
||||
## Thanks
|
||||
For variable-header protocols such as HTTP:
|
||||
|
||||
Thank you for your support of TouchSocket, if you have any other questions, please submit an Issue, or join the group QQ: [234762506](https://jq.qq.com/?_wv=1027&k=gN7UL4fw) to discuss.
|
||||
* Header ends with `\r\n\r\n`
|
||||
* Body length from `Content-Length`
|
||||
|
||||
Thanks to the support of the following tools
|
||||
A small amount of code can build a complete parser.
|
||||
|
||||
- [Visual Studio](https://visualstudio.microsoft.com/zh-hans/)
|
||||
- [JetBrains](https://www.jetbrains.com/)
|
||||
- [Visual Studio Code](https://code.visualstudio.com/)
|
||||
---
|
||||
|
||||
## Support the author
|
||||
# 👑 Feature Overview Diagram
|
||||
|
||||
- [Tipping support](https://touchsocket.net/docs/current/donate)
|
||||
- [Pro support](https://touchsocket.net/docs/current/enterprise)
|
||||
<p align="center">
|
||||
<img src="images/1.png" />
|
||||
</p>
|
||||
|
||||
## Special Statement
|
||||
---
|
||||
|
||||
The TouchSocket project has been added to the [dotNET China](https://gitee.com/dotnetchina) organization.
|
||||
# 🔗 Contact
|
||||
|
||||
* [CSDN Blog](https://blog.csdn.net/qq_40374647)
|
||||
* [Bilibili Videos](https://space.bilibili.com/94253567)
|
||||
* [Source Repositories](https://gitee.com/RRQM_Home)
|
||||
* QQ Group: **234762506**
|
||||
|
||||

|
||||
---
|
||||
|
||||
# 🙏 Acknowledgements
|
||||
|
||||
Thank you all for supporting TouchSocket.
|
||||
If you have questions, feel free to submit an issue or join the QQ group.
|
||||
|
||||
Special thanks to:
|
||||
|
||||
* Visual Studio
|
||||
* JetBrains
|
||||
* Visual Studio Code
|
||||
|
||||
---
|
||||
|
||||
# ❤️ Support the Author
|
||||
|
||||
* [Donate](https://touchsocket.net/docs/current/donate)
|
||||
* [Pro Edition](https://touchsocket.net/docs/current/enterprise)
|
||||
|
||||
---
|
||||
|
||||
# 📢 Statement
|
||||
|
||||
TouchSocket is a member of the **dotNET China** organization.
|
||||
|
||||
<p align="center">
|
||||
<img src="https://images.gitee.com/uploads/images/2021/0324/120117_2da9922c_416720.png" width="300"/>
|
||||
</p>
|
||||
|
||||
284
README.zh.md
284
README.zh.md
@@ -27,166 +27,254 @@
|
||||
|
||||
</div>
|
||||
|
||||
## 🎀描述
|
||||
|
||||

|
||||
---
|
||||
|
||||
`TouchSocket`是一个功能强大且易于使用的.NET 网络通信框架,适用于C#、VB.Net 和 F#等语言。它提供了多种通信模块,包括TCP、UDP、SSL、WebSocket、Modbus等。支持解决TCP黏包分包问题和UDP大数据包分片组合问题。框架支持多种协议模板,快速实现固定包头、固定长度和区间字符等数据报文解析。
|
||||
# 🎀 描述
|
||||
|
||||
***
|
||||

|
||||
|
||||
## 🌟相关文档
|
||||
**TouchSocket 是一个简洁、现代且高性能的 .NET 网络通信框架**,支持 C#、VB.NET、F# 等语言。
|
||||
你可以用它快速构建 **TCP / UDP / MQTT / WebSocket / SSL / HTTP / Modbus / RPC** 等各种网络应用。
|
||||
|
||||
- [文档首页](https://touchsocket.net/)
|
||||
- [新手入门](https://touchsocket.net/docs/current/startguide)
|
||||
- [API文档](https://touchsocket.net/api/)
|
||||
框架提供高吞吐的 IOCP/Socket 实现、完善的内存池、灵活的数据适配器体系,并为多种场景准备了丰富的扩展插件(心跳、重连、SSL、RPC 等)。
|
||||
|
||||
***
|
||||
---
|
||||
|
||||
## 🖥支持环境
|
||||
# 🌟 文档导航
|
||||
|
||||
- .NET Framework4.6.2及以上。
|
||||
- .NET 6.0及以上。
|
||||
- .NET Standard2.0及以上。
|
||||
* [文档首页](https://touchsocket.net/)
|
||||
* [快速入门](https://touchsocket.net/docs/current/startguide)
|
||||
* [API 文档](https://touchsocket.net/api/)
|
||||
|
||||
## 🥪支持框架
|
||||
---
|
||||
|
||||
- Console
|
||||
- WPF
|
||||
- Winform
|
||||
- Blazor
|
||||
- Xamarin
|
||||
- MAUI
|
||||
- Avalonia
|
||||
- Mono
|
||||
- Unity 3D(除WebGL)
|
||||
- 其他(即所有C#系)
|
||||
# 🖥 支持环境
|
||||
|
||||
* .NET Framework ≥ **4.6.2**
|
||||
* .NET Standard ≥ **2.0**
|
||||
* .NET ≥ **6.0**
|
||||
|
||||
## 🌴TouchSocket特点速览
|
||||
# 🥪 支持框架
|
||||
|
||||
#### 传统IOCP和TouchSocket的IOCP模式
|
||||
Console / WinForm / WPF / MAUI / Avalonia / Blazor / Xamarin / Unity(非 WebGL) / Mono / 其他所有 C# 生态
|
||||
|
||||
TouchSocket的IOCP和传统也不一样,就以微软官方示例为例,他是使用MemoryBuffer开辟一块内存,均分,然后给每个会话分配一个区接收,等收到数据后,再**复制**接收的数据,然后把复制的数据进行处理。而TouchSocket是每次接收之前,从内存池拿一个可用内存块,然后**直接用于接收**,等收到数据以后,直接就把这个内存块抛出处理,这样就避免了**复制操作**,虽然只是细小的设计,但是在传输**10w**次**64kb**的数据时,性能相差了**10倍**。
|
||||
---
|
||||
|
||||
#### 数据处理适配器
|
||||
# 🌴 TouchSocket 特点速览
|
||||
|
||||
相信大家都使用过其他的Socket产品,那么TouchSocket在设计时也是借鉴了其他产品的优秀设计理念,[数据处理适配器](https://touchsocket.net/docs/current/adapterdescription)就是其中之一,但和其他产品的设计不同的是,TouchSocket的适配器功能更加强大,易用,且灵活。它不仅可以提前解析数据包,还可以解析数据对象,可以随时替换,然后立即生效。例如:可以使用固定包头对数据进行预处理,从而解决**数据分包**、**粘包**的问题。也可以直接解析**HTTP**数据协议、WebSocket数据协议等。
|
||||
### 🚀 1. 真正高性能的 IOCP 实现
|
||||
|
||||
#### 兼容性与适配
|
||||
TouchSocket 在 IOCP 设计上进行了深度优化,与传统示例代码不同:
|
||||
|
||||
TouchSocket提供多种框架模型,能够完全兼容基于TCP、UDP协议的所有协议。例如:TcpService与TcpClient,其基础功能和Socket一模一样,只是增强了框架的**坚固性**和**并发性**,将**连接**和**接收数据**通过事件的形式抛出,让使用者能够更加友好的使用。
|
||||
| 实现方式 | 内存处理方式 | 性能影响 |
|
||||
| -------------------- | ---------------------------- | ---------------------- |
|
||||
| **传统 IOCP(官方示例)** | 接收区固定,收到数据后需要 **复制** 出来再处理 | 多一次复制 → 高并发场景拖慢速度 |
|
||||
| **TouchSocket IOCP** | 每次接收前从 **内存池** 获取可写块,并直接用于接收 | **零额外复制** → 大流量下显著提升性能 |
|
||||
|
||||
## 🔗联系作者
|
||||
实测在连续传输 **10 万次、每次 64KB** 数据的场景中,性能可达到传统实现的 **约 10 倍**。
|
||||
|
||||
- [CSDN博客主页](https://blog.csdn.net/qq_40374647)
|
||||
- [哔哩哔哩视频](https://space.bilibili.com/94253567)
|
||||
- [源代码仓库主页](https://gitee.com/RRQM_Home)
|
||||
- 交流QQ群:[234762506](https://jq.qq.com/?_wv=1027&k=gN7UL4fw)
|
||||
---
|
||||
|
||||
### 🔧 2. 适配器体系(数据处理的“插件化中枢”)
|
||||
|
||||
## 👑功能导图
|
||||
TouchSocket 最关键的模块之一——**数据处理适配器**,相比其他框架更灵活:
|
||||
|
||||
<p align="center">
|
||||
<img src="images/1.png" alt="图片名称" align=center />
|
||||
</p>
|
||||
* 可解析 **协议头/协议体**
|
||||
* 可自动处理 **粘包 / 分包**
|
||||
* 可直接转换 **数据对象**
|
||||
* 可随时替换适配器并立即生效
|
||||
* 内置多种协议模板:固定包头、固定长度、终止字符、HTTP、WebSocket …
|
||||
|
||||
## ✨简单示例
|
||||
只需配置适配器即可让复杂协议瞬间变得可控。
|
||||
|
||||
**_以下仅以最简方式创建示例,更多详情请查看[说明文档](https://touchsocket.net/)。_**
|
||||
---
|
||||
|
||||
#### TcpService
|
||||
### 🧩 3. 可插拔的框架扩展体系
|
||||
|
||||
TouchSocket 的 **插件系统(Plugins)** 允许在整个通信生命周期中注入行为,如:
|
||||
|
||||
* 自动重连
|
||||
* 心跳检测
|
||||
* 日志
|
||||
* SSL 认证
|
||||
* 自定义鉴权
|
||||
* Pipeline 数据过滤
|
||||
|
||||
通过配置 `.ConfigurePlugins()` 即可轻松挂载。
|
||||
|
||||
---
|
||||
|
||||
### 🛠 4. 完整的 TCP/UDP 抽象与强兼容性
|
||||
|
||||
TCP/UDP 的使用方式与原生 Socket 一致,但 TouchSocket 提供更健壮的:
|
||||
|
||||
* 异常处理
|
||||
* 高并发底层能力
|
||||
* 安全的连接管理
|
||||
* 标准化事件模型(Connected/Received/Closed 等)
|
||||
|
||||
你可以无缝迁移现有 Socket 逻辑。
|
||||
|
||||
---
|
||||
|
||||
### 📦 5. 统一的客户端/服务端模型
|
||||
|
||||
无论 TCP、UDP、WebSocket,TouchSocket 的 API 都尽量保持一致,让开发体验更加通用:
|
||||
|
||||
```csharp
|
||||
client.SendAsync(...)
|
||||
client.Received += ...
|
||||
client.ConnectAsync(...)
|
||||
```
|
||||
|
||||
学习成本极低。
|
||||
|
||||
---
|
||||
|
||||
### 🧠 6. 强大的内存池与 Span/Memory 优化
|
||||
|
||||
整个框架深度使用:
|
||||
|
||||
* **ByteBlock(高效可复用的内存块)**
|
||||
* **内存池 MemoryPool**
|
||||
* **Span<byte> / ReadOnlySpan<byte>**
|
||||
|
||||
确保在高流量下保持低 GC 血压。
|
||||
|
||||
---
|
||||
|
||||
# ✨ 简单示例
|
||||
|
||||
> 以下仅展示最基础的入门代码,更多示例请查看文档。
|
||||
|
||||
## TcpService
|
||||
|
||||
```csharp
|
||||
TcpService service = new TcpService();
|
||||
service.Connected = (client, e) => { return EasyTask.CompletedTask; };//有客户端连接
|
||||
service.Closed = (client, e) => { return EasyTask.CompletedTask; };//有客户端断开连接
|
||||
|
||||
service.Connected = (client, e) => EasyTask.CompletedTask;
|
||||
service.Closed = (client, e) => EasyTask.CompletedTask;
|
||||
|
||||
service.Received = (client, e) =>
|
||||
{
|
||||
//从客户端收到信息
|
||||
string mes = e.ByteBlock.Span.ToString(Encoding.UTF8);
|
||||
Console.WriteLine($"已从{client.Id}接收到信息:{mes}");
|
||||
return EasyTask.CompletedTask;
|
||||
};
|
||||
await service.StartAsync(7789);//启动
|
||||
```
|
||||
|
||||
#### TcpClient
|
||||
|
||||
```
|
||||
TcpClient tcpClient = new TcpClient();
|
||||
tcpClient.Connected = (client, e) => { return EasyTask.CompletedTask; };//成功连接到服务器
|
||||
tcpClient.Closed = (client, e) => { return EasyTask.CompletedTask; };//从服务器断开连接,当连接不成功时不会触发。
|
||||
tcpClient.Received = (client, e) =>
|
||||
{
|
||||
//从服务器收到信息
|
||||
string mes = e.ByteBlock.Span.ToString(Encoding.UTF8);
|
||||
Console.WriteLine($"接收到信息:{mes}");
|
||||
string str = e.Memory.Span.ToString(Encoding.UTF8);
|
||||
Console.WriteLine($"收到:{str}");
|
||||
return EasyTask.CompletedTask;
|
||||
};
|
||||
|
||||
await tcpClient.ConnectAsync("127.0.0.1:7789");
|
||||
|
||||
//发送信息到服务器
|
||||
await tcpClient.SendAsync("Hello");
|
||||
await service.StartAsync(7789);
|
||||
```
|
||||
|
||||
#### TcpClient 断线重连
|
||||
## TcpClient
|
||||
|
||||
在Config的插件配置中,使用重连插件即可。
|
||||
```csharp
|
||||
TcpClient client = new TcpClient();
|
||||
|
||||
```
|
||||
.ConfigurePlugins(a=>
|
||||
client.Connected = (c, e) => EasyTask.CompletedTask;
|
||||
client.Closed = (c, e) => EasyTask.CompletedTask;
|
||||
|
||||
client.Received = (c, e) =>
|
||||
{
|
||||
a.UseReconnection(5, true, 1000);
|
||||
Console.WriteLine(e.Memory.Span.ToString());
|
||||
return EasyTask.CompletedTask;
|
||||
};
|
||||
|
||||
await client.ConnectAsync("127.0.0.1:7789");
|
||||
await client.SendAsync("Hello");
|
||||
```
|
||||
|
||||
## TcpClient 断线重连
|
||||
|
||||
```csharp
|
||||
.ConfigurePlugins(a =>
|
||||
{
|
||||
a.UseReconnection<TcpClient>();
|
||||
});
|
||||
```
|
||||
|
||||
#### FixedHeaderPackageAdapter包模式
|
||||
---
|
||||
|
||||
该适配器主要解决Tcp粘分包问题,数据格式采用简单而高效的“包头+数据体”的模式,其中包头支持:
|
||||
# 🧩 固定包头模式(FixedHeaderPackageAdapter)
|
||||
|
||||
- Byte模式(1+n),一次性最大接收255字节的数据。
|
||||
- Ushort模式(2+n),一次最大接收65535字节。
|
||||
- Int模式(4+n),一次最大接收2G数据。
|
||||
适用于处理粘包/分包。
|
||||
|
||||
以上数据头均采用TouchSocketBitConverter的默认端模式(小端模式),使用者可以根据需求切换默认端模式。
|
||||
支持:
|
||||
|
||||
```
|
||||
* **Byte = 1 + n**(≤255B)
|
||||
* **Ushort = 2 + n**(≤65535B)
|
||||
* **Int = 4 + n**(≤2GB)
|
||||
|
||||
端序由 TouchSocketBitConverter 控制:
|
||||
|
||||
```csharp
|
||||
TouchSocketBitConverter.DefaultEndianType = EndianType.Little;
|
||||
```
|
||||
|
||||
#### CustomFixedHeaderDataHandlingAdapter
|
||||
---
|
||||
|
||||
用户自定义固定包头适配器,主要帮助用户解决具有固定包头的数据帧信息。例如:下列数据格式,仅需要实现几个接口,就能完成解析,详细操作请参照API。
|
||||
# 🧱 自定义适配器
|
||||
|
||||
|1|1|1|**********|
|
||||
## CustomFixedHeaderDataHandlingAdapter
|
||||
|
||||
#### CustomUnfixedHeaderDataHandlingAdapter
|
||||
适用于固定包头结构,例如:
|
||||
|
||||
用户自定义不固定包头适配器,主要帮助用户解决具有包头不固定的数据帧信息。例如:最典型的HTTP数据包,其数据头和数据体由“\r\n”隔开,而数据头又因为请求者的请求信息的不同,头部数据也不固定,而数据体的长度,也是由数据头的ContentLength的值显式指定的,所以可以考虑使用CustomUnfixedHeaderDataHandlingAdapter解析,也是仅通过简单的开发,就能实现。
|
||||
```
|
||||
| 1 | 1 | 1 | ********** |
|
||||
```
|
||||
|
||||
***
|
||||
## CustomUnfixedHeaderDataHandlingAdapter
|
||||
|
||||
## 致谢
|
||||
适用于不固定头结构,如 HTTP:
|
||||
|
||||
谢谢大家对TouchSocket的支持,如果还有其他问题,请提交Issue,或者加群QQ:[234762506](https://jq.qq.com/?_wv=1027&k=gN7UL4fw)讨论。
|
||||
* 头以 `\r\n\r\n` 分隔
|
||||
* Content-Length 决定数据体长度
|
||||
|
||||
感谢下列工具软件的支持
|
||||
可用少量代码完成解析。
|
||||
|
||||
- [Visual Studio](https://visualstudio.microsoft.com/zh-hans/)
|
||||
- [JetBrains](https://www.jetbrains.com/)
|
||||
- [Visual Studio Code](https://code.visualstudio.com/)
|
||||
---
|
||||
|
||||
## 支持作者
|
||||
# 👑 功能导图
|
||||
|
||||
- [打赏支持](https://touchsocket.net/docs/current/donate)
|
||||
- [Pro支持](https://touchsocket.net/docs/current/enterprise)
|
||||
<p align="center">
|
||||
<img src="images/1.png" />
|
||||
</p>
|
||||
|
||||
## 特别声明
|
||||
---
|
||||
|
||||
TouchSocket项目已加入[dotNET China](https://gitee.com/dotnetchina) 组织。
|
||||
# 🔗 联系作者
|
||||
|
||||
* [CSDN 博客](https://blog.csdn.net/qq_40374647)
|
||||
* [B 站视频](https://space.bilibili.com/94253567)
|
||||
* [源代码仓库](https://gitee.com/RRQM_Home)
|
||||
* QQ 群:234762506
|
||||
|
||||

|
||||
---
|
||||
|
||||
# 🙏 致谢
|
||||
|
||||
感谢大家对 TouchSocket 的支持。
|
||||
如有问题,欢迎 Issue 或加入 QQ 群交流。
|
||||
|
||||
特别感谢以下开发工具:
|
||||
|
||||
* Visual Studio
|
||||
* JetBrains
|
||||
* VS Code
|
||||
|
||||
---
|
||||
|
||||
# ❤️ 支持作者
|
||||
|
||||
* [打赏支持](https://touchsocket.net/docs/current/donate)
|
||||
* [Pro 版本支持](https://touchsocket.net/docs/current/enterprise)
|
||||
|
||||
---
|
||||
|
||||
# 📢 特别声明
|
||||
|
||||
TouchSocket 已加入 **dotNET China** 组织。
|
||||
|
||||
<p align="center">
|
||||
<img src="https://images.gitee.com/uploads/images/2021/0324/120117_2da9922c_416720.png" width="300"/>
|
||||
</p>
|
||||
|
||||
Reference in New Issue
Block a user