clrghouz/app/Classes/Sock/SocketServer.php

166 lines
4.0 KiB
PHP
Raw Normal View History

2021-05-07 12:07:26 +00:00
<?php
namespace App\Classes\Sock;
use Illuminate\Support\Facades\Log;
use App\Classes\Sock\Exception\{HAproxyException,SocketException};
2021-05-07 12:07:26 +00:00
final class SocketServer {
private const LOGKEY = 'SS-';
2021-05-07 12:07:26 +00:00
private $server; // Our server resource
private string $bind; // The IP address to bind to
private int $port; // The Port to bind to
private int $backlog = 5; // Number of incoming connections queued
2023-04-22 11:30:30 +00:00
private int $type; // Socket type
2021-05-07 12:07:26 +00:00
private array $handler; // The class and method that will handle our connection
2023-04-22 11:30:30 +00:00
public function __construct(int $port,string $bind='0.0.0.0',int $type=SOCK_STREAM)
2021-05-07 12:07:26 +00:00
{
$this->bind = $bind;
$this->port = $port;
2023-04-22 11:30:30 +00:00
$this->type = $type;
2021-05-07 12:07:26 +00:00
$this->createSocket();
2021-05-07 12:07:26 +00:00
if (socket_bind($this->server,$this->bind,$this->port) === FALSE)
throw new SocketException(SocketException::CANT_BIND_SOCKET,socket_strerror(socket_last_error($this->server)));
}
public function __get($key)
{
switch ($key) {
case 'handler':
return $this->handler;
default:
throw new \Exception('Unknown key: '.$key);
}
}
public function __set($key,$value)
{
switch ($key) {
case 'handler':
return $this->handler = $value;
default:
throw new \Exception('Unknown key: '.$key);
}
}
2021-05-07 12:07:26 +00:00
/**
* Create our Socket
*
* @throws SocketException
*/
private function createSocket(): void
2021-05-07 12:07:26 +00:00
{
/**
* 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');
2023-04-22 11:30:30 +00:00
switch ($this->type) {
case SOCK_STREAM:
$this->server = socket_create(AF_INET|AF_INET6,$this->type,SOL_TCP);
break;
case SOCK_DGRAM:
$this->server = socket_create(AF_INET|AF_INET6,$this->type,SOL_UDP);
break;
default:
throw new \Exception('Unknown socket_type:'.$this->type);
}
2021-05-07 12:07:26 +00:00
if ($this->server === FALSE)
throw new SocketException(SocketException::CANT_CREATE_SOCKET,socket_strerror(socket_last_error()));
socket_set_option($this->server,SOL_SOCKET,SO_REUSEADDR,1);
}
/**
* Our main loop where we listen for connections
*
* @throws SocketException
*/
public function listen()
{
if (! $this->handler)
throw new SocketException(SocketException::CANT_LISTEN,'Handler not set.');
2023-04-22 11:30:30 +00:00
if (in_array($this->type,[SOCK_STREAM,SOCK_SEQPACKET]))
if (socket_listen($this->server,$this->backlog) === FALSE)
throw new SocketException(SocketException::CANT_LISTEN,socket_strerror(socket_last_error($this->server)));
2021-05-07 12:07:26 +00:00
Log::info(sprintf('%s:- Listening on [%s:%d]',self::LOGKEY,$this->bind,$this->port));
2021-05-07 12:07:26 +00:00
2023-04-22 11:30:30 +00:00
switch ($this->type) {
case SOCK_STREAM:
$this->loop_tcp();
break;
case SOCK_DGRAM:
$this->loop_udp();
break;
}
2021-05-07 12:07:26 +00:00
socket_close($this->server);
Log::info(sprintf('%s:= Closed [%s:%d]',self::LOGKEY,$this->bind,$this->port));
2021-05-07 12:07:26 +00:00
}
/**
2021-07-19 14:23:41 +00:00
* Manage and execute incoming connections
2021-05-07 12:07:26 +00:00
*
* @throws SocketException
*/
private function loop_tcp(): void
2021-05-07 12:07:26 +00:00
{
while (TRUE) {
if (($accept = socket_accept($this->server)) === FALSE)
throw new SocketException(SocketException::CANT_ACCEPT,socket_strerror(socket_last_error($this->server)));
try {
$r = new SocketClient($accept);
} catch (HAproxyException $e) {
Log::notice(sprintf('%s:! HAPROXY Exception [%s]',self::LOGKEY,$e->getMessage()));
socket_close($accept);
continue;
2023-10-12 10:29:02 +00:00
} catch (\Exception $e) {
Log::notice(sprintf('%s:! Creating Socket client failed? [%s]',self::LOGKEY,$e->getMessage()));
2023-10-12 10:29:02 +00:00
socket_close($accept);
continue;
}
// If the handler returns a value, then that is the main thread
if (! $this->handler[0]->{$this->handler[1]}($r)) {
$r->close();
exit(0);
}
2021-05-07 12:07:26 +00:00
}
}
private function loop_udp(): void
2023-04-22 11:30:30 +00:00
{
while (TRUE) {
$r = new SocketClient($this->server);
if ($r->hasData(30)) {
2024-11-26 06:03:59 +00:00
if (! ($this->handler[0]->{$this->handler[1]}($r)))
exit(0);
// Sleep so our thread has a chance to pick up the data from our connection
usleep(50000);
}
2023-04-22 11:30:30 +00:00
}
}
2021-05-07 12:07:26 +00:00
}