tourze / workerman-chain-protocol
Workerman 链式协议处理器,支持多层协议栈处理数据编解码
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/tourze/workerman-chain-protocol
Requires
- php: ^8.1
- phpunit/php-timer: ^6.0.0
- psr/log: ^3|^2|^1
- symfony/event-dispatcher: ^6.4
- symfony/event-dispatcher-contracts: ^2.5 | ^3
- workerman/workerman: ^5.1
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-10-31 07:57:34 UTC
README
[]
(https://packagist.org/packages/tourze/workerman-chain-protocol)
[
]
(composer.json)
[
]
(https://github.com/tourze/php-monorepo/actions)
[
]
(https://codecov.io/gh/tourze/php-monorepo)
[
]
(LICENSE)
Chain protocol processing for Workerman. This package allows you to create a chain of protocol parsers to decode and encode data in Workerman connections.
Features
- Chain-style protocol processing for Workerman
- Protocol-agnostic, works with any protocol that implements Workerman's ProtocolInterface
- Support for both TCP and UDP connections
- Event-driven architecture with Symfony Event Dispatcher
- Comprehensive logging
- Performance monitoring using PHP Timer
Installation
composer require tourze/workerman-chain-protocol
Quick Start
<?php use Tourze\Workerman\ChainProtocol\ChainProtocol; use Tourze\Workerman\ChainProtocol\Container; use Workerman\Worker; // Setup your custom protocol classes that implement ProtocolInterface Container::$decodeProtocols = [ MyProtocol1::class, MyProtocol2::class, // Add more protocols as needed ]; Container::$encodeProtocols = [ MyProtocol2::class, MyProtocol1::class, // The order matters for encoding (reverse of decoding) ]; // Setup logger if needed Container::$logger = new MyLogger(); // Setup event dispatcher if needed Container::$eventDispatcher = new MyEventDispatcher(); // Create a Workerman server with ChainProtocol $worker = new Worker('tcp://0.0.0.0:8080'); $worker->protocol = ChainProtocol::class; $worker->onMessage = function($connection, $data) { // $data is already decoded through all protocols in the chain // Process your business logic here // Send a response, which will be encoded through all protocols in the chain $connection->send('Your response'); }; Worker::runAll();
Dependencies
This package requires:
- PHP 8.1 or higher
- workerman/workerman ^5.1
- symfony/event-dispatcher ^7.3
- symfony/event-dispatcher-contracts ^3
- tourze/workerman-connection-context 0.0.*
- psr/log ^1|^2|^3
- phpunit/php-timer ^7.0
Advanced Usage
Custom Protocol Implementation
<?php use Workerman\Protocols\ProtocolInterface; use Workerman\Connection\ConnectionInterface; class MyCustomProtocol implements ProtocolInterface { public static function input(string $buffer, ConnectionInterface $connection): int { // Return the length of a complete packet, or 0 if more data is needed return strlen($buffer) >= 4 ? unpack('N', substr($buffer, 0, 4))[1] + 4 : 0; } public static function decode(string $buffer, ConnectionInterface $connection): string { // Remove the length header and return the actual data return substr($buffer, 4); } public static function encode(mixed $data, ConnectionInterface $connection): string { // Add a length header to the data return pack('N', strlen($data)) . $data; } }
Event Handling
<?php use Symfony\Component\EventDispatcher\EventDispatcher; use Tourze\Workerman\ChainProtocol\Event\ChainDataDecodedEvent; use Tourze\Workerman\ChainProtocol\Container; $eventDispatcher = new EventDispatcher(); $eventDispatcher->addListener(ChainDataDecodedEvent::class, function(ChainDataDecodedEvent $event) { // Log or process decoded data error_log('Data decoded: ' . $event->getBuffer()); }); Container::$eventDispatcher = $eventDispatcher;
Custom Logger Configuration
<?php use Monolog\Logger; use Monolog\Handler\StreamHandler; use Tourze\Workerman\ChainProtocol\Container; $logger = new Logger('chain-protocol'); $logger->pushHandler(new StreamHandler('path/to/logfile.log', Logger::DEBUG)); Container::$logger = $logger;
Contributing
Please see CONTRIBUTING.md for details.
License
The MIT License (MIT). Please see License File for more information.