2017 © Pedro Peláez
 

library packet-handler

Thruster PacketHandler Component

image

thruster/packet-handler

Thruster PacketHandler Component

  • Wednesday, May 25, 2016
  • by gcds
  • Repository
  • 1 Watchers
  • 0 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

PacketHandler Component

[Latest Version] (https://github.com/ThrusterIO/packet-handler/releases) [Software License] (LICENSE) [Build Status] (https://travis-ci.org/ThrusterIO/packet-handler) [Code Coverage] (https://scrutinizer-ci.com/g/ThrusterIO/packet-handler) [Quality Score] (https://scrutinizer-ci.com/g/ThrusterIO/packet-handler) [Total Downloads] (https://packagist.org/packets/thruster/packet-handler), (*1)

[Email] (mailto:team@thruster.io), (*2)

The Thruster PacketHandler Component., (*3)

Install

Via Composer, (*4)

$ composer require thruster/packet-handler

Usage

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();
}

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details., (*5)

License

Please see License File for more information., (*6)

The Versions

25/05 2016

dev-master

9999999-dev https://thruster.io

Thruster PacketHandler Component

  Sources   Download

MIT

The Requires

 

The Development Requires

thruster packet-handler

25/05 2016

1.0.0

1.0.0.0 https://thruster.io

Thruster PacketHandler Component

  Sources   Download

MIT

The Requires

 

The Development Requires

thruster packet-handler