65 lines
1.7 KiB
PHP
65 lines
1.7 KiB
PHP
|
<?php
|
||
|
require __DIR__ . 'vendor/autoload.php';
|
||
|
require __DIR__ . 'includes/wrapper-swoole.php';
|
||
|
|
||
|
use Swoole\Runtime;
|
||
|
use Swoole\Table;
|
||
|
use Swoole\Server;
|
||
|
Runtime::enableCoroutine();
|
||
|
$table = new Table(1024);
|
||
|
$table->column('agi', Swoole\Table::TYPE_INT);
|
||
|
$table->column('data', Swoole\Table::TYPE_STRING, 65536);
|
||
|
$table->create();
|
||
|
$server = new Swoole\Server("0.0.0.0", 4573);
|
||
|
$server->table = $table;
|
||
|
|
||
|
$server->on('start', function ($server) {
|
||
|
echo "LOG ======> Servidor iniciou na porta 4573...." . PHP_EOL;
|
||
|
});
|
||
|
|
||
|
$server->on("connect", function (Server $server, $fd) {
|
||
|
echo "LOG ======> Cliente $fd conectado...." . PHP_EOL;
|
||
|
});
|
||
|
|
||
|
$server->on("receive", function ($server, $fd, $reactor_id, $data) {
|
||
|
echo "LOG ======> Dado recebido $data" . PHP_EOL;
|
||
|
$agiAlreadyExecuting = $server->table->get($fd, 'agi') === 1;
|
||
|
if (!$agiAlreadyExecuting) {
|
||
|
$server->table->set($fd, array(
|
||
|
'agi' => 1,
|
||
|
'data' => '',
|
||
|
));
|
||
|
}
|
||
|
|
||
|
//FIRST CONNECTION
|
||
|
if (!$agiAlreadyExecuting) {
|
||
|
$server->table->set($fd, array(
|
||
|
'agi' => 1,
|
||
|
'data' => '',
|
||
|
));
|
||
|
$agi = new \Hit\Ami\AGIEntrada(
|
||
|
array(
|
||
|
'agiRunCmd' => agiRunCmdSwooleWrapper($fd, $server),
|
||
|
'agiLog' => function ($info) {
|
||
|
print_r($info);
|
||
|
print_r('');
|
||
|
}
|
||
|
)
|
||
|
);
|
||
|
$agi->run();
|
||
|
} else {
|
||
|
//ALREADY CONNECTED, ONLY APPEND DATA TO BUFFER
|
||
|
$agiConnectionData = $server->table->get($fd, 'data');
|
||
|
$agiConnectionData .= $data;
|
||
|
$server->table->set($fd, array(
|
||
|
'agi' => 1,
|
||
|
'data' => $agiConnectionData
|
||
|
));
|
||
|
}
|
||
|
});
|
||
|
|
||
|
$server->on("close", function ($server, $fd) {
|
||
|
echo "LOG ======> Cliente $fd desconectado...." . PHP_EOL;
|
||
|
});
|
||
|
|
||
|
$server->start();
|