clrghouz/app/Console/Commands/StartServer.php
2021-07-02 23:44:20 +10:00

103 lines
2.1 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use App\Classes\Protocol\{Binkp,EMSI};
use App\Classes\Sock\SocketException;
use App\Classes\Sock\SocketServer;
use App\Models\Setup;
class StartServer extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'server:start';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Start Server';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$o = Setup::findOrFail(config('app.id'));
// @todo This should be a config item.
$start = [
'emsi' => [
'address'=>Setup::EMSI_BIND,
'port'=>Setup::EMSI_PORT,
'class'=>new EMSI($o),
],
'binkp' => [
'address'=>Setup::BINKP_BIND,
'port'=>Setup::BINKP_PORT,
'class'=>new Binkp($o),
],
];
$children = collect();
Log::debug(sprintf('%s:+ Starting Servers...',__METHOD__));
foreach ($start as $item => $config) {
Log::debug(sprintf('%s: - Starting: [%s]',__METHOD__,$item));
$pid = pcntl_fork();
if ($pid == -1)
die('could not fork');
// We are the child
if (! $pid) {
Log::info(sprintf('%s: - Started: [%s]',__METHOD__,$item));
$server = new SocketServer($config['port'],$config['address']);
$server->setConnectionHandler([$config['class'],'onConnect']);
try {
$server->listen();
} catch (SocketException $e) {
if ($e->getMessage() == 'Can\'t accept connections: "Success"')
Log::debug('Server Terminated');
else
Log::emergency('Uncaught Message: '.$e->getMessage());
}
Log::info(sprintf('%s: - Finished: [%s]',__METHOD__,$item));
// Child finished we need to get out of this loop.
exit;
}
$children->put($pid,$item);
}
// Wait for children to exit
while ($children->count()) {
// Wait for children to finish
$exited = pcntl_wait($status);
Log::info(sprintf('%s: - Exited: [%s]',__METHOD__,$children->pull($exited)));
}
// Done
Log::debug(sprintf('%s:= Finished.',__METHOD__));
}
}