当前位置:网站首页>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
实际上,该插件也能用于客户端。同时,也能限制发送流量限制。
边栏推荐
猜你喜欢
數據訪問 - EntityFramework集成
怎样在电脑上设置路由器的WiFi密码
Jarvis OJ webshell analysis
"21 days proficient in typescript-3" - install and build a typescript development environment md
DeSci:去中心化科学是Web3.0的新趋势?
Learnopongl notes (I)
Games101 notes (I)
Flet教程之 09 NavigationRail 基础入门(教程含源码)
Android privacy sandbox developer preview 3: privacy, security and personalized experience
【729. 我的日程安排表 I】
随机推荐
Spring Festival Limited "forget trouble in the year of the ox" gift bag waiting for you to pick it up~
如何安装mysql
Flet tutorial 12 stack overlapping to build a basic introduction to graphic and text mixing (tutorial includes source code)
Data access - entityframework integration
【深度学习】深度学习如何影响运筹学?
Google Earth Engine(GEE)——Kernel核函数简单介绍以及灰度共生矩阵
面对新的挑战,成为更好的自己--进击的技术er
Win11如何给应用换图标?Win11给应用换图标的方法
有序链表集合求交集 方法 总结
HiEngine:可媲美本地的云原生内存数据库引擎
[brush questions] effective Sudoku
Deep dive kotlin synergy (XXI): flow life cycle function
PSPNet | 语义分割及场景分析
Cs231n notes (bottom) - applicable to 0 Foundation
单商户 V4.4,初心未变,实力依旧!
搜索 正排索引 和 倒排索引 区别
美国芯片傲不起来了,中国芯片成功在新兴领域夺得第一名
yarn 常用命令
How can programmers improve their situation?
Summary of PHP pseudo protocol of cisp-pte