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)
15 use vars qw($VERSION $BRANCH);
16 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
17 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
18 $main::build += $VERSION;
19 $main::branch += $BRANCH;
26 use vars qw(%rd_callbacks %wt_callbacks %er_callbacks $rd_handles $wt_handles $er_handles $now %conns $noconns $blocking_supported $cnum);
31 $rd_handles = IO::Select->new();
32 $wt_handles = IO::Select->new();
33 $er_handles = IO::Select->new();
38 # Checks if blocking is supported
40 require POSIX; POSIX->import(qw(O_NONBLOCK F_SETFL F_GETFL))
42 if ($@ || $main::is_win) {
43 # print STDERR "POSIX Blocking *** NOT *** supported $@\n";
44 $blocking_supported = 0;
46 $blocking_supported = 1;
47 # print STDERR "POSIX Blocking enabled\n";
51 # import as many of these errno values as are available
53 require Errno; Errno->import(qw(EAGAIN EINPROGRESS EWOULDBLOCK));
57 require Socket; Socket->import(qw(IPPROTO_TCP TCP_NODELAY));
59 unless (*IPPROTO_TCP && !$^O =~ /^MS/) {
60 dbg("IPPROTO_TCP and TCP_NODELAY manually defined");
61 eval '*IPPROTO_TCP = sub { 6 };';
62 eval '*TCP_NODELAY = sub { 1 };';
64 # http://support.microsoft.com/support/kb/articles/Q150/5/37.asp
65 # defines EINPROGRESS as 10035. We provide it here because some
66 # Win32 users report POSIX::EINPROGRESS is not vendor-supported.
67 if ($^O eq 'MSWin32') {
68 eval '*EINPROGRESS = sub { 10036 };';
69 eval '*EWOULDBLOCK = *EAGAIN = sub { 10035 };';
70 eval '*F_GETFL = sub { 0 };';
71 eval '*F_SETFL = sub { 0 };';
72 eval '*IPPROTO_TCP = sub { 6 };';
73 eval '*TCP_NODELAY = sub { 1 };';
74 $blocking_supported = 1;
80 my $eagain = eval {EAGAIN()};
81 my $einprogress = eval {EINPROGRESS()};
82 my $ewouldblock = eval {EWOULDBLOCK()};
88 #-----------------------------------------------------------------
89 # Generalised initializer
93 my ($pkg, $rproc) = @_;
95 my $class = $obj || $pkg;
106 cnum => (($cnum < 999) ? (++$cnum) : ($cnum = 1)),
111 dbg("Connection created ($noconns)") if isdbg('connll');
112 return bless $conn, $class;
118 my $callback = shift;
119 $conn->{eproc} = $callback;
120 set_event_handler($conn->{sock}, error => $callback) if exists $conn->{sock};
126 my $callback = shift;
127 $conn->{rproc} = $callback;
132 return unless $blocking_supported;
134 # Make the handle stop blocking, the Windows way.
136 # 126 is FIONBIO (some docs say 0x7F << 16)
138 0x80000000 | (4 << 16) | (ord('f') << 8) | 126,
142 my $flags = fcntl ($_[0], F_GETFL, 0);
144 $flags &= ~O_NONBLOCK;
146 $flags |= O_NONBLOCK;
148 fcntl ($_[0], F_SETFL, $flags);
160 $call = $pkg->{call} unless $call;
161 return undef unless $call;
162 dbg("changing $pkg->{call} to $call") if isdbg('connll') && exists $pkg->{call} && $call ne $pkg->{call};
163 delete $conns{$pkg->{call}} if exists $pkg->{call} && exists $conns{$pkg->{call}} && $pkg->{call} ne $call;
164 $pkg->{call} = $call;
165 $ref = $conns{$call} = $pkg;
166 dbg("Connection $pkg->{cnum} $call stored") if isdbg('connll');
168 $ref = $conns{$call};
173 # this is only called by any dependent processes going away unexpectedly
176 my ($pkg, $pid) = @_;
178 my @pid = grep {$_->{pid} == $pid} values %conns;
179 foreach my $p (@pid) {
180 &{$p->{eproc}}($p, "$pid has gorn") if exists $p->{eproc};
185 #-----------------------------------------------------------------
188 my ($pkg, $to_host, $to_port, $rproc) = @_;
190 # Create a connection end-point object
193 $conn = $pkg->new($rproc);
195 $conn->{peerhost} = $to_host;
196 $conn->{peerport} = $to_port;
197 $conn->{sort} = 'Outgoing';
199 # Create a new internet socket
200 my $sock = IO::Socket::INET->new();
201 return undef unless $sock;
203 my $proto = getprotobyname('tcp');
204 $sock->socket(AF_INET, SOCK_STREAM, $proto) or return undef;
207 $conn->{blocking} = 0;
209 my $ip = gethostbyname($to_host);
210 # my $r = $sock->connect($to_port, $ip);
211 my $r = connect($sock, pack_sockaddr_in($to_port, $ip));
212 return undef unless $r || _err_will_block($!);
214 $conn->{sock} = $sock;
216 if ($conn->{rproc}) {
217 my $callback = sub {$conn->_rcv};
218 set_event_handler ($sock, read => $callback);
225 return if exists $conn->{disconnecting};
227 $conn->{disconnecting} = 1;
228 my $sock = delete $conn->{sock};
229 $conn->{state} = 'E';
230 $conn->{timeout}->del if $conn->{timeout};
232 # be careful to delete the correct one
234 if ($call = $conn->{call}) {
235 my $ref = $conns{$call};
236 delete $conns{$call} if $ref && $ref == $conn;
238 $call ||= 'unallocated';
239 dbg("Connection $conn->{cnum} $call disconnected") if isdbg('connll');
241 # get rid of any references
243 if (ref($conn->{$_})) {
248 if (defined($sock)) {
249 set_event_handler ($sock, read => undef, write => undef, error => undef);
254 unless ($main::is_win) {
255 kill 'TERM', $conn->{pid} if exists $conn->{pid};
261 my ($conn, $msg) = @_;
262 $conn->enqueue($msg);
263 $conn->_send (1); # 1 ==> flush
267 my ($conn, $msg) = @_;
268 $conn->enqueue($msg);
269 my $sock = $conn->{sock};
270 return unless defined($sock);
271 set_event_handler ($sock, write => sub {$conn->_send(0)});
276 push (@{$conn->{outqueue}}, defined $_[0] ? $_[0] : '');
280 my ($conn, $flush) = @_;
281 my $sock = $conn->{sock};
282 return unless defined($sock);
283 my $rq = $conn->{outqueue};
285 # If $flush is set, set the socket to blocking, and send all
286 # messages in the queue - return only if there's an error
287 # If $flush is 0 (deferred mode) make the socket non-blocking, and
288 # return to the event loop only after every message, or if it
289 # is likely to block in the middle of a message.
291 if ($conn->{blocking} != $flush) {
292 blocking($sock, $flush);
293 $conn->{blocking} = $flush;
295 my $offset = (exists $conn->{send_offset}) ? $conn->{send_offset} : 0;
299 my $mlth = length($msg);
300 my $bytes_to_write = $mlth - $offset;
301 my $bytes_written = 0;
302 confess("Negative Length! msg: '$msg' lth: $mlth offset: $offset") if $bytes_to_write < 0;
303 while ($bytes_to_write > 0) {
304 $bytes_written = syswrite ($sock, $msg,
305 $bytes_to_write, $offset);
306 if (!defined($bytes_written)) {
307 if (_err_will_block($!)) {
308 # Should happen only in deferred mode. Record how
309 # much we have already sent.
310 $conn->{send_offset} = $offset;
311 # Event handler should already be set, so we will
312 # be called back eventually, and will resume sending
315 &{$conn->{eproc}}($conn, $!) if exists $conn->{eproc};
317 return 0; # fail. Message remains in queue ..
319 } elsif (isdbg('raw')) {
320 my $call = $conn->{call} || 'none';
321 dbgdump('raw', "$call send $bytes_written: ", $msg);
323 $offset += $bytes_written;
324 $bytes_to_write -= $bytes_written;
326 delete $conn->{send_offset};
329 #last unless $flush; # Go back to select and wait
330 # for it to fire again.
332 # Call me back if queue has not been drained.
334 set_event_handler ($sock, write => undef);
335 if (exists $conn->{close_on_empty}) {
336 &{$conn->{eproc}}($conn, undef) if exists $conn->{eproc};
346 my $oldsock = $conn->{sock};
347 my $rc = $rd_callbacks{$oldsock};
348 my $wc = $wt_callbacks{$oldsock};
349 my $ec = $er_callbacks{$oldsock};
350 my $sock = $oldsock->new_from_fd($oldsock, "w+");
352 set_event_handler($oldsock, read=>undef, write=>undef, error=>undef);
353 $conn->{sock} = $sock;
354 set_event_handler($sock, read=>$rc, write=>$wc, error=>$ec);
359 sub _err_will_block {
360 return 0 unless $blocking_supported;
361 return ($_[0] == $eagain || $_[0] == $ewouldblock || $_[0] == $einprogress);
367 $conn->{close_on_empty} = 1;
370 #-----------------------------------------------------------------
371 # Receive side routines
374 @_ == 4 || die "Msg->new_server (myhost, myport, login_proc\n";
375 my ($pkg, $my_host, $my_port, $login_proc) = @_;
376 my $self = $pkg->new($login_proc);
378 $self->{sock} = IO::Socket::INET->new (
379 LocalAddr => "$my_host:$my_port",
380 # LocalPort => $my_port,
384 die "Could not create socket: $! \n" unless $self->{sock};
385 set_event_handler ($self->{sock}, read => sub { $self->new_client } );
395 my ($l, $t) = unpack "ll", getsockopt($conn->{sock}, SOL_SOCKET, SO_LINGER);
396 my $k = unpack 'l', getsockopt($conn->{sock}, SOL_SOCKET, SO_KEEPALIVE);
397 my $n = unpack "l", getsockopt($conn->{sock}, IPPROTO_TCP, TCP_NODELAY);
398 dbg("Linger is: $l $t, keepalive: $k, nagle: $n");
401 setsockopt($conn->{sock}, SOL_SOCKET, SO_LINGER, pack("ll", 0, 0)) or confess "setsockopt linger: $!";
402 setsockopt($conn->{sock}, SOL_SOCKET, SO_KEEPALIVE, 1) or confess "setsockopt keepalive: $!";
403 unless ($main::is_win) {
404 setsockopt($conn->{sock}, IPPROTO_TCP, TCP_NODELAY, 1) or confess "setsockopt: $!";
406 $conn->{sock}->autoflush(0);
409 my ($l, $t) = unpack "ll", getsockopt($conn->{sock}, SOL_SOCKET, SO_LINGER);
410 my $k = unpack 'l', getsockopt($conn->{sock}, SOL_SOCKET, SO_KEEPALIVE);
411 my $n = unpack "l", getsockopt($conn->{sock}, IPPROTO_TCP, TCP_NODELAY);
412 dbg("Linger is: $l $t, keepalive: $k, nagle: $n");
420 if ($conn->{msg} =~ /\n/) {
421 my @lines = split /\r?\n/, $conn->{msg};
422 if ($conn->{msg} =~ /\n$/) {
425 $conn->{msg} = pop @lines;
428 &{$conn->{rproc}}($conn, defined $_ ? $_ : '');
433 sub _rcv { # Complement to _send
434 my $conn = shift; # $rcv_now complement of $flush
435 # Find out how much has already been received, if at all
436 my ($msg, $offset, $bytes_to_read, $bytes_read);
437 my $sock = $conn->{sock};
438 return unless defined($sock);
441 if ($conn->{blocking}) {
443 $conn->{blocking} = 0;
445 $bytes_read = sysread ($sock, $msg, 1024, 0);
446 if (defined ($bytes_read)) {
447 if ($bytes_read > 0) {
449 my $call = $conn->{call} || 'none';
450 dbgdump('raw', "$call read $bytes_read: ", $msg);
453 my @ch = split //, $msg;
458 $conn->{msg} =~ s/.$//;
465 set_event_handler ($sock, write => sub{$conn->_send(0)});
466 push @{$conn->{outqueue}}, $out;
469 $conn->{msg} .= $msg;
473 if (_err_will_block($!)) {
481 if (defined $bytes_read && $bytes_read == 0) {
482 &{$conn->{eproc}}($conn, $!) if exists $conn->{eproc};
485 unless ($conn->{disable_read}) {
486 $conn->dequeue if exists $conn->{msg};
492 my $server_conn = shift;
493 my $sock = $server_conn->{sock}->accept();
495 my $conn = $server_conn->new($server_conn->{rproc});
496 $conn->{sock} = $sock;
499 $conn->{blocking} = 0;
500 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost} = $sock->peerhost(), $conn->{peerport} = $sock->peerport());
501 $conn->{sort} = 'Incoming';
503 $conn->{eproc} = $eproc;
504 set_event_handler ($sock, error => $eproc);
507 $conn->{rproc} = $rproc;
508 my $callback = sub {$conn->_rcv};
509 set_event_handler ($sock, read => $callback);
510 } else { # Login failed
511 &{$conn->{eproc}}($conn, undef) if exists $conn->{eproc};
515 dbg("Msg: error on accept ($!)") if isdbg('err');
522 set_event_handler ($conn->{sock}, read => undef, write => undef, error => undef );
523 $conn->{sock}->close;
526 # close all clients (this is for forking really)
527 sub close_all_clients
529 foreach my $conn (values %conns) {
537 set_event_handler ($conn->{sock}, read => undef);
538 return $_[0] ? $conn->{disable_read} = $_[0] : $_[0];
542 #----------------------------------------------------
543 # Event loop routines used by both client and server
545 sub set_event_handler {
546 shift unless ref($_[0]); # shift if first arg is package name
547 my ($handle, %args) = @_;
549 if (exists $args{'write'}) {
550 $callback = $args{'write'};
552 $wt_callbacks{$handle} = $callback;
553 $wt_handles->add($handle);
555 delete $wt_callbacks{$handle};
556 $wt_handles->remove($handle);
559 if (exists $args{'read'}) {
560 $callback = $args{'read'};
562 $rd_callbacks{$handle} = $callback;
563 $rd_handles->add($handle);
565 delete $rd_callbacks{$handle};
566 $rd_handles->remove($handle);
569 if (exists $args{'error'}) {
570 $callback = $args{'error'};
572 $er_callbacks{$handle} = $callback;
573 $er_handles->add($handle);
575 delete $er_callbacks{$handle};
576 $er_handles->remove($handle);
582 my ($pkg, $loop_count, $timeout) = @_; # event_loop(1) to process events once
583 my ($conn, $r, $w, $e, $rset, $wset, $eset);
586 # Quit the loop if no handles left to process
587 last unless ($rd_handles->count() || $wt_handles->count());
589 ($rset, $wset, $eset) = IO::Select->select($rd_handles, $wt_handles, $er_handles, $timeout);
591 foreach $e (@$eset) {
592 &{$er_callbacks{$e}}($e) if exists $er_callbacks{$e};
594 foreach $r (@$rset) {
595 &{$rd_callbacks{$r}}($r) if exists $rd_callbacks{$r};
597 foreach $w (@$wset) {
598 &{$wt_callbacks{$w}}($w) if exists $wt_callbacks{$w};
603 if (defined($loop_count)) {
604 last unless --$loop_count;
611 my ($pkg, $interval) = @_;
613 while (time - $now < $interval) {
614 $pkg->event_loop(10, 0.01);
621 my $call = $conn->{call} || 'unallocated';
622 my $host = $conn->{peerhost} || '';
623 my $port = $conn->{peerport} || '';
624 dbg("Connection $conn->{cnum} $call [$host $port] being destroyed") if isdbg('connll');