Skip to content

[WIP]feature(server): integrate tcp server and listenerManager #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Asiries335\redisSteamPhp\Consumer\DemoConsumer;
use Asiries335\redisSteamPhp\Consumer\Manager\TcpConsumerManager;
use Asiries335\redisSteamPhp\Server\TcpServer;
use Asiries335\redisSteamPhp\ServerProvider;

require_once __DIR__ . '/vendor/autoload.php';

// set server
$tcpServer = new TcpServer();
$tcpServer->setConfig([
'host' => '0.0.0.0',
'port' => '1236'
]);

// set listener manager
$tcpConsumerManager = new TcpConsumerManager();
$tcpConsumerManager->setConsumers([
new DemoConsumer()
]);

// run
$serverProvider = new ServerProvider(
$tcpServer,
$tcpConsumerManager
);

$serverProvider->boot();

5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
"require": {
"predis/predis": "^1.1",
"react/event-loop": "^1.1.1",
"dto/dto": "^3.2"
"dto/dto": "^3.2",
"react/stream": "^1.2",
"clue/redis-react": "^2.6",
"ext-sockets": "*"
},
"require-dev": {
"phpunit/phpunit": "^8.5",
Expand Down
8 changes: 8 additions & 0 deletions src/Consumer/ConsumerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Asiries335\redisSteamPhp\Consumer;

interface ConsumerInterface
{
public function handle($payload);
}
11 changes: 11 additions & 0 deletions src/Consumer/DemoConsumer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Asiries335\redisSteamPhp\Consumer;

class DemoConsumer implements ConsumerInterface
{

public function handle($payload) {
echo 'привет я Demo Consumer - ' . $payload;
}
}
26 changes: 26 additions & 0 deletions src/Consumer/Manager/ConsumerManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Asiries335\redisSteamPhp\Consumer\Manager;

use Asiries335\redisSteamPhp\Consumer\ConsumerInterface;

interface ConsumerManagerInterface
{
/**
* @param ConsumerInterface[] $consumers
* @return void
*/
public function setConsumers(array $consumers): void;

/**
* @param mixed $payload
* @return void
*/
public function handle($payload): void;


/**
* @return ConsumerInterface[]
*/
public function list(): array;
}
38 changes: 38 additions & 0 deletions src/Consumer/Manager/TcpConsumerManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Asiries335\redisSteamPhp\Consumer\Manager;

use Asiries335\redisSteamPhp\Consumer\ConsumerInterface;

class TcpConsumerManager implements ConsumerManagerInterface
{
/**
* @var ConsumerInterface[]
*/
private array $consumers;

/**
* @param ConsumerInterface[] $consumers
* @return void
*/
public function setConsumers(array $consumers): void {
$this->consumers = $consumers;
}

/**
* @param mixed $payload
* @return void
*/
public function handle($payload): void {
foreach ($this->consumers as $consumer) {
$consumer->handle($payload);
}
}

/**
* @return ConsumerInterface[]
*/
public function list(): array {
return $this->consumers;
}
}
31 changes: 31 additions & 0 deletions src/Server/ServerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Asiries335\redisSteamPhp\Server;

use Asiries335\redisSteamPhp\Consumer\ConsumerInterface;

interface ServerInterface
{

/**
* @param ConsumerInterface[] $consumers
* @return void
*/
public function setConsumers(array $consumers): void;

/**
* @param array $config
* @return void
*/
public function setConfig(array $config): void;

/**
* @return void
*/
public function start(): void;

/**
* @return void
*/
public function down(): void;
}
74 changes: 74 additions & 0 deletions src/Server/TcpServer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Asiries335\redisSteamPhp\Server;

use Asiries335\redisSteamPhp\Consumer\ConsumerInterface;
use React\Socket\ConnectionInterface;

/**
*
*/
class TcpServer implements ServerInterface
{

private array $config;

/**
* @var \React\Socket\ServerInterface
*/
private $tcpServer = null;


/**
* @var ConsumerInterface[]
*/
private array $consumers;

/**
* @param array $config
* @return void
*/
public function setConfig(array $config): void {
$this->config = $config;
}

/**
* @return void
*/
public function start(): void {
if ($this->tcpServer !== null) {
return;
}

$ip = $this->config['ip'] ?? '0.0.0.0';
$port = $this->config['port'] ?? '2341';

$this->tcpServer = new \React\Socket\TcpServer($ip . ':' . $port);

$this->tcpServer->on('connection', function (ConnectionInterface $connection) {

$connection->pipe($connection);

$connection->on('data', function ($payload) {
foreach ($this->consumers as $consumer) {
$consumer->handle($payload);
}
});
});
}

/**
* @return void
*/
public function down(): void {
$this->tcpServer->close();
}

/**
* @param ConnectionInterface[] $consumers
* @return void
*/
public function setConsumers(array $consumers): void {
$this->consumers = $consumers;
}
}
30 changes: 30 additions & 0 deletions src/ServerProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Asiries335\redisSteamPhp;

use Asiries335\redisSteamPhp\Consumer\Manager\ConsumerManagerInterface;
use Asiries335\redisSteamPhp\Server\ServerInterface;

class ServerProvider
{
private ServerInterface $server;
private ConsumerManagerInterface $consumerManager;

/**
* @param ServerInterface $server
* @param ConsumerManagerInterface $consumerManager
*
* @return void
*/
public function __construct(ServerInterface $server, ConsumerManagerInterface $consumerManager) {
$this->server = $server;
$this->consumerManager = $consumerManager;
}

public function boot(): void {
$this->server->setConsumers($this->consumerManager->list());
$this->server->start();
}


}