aboutsummaryrefslogtreecommitdiff
path: root/lib/MasterServer/TCP/BrowserHost.pm
blob: 855b2c0f59518ef6be7d4c81f0d19f6be5ee544d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

package MasterServer::TCP::BrowserHost;

use strict;
use warnings;
use AnyEvent::Socket;
use AnyEvent::Handle;
use Exporter 'import';

our @EXPORT = qw| browser_host clean_tcp_handle|;

################################################################################
## wait for incoming TCP connections from game clients and other masterservers.
## respond with secure/validate, contact info and/or server lists.
## allow other masterservers to synchronize
################################################################################
sub browser_host {
  my $self = shift;

  my $browser = tcp_server undef, $self->{listen_port}, sub {
    my ($fh, $a, $p) = @_;
    
    # validated? yes = 1 no = 0
    my $auth = 0; 
    
    # debug -- new connection opened
    $self->log("tcp","New connection from $a:$p");
    
    # prep a challenge
    my $secure = $self->secure_string();
    
    # handle received data
    my $h; $h = AnyEvent::Handle->new(
      fh        => $fh,
      poll      => 'r',
      timeout   => 5,
      on_eof    => sub {$self->clean_tcp_handle(@_)},
      on_error  => sub {$self->clean_tcp_handle(@_)},
      on_read   => sub {$self->read_tcp_handle($h, $a, $p, $secure, @_)},
    );
    
    # part 1: send \basic\\secure\$key\
    $h->push_write("\\basic\\\\secure\\$secure\\final\\");
    
    # keep handle alive longer and store authentication info
    $self->{browser_clients}->{$h} = [$h, $auth];
    return;
  };
  
  # startup of TCP server complete 
  $self->log("info", "Listening for TCP connections on port $self->{listen_port}.");
  return $browser;
}

################################################################################
## clean handles on timeouts, completed requests and/or errors
################################################################################
sub clean_tcp_handle{
  my ($self, $c) = @_;
  # clean and close the connection
  delete ($self->{browser_clients}->{$c});  
  $c->destroy();
}

1;