2021-11-09 00:15:47 +01:00
|
|
|
package Cualsea::Server::Loop;
|
|
|
|
|
|
|
|
use v5.30.0;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use IO::Socket::UNIX;
|
2021-11-11 02:32:25 +01:00
|
|
|
use Cualsea::FileSocket;
|
|
|
|
use Cualsea::Server::MessageController;
|
2021-11-09 00:15:47 +01:00
|
|
|
|
|
|
|
# The built-in constants are not that good.
|
|
|
|
use Const::Fast;
|
|
|
|
|
|
|
|
sub new {
|
|
|
|
my $class = shift;
|
|
|
|
|
|
|
|
# Return is optional but helpful while reading.
|
|
|
|
return bless {}, $class;
|
|
|
|
}
|
|
|
|
|
2021-11-11 02:32:25 +01:00
|
|
|
sub _socket {
|
2021-11-09 00:15:47 +01:00
|
|
|
my $self = shift;
|
|
|
|
if ( !defined $self->{socket} ) {
|
2021-11-11 02:32:25 +01:00
|
|
|
if (-e $Cualsea::FileSocket::SOCKET_PATH) {
|
|
|
|
warn 'Overwritting cualsea socket, this may be a bug.';
|
|
|
|
system 'rm', $Cualsea::FileSocket::SOCKET_PATH;
|
|
|
|
}
|
|
|
|
$self->{socket} = IO::Socket::UNIX->new(
|
2021-11-09 00:15:47 +01:00
|
|
|
Type => SOCK_STREAM(),
|
2021-11-11 02:32:25 +01:00
|
|
|
Local => $Cualsea::FileSocket::SOCKET_PATH,
|
2021-11-09 00:15:47 +01:00
|
|
|
Listen => 1,
|
|
|
|
);
|
2021-11-11 02:32:25 +01:00
|
|
|
chmod 0777, $Cualsea::FileSocket::SOCKET_PATH;
|
2021-11-09 00:15:47 +01:00
|
|
|
}
|
|
|
|
return $self->{socket};
|
|
|
|
}
|
|
|
|
|
2021-11-11 02:32:25 +01:00
|
|
|
sub _message_controller {
|
|
|
|
my $self = shift;
|
|
|
|
if (!defined $self->{message_controller}) {
|
|
|
|
$self->{message_controller} = Cualsea::Server::MessageController->new;
|
|
|
|
}
|
|
|
|
return $self->{message_controller};
|
|
|
|
}
|
|
|
|
|
2021-11-09 00:15:47 +01:00
|
|
|
sub run {
|
|
|
|
my $self = shift;
|
2021-11-11 02:32:25 +01:00
|
|
|
my $socket = $self->_socket;
|
2021-11-09 00:15:47 +01:00
|
|
|
my $conn = $socket->accept;
|
2021-11-11 02:32:25 +01:00
|
|
|
my $message_controller = $self->_message_controller;
|
2021-11-09 00:15:47 +01:00
|
|
|
if ($conn) {
|
2021-11-11 02:32:25 +01:00
|
|
|
$message_controller->dispatch ( socket => $conn );
|
2021-11-09 00:15:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
1
|