2 # This has been taken from the 'Advanced Perl Programming' book by Sriram Srinivasan
4 # I am presuming that the code is distributed on the same basis as perl itself.
6 # I have modified it to suit my devious purposes (Dirk Koopman G1TLH)
18 use Errno qw(EWOULDBLOCK EAGAIN EINPROGRESS);
19 use POSIX qw(F_GETFL F_SETFL O_NONBLOCK);
21 use vars qw(%rd_callbacks %wt_callbacks %er_callbacks $rd_handles $wt_handles $er_handles $now %conns $noconns);
26 $rd_handles = IO::Select->new();
27 $wt_handles = IO::Select->new();
28 $er_handles = IO::Select->new();
31 my $blocking_supported = 0;
34 # Checks if blocking is supported
36 require POSIX; POSIX->import(qw (F_SETFL O_NONBLOCK));
38 $blocking_supported = 1 unless $@;
43 my $eagain = eval {EAGAIN()};
44 my $einprogress = eval {EINPROGRESS()};
45 my $ewouldblock = eval {EWOULDBLOCK()};
49 #-----------------------------------------------------------------
50 # Generalised initializer
54 my ($pkg, $rproc) = @_;
56 my $class = $obj || $pkg;
69 dbg('connll', "Connection created ($noconns)");
70 return bless $conn, $class;
77 $conn->{eproc} = $callback;
78 set_event_handler($conn->{sock}, error => $callback) if exists $conn->{sock};
85 $conn->{rproc} = $callback;
90 return unless $blocking_supported;
92 my $flags = fcntl ($_[0], F_GETFL, 0);
94 $flags &= ~O_NONBLOCK;
98 fcntl ($_[0], F_SETFL, $flags);
109 $call = $pkg->{call} unless $call;
110 return undef unless $call;
111 confess "changing $pkg->{call} to $call" if exists $pkg->{call} && $call ne $pkg->{call};
112 $pkg->{call} = $call;
113 $ref = $conns{$call} = $pkg;
114 dbg('connll', "Connection $call stored");
116 $ref = $conns{$call};
121 # this is only called by any dependent processes going away unexpectedly
124 my ($pkg, $pid) = @_;
126 my @pid = grep {$_->{pid} == $pid} values %conns;
128 &{$_->{eproc}}($_, "$pid has gorn") if exists $_->{eproc};
133 #-----------------------------------------------------------------
136 my ($pkg, $to_host, $to_port, $rproc) = @_;
138 # Create a connection end-point object
141 $conn = $pkg->new($rproc);
143 $conn->{peerhost} = $to_host;
144 $conn->{peerport} = $to_port;
145 $conn->{sort} = 'Outgoing';
147 # Create a new internet socket
148 my $sock = IO::Socket::INET->new();
149 return undef unless $sock;
151 my $proto = getprotobyname('tcp');
152 $sock->socket(AF_INET, SOCK_STREAM, $proto) or return undef;
155 my $ip = gethostbyname($to_host);
156 # my $r = $sock->connect($to_port, $ip);
157 my $r = connect($sock, pack_sockaddr_in($to_port, $ip));
158 return undef unless $r || _err_will_block($r);
160 $conn->{sock} = $sock;
162 if ($conn->{rproc}) {
163 my $callback = sub {$conn->_rcv};
164 set_event_handler ($sock, read => $callback);
171 return if exists $conn->{disconnecting};
173 $conn->{disconnecting} = 1;
174 my $sock = delete $conn->{sock};
175 $conn->{state} = 'E';
176 $conn->{timeout}->del if $conn->{timeout};
178 # be careful to delete the correct one
180 if ($call = $conn->{call}) {
181 my $ref = $conns{$call};
182 delete $conns{$call} if $ref && $ref == $conn;
184 $call ||= 'unallocated';
185 dbg('connll', "Connection $call disconnected");
187 unless ($^O =~ /^MS/i) {
188 kill 'TERM', $conn->{pid} if exists $conn->{pid};
191 # get rid of any references
193 if (ref($conn->{$_})) {
198 return unless defined($sock);
199 set_event_handler ($sock, read => undef, write => undef, error => undef);
205 my ($conn, $msg) = @_;
206 $conn->enqueue($msg);
207 $conn->_send (1); # 1 ==> flush
211 my ($conn, $msg) = @_;
212 $conn->enqueue($msg);
213 my $sock = $conn->{sock};
214 return unless defined($sock);
215 set_event_handler ($sock, write => sub {$conn->_send(0)});
220 push (@{$conn->{outqueue}}, defined $_[0] ? $_[0] : '');
224 my ($conn, $flush) = @_;
225 my $sock = $conn->{sock};
226 return unless defined($sock);
227 my $rq = $conn->{outqueue};
229 # If $flush is set, set the socket to blocking, and send all
230 # messages in the queue - return only if there's an error
231 # If $flush is 0 (deferred mode) make the socket non-blocking, and
232 # return to the event loop only after every message, or if it
233 # is likely to block in the middle of a message.
235 blocking($sock, $flush);
236 my $offset = (exists $conn->{send_offset}) ? $conn->{send_offset} : 0;
240 my $mlth = length($msg);
241 my $bytes_to_write = $mlth - $offset;
242 my $bytes_written = 0;
243 confess("Negative Length! msg: '$msg' lth: $mlth offset: $offset") if $bytes_to_write < 0;
244 while ($bytes_to_write > 0) {
245 $bytes_written = syswrite ($sock, $msg,
246 $bytes_to_write, $offset);
247 if (!defined($bytes_written)) {
248 if (_err_will_block($!)) {
249 # Should happen only in deferred mode. Record how
250 # much we have already sent.
251 $conn->{send_offset} = $offset;
252 # Event handler should already be set, so we will
253 # be called back eventually, and will resume sending
256 &{$conn->{eproc}}($conn, $!) if exists $conn->{eproc};
258 return 0; # fail. Message remains in queue ..
261 $offset += $bytes_written;
262 $bytes_to_write -= $bytes_written;
264 delete $conn->{send_offset};
267 last unless $flush; # Go back to select and wait
268 # for it to fire again.
270 # Call me back if queue has not been drained.
272 set_event_handler ($sock, write => sub {$conn->_send(0)});
274 set_event_handler ($sock, write => undef);
275 if (exists $conn->{close_on_empty}) {
276 &{$conn->{eproc}}($conn, undef) if exists $conn->{eproc};
283 sub _err_will_block {
284 return 0 unless $blocking_supported;
285 return ($_[0] == $eagain || $_[0] == $ewouldblock || $_[0] == $einprogress);
291 $conn->{close_on_empty} = 1;
294 #-----------------------------------------------------------------
295 # Receive side routines
298 @_ == 4 || die "Msg->new_server (myhost, myport, login_proc\n";
299 my ($pkg, $my_host, $my_port, $login_proc) = @_;
300 my $self = $pkg->new($login_proc);
302 $self->{sock} = IO::Socket::INET->new (
303 LocalAddr => $my_host,
304 LocalPort => $my_port,
308 die "Could not create socket: $! \n" unless $self->{sock};
309 set_event_handler ($self->{sock}, read => sub { $self->new_client } );
317 if ($conn->{msg} =~ /\n/) {
318 my @lines = split /\r?\n/, $conn->{msg};
319 if ($conn->{msg} =~ /\n$/) {
322 $conn->{msg} = pop @lines;
325 &{$conn->{rproc}}($conn, defined $_ ? $_ : '');
330 sub _rcv { # Complement to _send
331 my $conn = shift; # $rcv_now complement of $flush
332 # Find out how much has already been received, if at all
333 my ($msg, $offset, $bytes_to_read, $bytes_read);
334 my $sock = $conn->{sock};
335 return unless defined($sock);
339 $bytes_read = sysread ($sock, $msg, 1024, 0);
340 if (defined ($bytes_read)) {
341 if ($bytes_read > 0) {
342 $conn->{msg} .= $msg;
345 if (_err_will_block($!)) {
353 if (defined $bytes_read && $bytes_read == 0) {
354 &{$conn->{eproc}}($conn, $!) if exists $conn->{eproc};
357 $conn->dequeue if exists $conn->{msg};
362 my $server_conn = shift;
363 my $sock = $server_conn->{sock}->accept();
364 my $conn = $server_conn->new($server_conn->{rproc});
365 $conn->{sock} = $sock;
366 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost} = $sock->peerhost(), $conn->{peerport} = $sock->peerport());
367 $conn->{sort} = 'Incoming';
369 $conn->{eproc} = $eproc;
370 set_event_handler ($sock, error => $eproc);
373 $conn->{rproc} = $rproc;
374 my $callback = sub {$conn->_rcv};
375 set_event_handler ($sock, read => $callback);
376 } else { # Login failed
377 &{$conn->{eproc}}($conn, undef) if exists $conn->{eproc};
385 set_event_handler ($conn->{sock}, read => undef, write => undef, error => undef );
386 $conn->{sock}->close;
389 # close all clients (this is for forking really)
390 sub close_all_clients
392 for (values %conns) {
397 #----------------------------------------------------
398 # Event loop routines used by both client and server
400 sub set_event_handler {
401 shift unless ref($_[0]); # shift if first arg is package name
402 my ($handle, %args) = @_;
404 if (exists $args{'write'}) {
405 $callback = $args{'write'};
407 $wt_callbacks{$handle} = $callback;
408 $wt_handles->add($handle);
410 delete $wt_callbacks{$handle};
411 $wt_handles->remove($handle);
414 if (exists $args{'read'}) {
415 $callback = $args{'read'};
417 $rd_callbacks{$handle} = $callback;
418 $rd_handles->add($handle);
420 delete $rd_callbacks{$handle};
421 $rd_handles->remove($handle);
424 if (exists $args{'error'}) {
425 $callback = $args{'error'};
427 $er_callbacks{$handle} = $callback;
428 $er_handles->add($handle);
430 delete $er_callbacks{$handle};
431 $er_handles->remove($handle);
437 my ($pkg, $loop_count, $timeout) = @_; # event_loop(1) to process events once
438 my ($conn, $r, $w, $e, $rset, $wset, $eset);
441 # Quit the loop if no handles left to process
442 last unless ($rd_handles->count() || $wt_handles->count());
444 ($rset, $wset) = IO::Select->select($rd_handles, $wt_handles, $er_handles, $timeout);
446 foreach $e (@$eset) {
447 &{$er_callbacks{$e}}($e) if exists $er_callbacks{$e};
449 foreach $r (@$rset) {
450 &{$rd_callbacks{$r}}($r) if exists $rd_callbacks{$r};
452 foreach $w (@$wset) {
453 &{$wt_callbacks{$w}}($w) if exists $wt_callbacks{$w};
458 if (defined($loop_count)) {
459 last unless --$loop_count;
467 my $call = $conn->{call} || 'unallocated';
468 dbg('connll', "Connection $call being destroyed ($noconns)");