PHP code example of tourze / quic-streams

1. Go to this page and download the library: Download tourze/quic-streams 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-streams example snippets


use Tourze\QUIC\Streams\StreamManager;
use Tourze\QUIC\FlowControl\FlowControlManager;

// 创建流控制管理器(可选)
$flowControlManager = new FlowControlManager();

// 创建流管理器
$streamManager = new StreamManager(
    isServer: true, 
    flowControlManager: $flowControlManager
);

use Tourze\QUIC\Core\Enum\StreamType;

// 创建双向流
$bidirectionalStream = $streamManager->createStream(StreamType::CLIENT_BIDI);

// 创建单向流
$unidirectionalStream = $streamManager->createStream(StreamType::CLIENT_UNI);

// 发送数据
$stream->send('Hello QUIC!', fin: false);
$stream->send('Final message', fin: true);

// 接收数据
$stream->receive('Received data', offset: 0, fin: false);

use Tourze\QUIC\Streams\StreamStateMachine;

$stateMachine = new StreamStateMachine();

// 状态转换
$success = $stateMachine->transitionSend(StreamSendState::SEND);

// 检查状态
if ($stateMachine->canSend()) {
    // 可以发送数据
}

if ($stateMachine->isClosed()) {
    // 流已关闭
}

use Tourze\QUIC\Streams\StreamBuffer;

// 创建缓冲区(默认1MB)
$buffer = new StreamBuffer();

// 添加发送数据
$offset = $buffer->addSendData('Data to send');

// 获取发送数据
$sendData = $buffer->getSendData(maxLength: 1200);

// 添加接收数据
$buffer->addRecvData('Received data', offset: 0);

// 获取连续的接收数据
$recvData = $buffer->getRecvData();

use Tourze\QUIC\Streams\StreamException;
use Tourze\QUIC\Core\Enum\QuicError;

try {
    $stream->send('data');
} catch (StreamException $e) {
    $quicError = $e->getQuicError();
    echo "QUIC Error: " . $quicError->name;
}