PHP code example of tourze / quic-flow-control
1. Go to this page and download the library: Download tourze/quic-flow-control library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tourze / quic-flow-control example snippets
use Tourze\QUIC\FlowControl\FlowControlManager;
// 创建流量控制管理器
$manager = new FlowControlManager(
1048576, // 连接级最大数据量 (1MB)
1048576, // 本地连接级最大数据量
65536, // 流级最大数据量 (64KB)
65536 // 本地流级最大数据量
);
// 创建流
$stream = $manager->createStream(1);
// 发送数据
$success = $manager->sendData(1, 1024);
if (!$success) {
echo "发送失败,窗口不足\n";
}
// 接收数据
$manager->receiveData(1, 512);
// 检查健康状态
$health = $manager->getHealthStatus();
echo "健康状态: " . $health['status'] . "\n";
// 获取待发送的控制帧
$frames = $manager->getPendingFrames();
foreach ($frames as $frame) {
echo "待发送: " . $frame['type'] . "\n";
}
use Tourze\QUIC\FlowControl\FlowControlManager;
use Tourze\QUIC\Core\Constants;
// 创建高性能配置的流量控制管理器
$manager = new FlowControlManager(
16 * 1024 * 1024, // 16MB 连接窗口 - 适合高带宽场景
16 * 1024 * 1024, // 16MB 本地连接窗口
1024 * 1024, // 1MB 流窗口 - 适合大文件传输
1024 * 1024 // 1MB 本地流窗口
);
// 获取详细统计信息
$stats = $manager->getFullStats();
// 连接级统计
$connectionStats = $stats['connection'];
echo "连接窗口利用率: " . ($connectionStats['send_window']['utilization'] * 100) . "%\n";
echo "已发送字节: " . $connectionStats['send_window']['bytes_sent'] . "\n";
echo "窗口大小: " . $connectionStats['send_window']['window_size'] . "\n";
// 流级统计
foreach ($stats['streams'] as $streamId => $streamStats) {
echo "流 {$streamId} 状态: " . ($streamStats['blocked'] ? '阻塞' : '正常') . "\n";
echo "流 {$streamId} 利用率: " . ($streamStats['send_window']['utilization'] * 100) . "%\n";
}
// 健康状态监控
$health = $stats['health'];
if ($health['status'] !== 'healthy') {
echo "警告: 流量控制状态异常 - " . $health['status'] . "\n";
foreach ($health['warnings'] as $warning) {
echo "- " . $warning . "\n";
}
}
// 处理接收到的流控制帧
foreach ($receivedFrames as $frame) {
switch ($frame['type']) {
case 'MAX_DATA':
$manager->handleMaxDataFrame($frame['max_data']);
break;
case 'MAX_STREAM_DATA':
$manager->handleMaxStreamDataFrame(
$frame['stream_id'],
$frame['max_stream_data']
);
break;
case 'DATA_BLOCKED':
$manager->handleDataBlockedFrame($frame['data_limit']);
break;
case 'STREAM_DATA_BLOCKED':
$manager->handleStreamDataBlockedFrame(
$frame['stream_id'],
$frame['stream_data_limit']
);
break;
}
}
// 获取需要发送的控制帧
$pendingFrames = $manager->getPendingFrames();
foreach ($pendingFrames as $frame) {
// 发送到网络层
sendToNetwork($frame);
}
// 1. 批量操作多个流
$streamIds = [1, 3, 5, 7, 9];
foreach ($streamIds as $streamId) {
if ($manager->canSendData($streamId, 1024)) {
$manager->sendData($streamId, 1024);
}
}
// 2. 定期检查健康状态
$healthCheckInterval = 30; // 30秒
if (time() % $healthCheckInterval === 0) {
$health = $manager->getHealthStatus();
if ($health['status'] !== 'healthy') {
// 记录警告或采取纠正措施
logWarning('Flow control health issue', $health);
}
}
// 3. 动态调整窗口大小(根据网络条件)
$connectionStats = $manager->getConnectionController()->getConnectionStats();
$utilization = $connectionStats['connection']['send_window']['utilization'];
if ($utilization > 0.8) {
// 高利用率 - 考虑请求更大窗口
echo "建议请求更大的连接窗口\n";
}
use Tourze\QUIC\FlowControl\Exception\FlowControlException;
use Tourze\QUIC\FlowControl\Exception\InvalidFlowControlWindowException;
try {
$success = $manager->sendData($streamId, $dataSize);
if (!$success) {
// 发送失败,检查原因
if ($manager->isConnectionBlocked()) {
echo "连接级流量控制阻塞\n";
} elseif ($manager->isStreamBlocked($streamId)) {
echo "流级流量控制阻塞\n";
}
}
} catch (FlowControlException $e) {
echo "流量控制错误: " . $e->getMessage() . "\n";
// 重置流量控制状态(极端情况)
$manager->reset();
} catch (InvalidFlowControlWindowException $e) {
echo "无效的流量控制窗口: " . $e->getMessage() . "\n";
}
bash
cd packages/quic-flow-control
php examples/demo.php