[] (https://github.com/ThrusterIO/packet-handler/releases) [] (LICENSE) [] (https://travis-ci.org/ThrusterIO/packet-handler) [] (https://scrutinizer-ci.com/g/ThrusterIO/packet-handler) [] (https://scrutinizer-ci.com/g/ThrusterIO/packet-handler) [] (https://packagist.org/packets/thruster/packet-handler)
The Thruster PacketHandler Component.
Via Composer
$ composer require thruster/packet-handler
use Thruster\Component\EventLoop\EventLoop;
use Thruster\Component\Socket\SocketPair;
use Thruster\Component\PacketHandler\Packet;
use Thruster\Component\PacketHandler\PacketHandler;
use Thruster\Component\PacketHandler\StreamHandler;
class PingPacket extends Packet
{
const NAME = 'ping';
public function __construct()
{
parent::__construct(self::NAME);
}
}
class PongPacket extends Packet
{
const NAME = 'pong';
private $pid;
public function __construct()
{
$this->pid = posix_getpid();
parent::__construct(self::NAME);
}
/**
* @return int
*/
public function getPid()
{
return $this->pid;
}
}
$loop = new EventLoop();
$socketPair = new SocketPair($loop);
$socketPair->create();
$packetHandler = new PacketHandler();
$packetHandler->addHandler(PingPacket::NAME, function (PingPacket $packet) {
$packet->getStreamHandler()->send(new PongPacket());
});
$packetHandler->addHandler(PongPacket::NAME, function (PongPacket $packet) {
echo posix_getpid() . ': Received PONG from ' . $packet->getPid() . PHP_EOL;
});
if (pcntl_fork() > 0) {
$connection = $socketPair->useLeft();
$packetHandler->addProvider(new StreamHandler($connection));
$loop->addPeriodicTimer(2, function () use ($packetHandler) {
$packetHandler->dispatch(new PingPacket());
});
$loop->run();
} else {
$loop->afterFork();
$connection = $socketPair->useRight();
$packetHandler->addProvider(new StreamHandler($connection));
$loop->run();
}
$ composer test
Please see CONTRIBUTING and CONDUCT for details.
Please see License File for more information.