This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
vbbs/app/Console/Commands/Server.php

79 lines
1.5 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',$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('Server Terminated.');
else
Log::emergency('Uncaught Message: '.$e->getMessage());
}
}
}