migration to AnyEvent::Socket
[AnyEvent-HTTPD.git] / lib / AnyEvent / HTTPD / HTTPServer.pm
blob81f37e080386ae4d72762ccc6dd0b0c4298dbd12
1 package AnyEvent::HTTPD::HTTPServer;
2 use strict;
3 no warnings;
5 use IO::Socket::INET;
6 use Object::Event;
7 use AnyEvent::Handle;
8 use AnyEvent::Socket;
10 use AnyEvent::HTTPD::HTTPConnection;
12 our @ISA = qw/Object::Event/;
14 =head1 NAME
16 AnyEvent::HTTPD::HTTPServer - A simple and plain http server
18 =head1 DESCRIPTION
20 This class handles incoming TCP connections for HTTP clients.
21 It's used by L<AnyEvent::HTTPD> to do it's job.
23 It has no public interface yet.
25 =head1 COPYRIGHT & LICENSE
27 Copyright 2008 Robin Redeker, all rights reserved.
29 This program is free software; you can redistribute it and/or modify it
30 under the same terms as Perl itself.
32 =cut
34 sub new {
35 my $this = shift;
36 my $class = ref($this) || $this;
37 my $self = { @_ };
38 bless $self, $class;
40 my $sock =
41 $self->{sock} =
42 IO::Socket::INET->new (
43 Listen => 10,
44 LocalPort => $self->{port},
45 ReuseAddr => 1,
46 Blocking => 0
49 $sock or die "Couldn't create listening socket: $!";
51 tcp_server undef, $self->{port}, sub {
52 my ($fh) = @_;
53 unless ($fh) {
54 $self->event (error => "couldn't accept client: $!");
55 return;
57 $self->accept_connection ($fh);
60 return $self
63 sub accept_connection {
64 my ($self, $fh) = @_;
66 my $htc = AnyEvent::HTTPD::HTTPConnection->new (fh => $fh);
67 $self->{handles}->{$htc} = $htc;
69 $htc->reg_cb (disconnect => sub {
70 delete $self->{handles}->{$_[0]};
71 $self->event (disconnect => $_[0])
72 });
74 $self->event (connect => $htc);