ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/tcp.ext
(Generate patch)

Comparing deliantra/server/ext/tcp.ext (file contents):
Revision 1.5 by root, Sat Dec 23 03:38:43 2006 UTC vs.
Revision 1.22 by root, Tue Oct 30 17:32:47 2012 UTC

1#! perl 1#! perl # mandatory
2 2
3# this listens for new tcp connections and hands them over to the server core 3# this listens for new tcp connections and hands them over to the server core
4# wether this being an extension introduces or reduces stability problems 4# whether this being an extension introduces or reduces stability problems
5# is unknown as of today. 5# is unknown as of today.
6 6
7use Socket; 7use Socket;
8use IO::Socket::INET; 8use AnyEvent::Socket;
9 9
10our $LISTEN = new IO::Socket::INET 10CONF BIND_ADDRESSES = [[undef, 13327]];
11 LocalPort => cf::settings->csport,
12 Listen => 1,
13 Blocking => 0, # bugfix workaround for Event calling handler twice :(
14 ReuseAddr => 1;
15 11
16if (!$LISTEN) { 12our $MAX_DETECT = 16; # how many bytes to raed to identify the protocol
17 # extension yes, completely stupid, not yet 13
18 warn "unable to establish listen sockect, exiting.\n"; 14our @LISTENERS;
19 exit (2); 15
16for (@$BIND_ADDRESSES) {
17 my ($host, $port) = @$_;
18 cf::info "listening on ", (format_hostport $host, $port), "\n";
19
20 push @LISTENERS, tcp_server $host, $port, sub {
21 my ($fh, $host, $port) = @_
22 or return;
23
24 my $lhost = AnyEvent::Socket::format_address
25 +(AnyEvent::Socket::unpack_sockaddr getsockname $fh)[1];
26
27 my $id = format_hostport $host, $port;
28
29 cf::info "$id: new connection\n"
30 if $lhost ne $host; # do not log connections from the host, e.g. for watchdogs
31
32 my $buf;
33 my $w; $w = AE::io $fh, 0, sub {
34 my $len = sysread $fh, $buf, 512, length $buf;
35
36 if ($len) {
37 if ($buf =~ /^..version /s) { # deliantra protocol
38 undef $w;
39
40 my $ns = cf::client::create fileno $fh, $host;
41 $ns->inbuf_append ($buf);
42
43 } elsif ($buf =~ /^GET / && defined &ext::http::server) { # http or websocket
44 undef $w;
45
46 &ext::http::server ($id, $fh, $buf);
47
48 } elsif (length $buf >= $MAX_DETECT) { # unable to detect protocol
49 undef $w;
50
51 cf::info "$id: protocol detection error\n";
52 }
53 } else {
54 undef $w;
55
56 cf::info "$id: read error during protocol detection\n";
57 }
58 };
59 };
20} 60}
21 61
22Event->io (fd => $LISTEN, nice => 1, poll => 'r', data => cf::WF_AUTOCANCEL, cb => sub {
23 my ($fh, $peername) = $LISTEN->accept
24 or return;
25
26 my $fd = fileno $fh;
27 my $host = inet_ntoa +(sockaddr_in $peername)[1];
28
29 warn "new connection from $host\n";
30
31 cf::client::create $fd, $host;
32});

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines