当前位置:网站首页>Supersocket is Use in net5 - concept
Supersocket is Use in net5 - concept
2022-04-23 03:18:00 【Pingshan CP3】
background : Recently, there is a communication message project , Thinking about .net core Use in SuperSocket To achieve , But it is found that the use is still the same as .NetFramework There are some differences , Then on SuperSocket Some of the methods used in are not very familiar , Take this opportunity to learn again
1. Use SuperSocket Purpose 、 benefits :
(1) encapsulation Socket Connect , Developers don't have to worry about and write too much code in Socket Connection is broken 、 Create multiple Server And so on 、 Good for Socket Configuration of
(2) Better manage client connections (AppSession)
(3) Built in command line protocol ( Built in command line pipeline filter )
(4) Built in PipelineFilter Templates
(5) command Command And command filters CommandFilterAttribute、 Global command filter
(6)WebSocket Server quick build
(7) Enable transport layer security support TLS
(8) And ASP.Net Core Website integration , Run in one program at the same time as the website
2. Some basic concepts :
Package Type: Package type , In fact, it is the data structure corresponding to the transmission data , You can choose the corresponding packet type to receive data according to your program message protocol , The built-in package types are :TextPackageInfo、StringPackageInfo;
TextPackageInfo It's an ordinary text structure :

StringPackageInfo Is simple, including key values and body Data structure of , Some built-in Filter By default, the filtered data will be parsed into StringPackageInfo class .StringPackageInfo Class uses a space to split the request key And parameters . When the client sends data to the server
"ADD user1 123456\r\n"
The data received is :
Key: "ADD"
Body: "user1 123456";
Parameters: ["user1", "123456"]

PipelineFilter Type: Pipe filter type , This type plays an important role in network protocol decoding . It defines how we will IO The stream is decoded into packets that the application can understand .IPipelineFilter yes PipelineFilter The basic interface of , When customizing the filter, you need to implement it .SuperSocket2.0 The types of built-in filters are :
TerminatorPipelineFilter Terminator filter ( Filter and parse the data before the specified Terminator , Default resolution to StringPackageInfo class )

TerminatorTextPipelineFilter Terminator text pipeline filter ( Filter and parse the data before the specified Terminator , Default resolution to TextPackageInfo class )

LinePipelineFilter Line filter ( The most common filter , Except for command line restrictions , Other unlimited , Default resolution to TextPackageInfo class )
CommandLinePipelineFilter Command line pipeline filter ( The most common filter , Except for command line restrictions , Other unlimited , Default resolution to StringPackageInfo class )
BeginEndMarkPipelineFilter Pipe filters with fixed start and end marks ( Filter valid data according to start and end characters , Default resolution to StringPackageInfo class )

FixedSizePipelineFilter Fixed length filter ( Only parse data of specified length , When the data length is less than the specified length , An error will be reported and the connection will be disconnected and reconnected , Default resolution to StringPackageInfo class )

FixedHeaderPipelineFilter Fixed beginning ( length ) Pipe filter ( By specifying the length of the beginning of the packet , rewrite GetBodyLengthFromHeader Method , Get the length of a valid packet from the beginning packet , Default resolution to StringPackageInfo class )

