当前位置:网站首页>C# TCP如何限制单个客户端的访问流量
C# TCP如何限制单个客户端的访问流量
2022-07-05 16:19:00 【若汝棋茗】
一、说明
使用RRQMSocket创建服务器后,想实现一个限流功能,应该如何实现呢?
二、程序集源码
2.1 源码位置
2.2 说明文档
三、安装
Nuget安装RRQMSocket
即可,具体步骤详看链接博客。
四、创建插件
public class MyThrottlingPlugin : TcpPluginBase
{
private readonly int m_max;
[DependencyInject(10)]
public MyThrottlingPlugin(int max)
{
this.m_max = max;
this.Order = int.MaxValue;//提升优先级
}
protected override void OnConnected(ITcpClientBase client, RRQMEventArgs e)
{
client.InitFlowGate(this.m_max);//初始化流量计数器。
base.OnConnected(client, e);
}
protected override void OnReceivedData(ITcpClientBase client, ReceivedDataEventArgs e)
{
client.GetFlowGate().AddCheckWait(e.ByteBlock.Len);//此处假设接收的是ByteBlock数据。如果是自定义适配器,按需增量即可。
base.OnReceivedData(client, e);
}
}
五、创建扩展类
/// <summary>
/// 一个流量计数器扩展。
/// </summary>
static class DependencyExtensions
{
public static readonly DependencyProperty FlowGateProperty =
DependencyProperty.Register("FlowGate", typeof(FlowGate), typeof(DependencyExtensions), null);
public static void InitFlowGate(this IRRQMDependencyObject dependencyObject, int max)
{
dependencyObject.SetValue(FlowGateProperty, new FlowGate() {
Maximum = max });
}
public static FlowGate GetFlowGate(this IRRQMDependencyObject dependencyObject)
{
return dependencyObject.GetValue<FlowGate>(FlowGateProperty);
}
}
六、启动服务器
static void Main(string[] args)
{
TcpService service = new TcpService();
service.Connecting += (client, e) => {
};//有客户端正在连接
service.Connected += (client, e) => {
};//有客户端连接
service.Disconnected += (client, e) => {
};//有客户端断开连接
service.Received += (client, byteBlock, requestInfo) =>
{
//从客户端收到信息
string mes = Encoding.UTF8.GetString(byteBlock.Buffer, 0, byteBlock.Len);
Console.WriteLine($"已从{
client.ID}接收到信息:{
mes}");
client.Send(mes);//将收到的信息直接返回给发送方
};
service.Setup(new RRQMConfig()//载入配置
.UsePlugin()
.SetListenIPHosts(new IPHost[] {
new IPHost("127.0.0.1:7789"), new IPHost(7790) })//同时监听两个地址
.SetMaxCount(10000)
.SetThreadCount(100))
.Start();//启动
service.AddPlugin<MyThrottlingPlugin>();
Console.ReadLine();
}
七、效果
本文示例demo
实际上,该插件也能用于客户端。同时,也能限制发送流量限制。
边栏推荐
- Combined use of vant popup+ other components and pit avoidance Guide
- PHP 严格模式
- How to uninstall MySQL cleanly
- Benji Banas membership pass holders' second quarter reward activities update list
- 【 brosser le titre 】 chemise culturelle de l'usine d'oies
- China Radio and television officially launched 5g services, and China Mobile quickly launched free services to retain users
- Some cognitive thinking
- Games101 notes (I)
- Win11提示无法安全下载软件怎么办?Win11无法安全下载软件
- Google Earth Engine(GEE)——Kernel核函数简单介绍以及灰度共生矩阵
猜你喜欢
Cs231n notes (bottom) - applicable to 0 Foundation
Deep dive kotlin synergy (XXI): flow life cycle function
如何将mysql卸载干净
[61dctf]fm
中间表是如何被消灭的?
scratch五彩糖葫芦 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
Desci: is decentralized science the new trend of Web3.0?
[brush title] goose factory shirt problem
Deep learning plus
2020-2022两周年创作纪念日
随机推荐
Get ready for the pre-season card game MotoGP ignition champions!
Spring Festival Limited "forget trouble in the year of the ox" gift bag waiting for you to pick it up~
It is forbidden to copy content JS code on the website page
Seaborn draws 11 histograms
What is the difference between EDI license and ICP business license
[deep learning] [original] let yolov6-0.1.0 support the txt reading dataset mode of yolov5
中国广电正式推出5G服务,中国移动赶紧推出免费服务挽留用户
Can you help me see what the problem is? [ERROR] Could not execute SQL stateme
网站页面禁止复制内容 JS代码
Global Data Center released DC brain system, enabling intelligent operation and management through science and technology
解决CMakeList find_package找不到Qt5,找不到ECM
美国芯片傲不起来了,中国芯片成功在新兴领域夺得第一名
搜索 正排索引 和 倒排索引 区别
2020-2022 two-year anniversary of creation
Reduce the cost by 40%! Container practice of redis multi tenant cluster
Cartoon: what is distributed transaction?
Starkware: to build ZK "universe"
[61dctf]fm
单商户 V4.4,初心未变,实力依旧!
Jarvis OJ 远程登录协议