ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/Socket.pm
Revision: 1.44
Committed: Mon Nov 24 07:55:28 2008 UTC (15 years, 6 months ago) by root
Branch: MAIN
CVS Tags: rel-5_1, rel-5_11
Changes since 1.43: +1 -1 lines
Log Message:
5.1

File Contents

# Content
1 =head1 NAME
2
3 Coro::Socket - non-blocking socket-I/O
4
5 =head1 SYNOPSIS
6
7 use Coro::Socket;
8
9 # listen on an ipv4 socket
10 my $socket = new Coro::Socket PeerHost => "localhost",
11 PeerPort => 'finger';
12
13 # listen on any other type of socket
14 my $socket = Coro::Socket->new_from_fh
15 (IO::Socket::UNIX->new
16 Local => "/tmp/socket",
17 Type => SOCK_STREAM,
18 );
19
20 =head1 DESCRIPTION
21
22 This module is an L<AnyEvent> user, you need to make sure that you use and
23 run a supported event loop.
24
25 This module implements socket-handles in a coroutine-compatible way,
26 that is, other coroutines can run while reads or writes block on the
27 handle. See L<Coro::Handle>, especially the note about prefering method
28 calls.
29
30 =over 4
31
32 =cut
33
34 package Coro::Socket;
35
36 no warnings "uninitialized";
37
38 use strict;
39
40 use Errno ();
41 use Carp qw(croak);
42 use Socket;
43 use IO::Socket::INET ();
44
45 use Coro::Util ();
46
47 use base qw(Coro::Handle IO::Socket::INET);
48
49 our $VERSION = 5.1;
50
51 our (%_proto, %_port);
52
53 sub _proto($) {
54 $_proto{$_[0]} ||= do {
55 ((getprotobyname $_[0])[2] || (getprotobynumber $_[0])[2])
56 or croak "unsupported protocol: $_[0]";
57 };
58 }
59
60 sub _port($$) {
61 $_port{$_[0],$_[1]} ||= do {
62 return $_[0] if $_[0] =~ /^\d+$/;
63
64 $_[0] =~ /([^(]+)\s*(?:\((\d+)\))?/x
65 or croak "unparsable port number: $_[0]";
66 ((getservbyname $1, $_[1])[2]
67 || (getservbyport $1, $_[1])[2]
68 || $2)
69 or croak "unknown port: $_[0]";
70 };
71 }
72
73 sub _sa($$$) {
74 my ($host, $port, $proto) = @_;
75
76 $port or $host =~ s/:([^:]+)$// and $port = $1;
77
78 my $_proto = _proto($proto);
79 my $_port = _port($port, $proto);
80
81 my $_host = Coro::Util::inet_aton $host
82 or croak "$host: unable to resolve";
83
84 pack_sockaddr_in $_port, $_host
85 }
86
87 =item $fh = new Coro::Socket param => value, ...
88
89 Create a new non-blocking tcp handle and connect to the given host
90 and port. The parameter names and values are mostly the same as for
91 IO::Socket::INET (as ugly as I think they are).
92
93 The parameters officially supported currently are: C<ReuseAddr>,
94 C<LocalPort>, C<LocalHost>, C<PeerPort>, C<PeerHost>, C<Listen>, C<Timeout>.
95
96 $fh = new Coro::Socket PeerHost => "localhost", PeerPort => 'finger';
97
98 =cut
99
100 sub _prepare_socket {
101 my ($self, $arg) = @_;
102
103 $self
104 }
105
106 sub new {
107 my ($class, %arg) = @_;
108
109 $arg{Proto} ||= 'tcp';
110 $arg{LocalHost} ||= delete $arg{LocalAddr};
111 $arg{PeerHost} ||= delete $arg{PeerAddr};
112 defined ($arg{Type}) or $arg{Type} = $arg{Proto} eq "tcp" ? SOCK_STREAM : SOCK_DGRAM;
113
114 socket my $fh, PF_INET, $arg{Type}, _proto ($arg{Proto})
115 or return;
116
117 my $self = bless Coro::Handle->new_from_fh (
118 $fh,
119 timeout => $arg{Timeout},
120 forward_class => $arg{forward_class},
121 partial => $arg{partial},
122 ), $class
123 or return;
124
125 $self->configure (\%arg)
126 }
127
128 sub configure {
129 my ($self, $arg) = @_;
130
131 if ($arg->{ReuseAddr}) {
132 $self->setsockopt (SOL_SOCKET, SO_REUSEADDR, 1)
133 or croak "setsockopt(SO_REUSEADDR): $!";
134 }
135
136 if ($arg->{ReusePort}) {
137 $self->setsockopt (SOL_SOCKET, SO_REUSEPORT, 1)
138 or croak "setsockopt(SO_REUSEPORT): $!";
139 }
140
141 if ($arg->{Broadcast}) {
142 $self->setsockopt (SOL_SOCKET, SO_BROADCAST, 1)
143 or croak "setsockopt(SO_BROADCAST): $!";
144 }
145
146 if ($arg->{LocalPort} || $arg->{LocalHost}) {
147 my @sa = _sa($arg->{LocalHost} || "0.0.0.0", $arg->{LocalPort} || 0, $arg->{Proto});
148 $self->bind ($sa[0])
149 or croak "bind($arg->{LocalHost}:$arg->{LocalPort}): $!";
150 }
151
152 if ($arg->{PeerHost}) {
153 my @sa = _sa ($arg->{PeerHost}, $arg->{PeerPort}, $arg->{Proto});
154
155 for (@sa) {
156 $! = 0;
157
158 if ($self->connect ($_)) {
159 next unless writable $self;
160 $! = unpack "i", $self->getsockopt (SOL_SOCKET, SO_ERROR);
161 }
162
163 $! or last;
164
165 $!{ECONNREFUSED} or $!{ENETUNREACH} or $!{ETIMEDOUT} or $!{EHOSTUNREACH}
166 or return;
167 }
168 } elsif (exists $arg->{Listen}) {
169 $self->listen ($arg->{Listen})
170 or return;
171 }
172
173 $self
174 }
175
176 1;
177
178 =back
179
180 =head1 AUTHOR
181
182 Marc Lehmann <schmorp@schmorp.de>
183 http://home.schmorp.de/
184
185 =cut
186