79 lines
1.6 KiB
PHP
79 lines
1.6 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Viewdata/Videotex Server
|
|
*
|
|
* Inspired by Rob O'Donnell at irrelevant.com
|
|
*/
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Sock\{SocketServer,SocketException};
|
|
|
|
use App\Models\Mode;
|
|
|
|
class Server extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'server {--mode=VideoTex : Server Mode Profile}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Start Server';
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return mixed
|
|
* @throws \Sock\SocketException
|
|
*/
|
|
public function handle()
|
|
{
|
|
/**
|
|
* Check dependencies
|
|
*/
|
|
if (! extension_loaded('sockets' ) ) {
|
|
throw new SocketException(SocketException::CANT_ACCEPT,'Missing sockets extension');
|
|
}
|
|
|
|
if (! extension_loaded('pcntl' ) ) {
|
|
throw new SocketException(SocketException::CANT_ACCEPT,'Missing pcntl extension');
|
|
}
|
|
|
|
$mo = Mode::where('name','LIKE',$this->option('mode'))->firstOrFail();
|
|
|
|
$server = new SocketServer(config('app.port'),config('app.bind'));
|
|
$server->init();
|
|
$server->setConnectionHandler([$mo->server(),'onConnect']);
|
|
|
|
try {
|
|
$server->listen();
|
|
|
|
} catch (SocketException $e) {
|
|
if ($e->getMessage() == 'Can\'t accept connections: "Success"')
|
|
Log::debug(sprintf('Server Terminated: %s',$mo->name));
|
|
else
|
|
Log::emergency('Uncaught Message: '.$e->getMessage(),['name'=>$mo->name]);
|
|
}
|
|
}
|
|
} |