Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

new Games::VoxEngine::Launcher module. It forks both client & server,…

… which communicate using AnyEvent "portable pipes"
  • Loading branch information...
commit cc6e8d2adf049523749bc9b6b84e362a148a8631 1 parent 3891b6b
@zpmorgan authored
View
22 examples/minecrap.pl
@@ -1,14 +1,20 @@
#!/usr/bin/env perl
use common::sense;
use Games::VoxEngine;
-use Games::VoxEngine::Logging;
-use Games::VoxEngine::Client;
-use Games::VoxEngine::Server;
+#use Games::VoxEngine::Logging;
+#use Games::VoxEngine::Client;
+#use Games::VoxEngine::Server;
+use Games::VoxEngine::Launcher;
use Getopt::Long;
-use IO::Pipe;
-use AnyEvent;
+#use AnyEvent;
-vox_enable_log_categories ('network', 'info', 'error', 'warn', 'chat');
+my $launchy = Games::VoxEngine::Launcher->new(
+
+);
+$launchy->launch();
+
+__END__
+#vox_enable_log_categories ('debug','network', 'info', 'error', 'warn', 'chat');
my $spawn_server = 1;
my $login_name = 'foo';
@@ -22,7 +28,7 @@
if (fork()==0){
#run server.
- # vox_enable_log_categories ('debug', 'info', 'error', 'warn', 'chat');
+ vox_enable_log_categories ('debug', 'info', 'error', 'warn', 'chat');
Games::VoxEngine::Debug::init ("server");
my $server = Games::VoxEngine::Server->new(
@@ -35,7 +41,7 @@
exit(0);
}
- vox_enable_log_categories ('debug', 'info', 'error', 'warn', 'chat');
+ vox_enable_log_categories ('debug','network', 'info', 'error', 'warn', 'chat');
Games::VoxEngine::Debug::init ("client");
#run client.
my $client = eval { Games::VoxEngine::Client->new (
View
1  lib/Games/VoxEngine.pm
@@ -289,6 +289,7 @@ our $SHELL;
sub init {
my ($name) = @_;
+ Games::VoxEngine::Logging::set_metacat($name);
return unless $ENV{PERL_GAMES_CONSTRUDER_DEBUG};
View
1  lib/Games/VoxEngine/Client.pm
@@ -161,6 +161,7 @@ sub is_connected{
sub start {
my ($self) = @_;
+ $self->send_server({cmd => "BLAH BLAH BLAH BLAH", foo=>"BLAH BLAH BLAH"});
$self->_cv->recv;
}
View
2  lib/Games/VoxEngine/Client/Frontend.pm
@@ -100,7 +100,7 @@ sub exit_app {
my ($self) = @_;
$self->{client}->disconnect;
$self->{client}->stop;
- #exit;
+ exit;
}
sub resize_app {
View
157 lib/Games/VoxEngine/Launcher.pm
@@ -0,0 +1,157 @@
+# Games::VoxEngine - A voxel game engine.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+package Games::VoxEngine::Launcher;
+use Mouse;
+use Games::VoxEngine;
+use Games::VoxEngine::Logging;
+use Games::VoxEngine::Client;
+use Games::VoxEngine::Server;
+use AnyEvent;
+use AnyEvent::Util;
+
+=head1 NAME
+
+Games::VoxEngine::Launcher - This module spawns server & client processes.
+
+=over 4
+
+=cut
+
+has _server_pid => (
+ isa => 'Int',
+ is => 'rw',
+);
+has _client_pid => (
+ isa => 'Int',
+ is => 'rw',
+);
+has run_locally => (
+ is => 'Bool',
+ is => 'ro',
+ default => 1,
+);
+
+has _client_to_server => (
+ isa => 'ArrayRef',
+ is => 'rw',
+ default => sub{[]},
+);
+has _server_to_client => (
+ isa => 'ArrayRef',
+ is => 'rw',
+ default => sub{[]},
+);
+has login_name => (
+ isa => 'Str',
+ is => 'rw',
+ default => 'foo',
+);
+
+sub BUILD{
+ my $self = shift;
+
+ vox_enable_log_categories ('info', 'error', 'warn');
+ vox_log('foo', 'BUILDing launcher pipes');
+
+ #server reads, client writes.
+ my ($r,$w) = AnyEvent::Util::portable_pipe();
+ $self->_client_to_server->[0] = $r;
+ $self->_client_to_server->[1] = $w;
+
+ #client reads, server writes.
+ my ($r2,$w2) = AnyEvent::Util::portable_pipe();
+ $self->_server_to_client->[0] = $r2;
+ $self->_server_to_client->[1] = $w2;
+}
+
+sub launch {
+ my $self = shift;
+ exit(1) unless (2 == @{$self->_client_to_server});
+ $self->start_server();
+ $self->start_client();
+ $self->haunt();
+}
+
+sub start_server {
+ my $self = shift;
+ my $server_pid = fork();
+ if ($server_pid){
+ $self->_server_pid($server_pid);
+ }
+ else { #this is child. so run server.
+ vox_enable_log_categories ('info', 'error', 'warn');
+ Games::VoxEngine::Debug::init ("server");
+
+ my $server = eval { Games::VoxEngine::Server->new(
+ run_locally => [$self->_client_to_server->[0], $self->_server_to_client->[1]],
+ # temporary => 1,
+ ) };
+ if ($@) {
+ vox_log error => "Couldn't initialize server: $@ ";
+ exit 1;
+ }
+ $server->listen;
+ exit(0);
+ }
+}
+
+sub start_client{
+ my $self = shift;
+ my $client_pid = fork();
+ if ($client_pid){
+ $self->_client_pid($client_pid);
+ }
+ else { #this is child. so run client.
+
+ vox_enable_log_categories ('info', 'error', 'warn');
+ Games::VoxEngine::Debug::init ("client");
+ my $client = eval { Games::VoxEngine::Client->new (
+ auto_login => $self->login_name,
+ run_locally => [$self->_server_to_client->[0], $self->_client_to_server->[1]],
+ #host => 'localhost',
+ #port => 9364,
+ ) };
+ if ($@) {
+ vox_log error => "Couldn't initialize client: $@";
+ exit 1;
+ }
+ $client->start;
+ }
+}
+
+sub haunt {
+ my $self = shift;
+sleep(5);
+}
+
+
+1;
+=back
+
+=head1 AUTHOR
+
+Zach Morgan, C<< <zpmorgan@gmail.com> >>
+
+=head1 COPYRIGHT & LICENSE
+
+Copyright 2011 Zach Morgan, all rights reserved.
+
+This program is free software; you can redistribute it and/or modify it
+under the terms of the GNU Affero General Public License.
+
+=cut
+
+
View
11 lib/Games/VoxEngine/Logging.pm
@@ -26,6 +26,7 @@ our @EXPORT = qw/
vox_enable_log_categories
vox_disable_log_categories
/;
+#use Carp 'confess';
=head1 NAME
@@ -44,6 +45,11 @@ Games::VoxEngine::Logging - This module takes care of logging construder client
our %CATEGORIES;
our $LOGFILE;
our $LOGFILE_FH;
+our $metacat = '';
+
+sub set_metacat{
+ $metacat = shift . ' ';
+}
sub vox_enable_log_categories {
my (@cats) = @_;
@@ -87,7 +93,10 @@ sub vox_log {
my $date =
sprintf "%04d-%02d-%02d %02d:%02d:%02d",
$year + 1900, $mon + 1, $mday, $hour, $min, $sec;
- my $str = sprintf "%s+%5.4f [%s]: $fmt", $date, $tr, $category, @args;
+ eval{$fmt = sprintf($fmt,@args)};
+ #network sometimes sends data which messes up $fmt.
+ print "$fmt failed to format: $@" and return if $@;
+ my $str = sprintf "%s+%5.4f [$metacat$category]: $fmt", $date, $tr;
$str .= "\n" unless $str =~ /\n$/s;
if ($LOGFILE ne '') {
View
33 lib/Games/VoxEngine/Server.pm
@@ -128,13 +128,28 @@ sub pipe_listen{
fh => $self->pipe_from_client,
on_error => sub {
my ($hdl, $fatal, $msg) = @_;
+ vox_log (network => "received error from client.");
$hdl->destroy;
$self->client_disconnected ($cid, "error: $msg");
},
on_eof => sub {
- my ($hdl, $fatal, $msg) = @_;
+ my $hdl = shift;
+ vox_log (network => "received EOF from client.");
$hdl->destroy;
- $self->client_disconnected ($cid, "error: $msg");
+ $self->client_disconnected ($cid, "pipe closed.");
+ },
+ on_read => sub{
+ my $hdl = shift;
+ vox_log (debug => "on_read: $hdl->{rbuf}");
+ #vox_log (debug => "pushing a read on server");
+ $hdl->push_read( packstring => 'N', sub{
+ my ($handle,$string) = @_;
+ # vox_log (debug => "pushed read initiated.");
+ vox_log (debug => "data: $string");
+ $self->handle_protocol($cid,$string);
+ return 1;
+ });
+ return 0; #leave the read buffer.
},
);
my $hdl_out = AnyEvent::Handle->new(
@@ -148,7 +163,7 @@ sub pipe_listen{
$self->{clients}->{$cid}{in} = $hdl_in;
$self->{clients}->{$cid}{out} = $hdl_out;
$self->client_connected ($cid);
- $self->handle_protocol ($cid);
+ #$self->handle_protocol ($cid);
}
#start a tcp server on $self->port
@@ -193,13 +208,11 @@ sub shutdown {
}
sub handle_protocol {
- my ($self, $cid) = @_;
+ my ($self, $cid, $string) = @_;
- $self->{clients}->{$cid}{in}->push_read (packstring => "N", sub {
- my ($handle, $string) = @_;
+ if ($self->{clients}->{$cid}){
$self->handle_packet ($cid, data2packet ($string));
- $self->handle_protocol ($cid);
- }) if $self->{clients}->{$cid};
+ }
}
sub send_client {
@@ -249,12 +262,13 @@ sub push_transfer {
}
sub client_disconnected {
- my ($self, $cid) = @_;
+ my ($self, $cid, $msg) = @_;
my $pl = delete $self->{players}->{$cid};
$pl->logout if $pl;
delete $self->{player_guards}->{$cid};
delete $self->{clients}->{$cid};
vox_log (info => "Client disconnected: %s", $cid);
+ vox_log (info => "disconnect msg: %s", $msg);
vox_log (info => "temp: " . $self->temporary);
if ($self->temporary){
@@ -379,6 +393,7 @@ sub handle_packet {
}
} elsif ($hdr->{cmd} eq 'login') {
+ $hdr->{name} = 0;
if ($hdr->{name} ne '') {
$self->login ($cid, $hdr->{name})
Please sign in to comment.
Something went wrong with that request. Please try again.