diff --git a/Build.PL b/Build.PL index 715a303..e06748e 100755 --- a/Build.PL +++ b/Build.PL @@ -10,6 +10,8 @@ my $build = Module::Build->new( dist_abstract => 'The emulator webpage.', requires => { 'Mojolicious' => 0, + 'Moo' => 0, + 'Types::Standard' => 0, }, ); $build->create_build_script; diff --git a/lib/MSGBA/Web.pm b/lib/MSGBA/Web.pm index e69de29..57bce0f 100644 --- a/lib/MSGBA/Web.pm +++ b/lib/MSGBA/Web.pm @@ -0,0 +1,21 @@ +package MSGBA::Web; +use Mojo::Base 'Mojolicious', -signatures; + +# This method will run once at server start +sub startup ($self) { + + # Load configuration from config file + my $config = $self->plugin('NotYAMLConfig' => { file => './msgba-web.yml' }); + + # Configure the application + $self->secrets($config->{secrets}); + + # Router + my $r = $self->routes; + + # Normal route to controller + $r->get('/')->to('Static#index'); + $r->websocket('/ws')->to('WS#proxy'); +} + +1; diff --git a/lib/MSGBA/Web/Controller/Static.pm b/lib/MSGBA/Web/Controller/Static.pm new file mode 100644 index 0000000..fb5d6f7 --- /dev/null +++ b/lib/MSGBA/Web/Controller/Static.pm @@ -0,0 +1,10 @@ +package MSGBA::Web::Controller::Static; + +use v5.34.1; + +use strict; +use warnings; + +use Mojo::Base 'Mojolicious::Controller', -signatures; + +1; diff --git a/lib/MSGBA/Web/Controller/WS.pm b/lib/MSGBA/Web/Controller/WS.pm new file mode 100644 index 0000000..2cd3738 --- /dev/null +++ b/lib/MSGBA/Web/Controller/WS.pm @@ -0,0 +1,110 @@ +package MSGBA::Web::Controller::WS; + +use v5.34.1; + +use strict; +use warnings; + +use Mojo::Base 'Mojolicious::Controller', -signatures; + +sub proxy { + my $self = shift; + my $conn = MSGBA::Web::Controller::WS::Connection->new(ws => $self, config => $self->config); + return $conn->handle(); +} + +package MSGBA::Web::Controller::WS::Connection { + use v5.34.1; + + use strict; + use warnings; + + use Moo; + use IO::Socket::UNIX; + + + has config => ( + is => 'ro', + required => 1, + ); + + has ws => ( + is => 'ro', + required => 1, + ); + + has msgba_connection => ( + is => 'rw', + required => 0, + ); + + sub handle { + my $self = shift; + my $ws = $self->ws; + $self->_build_msgba_connection; + $ws->on('message', sub { + my ($ws, $bytes) = @_; + if (!$bytes) { + warn "Received empty message"; + return; + } + say "Received message"; + say unpack 'H*', $bytes; + open my $fh, '<', \$bytes; + read $fh, my $id, 8; + read $fh, my $size, 8; + $size = unpack 'Q>', $size; + close $fh; + $self->msgba_connection->print($bytes); + }); + return; + #while (my $packet = $self->read_packet) { + # $self->handle_packet($packet); + #} + #close $self->msgba_connection; + #$ws->closed; + } + + sub handle_packet { + my $self = shift; + my $packet = shift; + my $ws = $self->ws; + my ($id, $size, $raw_data) = $packet->@{qw/id size raw_data/}; + $ws->send({binary => "${id}${size}${raw_data}"}); + } + + sub read_packet { + my $self = shift; + my $fh = $self->msgba_connection; + my $ws = $self->ws; + my ($id, $size, $raw_data); + if ((read $fh, $id, 8) != 8) { + return; + } + if ((read $fh, $size, 8) != 8) { + return; + } + my $size_num = unpack 'Q>', $size; + if ((read $fh, $raw_data, $size_num) != $size_num) { + return; + } + return { + id => $id, + size => $size, + raw_data => $raw_data, + }; + } + + sub _build_msgba_connection { + my $self = shift; + my $config = $self->config; + my $msgba_connection = IO::Socket::UNIX->new( + Type => SOCK_STREAM(), + Peer => $config->{domain_socket}, + ) or die "@{[$config->{domain_socket}]}: $!"; + $self->msgba_connection($msgba_connection); + } + +}; + +1; diff --git a/lib/msgba-web.pm b/lib/msgba-web.pm deleted file mode 100644 index 8804e06..0000000 --- a/lib/msgba-web.pm +++ /dev/null @@ -1,20 +0,0 @@ -package msgba-web; -use Mojo::Base 'Mojolicious', -signatures; - -# This method will run once at server start -sub startup ($self) { - - # Load configuration from config file - my $config = $self->plugin('NotYAMLConfig'); - - # Configure the application - $self->secrets($config->{secrets}); - - # Router - my $r = $self->routes; - - # Normal route to controller - $r->get('/')->to('Example#welcome'); -} - -1; diff --git a/lib/msgba-web/Controller/Example.pm b/lib/msgba-web/Controller/Example.pm deleted file mode 100644 index 36ed71b..0000000 --- a/lib/msgba-web/Controller/Example.pm +++ /dev/null @@ -1,11 +0,0 @@ -package msgba-web::Controller::Example; -use Mojo::Base 'Mojolicious::Controller', -signatures; - -# This action will render a template -sub welcome ($self) { - - # Render template "example/welcome.html.ep" with message - $self->render(msg => 'Welcome to the Mojolicious real-time web framework!'); -} - -1; diff --git a/msgba-web.yml b/msgba-web.yml new file mode 100644 index 0000000..5072808 --- /dev/null +++ b/msgba-web.yml @@ -0,0 +1,2 @@ +--- +domain_socket: "/home/sergio/msgba/msgba.sock" diff --git a/script/msgba-web b/script/msgba-web index 0576d4e..e53e52a 100755 --- a/script/msgba-web +++ b/script/msgba-web @@ -8,4 +8,4 @@ use lib curfile->dirname->sibling('lib')->to_string; use Mojolicious::Commands; # Start command line interface for application -Mojolicious::Commands->start_app('msgba-web'); +Mojolicious::Commands->start_app('MSGBA::Web'); diff --git a/templates/static/index.html.ep b/templates/static/index.html.ep new file mode 100644 index 0000000..0039fb4 --- /dev/null +++ b/templates/static/index.html.ep @@ -0,0 +1,170 @@ + + + + + + +
+

msGBA Emulator Online for GBA.

+
+
+ + +
+
+
+ + Savestate (A ss file from mgba...) + + + +
+
+ + +