83c82be6e1a70bcb04433187b1df6ca020d3046d
[spider.git] / perl / Msg.pm
1 #
2 # This has been taken from the 'Advanced Perl Programming' book by Sriram Srinivasan 
3 #
4 # I am presuming that the code is distributed on the same basis as perl itself.
5 #
6 # I have modified it to suit my devious purposes (Dirk Koopman G1TLH)
7 #
8 #
9 #
10
11 package Msg;
12
13 use strict;
14
15 use DXUtil;
16
17 use IO::Select;
18 use DXDebug;
19 use Timer;
20
21 use vars qw(%rd_callbacks %wt_callbacks %er_callbacks $rd_handles $wt_handles $er_handles $now %conns $noconns $blocking_supported $cnum $total_in $total_out $io_socket);
22
23 %rd_callbacks = ();
24 %wt_callbacks = ();
25 %er_callbacks = ();
26 $rd_handles   = IO::Select->new();
27 $wt_handles   = IO::Select->new();
28 $er_handles   = IO::Select->new();
29 $total_in = $total_out = 0;
30
31 $now = time;
32
33 BEGIN {
34     # Checks if blocking is supported
35     eval {
36                 local $^W;
37         require POSIX; POSIX->import(qw(O_NONBLOCK F_SETFL F_GETFL))
38     };
39
40         eval {
41                 local $^W;
42                 require IO::Socket::INET6;
43         };
44
45         if ($@) {
46                 dbg($@);
47                 require IO::Socket;
48                 $io_socket = 'IO::Socket::INET';
49         } else {
50                 $io_socket = 'IO::Socket::INET6';
51         }
52         $io_socket->import;
53
54         if ($@ || $main::is_win) {
55                 $blocking_supported = $io_socket->can('blocking') ? 2 : 0;
56         } else {
57                 $blocking_supported = $io_socket->can('blocking') ? 2 : 1;
58         }
59
60
61         # import as many of these errno values as are available
62         eval {
63                 local $^W;
64                 require Errno; Errno->import(qw(EAGAIN EINPROGRESS EWOULDBLOCK));
65         };
66
67         unless ($^O eq 'MSWin32') {
68                 if ($] >= 5.6) {
69                         eval {
70                                 local $^W;
71                                 require Socket; Socket->import(qw(IPPROTO_TCP TCP_NODELAY));
72                         };
73                 } else {
74                         dbg("IPPROTO_TCP and TCP_NODELAY manually defined");
75                         eval 'sub IPPROTO_TCP {     6 };';
76                         eval 'sub TCP_NODELAY {     1 };';
77                 }
78         }
79         # http://support.microsoft.com/support/kb/articles/Q150/5/37.asp
80         # defines EINPROGRESS as 10035.  We provide it here because some
81         # Win32 users report POSIX::EINPROGRESS is not vendor-supported.
82         if ($^O eq 'MSWin32') { 
83                 eval '*EINPROGRESS = sub { 10036 };' unless defined *EINPROGRESS;
84                 eval '*EWOULDBLOCK = *EAGAIN = sub { 10035 };' unless defined *EWOULDBLOCK;
85                 eval '*F_GETFL     = sub {     0 };' unless defined *F_GETFL;
86                 eval '*F_SETFL     = sub {     0 };' unless defined *F_SETFL;
87                 eval 'sub IPPROTO_TCP  {     6 };';
88                 eval 'sub TCP_NODELAY  {     1 };';
89                 $blocking_supported = 0;   # it appears that this DOESN'T work :-(
90         } 
91 }
92
93 my $w = $^W;
94 $^W = 0;
95 my $eagain = eval {EAGAIN()};
96 my $einprogress = eval {EINPROGRESS()};
97 my $ewouldblock = eval {EWOULDBLOCK()};
98 $^W = $w;
99 $cnum = 0;
100
101
102 #
103 #-----------------------------------------------------------------
104 # Generalised initializer
105
106 sub new
107 {
108     my ($pkg, $rproc) = @_;
109         my $obj = ref($pkg);
110         my $class = $obj || $pkg;
111
112     my $conn = {
113         rproc => $rproc,
114                 inqueue => [],
115                 outqueue => [],
116                 state => 0,
117                 lineend => "\r\n",
118                 csort => 'telnet',
119                 timeval => 60,
120                 blocking => 0,
121                 cnum => (($cnum < 999) ? (++$cnum) : ($cnum = 1)),
122     };
123
124         $noconns++;
125         
126         dbg("$class Connection $conn->{cnum} created (total $noconns)") if isdbg('connll');
127         return bless $conn, $class;
128 }
129
130 sub set_error
131 {
132         my $conn = shift;
133         my $callback = shift;
134         $conn->{eproc} = $callback;
135         set_event_handler($conn->{sock}, error => $callback) if exists $conn->{sock};
136 }
137
138 sub set_rproc
139 {
140         my $conn = shift;
141         my $callback = shift;
142         $conn->{rproc} = $callback;
143 }
144
145 sub blocking
146 {
147         return unless $blocking_supported;
148
149         # Make the handle stop blocking, the Windows way.
150         if ($blocking_supported) { 
151                 $_[0]->blocking($_[1]);
152         } else {
153                 my $flags = fcntl ($_[0], F_GETFL, 0);
154                 if ($_[1]) {
155                         $flags &= ~O_NONBLOCK;
156                 } else {
157                         $flags |= O_NONBLOCK;
158                 }
159                 fcntl ($_[0], F_SETFL, $flags);
160         }
161 }
162
163 # save it
164 sub conns
165 {
166         my $pkg = shift;
167         my $call = shift;
168         my $ref;
169         
170         if (ref $pkg) {
171                 $call = $pkg->{call} unless $call;
172                 return undef unless $call;
173                 dbg((ref $pkg) . " changing $pkg->{call} to $call") if isdbg('connll') && exists $pkg->{call} && $call ne $pkg->{call};
174                 delete $conns{$pkg->{call}} if exists $pkg->{call} && exists $conns{$pkg->{call}} && $pkg->{call} ne $call; 
175                 $pkg->{call} = $call;
176                 $ref = $conns{$call} = $pkg;
177                 dbg((ref $pkg) . " Connection $pkg->{cnum} $call stored") if isdbg('connll');
178         } else {
179                 $ref = $conns{$call};
180         }
181         return $ref;
182 }
183
184 # this is only called by any dependent processes going away unexpectedly
185 sub pid_gone
186 {
187         my ($pkg, $pid) = @_;
188         
189         my @pid = grep {$_->{pid} == $pid} values %conns;
190         foreach my $p (@pid) {
191                 &{$p->{eproc}}($p, "$pid has gorn") if exists $p->{eproc};
192                 $p->disconnect;
193         }
194 }
195
196 sub ax25
197 {
198         my $conn = shift;
199         return $conn->{csort} eq 'ax25';
200 }
201
202 sub peerhost
203 {
204         my $conn = shift;
205         $conn->{peerhost} ||= 'ax25' if $conn->ax25;
206         $conn->{peerhost} ||= $conn->{sock}->peerhost if $conn->{sock} && $conn->{sock}->isa('IO::Socket::INET');
207         $conn->{peerhost} ||= 'UNKNOWN';
208         return $conn->{peerhost};
209 }
210
211 #-----------------------------------------------------------------
212 # Send side routines
213 sub connect {
214     my ($pkg, $to_host, $to_port, $rproc) = @_;
215
216     # Create a connection end-point object
217     my $conn = $pkg;
218         unless (ref $pkg) {
219                 $conn = $pkg->new($rproc);
220         }
221         $conn->{peerhost} = $to_host;
222         $conn->{peerport} = $to_port;
223         $conn->{sort} = 'Outgoing';
224
225         dbg((ref $conn) . " connecting $conn->{cnum} to $to_host:$to_port") if isdbg('connll');
226         
227         my $sock;
228         if ($blocking_supported) {
229                 $sock = $io_socket->new(PeerAddr => $to_host, PeerPort => $to_port, Proto => 'tcp', Blocking =>0) or return undef;
230         } else {
231                 # Create a new internet socket
232                 $sock = $io_socket->new();
233                 return undef unless $sock;
234
235                 my $proto = getprotobyname('tcp');
236                 $sock->socket(AF_INET, SOCK_STREAM, $proto) or return undef;
237
238                 blocking($sock, 0);
239                 $conn->{blocking} = 0;
240
241                 # does the host resolve?
242                 my $ip = gethostbyname($to_host);
243                 return undef unless $ip;
244
245                 my $r = connect($sock, pack_sockaddr_in($to_port, $ip));
246                 return undef unless $r || _err_will_block($!);
247         }
248         
249         $conn->{sock} = $sock;
250 #       $conn->{peerhost} = $sock->peerhost;    # for consistency
251
252         dbg((ref $conn) . " connected $conn->{cnum} to $to_host:$to_port") if isdbg('connll');
253
254     if ($conn->{rproc}) {
255         my $callback = sub {$conn->_rcv};
256         set_event_handler ($sock, read => $callback);
257     }
258     return $conn;
259 }
260
261 sub start_program
262 {
263         my ($conn, $line, $sort) = @_;
264         my $pid;
265         
266         local $^F = 10000;              # make sure it ain't closed on exec
267         my ($a, $b) = $io_socket->socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
268         if ($a && $b) {
269                 $a->autoflush(1);
270                 $b->autoflush(1);
271                 $pid = fork;
272                 if (defined $pid) {
273                         if ($pid) {
274                                 close $b;
275                                 $conn->{sock} = $a;
276                                 $conn->{csort} = $sort;
277                                 $conn->{lineend} = "\cM" if $sort eq 'ax25';
278                                 $conn->{pid} = $pid;
279                                 if ($conn->{rproc}) {
280                                         my $callback = sub {$conn->_rcv};
281                                         Msg::set_event_handler ($a, read => $callback);
282                                 }
283                                 dbg("connect $conn->{cnum}: started pid: $conn->{pid} as $line") if isdbg('connect');
284                         } else {
285                                 $^W = 0;
286                                 dbgclose();
287                                 STDIN->close;
288                                 STDOUT->close;
289                                 STDOUT->close;
290                                 *STDIN = IO::File->new_from_fd($b, 'r') or die;
291                                 *STDOUT = IO::File->new_from_fd($b, 'w') or die;
292                                 *STDERR = IO::File->new_from_fd($b, 'w') or die;
293                                 close $a;
294                                 unless ($main::is_win) {
295                                         #                                               $SIG{HUP} = 'IGNORE';
296                                         $SIG{HUP} = $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = 'DEFAULT';
297                                         alarm(0);
298                                 }
299                                 exec "$line" or dbg("exec '$line' failed $!");
300                         } 
301                 } else {
302                         dbg("cannot fork for $line");
303                 }
304         } else {
305                 dbg("no socket pair $! for $line");
306         }
307         return $pid;
308 }
309
310 sub disconnect 
311 {
312     my $conn = shift;
313         return if exists $conn->{disconnecting};
314
315         $conn->{disconnecting} = 1;
316     my $sock = delete $conn->{sock};
317         $conn->{state} = 'E';
318         $conn->{timeout}->del if $conn->{timeout};
319
320         # be careful to delete the correct one
321         my $call;
322         if ($call = $conn->{call}) {
323                 my $ref = $conns{$call};
324                 delete $conns{$call} if $ref && $ref == $conn;
325         }
326         $call ||= 'unallocated';
327         dbg((ref $conn) . " Connection $conn->{cnum} $call disconnected") if isdbg('connll');
328         
329         # get rid of any references
330         for (keys %$conn) {
331                 if (ref($conn->{$_})) {
332                         delete $conn->{$_};
333                 }
334         }
335
336         if (defined($sock)) {
337                 set_event_handler ($sock, read => undef, write => undef, error => undef);
338                 shutdown($sock, 2);
339                 close($sock);
340         }
341         
342         unless ($main::is_win) {
343                 kill 'TERM', $conn->{pid} if exists $conn->{pid};
344         }
345 }
346
347 sub send_now {
348     my ($conn, $msg) = @_;
349     $conn->enqueue($msg);
350     $conn->_send (1); # 1 ==> flush
351 }
352
353 sub send_later {
354     my ($conn, $msg) = @_;
355     $conn->enqueue($msg);
356     my $sock = $conn->{sock};
357     return unless defined($sock);
358     set_event_handler ($sock, write => sub {$conn->_send(0)});
359 }
360
361 sub enqueue {
362     my $conn = shift;
363     push (@{$conn->{outqueue}}, defined $_[0] ? $_[0] : '');
364 }
365
366 sub _send {
367     my ($conn, $flush) = @_;
368     my $sock = $conn->{sock};
369     return unless defined($sock);
370     my $rq = $conn->{outqueue};
371
372     # If $flush is set, set the socket to blocking, and send all
373     # messages in the queue - return only if there's an error
374     # If $flush is 0 (deferred mode) make the socket non-blocking, and
375     # return to the event loop only after every message, or if it
376     # is likely to block in the middle of a message.
377
378 #       if ($conn->{blocking} != $flush) {
379 #               blocking($sock, $flush);
380 #               $conn->{blocking} = $flush;
381 #       }
382     my $offset = (exists $conn->{send_offset}) ? $conn->{send_offset} : 0;
383
384     while (@$rq) {
385         my $msg            = $rq->[0];
386                 my $mlth           = length($msg);
387         my $bytes_to_write = $mlth - $offset;
388         my $bytes_written  = 0;
389                 confess("Negative Length! msg: '$msg' lth: $mlth offset: $offset") if $bytes_to_write < 0;
390         while ($bytes_to_write > 0) {
391             $bytes_written = syswrite ($sock, $msg,
392                                        $bytes_to_write, $offset);
393             if (!defined($bytes_written)) {
394                 if (_err_will_block($!)) {
395                     # Should happen only in deferred mode. Record how
396                     # much we have already sent.
397                     $conn->{send_offset} = $offset;
398                     # Event handler should already be set, so we will
399                     # be called back eventually, and will resume sending
400                     return 1;
401                 } else {    # Uh, oh
402                                         &{$conn->{eproc}}($conn, $!) if exists $conn->{eproc};
403                                         $conn->disconnect;
404                     return 0; # fail. Message remains in queue ..
405                 }
406             } elsif (isdbg('raw')) {
407                                 my $call = $conn->{call} || 'none';
408                                 dbgdump('raw', "$call send $bytes_written: ", $msg);
409                         }
410                         $total_out      += $bytes_written;
411             $offset         += $bytes_written;
412             $bytes_to_write -= $bytes_written;
413         }
414         delete $conn->{send_offset};
415         $offset = 0;
416         shift @$rq;
417         #last unless $flush; # Go back to select and wait
418                             # for it to fire again.
419     }
420     # Call me back if queue has not been drained.
421     unless (@$rq) {
422         set_event_handler ($sock, write => undef);
423                 if (exists $conn->{close_on_empty}) {
424                         &{$conn->{eproc}}($conn, undef) if exists $conn->{eproc};
425                         $conn->disconnect; 
426                 }
427     }
428     1;  # Success
429 }
430
431 sub dup_sock
432 {
433         my $conn = shift;
434         my $oldsock = $conn->{sock};
435         my $rc = $rd_callbacks{$oldsock};
436         my $wc = $wt_callbacks{$oldsock};
437         my $ec = $er_callbacks{$oldsock};
438         my $sock = $oldsock->new_from_fd($oldsock, "w+");
439         if ($sock) {
440                 set_event_handler($oldsock, read=>undef, write=>undef, error=>undef);
441                 $conn->{sock} = $sock;
442                 set_event_handler($sock, read=>$rc, write=>$wc, error=>$ec);
443                 $oldsock->close;
444         }
445 }
446
447 sub _err_will_block {
448         return 0 unless $blocking_supported;
449         return ($_[0] == $eagain || $_[0] == $ewouldblock || $_[0] == $einprogress);
450 }
451
452 sub close_on_empty
453 {
454         my $conn = shift;
455         $conn->{close_on_empty} = 1;
456 }
457
458 #-----------------------------------------------------------------
459 # Receive side routines
460
461 sub new_server {
462     @_ == 4 || die "Msg->new_server (myhost, myport, login_proc\n";
463     my ($pkg, $my_host, $my_port, $login_proc) = @_;
464         my $self = $pkg->new($login_proc);
465         
466     $self->{sock} = $io_socket->new (
467                                           LocalAddr => $my_host,
468                                           LocalPort => $my_port,
469                                           Listen    => SOMAXCONN,
470                                           Proto     => 'tcp',
471                                           Reuse => 1);
472     die "Could not create socket: $! \n" unless $self->{sock};
473     set_event_handler ($self->{sock}, read => sub { $self->new_client }  );
474         return $self;
475 }
476
477
478 sub nolinger
479 {
480         my $conn = shift;
481
482         unless ($main::is_win) {
483                 if (isdbg('sock')) {
484                         my ($l, $t) = unpack "ll", getsockopt($conn->{sock}, SOL_SOCKET, SO_LINGER); 
485                         my $k = unpack 'l', getsockopt($conn->{sock}, SOL_SOCKET, SO_KEEPALIVE);
486                         my $n = $main::is_win ? 0 : unpack "l", getsockopt($conn->{sock}, IPPROTO_TCP, TCP_NODELAY);
487                         dbg("Linger is: $l $t, keepalive: $k, nagle: $n");
488                 }
489                 
490                 eval {setsockopt($conn->{sock}, SOL_SOCKET, SO_KEEPALIVE, 1)} or dbg("setsockopt keepalive: $!");
491                 eval {setsockopt($conn->{sock}, SOL_SOCKET, SO_LINGER, pack("ll", 0, 0))} or dbg("setsockopt linger: $!");
492                 eval {setsockopt($conn->{sock}, IPPROTO_TCP, TCP_NODELAY, 1)} or eval {setsockopt($conn->{sock}, SOL_SOCKET, TCP_NODELAY, 1)} or dbg("setsockopt tcp_nodelay: $!");
493                 $conn->{sock}->autoflush(0);
494
495                 if (isdbg('sock')) {
496                         my ($l, $t) = unpack "ll", getsockopt($conn->{sock}, SOL_SOCKET, SO_LINGER); 
497                         my $k = unpack 'l', getsockopt($conn->{sock}, SOL_SOCKET, SO_KEEPALIVE);
498                         my $n = $main::is_win ? 0 : unpack "l", getsockopt($conn->{sock}, IPPROTO_TCP, TCP_NODELAY);
499                         dbg("Linger is: $l $t, keepalive: $k, nagle: $n");
500                 }
501         } 
502 }
503
504 sub dequeue
505 {
506         my $conn = shift;
507         return if $conn->{disconnecting};
508         
509         if ($conn->{msg} =~ /\cJ/) {
510                 my @lines = split /\cM?\cJ/, $conn->{msg};
511                 if ($conn->{msg} =~ /\cM?\cJ$/) {
512                         delete $conn->{msg};
513                 } else {
514                         $conn->{msg} = pop @lines;
515                 }
516                 for (@lines) {
517                         last if $conn->{disconnecting};
518                         &{$conn->{rproc}}($conn, defined $_ ? $_ : '');
519                 }
520         }
521 }
522
523 sub _rcv {                     # Complement to _send
524     my $conn = shift; # $rcv_now complement of $flush
525     # Find out how much has already been received, if at all
526     my ($msg, $offset, $bytes_to_read, $bytes_read);
527     my $sock = $conn->{sock};
528     return unless defined($sock);
529
530         my @lines;
531 #       if ($conn->{blocking}) {
532 #               blocking($sock, 0);
533 #               $conn->{blocking} = 0;
534 #       }
535         $bytes_read = sysread ($sock, $msg, 1024, 0);
536         if (defined ($bytes_read)) {
537                 if ($bytes_read > 0) {
538                         $total_in += $bytes_read;
539                         if (isdbg('raw')) {
540                                 my $call = $conn->{call} || 'none';
541                                 dbgdump('raw', "$call read $bytes_read: ", $msg);
542                         }
543                         if ($conn->{echo}) {
544                                 my @ch = split //, $msg;
545                                 my $out;
546                                 for (@ch) {
547                                         if (/[\cH\x7f]/) {
548                                                 $out .= "\cH \cH";
549                                                 $conn->{msg} =~ s/.$//;
550                                         } else {
551                                                 $out .= $_;
552                                                 $conn->{msg} .= $_;
553                                         }
554                                 }
555                                 if (defined $out) {
556                                         set_event_handler ($sock, write => sub{$conn->_send(0)});
557                                         push @{$conn->{outqueue}}, $out;
558                                 }
559                         } else {
560                                 $conn->{msg} .= $msg;
561                         }
562                 } 
563         } else {
564                 if (_err_will_block($!)) {
565                         return ; 
566                 } else {
567                         $bytes_read = 0;
568                 }
569     }
570
571 FINISH:
572     if (defined $bytes_read && $bytes_read == 0) {
573                 &{$conn->{eproc}}($conn, $!) if exists $conn->{eproc};
574                 $conn->disconnect;
575     } else {
576                 unless ($conn->{disable_read}) {
577                         $conn->dequeue if exists $conn->{msg};
578                 }
579         }
580 }
581
582 sub new_client {
583         my $server_conn = shift;
584     my $sock = $server_conn->{sock}->accept();
585         if ($sock) {
586                 my $conn = $server_conn->new($server_conn->{rproc});
587                 $conn->{sock} = $sock;
588                 blocking($sock, 0);
589                 $conn->nolinger;
590                 $conn->{blocking} = 0;
591                 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost} = $sock->peerhost(), $conn->{peerport} = $sock->peerport());
592                 $conn->{sort} = 'Incoming';
593                 if ($eproc) {
594                         $conn->{eproc} = $eproc;
595                         set_event_handler ($sock, error => $eproc);
596                 }
597                 if ($rproc) {
598                         $conn->{rproc} = $rproc;
599                         my $callback = sub {$conn->_rcv};
600                         set_event_handler ($sock, read => $callback);
601                 } else {  # Login failed
602                         &{$conn->{eproc}}($conn, undef) if exists $conn->{eproc};
603                         $conn->disconnect();
604                 }
605         } else {
606                 dbg("Msg: error on accept ($!)") if isdbg('err');
607         }
608 }
609
610 sub close_server
611 {
612         my $conn = shift;
613         set_event_handler ($conn->{sock}, read => undef, write => undef, error => undef );
614         $conn->{sock}->close;
615 }
616
617 # close all clients (this is for forking really)
618 sub close_all_clients
619 {
620         foreach my $conn (values %conns) {
621                 $conn->disconnect;
622         }
623 }
624
625 sub disable_read
626 {
627         my $conn = shift;
628         set_event_handler ($conn->{sock}, read => undef);
629         return $_[0] ? $conn->{disable_read} = $_[0] : $_[0];
630 }
631
632 #
633 #----------------------------------------------------
634 # Event loop routines used by both client and server
635
636 sub set_event_handler {
637     shift unless ref($_[0]); # shift if first arg is package name
638     my ($handle, %args) = @_;
639     my $callback;
640     if (exists $args{'write'}) {
641         $callback = $args{'write'};
642         if ($callback) {
643             $wt_callbacks{$handle} = $callback;
644             $wt_handles->add($handle);
645         } else {
646             delete $wt_callbacks{$handle};
647             $wt_handles->remove($handle);
648         }
649     }
650     if (exists $args{'read'}) {
651         $callback = $args{'read'};
652         if ($callback) {
653             $rd_callbacks{$handle} = $callback;
654             $rd_handles->add($handle);
655         } else {
656             delete $rd_callbacks{$handle};
657             $rd_handles->remove($handle);
658        }
659     }
660     if (exists $args{'error'}) {
661         $callback = $args{'error'};
662         if ($callback) {
663             $er_callbacks{$handle} = $callback;
664             $er_handles->add($handle);
665         } else {
666             delete $er_callbacks{$handle};
667             $er_handles->remove($handle);
668        }
669     }
670 }
671
672 sub event_loop {
673     my ($pkg, $loop_count, $timeout, $wronly) = @_; # event_loop(1) to process events once
674     my ($conn, $r, $w, $e, $rset, $wset, $eset);
675     while (1) {
676  
677        # Quit the loop if no handles left to process
678                 if ($wronly) {
679                         last unless $wt_handles->count();
680         
681                         ($rset, $wset, $eset) = IO::Select->select(undef, $wt_handles, undef, $timeout);
682                         
683                         foreach $w (@$wset) {
684                                 &{$wt_callbacks{$w}}($w) if exists $wt_callbacks{$w};
685                         }
686                 } else {
687                         
688                         last unless ($rd_handles->count() || $wt_handles->count());
689         
690                         ($rset, $wset, $eset) = IO::Select->select($rd_handles, $wt_handles, $er_handles, $timeout);
691                         
692                         foreach $e (@$eset) {
693                                 &{$er_callbacks{$e}}($e) if exists $er_callbacks{$e};
694                         }
695                         foreach $r (@$rset) {
696                                 &{$rd_callbacks{$r}}($r) if exists $rd_callbacks{$r};
697                         }
698                         foreach $w (@$wset) {
699                                 &{$wt_callbacks{$w}}($w) if exists $wt_callbacks{$w};
700                         }
701                 }
702
703                 Timer::handler;
704                 
705         if (defined($loop_count)) {
706             last unless --$loop_count;
707         }
708     }
709 }
710
711 sub sleep
712 {
713         my ($pkg, $interval) = @_;
714         my $now = time;
715         while (time - $now < $interval) {
716                 $pkg->event_loop(10, 0.01);
717         }
718 }
719
720 sub DESTROY
721 {
722         my $conn = shift;
723         my $call = $conn->{call} || 'unallocated';
724         my $host = $conn->{peerhost} || '';
725         my $port = $conn->{peerport} || '';
726         $noconns--;
727         dbg((ref $conn) . " Connection $conn->{cnum} $call [$host $port] being destroyed (total $noconns)") if isdbg('connll');
728 }
729
730 1;
731
732 __END__
733