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 vars qw(%rd_callbacks %wt_callbacks $rd_handles $wt_handles);
22 $rd_handles = IO::Select->new();
23 $wt_handles = IO::Select->new();
24 my $blocking_supported = 0;
27 # Checks if blocking is supported
29 require POSIX; POSIX->import(qw (F_SETFL O_NONBLOCK EAGAIN));
31 $blocking_supported = 1 unless $@;
34 #-----------------------------------------------------------------
37 my ($pkg, $to_host, $to_port,$rcvd_notification_proc) = @_;
39 # Create a new internet socket
41 my $sock = IO::Socket::INET->new (
47 return undef unless $sock;
49 # Create a connection end-point object
52 rcvd_notification_proc => $rcvd_notification_proc,
55 if ($rcvd_notification_proc) {
56 my $callback = sub {_rcv($conn)};
57 set_event_handler ($sock, "read" => $callback);
59 return bless $conn, $pkg;
64 my $sock = delete $conn->{sock};
65 return unless defined($sock);
66 set_event_handler ($sock, "read" => undef, "write" => undef);
72 my ($conn, $msg) = @_;
73 _enqueue ($conn, $msg);
74 $conn->_send (1); # 1 ==> flush
78 my ($conn, $msg) = @_;
79 _enqueue($conn, $msg);
80 my $sock = $conn->{sock};
81 return unless defined($sock);
82 set_event_handler ($sock, "write" => sub {$conn->_send(0)});
86 my ($conn, $msg) = @_;
87 # prepend length (encoded as network long)
88 my $len = length($msg);
89 $msg =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
90 push (@{$conn->{queue}}, $msg . "\n");
94 my ($conn, $flush) = @_;
95 my $sock = $conn->{sock};
96 return unless defined($sock);
97 my ($rq) = $conn->{queue};
99 # If $flush is set, set the socket to blocking, and send all
100 # messages in the queue - return only if there's an error
101 # If $flush is 0 (deferred mode) make the socket non-blocking, and
102 # return to the event loop only after every message, or if it
103 # is likely to block in the middle of a message.
105 $flush ? $conn->set_blocking() : $conn->set_non_blocking();
106 my $offset = (exists $conn->{send_offset}) ? $conn->{send_offset} : 0;
110 my $mlth = length($msg);
111 my $bytes_to_write = $mlth - $offset;
112 my $bytes_written = 0;
113 confess("Negative Length! msg: '$msg' lth: $mlth offset: $offset") if $bytes_to_write < 0;
114 while ($bytes_to_write > 0) {
115 $bytes_written = syswrite ($sock, $msg,
116 $bytes_to_write, $offset);
117 if (!defined($bytes_written)) {
118 if (_err_will_block($!)) {
119 # Should happen only in deferred mode. Record how
120 # much we have already sent.
121 $conn->{send_offset} = $offset;
122 # Event handler should already be set, so we will
123 # be called back eventually, and will resume sending
126 delete $conn->{send_offset};
127 $conn->handle_send_err($!);
129 return 0; # fail. Message remains in queue ..
132 $offset += $bytes_written;
133 $bytes_to_write -= $bytes_written;
135 delete $conn->{send_offset};
138 last unless $flush; # Go back to select and wait
139 # for it to fire again.
141 # Call me back if queue has not been drained.
143 set_event_handler ($sock, "write" => sub {$conn->_send(0)});
145 set_event_handler ($sock, "write" => undef);
150 sub _err_will_block {
151 if ($blocking_supported) {
152 return ($_[0] == EAGAIN());
156 sub set_non_blocking { # $conn->set_blocking
157 if ($blocking_supported) {
158 # preserve other fcntl flags
159 my $flags = fcntl ($_[0], F_GETFL(), 0);
160 fcntl ($_[0], F_SETFL(), $flags | O_NONBLOCK());
164 if ($blocking_supported) {
165 my $flags = fcntl ($_[0], F_GETFL(), 0);
166 $flags &= ~O_NONBLOCK(); # Clear blocking, but preserve other flags
167 fcntl ($_[0], F_SETFL(), $flags);
171 sub handle_send_err {
172 # For more meaningful handling of send errors, subclass Msg and
174 my ($conn, $err_msg) = @_;
175 warn "Error while sending: $err_msg \n";
176 set_event_handler ($conn->{sock}, "write" => undef);
179 #-----------------------------------------------------------------
180 # Receive side routines
182 my ($g_login_proc,$g_pkg);
185 @_ == 4 || die "Msg->new_server (myhost, myport, login_proc)\n";
186 my ($pkg, $my_host, $my_port, $login_proc) = @_;
188 $main_socket = IO::Socket::INET->new (
189 LocalAddr => $my_host,
190 LocalPort => $my_port,
194 die "Could not create socket: $! \n" unless $main_socket;
195 set_event_handler ($main_socket, "read" => \&_new_client);
196 $g_login_proc = $login_proc; $g_pkg = $pkg;
199 sub _rcv { # Complement to _send
200 my $conn = shift; # $rcv_now complement of $flush
201 # Find out how much has already been received, if at all
202 my ($msg, $offset, $bytes_to_read, $bytes_read);
203 my $sock = $conn->{sock};
204 return unless defined($sock);
207 $conn->set_non_blocking();
208 $bytes_read = sysread ($sock, $msg, 1024, 0);
209 if (defined ($bytes_read)) {
210 if ($bytes_read > 0) {
212 @lines = split /\n/, $msg;
213 $lines[0] = $conn->{msg} . $lines[0] if $conn->{msg};
217 $conn->{msg} = pop @lines;
220 $conn->{msg} .= $msg;
224 if (_err_will_block($!)) {
232 if (defined $bytes_read && $bytes_read == 0) {
233 # $conn->disconnect();
234 &{$conn->{rcvd_notification_proc}}($conn, undef, $!);
240 $msg =~ s/\%([2-9A-F][0-9A-F])/chr(hex($1))/eg;
241 $msg =~ s/[\x00-\x08\x0a-\x1f\x9b\x8e]/./g; # immutable CSI sequence + control characters
242 &{$conn->{rcvd_notification_proc}}($conn, $msg, $!);
248 my $sock = $main_socket->accept();
251 'state' => 'connected'
253 my $rcvd_notification_proc =
254 &$g_login_proc ($conn, $sock->peerhost(), $sock->peerport());
255 if ($rcvd_notification_proc) {
256 $conn->{rcvd_notification_proc} = $rcvd_notification_proc;
257 my $callback = sub {_rcv($conn)};
258 set_event_handler ($sock, "read" => $callback);
259 } else { # Login failed
266 set_event_handler ($main_socket, "read" => undef);
271 #----------------------------------------------------
272 # Event loop routines used by both client and server
274 sub set_event_handler {
275 shift unless ref($_[0]); # shift if first arg is package name
276 my ($handle, %args) = @_;
278 if (exists $args{'write'}) {
279 $callback = $args{'write'};
281 $wt_callbacks{$handle} = $callback;
282 $wt_handles->add($handle);
284 delete $wt_callbacks{$handle};
285 $wt_handles->remove($handle);
288 if (exists $args{'read'}) {
289 $callback = $args{'read'};
291 $rd_callbacks{$handle} = $callback;
292 $rd_handles->add($handle);
294 delete $rd_callbacks{$handle};
295 $rd_handles->remove($handle);
301 my ($pkg, $loop_count, $timeout) = @_; # event_loop(1) to process events once
302 my ($conn, $r, $w, $rset, $wset);
304 # Quit the loop if no handles left to process
305 last unless ($rd_handles->count() || $wt_handles->count());
307 IO::Select->select ($rd_handles, $wt_handles, undef, $timeout);
308 foreach $r (@$rset) {
309 &{$rd_callbacks{$r}} ($r) if exists $rd_callbacks{$r};
311 foreach $w (@$wset) {
312 &{$wt_callbacks{$w}}($w) if exists $wt_callbacks{$w};
314 if (defined($loop_count)) {
315 last unless --$loop_count;