PHPackages                             tourze/quic-core - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. tourze/quic-core

ActiveLibrary

tourze/quic-core
================

QUIC协议核心基础库 - 协议常量、错误码、枚举类型和基础工具

0.1.0(6mo ago)02396MITPHPCI passing

Since Jun 3Pushed 6mo agoCompare

[ Source](https://github.com/tourze/quic-core)[ Packagist](https://packagist.org/packages/tourze/quic-core)[ RSS](/packages/tourze-quic-core/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (6)Versions (3)Used By (6)

QUIC Core Package
=================

[](#quic-core-package)

[English](README.md) | [中文](README.zh-CN.md)

[![PHP Version](https://camo.githubusercontent.com/7535257ca228724c93658bd52583d4e47a9bab02c356abf6e54c1d575f2151e6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e312532422d626c75652e737667)](https://php.net)[![Tests](https://camo.githubusercontent.com/435b09605d60e0ad49de8f333480906e91aabcedcdd1172ac6496404852aed09/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f54657374732d3138362532305061737365642d677265656e2e737667)](tests/)[![PHPStan](https://camo.githubusercontent.com/c21c85be426f99098eaf28c24c2cc23a5e617c5e4b47f09edc3fae94846357bc/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d4c6576656c2532304d61782d627269676874677265656e2e737667)](https://github.com/phpstan/phpstan)[![License](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](LICENSE)[![Coverage](https://camo.githubusercontent.com/f751cf9ce74c566617800f7e88d9896e9bd6e07fad747e3144ae9ef03e880788/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f436f7665726167652d3130302532352d627269676874677265656e2e737667)](tests/)

QUIC协议核心基础库，提供QUIC协议的核心枚举、常量、工具类和异常处理机制。

目录
--

[](#目录)

- [功能特性](#%E5%8A%9F%E8%83%BD%E7%89%B9%E6%80%A7)
- [安装](#%E5%AE%89%E8%A3%85)
- [快速开始](#%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7%8B)
- [规范遵循](#%E8%A7%84%E8%8C%83%E9%81%B5%E5%BE%AA)
- [架构设计](#%E6%9E%B6%E6%9E%84%E8%AE%BE%E8%AE%A1)
- [测试](#%E6%B5%8B%E8%AF%95)
- [开发](#%E5%BC%80%E5%8F%91)
- [性能](#%E6%80%A7%E8%83%BD)
- [贡献](#%E8%B4%A1%E7%8C%AE)
- [许可证](#%E8%AE%B8%E5%8F%AF%E8%AF%81)

功能特性
----

[](#功能特性)

### 🎯 核心枚举

[](#-核心枚举)

- **QuicError** - QUIC错误码枚举，包含所有RFC 9000定义的错误类型
- **ConnectionState** - 连接状态管理，支持状态转换验证
- **FrameType** - 帧类型定义，包含完整的帧特性判断方法
- **PacketType** - 包类型枚举，支持长/短包头判断
- **StreamSendState/StreamRecvState** - 流状态机实现
- **StreamType** - 流类型识别（双向/单向，客户端/服务端）
- **TLSState** - TLS握手状态枚举
- **ECNState** - ECN功能状态枚举
- **PathState** - 路径验证状态枚举

### 🛠️ 工具类

[](#️-工具类)

- **VariableInteger** - 变长整数编解码器，完全符合RFC 9000规范
- **ConnectionId** - 连接ID生成器和管理工具
- **Constants** - 完整的QUIC协议常量定义

### ⚠️ 异常处理

[](#️-异常处理)

- **QuicException** - 基础异常类，集成QUIC错误码
- **ConnectionException** - 连接层异常
- **StreamException** - 流层异常
- **FrameException** - 帧处理异常
- **CryptoException** - 加密层异常

安装
--

[](#安装)

```
composer require tourze/quic-core
```

快速开始
----

[](#快速开始)

### 变长整数编解码

[](#变长整数编解码)

```
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);
```

### 连接ID管理

[](#连接id管理)

```
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;
```

规范遵循
----

[](#规范遵循)

本包严格遵循以下规范：

- 📋 **RFC 9000** - QUIC: A UDP-Based Multiplexed and Secure Transport
- 🔐 **RFC 9001** - Using TLS to Secure QUIC
- 🛡️ **RFC 9002** - QUIC Loss Detection and Congestion Control
- 📝 **PSR-1** - Basic Coding Standard
- 🏗️ **PSR-4** - Autoloader Standard
- ✨ **PSR-12** - Extended Coding Style

架构设计
----

[](#架构设计)

### 设计原则

[](#设计原则)

- **第一性原理** - 从QUIC协议本质出发设计
- **SOLID原则** - 单一职责、开闭原则、里式替换等
- **DRY原则** - 避免重复代码
- **KISS原则** - 保持简单易懂
- **YAGNI原则** - 只实现需要的功能

### 包结构

[](#包结构)

```
src/
├── Enum/           # 核心枚举类
├── Exception/      # 异常处理类
├── Constants.php   # 协议常量
├── VariableInteger.php  # 变长整数工具
└── ConnectionId.php     # 连接ID工具

tests/
├── Enum/          # 枚举测试
├── Exception/     # 异常测试
└── *.php         # 工具类测试

```

测试
--

[](#测试)

运行所有测试：

```
composer install
vendor/bin/phpunit
```

测试统计：

- ✅ **186个测试** 全部通过
- ✅ **898个断言** 全部成功
- 📊 **100%** 核心功能覆盖

开发
--

[](#开发)

### 环境要求

[](#环境要求)

- PHP 8.1+
- ext-mbstring

### 开发依赖

[](#开发依赖)

- PHPUnit 10.0+ （测试框架）
- PHPStan 2.1+ （静态分析）

### 代码风格

[](#代码风格)

本项目遵循PSR-12编码标准，并有以下额外规范：

- 使用PHP 8.1+特性（枚举、readonly属性等）
- 中文注释说明
- 严格类型声明
- 完整的PHPDoc注释

性能
--

[](#性能)

### 基准测试

[](#基准测试)

VariableInteger编解码性能（10,000次迭代）：

- 编码：&lt; 0.1秒
- 解码：&lt; 0.1秒
- 往返转换：&lt; 0.2秒

### 内存使用

[](#内存使用)

- 枚举类：零运行时开销
- 工具类：最小内存占用
- 异常类：继承标准Exception

贡献
--

[](#贡献)

欢迎提交Issue和Pull Request！

### 开发流程

[](#开发流程)

1. Fork本仓库
2. 创建特性分支 (`git checkout -b feature/AmazingFeature`)
3. 提交更改 (`git commit -m 'Add some AmazingFeature'`)
4. 推送到分支 (`git push origin feature/AmazingFeature`)
5. 开启Pull Request

### 提交规范

[](#提交规范)

- 🎯 feat: 新功能
- 🐛 fix: 修复bug
- 📚 docs: 文档更新
- 🎨 style: 代码格式调整
- ♻️ refactor: 重构代码
- ✅ test: 添加测试
- 🔧 chore: 构建工具等

许可证
---

[](#许可证)

[MIT License](LICENSE)

相关链接
----

[](#相关链接)

- [QUIC Working Group](https://quicwg.org/)
- [RFC 9000 - QUIC Protocol](https://tools.ietf.org/html/rfc9000)
- [PHP Documentation](https://php.net)

---

**注意**: 这是一个基础库，提供QUIC协议的核心类型和工具。完整的QUIC实现需要配合其他协议层包使用。

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance68

Regular maintenance activity

Popularity11

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity28

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~155 days

Total

2

Last Release

187d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e354fdb316da535dfa8ba2e9193a473c403b6bc6fb9170778d1dc50e304c6e9d?d=identicon)[tourze](/maintainers/tourze)

---

Top Contributors

[![tourze](https://avatars.githubusercontent.com/u/13899502?v=4)](https://github.com/tourze "tourze (2 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tourze-quic-core/health.svg)

```
[![Health](https://phpackages.com/badges/tourze-quic-core/health.svg)](https://phpackages.com/packages/tourze-quic-core)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
