PHP code example of tourze / quic-core
1. Go to this page and download the library: Download tourze/quic-core 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-core example snippets
use Tourze\QUIC\Core\VariableInteger;
// 编码
$encoded = VariableInteger::encode(12345);
// 解码
[$value, $consumed] = VariableInteger::decode($encoded);
echo $value; // 12345
echo $consumed; // 消耗的字节数
// 批量操作
$values = [100, 200, 300];
$encoded = VariableInteger::encodeMultiple($values);
[$decoded, $totalConsumed] = VariableInteger::decodeMultiple($encoded, 3);
use Tourze\QUIC\Core\ConnectionId;
// 生成连接ID
$connectionId = ConnectionId::generate(8); // 生成8字节连接ID
$randomId = ConnectionId::random(4, 16); // 生成4-16字节随机长度ID
// 十六进制转换
$hex = ConnectionId::toHex($connectionId);
$restored = ConnectionId::fromHex($hex);
// 重置令牌
$secret = random_bytes(16);
$token = ConnectionId::generateResetToken($connectionId, $secret);
$isValid = ConnectionId::verifyResetToken($connectionId, $token, $secret);
use Tourze\QUIC\Core\Exception\ConnectionException;
use Tourze\QUIC\Core\Exception\StreamException;
// 连接异常
throw ConnectionException::refused('服务器拒绝连接');
throw ConnectionException::protocolViolation('无效帧格式');
// 流异常
throw StreamException::stateError('流已关闭');
throw StreamException::flowControlError('超出流量限制');
use Tourze\QUIC\Core\Enum\ConnectionState;
use Tourze\QUIC\Core\Enum\FrameType;
use Tourze\QUIC\Core\Enum\QuicError;
// 连接状态管理
$state = ConnectionState::NEW;
if ($state->canSendData()) {
// 可以发送数据
}
// 帧类型判断
$frameType = FrameType::STREAM;
if ($frameType->isStreamFrame()) {
// 处理流帧
}
// 错误分类
$error = QuicError::CONNECTION_REFUSED;
if ($error->isFatal()) {
// 致命错误,关闭连接
}
use Tourze\QUIC\Core\Constants;
// 协议版本
$version = Constants::VERSION_1;
$isSupported = Constants::isSupportedVersion($version);
// 默认配置
$params = Constants::getDefaultTransportParameters();
// 大小限制
$maxPacketSize = Constants::MAX_PACKET_SIZE;
$maxConnectionIdLength = Constants::MAX_CONNECTION_ID_LENGTH;
text
src/
├── Enum/ # 核心枚举类
├── Exception/ # 异常处理类
├── Constants.php # 协议常量
├── VariableInteger.php # 变长整数工具
└── ConnectionId.php # 连接ID工具
tests/
├── Enum/ # 枚举测试
├── Exception/ # 异常测试
└── *.php # 工具类测试