用asio2如何实现网络限速

asio2框架实现了网络速度限制功能,使用起来很简单(可以参考example文件夹的示例:/asio2/example/rate_limit/rate_limit.cpp):

首先定义一个宏开启这个功能

#define ASIO2_INCLUDE_RATE_LIMIT
服务端限速
asio2::tcp_rate_server tcp_server;

tcp_server.bind_connect([](std::shared_ptr<asio2::tcp_rate_session>& session_ptr)
{
	// 针对每个连接,你可以设置不同的速度限制
	// 可以只设置发送速度,不设置接收速度,等等,如果没有下面的设置的话,默认是不限速

	// 设置发送速度 这里256是指每秒发送256个字节 以下同理
	session_ptr->socket().rate_policy().write_limit(256);

	// 设置接收速度
	session_ptr->socket().rate_policy().read_limit(256);

}).bind_recv([](auto& session_ptr, std::string_view data)
{
	printf("recv : %zu %.*s\n", data.size(), (int)data.size(), data.data());

	session_ptr->async_send(data);
});

tcp_server.start("0.0.0.0", 8102);
客户端限速
asio2::tcp_rate_client tcp_client;

tcp_client.bind_connect([&tcp_client]()
{
	// 设置发送速度
	tcp_client.socket().rate_policy().write_limit(512);

	// 设置接收速度
	tcp_client.socket().rate_policy().read_limit(512);

	if (!asio2::get_last_error())
	{
		std::string str;

		for (int i = 0; i < 1024; i++)
		{
			str += '0' + std::rand() % 64;
		}

		tcp_client.async_send(str);
	}
}).bind_recv([&tcp_client](std::string_view data)
{
	tcp_client.async_send(data);
});

tcp_client.start("127.0.0.1", 8102);

多线程设置时要注意,如:

std::thread([&tcp_client]()
{
	// 注意:如果是在其它线程中设置限速的话,最好用post投递一个事件,这样确保
	// 设置速度的函数是在io_context 线程中调用的,确保线程安全
	tcp_client.post([&tcp_client]()
	{
		tcp_client.socket().rate_policy().read_limit(512);
	});
}).detach();

最后编辑于:2023-03-30

ASIO 2.3 SDK Contents --------------------- readme.txt - this file changes.txt - contains change information between SDK releases ASIO SDK 2.3.pdf - ASIO SDK 2.3 specification Steinberg ASIO Licensing Agreement.pdf - Licencing Agreement common: asio.h - ASIO C definition iasiodrv.h - interface definition for the ASIO driver class asio.cpp - asio host interface (not used on Mac) asiodrvr.h asiodrvr.cpp - ASIO driver class base definition combase.h combase.cpp - COM base definitions (PC only) dllentry.cpp - DLL functions (PC only) register.cpp - driver self registration functionality wxdebug.h debugmessage.cpp - some debugging help host: asiodrivers.h asiodrivers.cpp - ASIO driver managment (enumeration and instantiation) ASIOConvertSamples.h ASIOConvertSamples.cpp - sample data format conversion class ginclude.h - platform specific definitions host/pc: asiolist.h asiolist.cpp - instantiates an ASIO driver via the COM model host/sample: hostsample.cpp - a simple console app which shows ASIO hosting hostsample.dsp - MSVC++ 5.0 project hostsample.vcproj - Visual Studio 2005 project (32 and 64 bit targets) driver/asiosample: asiosmpl.h asiosmpl.cpp - ASIO 2.0 sample driver wintimer.cpp - bufferSwitch() wakeup thread (Windows) asiosample.def - Windows DLL module export definition mactimer.cpp - bufferSwitch() wakeup thread (Macintosh) macnanosecs.cpp - Macintosh system reference time makesamp.cpp - Macintosh driver object instantiation driver/asiosample/asiosample: asiosample.dsp - MSVC++ 5.0 project asiosample.vcproj - Visual Studio 2005 project (32 and 64 bit targets)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值