ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/myhttpd/httpd.pl
(Generate patch)

Comparing Coro/myhttpd/httpd.pl (file contents):
Revision 1.37 by root, Sun Sep 2 01:03:53 2001 UTC vs.
Revision 1.41 by root, Mon Sep 10 22:16:20 2001 UTC

42my @pool; 42my @pool;
43 43
44# one "execution thread" 44# one "execution thread"
45sub handler { 45sub handler {
46 while () { 46 while () {
47 my $new = pop @newcons;
48 if ($new) { 47 if (@newcons) {
49 eval { 48 eval {
50 conn->new(@$new)->handle; 49 conn->new(@{pop @newcons})->handle;
51 }; 50 };
52 slog 1, "$@" if $@ && !ref $@; 51 slog 1, "$@" if $@ && !ref $@;
53 $connections->up; 52 $connections->up;
54 } else { 53 } else {
55 last if @pool >= $MAX_POOL; 54 last if @pool >= $MAX_POOL;
57 schedule; 56 schedule;
58 } 57 }
59 } 58 }
60} 59}
61 60
61sub listen_on {
62 my $listen = $_[0];
63
64 push @listen_sockets, $listen;
65
66 # the "main thread"
67 async {
68 slog 1, "accepting connections";
69 while () {
70 $connections->down;
71 push @newcons, [$listen->accept];
72 #slog 3, "accepted @$connections ".scalar(@pool);
73 if (@pool) {
74 (pop @pool)->ready;
75 } else {
76 async \&handler;
77 }
78
79 }
80 };
81}
82
62my $http_port = new Coro::Socket 83my $http_port = new Coro::Socket
63 LocalAddr => $SERVER_HOST, 84 LocalAddr => $SERVER_HOST,
64 LocalPort => $SERVER_PORT, 85 LocalPort => $SERVER_PORT,
65 ReuseAddr => 1, 86 ReuseAddr => 1,
66 Listen => 50, 87 Listen => 50,
67 or die "unable to start server"; 88 or die "unable to start server";
68 89
69push @listen_sockets, $http_port; 90listen_on $http_port;
91
92if ($SERVER_PORT2) {
93 my $http_port = new Coro::Socket
94 LocalAddr => $SERVER_HOST,
95 LocalPort => $SERVER_PORT2,
96 ReuseAddr => 1,
97 Listen => 50,
98 or die "unable to start server";
99
100 listen_on $http_port;
101}
70 102
71our $NOW; 103our $NOW;
72our $HTTP_NOW; 104our $HTTP_NOW;
73 105
74Event->timer(interval => 1, hard => 1, cb => sub { 106Event->timer(interval => 1, hard => 1, cb => sub {
75 $NOW = time; 107 $NOW = time;
76 $HTTP_NOW = time2str $NOW; 108 $HTTP_NOW = time2str $NOW;
77})->now; 109})->now;
78
79# the "main thread"
80async {
81 slog 1, "accepting connections";
82 while () {
83 $connections->down;
84 push @newcons, [$http_port->accept];
85 #slog 3, "accepted @$connections ".scalar(@pool);
86 if (@pool) {
87 (pop @pool)->ready;
88 } else {
89 async \&handler;
90 }
91
92 }
93};
94 110
95package conn; 111package conn;
96 112
97use Socket; 113use Socket;
98use HTTP::Date; 114use HTTP::Date;
141 $self; 157 $self;
142} 158}
143 159
144sub DESTROY { 160sub DESTROY {
145 my $self = shift; 161 my $self = shift;
146
147 $::conns--; 162 $::conns--;
148
149 $self->eoconn; 163 $self->eoconn;
150} 164}
151 165
152# end of connection 166# end of connection
153sub eoconn { 167sub eoconn {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines