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)
19 use vars qw(%rd_callbacks %wt_callbacks %er_callbacks $rd_handles $wt_handles $er_handles $now %conns $noconns $blocking_supported);
24 $rd_handles = IO::Select->new();
25 $wt_handles = IO::Select->new();
26 $er_handles = IO::Select->new();
31 # Checks if blocking is supported
33 require POSIX; POSIX->import(qw(O_NONBLOCK F_SETFL F_GETFL))
36 print STDERR "POSIX Blocking *** NOT *** supported $@\n";
38 $blocking_supported = 1;
39 print STDERR "POSIX Blocking enabled\n";
43 # import as many of these errno values as are available
45 require Errno; Errno->import(qw(EAGAIN EINPROGRESS EWOULDBLOCK));
51 my $eagain = eval {EAGAIN()};
52 my $einprogress = eval {EINPROGRESS()};
53 my $ewouldblock = eval {EWOULDBLOCK()};
57 #-----------------------------------------------------------------
58 # Generalised initializer
62 my ($pkg, $rproc) = @_;
64 my $class = $obj || $pkg;
78 dbg('connll', "Connection created ($noconns)");
79 return bless $conn, $class;
86 $conn->{eproc} = $callback;
87 set_event_handler($conn->{sock}, error => $callback) if exists $conn->{sock};
94 $conn->{rproc} = $callback;
99 return unless $blocking_supported;
101 my $flags = fcntl ($_[0], F_GETFL, 0);
103 $flags &= ~O_NONBLOCK;
105 $flags |= O_NONBLOCK;
107 fcntl ($_[0], F_SETFL, $flags);
118 $call = $pkg->{call} unless $call;
119 return undef unless $call;
120 confess "changing $pkg->{call} to $call" if exists $pkg->{call} && $call ne $pkg->{call};
121 $pkg->{call} = $call;
122 $ref = $conns{$call} = $pkg;
123 dbg('connll', "Connection $call stored");
125 $ref = $conns{$call};
130 # this is only called by any dependent processes going away unexpectedly
133 my ($pkg, $pid) = @_;
135 my @pid = grep {$_->{pid} == $pid} values %conns;
137 &{$_->{eproc}}($_, "$pid has gorn") if exists $_->{eproc};
142 #-----------------------------------------------------------------
145 my ($pkg, $to_host, $to_port, $rproc) = @_;
147 # Create a connection end-point object
150 $conn = $pkg->new($rproc);
152 $conn->{peerhost} = $to_host;
153 $conn->{peerport} = $to_port;
154 $conn->{sort} = 'Outgoing';
156 # Create a new internet socket
157 my $sock = IO::Socket::INET->new();
158 return undef unless $sock;
160 my $proto = getprotobyname('tcp');
161 $sock->socket(AF_INET, SOCK_STREAM, $proto) or return undef;
164 $conn->{blocking} = 0;
166 my $ip = gethostbyname($to_host);
167 # my $r = $sock->connect($to_port, $ip);
168 my $r = connect($sock, pack_sockaddr_in($to_port, $ip));
169 return undef unless $r || _err_will_block($!);
171 $conn->{sock} = $sock;
173 if ($conn->{rproc}) {
174 my $callback = sub {$conn->_rcv};
175 set_event_handler ($sock, read => $callback);
182 return if exists $conn->{disconnecting};
184 $conn->{disconnecting} = 1;
185 my $sock = delete $conn->{sock};
186 $conn->{state} = 'E';
187 $conn->{timeout}->del if $conn->{timeout};
189 # be careful to delete the correct one
191 if ($call = $conn->{call}) {
192 my $ref = $conns{$call};
193 delete $conns{$call} if $ref && $ref == $conn;
195 $call ||= 'unallocated';
196 dbg('connll', "Connection $call disconnected");
198 unless ($^O =~ /^MS/i) {
199 kill 'TERM', $conn->{pid} if exists $conn->{pid};
202 # get rid of any references
204 if (ref($conn->{$_})) {
209 return unless defined($sock);
210 set_event_handler ($sock, read => undef, write => undef, error => undef);
216 my ($conn, $msg) = @_;
217 $conn->enqueue($msg);
218 $conn->_send (1); # 1 ==> flush
222 my ($conn, $msg) = @_;
223 $conn->enqueue($msg);
224 my $sock = $conn->{sock};
225 return unless defined($sock);
226 set_event_handler ($sock, write => sub {$conn->_send(0)});
231 push (@{$conn->{outqueue}}, defined $_[0] ? $_[0] : '');
235 my ($conn, $flush) = @_;
236 my $sock = $conn->{sock};
237 return unless defined($sock);
238 my $rq = $conn->{outqueue};
240 # If $flush is set, set the socket to blocking, and send all
241 # messages in the queue - return only if there's an error
242 # If $flush is 0 (deferred mode) make the socket non-blocking, and
243 # return to the event loop only after every message, or if it
244 # is likely to block in the middle of a message.
246 if ($conn->{blocking} != $flush) {
247 blocking($sock, $flush);
248 $conn->{blocking} = $flush;
250 my $offset = (exists $conn->{send_offset}) ? $conn->{send_offset} : 0;
254 my $mlth = length($msg);
255 my $bytes_to_write = $mlth - $offset;
256 my $bytes_written = 0;
257 confess("Negative Length! msg: '$msg' lth: $mlth offset: $offset") if $bytes_to_write < 0;
258 while ($bytes_to_write > 0) {
259 $bytes_written = syswrite ($sock, $msg,
260 $bytes_to_write, $offset);
261 if (!defined($bytes_written)) {
262 if (_err_will_block($!)) {
263 # Should happen only in deferred mode. Record how
264 # much we have already sent.
265 $conn->{send_offset} = $offset;
266 # Event handler should already be set, so we will
267 # be called back eventually, and will resume sending
270 &{$conn->{eproc}}($conn, $!) if exists $conn->{eproc};
272 return 0; # fail. Message remains in queue ..
275 $offset += $bytes_written;
276 $bytes_to_write -= $bytes_written;
278 delete $conn->{send_offset};
281 last unless $flush; # Go back to select and wait
282 # for it to fire again.
284 # Call me back if queue has not been drained.
286 set_event_handler ($sock, write => sub {$conn->_send(0)});
288 set_event_handler ($sock, write => undef);
289 if (exists $conn->{close_on_empty}) {
290 &{$conn->{eproc}}($conn, undef) if exists $conn->{eproc};
300 my $oldsock = $conn->{sock};
301 my $rc = $rd_callbacks{$oldsock};
302 my $wc = $wt_callbacks{$oldsock};
303 my $ec = $er_callbacks{$oldsock};
304 my $sock = $oldsock->new_from_fd($oldsock, "w+");
306 set_event_handler($oldsock, read=>undef, write=>undef, error=>undef);
307 $conn->{sock} = $sock;
308 set_event_handler($sock, read=>$rc, write=>$wc, error=>$ec);
313 sub _err_will_block {
314 return 0 unless $blocking_supported;
315 return ($_[0] == $eagain || $_[0] == $ewouldblock || $_[0] == $einprogress);
321 $conn->{close_on_empty} = 1;
324 #-----------------------------------------------------------------
325 # Receive side routines
328 @_ == 4 || die "Msg->new_server (myhost, myport, login_proc\n";
329 my ($pkg, $my_host, $my_port, $login_proc) = @_;
330 my $self = $pkg->new($login_proc);
332 $self->{sock} = IO::Socket::INET->new (
333 LocalAddr => $my_host,
334 LocalPort => $my_port,
338 die "Could not create socket: $! \n" unless $self->{sock};
339 set_event_handler ($self->{sock}, read => sub { $self->new_client } );
347 if ($conn->{msg} =~ /\n/) {
348 my @lines = split /\r?\n/, $conn->{msg};
349 if ($conn->{msg} =~ /\n$/) {
352 $conn->{msg} = pop @lines;
355 &{$conn->{rproc}}($conn, defined $_ ? $_ : '');
360 sub _rcv { # Complement to _send
361 my $conn = shift; # $rcv_now complement of $flush
362 # Find out how much has already been received, if at all
363 my ($msg, $offset, $bytes_to_read, $bytes_read);
364 my $sock = $conn->{sock};
365 return unless defined($sock);
368 if ($conn->{blocking}) {
370 $conn->{blocking} = 0;
372 $bytes_read = sysread ($sock, $msg, 1024, 0);
373 if (defined ($bytes_read)) {
374 if ($bytes_read > 0) {
375 $conn->{msg} .= $msg;
378 if (_err_will_block($!)) {
386 if (defined $bytes_read && $bytes_read == 0) {
387 &{$conn->{eproc}}($conn, $!) if exists $conn->{eproc};
390 $conn->dequeue if exists $conn->{msg};
395 my $server_conn = shift;
396 my $sock = $server_conn->{sock}->accept();
398 my $conn = $server_conn->new($server_conn->{rproc});
399 $conn->{sock} = $sock;
401 $conn->{blocking} = 0;
402 my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost} = $sock->peerhost(), $conn->{peerport} = $sock->peerport());
403 $conn->{sort} = 'Incoming';
405 $conn->{eproc} = $eproc;
406 set_event_handler ($sock, error => $eproc);
409 $conn->{rproc} = $rproc;
410 my $callback = sub {$conn->_rcv};
411 set_event_handler ($sock, read => $callback);
412 } else { # Login failed
413 &{$conn->{eproc}}($conn, undef) if exists $conn->{eproc};
417 dbg('err', "Msg: error on accept ($!)");
424 set_event_handler ($conn->{sock}, read => undef, write => undef, error => undef );
425 $conn->{sock}->close;
428 # close all clients (this is for forking really)
429 sub close_all_clients
431 for (values %conns) {
437 #----------------------------------------------------
438 # Event loop routines used by both client and server
440 sub set_event_handler {
441 shift unless ref($_[0]); # shift if first arg is package name
442 my ($handle, %args) = @_;
444 if (exists $args{'write'}) {
445 $callback = $args{'write'};
447 $wt_callbacks{$handle} = $callback;
448 $wt_handles->add($handle);
450 delete $wt_callbacks{$handle};
451 $wt_handles->remove($handle);
454 if (exists $args{'read'}) {
455 $callback = $args{'read'};
457 $rd_callbacks{$handle} = $callback;
458 $rd_handles->add($handle);
460 delete $rd_callbacks{$handle};
461 $rd_handles->remove($handle);
464 if (exists $args{'error'}) {
465 $callback = $args{'error'};
467 $er_callbacks{$handle} = $callback;
468 $er_handles->add($handle);
470 delete $er_callbacks{$handle};
471 $er_handles->remove($handle);
477 my ($pkg, $loop_count, $timeout) = @_; # event_loop(1) to process events once
478 my ($conn, $r, $w, $e, $rset, $wset, $eset);
481 # Quit the loop if no handles left to process
482 last unless ($rd_handles->count() || $wt_handles->count());
484 ($rset, $wset) = IO::Select->select($rd_handles, $wt_handles, $er_handles, $timeout);
486 foreach $e (@$eset) {
487 &{$er_callbacks{$e}}($e) if exists $er_callbacks{$e};
489 foreach $r (@$rset) {
490 &{$rd_callbacks{$r}}($r) if exists $rd_callbacks{$r};
492 foreach $w (@$wset) {
493 &{$wt_callbacks{$w}}($w) if exists $wt_callbacks{$w};
498 if (defined($loop_count)) {
499 last unless --$loop_count;
506 my ($pkg, $interval) = @_;
508 while (time - $now < $interval) {
509 $pkg->event_loop(10, 0.01);
516 my $call = $conn->{call} || 'unallocated';
517 dbg('connll', "Connection $call being destroyed ($noconns)");