Tell the truth , These filters have not been used in actual projects yet , Because the use of these filters means that the message must comply with the command good protocol , With \r\n ending , But in practice, most of the application protocols do not change the other side of the docking , So there are only custom filters . Of course, these built-in filters can basically meet most of the protocols themselves ( If only the command-line protocol were not enforced ....)
Want to know more about the function and implementation process of these filters , You can see SuperSocket.ProtoBase.dll Source code
AppSession: Represents a logical socket connection , That is, a complete connection ; By inheritance AppSession class , Rewrite the connect and disconnect methods to implement your logic
public class MySession : AppSession
{
protected override async ValueTask OnSessionConnectedAsync()
{
// do something right after the sesssion is connected
}
protected override async ValueTask OnSessionClosedAsync(EventArgs e)
{
// do something right after the sesssion is closed
}
}
SuperSocketService: Service instance , Monitor and manage client connections , Application level operations and logic can be defined in it . You can extend SuperSocketService And cover the method
public class MySuperSocketService<TReceivePackageInfo> : SuperSocketService<TReceivePackageInfo>
where TReceivePackageInfo : class
{
public MySuperSocketService(IServiceProvider serviceProvider, IOptions<ServerOptions> serverOptions)
: base(serviceProvider, serverOptions)
{
}
protected override async ValueTask OnSessionConnectedAsync(IAppSession session)
{
// do something right after the sesssion is connected
await base.OnSessionConnectedAsync(session);
}
protected override async ValueTask OnSessionClosedAsync(IAppSession session)
{
// do something right after the sesssion is closed
await base.OnSessionClosedAsync(session);
}
protected override async ValueTask OnStartedAsync()
{
// do something right after the service is started
}
protected override async ValueTask OnStopAsync()
{
// do something right after the service is stopped
}
}
Command: Command processing , To handle requests from clients .
3. The configuration file
SuperSocket Also used JSON The configuration file appsettings.json;
newly added serverOptions node , Example :
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"serverOptions": {
"name": "TestServer",
"listeners": [
{
"ip": "Any",
"port": "2020"
},
{
"ip": "Any",
"port": "2021"
}
]
}
}
Configurable node variables , Refer to the official website :
- name: Name of the server ;
- maxPackageLength: Maximum package size allowed in the server ; Default 4M;
- receiveBufferSize: The size of the receive buffer ; Default 4k;
- sendBufferSize: Size of send buffer ; Default 4k;
- receiveTimeout: Receive timeout ; In Milliseconds ;
- sendTimeout: Send timeout ; In Milliseconds ;
- listeners: Listener endpoint for this server ;
- listeners/*/ip: The listener's listening IP;Any: whatever ipv4 ip Address ,IPv6Any: whatever ipv6 ip Address , Other actual IP Address ;
- listeners/*/port: The listening port of the listener ;
- listeners/*/backLog: Maximum length of the pending connection queue ;
- listeners/*/noDelay: Specified flow Socket Whether to use Nagle Algorithm ;
- Monitor /*/ Security : nothing /Ssl3/Tls11/Tls12/Tls13; Used for communication TLS Protocol version ;
- listeners/*/certificateOptions: be used for TLS encryption / Decrypted certificate options ;
These are the basic concepts , The following article describes how to start and use
版权声明
本文为[Pingshan CP3]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220627116849.html
边栏推荐
- 幂等性实践操作,基于业务讲解幂等性
- Top 9 task management system in 2022
- 可以接收多种数据类型参数——可变参数
- 2022 P cylinder filling training test questions and simulation test
- js 中,为一个里面带有input 的label 绑定事件后在父元素绑定单机事件,事件执行两次,求解
- Is it difficult to choose binary version control tools? After reading this article, you will find the answer
- 2022t elevator repair test simulation 100 questions and online simulation test
- 【VS Code】解决jupyter文件在vs code中显示异常的问题
- IDEA查看历史记录【文件历史和项目历史】
- “如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享
猜你喜欢

12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list

2022T电梯修理考试模拟100题及在线模拟考试

《C语言程序设计》(谭浩强第五版) 第9章 用户自己建立数据类型 习题解析与答案

Top 9 task management system in 2022

2022山东省安全员C证上岗证题库及在线模拟考试

一文了解全面静态代码分析

一套组合拳,打造一款 IDEA 护眼方案

Fight leetcode again (290. Word law)

ASP. Net 6 middleware series - conditional Middleware

How does Microsoft solve the problem of multiple PC programs
随机推荐
超好用的Excel异步导出功能
Mysql database
MySQL installation pit
js递归树结构计算每个节点的叶子节点的数量并且输出
be based on. NETCORE development blog project starblog - (1) why do you need to write your own blog?
通过 zxing 生成二维码
General testing technology [1] classification of testing
Iotos IOT middle platform is connected to the access control system of isecure center
Seminar playback video: how to improve Jenkins' ability to become a real Devops platform
JS recursive tree structure calculates the number of leaf nodes of each node and outputs it
js 中,为一个里面带有input 的label 绑定事件后在父元素绑定单机事件,事件执行两次,求解
[vs Code] solve the problem that the jupyter file displays exceptions in vs code
[Mysql] LEFT函數 | RIGHT函數
Top 9 task management system in 2022
Xamarin effect Chapter 22 recording effect
socket编程 send()与 recv()函数详解
手机连接电脑后,QT的QDIR怎么读取手机文件路径
LoadRunner - performance testing tool
The website JS in. Net core cefsharp chromium WebBrowser calls the C method in winfrom program
C syntax sugar empty merge operator [?] And null merge assignment operator [? =]