Permalink
Browse files

piped client-server communication is now an option. run ./examples/mi…

…necrap.pl to try. Server does not shut down automatically (has to be killed) for now.
  • Loading branch information...
1 parent dfb4718 commit ea6cd639237355b441e6ccc71da85397236d6bdd @zpmorgan committed Jul 20, 2011
Showing with 143 additions and 96 deletions.
  1. +1 −1 bin/construder_client
  2. +1 −1 bin/construder_server
  3. +0 −43 bin/minecrap_client.pl
  4. +12 −14 examples/minecrap.pl
  5. +79 −22 lib/Games/VoxEngine/Client.pm
  6. +50 −15 lib/Games/VoxEngine/Server.pm
@@ -1,4 +1,4 @@
-#!/opt/perl/bin/perl
+#!/usr/bin/env perl
use common::sense;
use Games::VoxEngine;
use Games::VoxEngine::Logging;
@@ -1,4 +1,4 @@
-#!/opt/perl/bin/perl
+#!/usr/bin/env perl
use common::sense;
use AnyEvent;
use Games::VoxEngine;
@@ -1,43 +0,0 @@
-#!/usr/bin/env perl
-use common::sense;
-use Games::VoxEngine;
-use Games::VoxEngine::Logging;
-use Games::VoxEngine::Client;
-use Getopt::Long;
-
-my $spawn_server = 0;
-my $login_name;
-GetOptions ('spawn!' => \$spawn_server,
- 'name' => \$login_name,
-);
-
-if ($spawn_server){
- if (fork()==0){
-
- exec('perl bin/construder_server');
- }
- sleep(5);
-}
-
-
-#vox_enable_log_categories ('all');
-vox_enable_log_categories ('info', 'error', 'warn', 'chat');
-
-Games::VoxEngine::Debug::init ("client");
-
-our $game = eval { Games::VoxEngine::Client->new (auto_login => $login_name) };
-if ($@) {
- vox_log (error => "Couldn't initialized client: %s", $@);
- exit 1;
-}
-
-our $in_ex; #does $in_ex do anything?
-$game->set_exception_cb (sub {
- my ($ex, $ev) = @_;
- return if $in_ex;
- local $in_ex = 1;
- vox_log (error => "exception in client (%s): %s", $ev, $ex);
- $game->{front}->msg ("Fatal Error: Exception in client caught: $ev: $ex");
-});
-
-$game->start;
View
@@ -8,7 +8,7 @@
use IO::Pipe;
use AnyEvent;
-vox_enable_log_categories ('info', 'error', 'warn', 'chat');
+vox_enable_log_categories ('network', 'info', 'error', 'warn', 'chat');
my $spawn_server = 1;
my $login_name = 'foo';
@@ -17,19 +17,18 @@
);
if ($spawn_server){
- my $server_to_client = IO::Pipe->new();
- my $client_to_server = IO::Pipe->new();
+ my ($client_from_server, $server_to_client) = AnyEvent::Util::portable_pipe();
+ my ($server_from_client, $client_to_server) = AnyEvent::Util::portable_pipe();
if (fork()==0){
#run server.
# vox_enable_log_categories ('debug', 'info', 'error', 'warn', 'chat');
Games::VoxEngine::Debug::init ("server");
my $server = Games::VoxEngine::Server->new(
- pipe_to_client => $server_to_client,
- pipe_from_client => $client_to_server,
- # log_categories => ['info', 'error', 'warn'],
- # log_file => '/tmp/minecrap_server.log',
+ #pipe_to_client => $server_to_client,
+ #pipe_from_client => $server_from_client,
+ run_locally => [$server_from_client, $server_to_client],
temporary => 1,
);
$server->listen;
@@ -41,12 +40,11 @@
#run client.
my $client = eval { Games::VoxEngine::Client->new (
auto_login => $login_name,
- pipe_from_server => $server_to_client,
- pipe_to_server => $client_to_server,
- # log_categories => ['info', 'error', 'warn'],
- # log_file => '/tmp/minecrap_client.log',
- host => 'localhost',
- port => 9364,
+ #pipe_to_server => $client_to_server,
+ #pipe_from_server => $client_from_server,
+ run_locally => [$client_from_server, $client_to_server],
+ #host => 'localhost',
+ #port => 9364,
) };
if ($@) {
vox_log (error => "Couldn't initialize client: %s", $@);
@@ -58,7 +56,7 @@
vox_log (error => "exception in client (%s): %s", $ev, $ex);
$client->{front}->msg ("Fatal Error: Exception in client caught: $ev: $ex");
});
- sleep(5);
+ #sleep(5);
$client->start;
@@ -53,14 +53,12 @@ sub _build_event_handler{
$EH->init_object_events;
return $EH;
}
-has 'pipe_to_server' => (
+has run_locally => (
is => 'ro',
- isa => 'IO::Pipe',
-);
-has 'pipe_from_server' => (
- is => 'ro',
- isa => 'IO::Pipe',
+ isa => 'ArrayRef',
);
+sub pipe_from_server { shift()->run_locally()->[0] }
+sub pipe_to_server { shift()->run_locally()->[1] }
has 'port' => (
isa => 'Int',
@@ -73,7 +71,14 @@ has 'host' => (
is => 'ro',
default => 'localhost',
);
-
+has srv_in => (
+ is => 'rw',
+ isa => 'AnyEvent::Handle',
+);
+has srv_out => (
+ is => 'rw',
+ isa => 'AnyEvent::Handle',
+);
sub BUILD {
my $self = shift;
@@ -142,6 +147,11 @@ sub BUILD {
return $self
}
+sub is_connected{
+ my $self = shift;
+ return ( $self->srv_in() && $self->srv_out() );
+}
+
sub start {
my ($self) = @_;
@@ -150,12 +160,44 @@ sub start {
$c->recv;
}
-sub reconnect {
+sub connect {
my ($self) = @_;
- $self->connect ($self->host, $self->port);
+ if ($self->run_locally){
+ $self->piped_connect();
+ }
+ else {
+ $self->socket_connect ($self->host, $self->port);
+ }
}
-sub connect {
+sub piped_connect{
+ my $self = shift;
+
+ vox_log (debug => "connecting to piped server");
+
+ my $hdl_in = AnyEvent::Handle->new(
+ fh => $self->pipe_from_server(),
+ on_error => sub {
+ my ($hdl, $fatal, $msg) = @_;
+ $hdl->destroy;
+ $self->disconnected;
+ },
+ );
+ my $hdl_out = AnyEvent::Handle->new(
+ fh => $self->pipe_to_server(),
+ on_error => sub {
+ my ($hdl, $fatal, $msg) = @_;
+ $hdl->destroy;
+ $self->disconnected;
+ },
+ );
+ $self->{srv_in} = $hdl_in;
+ $self->{srv_out} = $hdl_out;
+ $self->handle_protocol;
+ $self->connected;
+}
+
+sub socket_connect {
my ($self, $host, $port) = @_;
vox_log (debug => "connecting to server %s at port %d", $host, $port);
@@ -166,7 +208,7 @@ sub connect {
unless ($fh) {
vox_log (error => "Couldn't connect to server %s at port %d: %s", $host, $port, $!);
$self->{front}->msg ("Couldn't connect to server: $!");
- $self->{recon} = AE::timer 5, 0, sub { $self->reconnect; };
+ $self->{recon} = AE::timer 5, 0, sub { $self->connect; };
return;
}
@@ -178,8 +220,9 @@ sub connect {
$self->disconnected;
}
);
-
- $self->{srv} = $hdl;
+ # tcp is bidirectional. Same handle for both
+ $self->{srv_in} = $hdl;
+ $self->{srv_out} = $hdl;
$self->handle_protocol;
$self->connected;
};
@@ -188,7 +231,7 @@ sub connect {
sub handle_protocol {
my ($self) = @_;
- $self->{srv}->push_read (packstring => "N", sub {
+ $self->srv_in->push_read (packstring => "N", sub {
my ($handle, $string) = @_;
$self->handle_packet (data2packet ($string));
$self->handle_protocol;
@@ -197,16 +240,21 @@ sub handle_protocol {
sub send_server {
my ($self, $hdr, $body) = @_;
- if ($self->{srv}) {
- $self->{srv}->push_write (packstring => "N", packet2data ($hdr, $body));
+ if ($self->is_connected()) {
vox_log (network => "send[%d]> %s: %s", length ($body), $hdr->{cmd}, join (',', keys %$hdr));
+ $self->srv_out->push_write (packstring => "N", packet2data ($hdr, $body));
}
}
sub connected {
my ($self) = @_;
$self->{front}->msg ("Connected to Server!");
- vox_log (info => "connected to server %s on port %d", $self->host, $self->port);
+ if ($self->run_locally){
+ vox_log (info => "connected to piped server.");
+ } else {
+ vox_log (info => "connected to server %s on port %d", $self->host, $self->port);
+ }
+
$self->send_server ({ cmd => 'hello', version => "Games::VoxEngine::Client 0.1" });
}
@@ -327,11 +375,20 @@ sub handle_packet {
sub disconnected {
my ($self) = @_;
- delete $self->{srv};
- $self->{front}->msg ("Disconnected from server!");
- $self->{front}->clear_chunks;
- $self->{recon} = AE::timer 5, 0, sub { $self->reconnect; };
- vox_log (info => "disconnected from server");
+ if($self->run_locally){
+ #local server shut down. so exit?
+ $self->_cv->send();
+ vox_log (info => "disconnected from local server");
+ }
+ else{
+ #try to reconnect.
+ delete $self->{srv_in};
+ delete $self->{srv_out};
+ $self->{front}->msg ("Disconnected from server!");
+ $self->{front}->clear_chunks;
+ $self->{recon} = AE::timer 5, 0, sub { $self->connect; };
+ vox_log (info => "disconnected from server");
+ }
}
=back
Oops, something went wrong.

0 comments on commit ea6cd63

Please sign in to comment.