cualsea/cualsea-server/lib/Cualsea/Server/Loop.pm

57 lines
1.3 KiB
Perl
Raw Normal View History

package Cualsea::Server::Loop;
use v5.30.0;
use strict;
use warnings;
use IO::Socket::UNIX;
use Cualsea::FileSocket;
use Cualsea::Server::MessageController;
# 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;
}
sub _socket {
my $self = shift;
if ( !defined $self->{socket} ) {
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(
Type => SOCK_STREAM(),
Local => $Cualsea::FileSocket::SOCKET_PATH,
Listen => 1,
);
chmod 0777, $Cualsea::FileSocket::SOCKET_PATH;
}
return $self->{socket};
}
sub _message_controller {
my $self = shift;
if (!defined $self->{message_controller}) {
$self->{message_controller} = Cualsea::Server::MessageController->new;
}
return $self->{message_controller};
}
sub run {
my $self = shift;
my $socket = $self->_socket;
my $conn = $socket->accept;
my $message_controller = $self->_message_controller;
if ($conn) {
$message_controller->dispatch ( socket => $conn );
}
}
1