| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # | ||
| 4 | # Author: Slaven Rezic | ||
| 5 | # | ||
| 6 | # Copyright (C) 2017,2018 Slaven Rezic. All rights reserved. | ||
| 7 | # This package is free software; you can redistribute it and/or | ||
| 8 | # modify it under the same terms as Perl itself. | ||
| 9 | # | ||
| 10 | # Mail: slaven@rezic.de | ||
| 11 | # WWW: http://www.rezic.de/eserte/ | ||
| 12 | # | ||
| 13 | |||
| 14 | package Doit::WinRPC; | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | our $VERSION = '0.011'; | ||
| 19 | |||
| 20 | our @ISA = ('Doit::RPC'); | ||
| 21 | |||
| 22 | use constant STATE_LENGTH => 1; | ||
| 23 | use constant STATE_BODY => 2; | ||
| 24 | use constant STATE_CLOSED => 3; | ||
| 25 | |||
| 26 | sub debug { | ||
| 27 | my $self = shift; | ||
| 28 | if ($self->{debug}) { | ||
| 29 | Doit::Log::info($self->{label} . ': ' . $_[0]); | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | sub start_receive_data { | ||
| 34 | my($self) = @_; | ||
| 35 | $self->{_buffer} = ''; | ||
| 36 | $self->{_state} = STATE_LENGTH; | ||
| 37 | $self->{_curr_length} = undef; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub receive_data { | ||
| 41 | my($self) = @_; | ||
| 42 | my $pipe = $self->{pipe}; | ||
| 43 | |||
| 44 | while() { | ||
| 45 | $self->debug(" waiting for line from pipe"); | ||
| 46 | my $received_data = $pipe->Read; | ||
| 47 | if (!defined $received_data) { | ||
| 48 | $self->debug(" disconnect and close"); | ||
| 49 | $pipe->Disconnect; | ||
| 50 | $pipe->Close; | ||
| 51 | $self->{_state} = STATE_CLOSED; | ||
| 52 | return; | ||
| 53 | } | ||
| 54 | |||
| 55 | $self->{_buffer} .= $received_data; | ||
| 56 | BUFFER_LOOP: while () { | ||
| 57 | if ($self->{_state} == STATE_LENGTH) { | ||
| 58 | if (length($self->{_buffer}) >= 4) { | ||
| 59 | $self->{_curr_length} = unpack("N", substr($self->{_buffer}, 0, 4)); | ||
| 60 | $self->{_buffer} = substr($self->{_buffer}, 4); | ||
| 61 | $self->{_state} = STATE_BODY; | ||
| 62 | } else { | ||
| 63 | last BUFFER_LOOP; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | if ($self->{_state} == STATE_BODY) { | ||
| 67 | if (length($self->{_buffer}) >= $self->{_curr_length}) { | ||
| 68 | my $return_buffer = substr($self->{_buffer}, 0, $self->{_curr_length}); | ||
| 69 | $self->{_buffer} = substr($self->{_buffer}, $self->{_curr_length}); | ||
| 70 | $self->{_state} = STATE_LENGTH; | ||
| 71 | return $return_buffer; | ||
| 72 | } else { | ||
| 73 | last BUFFER_LOOP; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | { | ||
| 81 | package Doit::WinRPC::Server; | ||
| 82 | |||
| 83 | our @ISA = ('Doit::WinRPC'); | ||
| 84 | |||
| 85 | sub new { | ||
| 86 | my($class, $runner, $pipename, %options) = @_; | ||
| 87 | |||
| 88 | my $debug = delete $options{debug}; | ||
| 89 | die "Unhandled options: " . join(" ", %options) if %options; | ||
| 90 | |||
| 91 | bless { | ||
| 92 | runner => $runner, | ||
| 93 | pipename => $pipename, | ||
| 94 | debug => $debug, | ||
| 95 | label => 'WORKER', | ||
| 96 | }, $class; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub run { | ||
| 100 | my($self) = @_; | ||
| 101 | |||
| 102 | require Win32::Pipe; | ||
| 103 | |||
| 104 | $self->debug("Start worker ($$)..."); | ||
| 105 | my $pipename = $self->{pipename}; | ||
| 106 | my $pipe = Win32::Pipe->new($pipename) | ||
| 107 | or die "WORKER: Can't create named pipe '$pipename'"; | ||
| 108 | $self->{pipe} = $pipe; | ||
| 109 | $self->debug("named pipe was created"); | ||
| 110 | |||
| 111 | $self->debug("waiting for client"); | ||
| 112 | if (!$pipe->Connect) { | ||
| 113 | die "WORKER: error while Connect()"; | ||
| 114 | } | ||
| 115 | $self->debug("connected"); | ||
| 116 | |||
| 117 | $self->start_receive_data; | ||
| 118 | while () { | ||
| 119 | my $buffer = $self->receive_data; | ||
| 120 | my($context, @data); | ||
| 121 | if (defined $buffer) { | ||
| 122 | ($context, @data) = @{ Storable::thaw($buffer) }; | ||
| 123 | } | ||
| 124 | if (!defined $buffer || $data[0] =~ m{^exit$}) { | ||
| 125 | if (!defined $buffer) { | ||
| 126 | $self->debug(" got closed connection"); | ||
| 127 | } else { | ||
| 128 | $self->debug(" got exit command"); | ||
| 129 | $self->send_data('r', 'bye-bye'); | ||
| 130 | } | ||
| 131 | $pipe->Disconnect; | ||
| 132 | $pipe->Close; | ||
| 133 | return; | ||
| 134 | } | ||
| 135 | $self->debug(" calling method $data[0]"); | ||
| 136 | my($rettype, @ret) = $self->runner->call_wrapped_method($context, @data); | ||
| 137 | $self->debug(" sending result back"); | ||
| 138 | $self->send_data($rettype, @ret); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | sub send_data { | ||
| 143 | my($self, @cmd) = @_; | ||
| 144 | my $data = Storable::nfreeze(\@cmd); | ||
| 145 | my $pipe = $self->{pipe}; | ||
| 146 | $pipe->Write(pack("N", length($data))); | ||
| 147 | $pipe->Write($data); # XXX check result? | ||
| 148 | } | ||
| 149 | |||
| 150 | } | ||
| 151 | |||
| 152 | { | ||
| 153 | package Doit::WinRPC::Comm; | ||
| 154 | |||
| 155 | our @ISA = ('Doit::WinRPC'); | ||
| 156 | |||
| 157 | sub new { | ||
| 158 | my($class, $pipename, %options) = @_; | ||
| 159 | |||
| 160 | die "Please specify pipe name" if !defined $pipename; | ||
| 161 | my $debug = delete $options{debug}; | ||
| 162 | die "Unhandled options: " . join(" ", %options) if %options; | ||
| 163 | |||
| 164 | bless { | ||
| 165 | pipename => $pipename, | ||
| 166 | debug => $debug, | ||
| 167 | label => 'COMM', | ||
| 168 | }, $class; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub run { | ||
| 172 | my($self) = @_; | ||
| 173 | my $pipename = $self->{pipename}; | ||
| 174 | |||
| 175 | my $full_client_pipename = "\\\\.\\pipe\\$pipename"; | ||
| 176 | |||
| 177 | my $infh = \*STDIN; | ||
| 178 | my $outfh = \*STDOUT; | ||
| 179 | binmode $infh; | ||
| 180 | binmode $outfh; | ||
| 181 | |||
| 182 | require Win32::Pipe; | ||
| 183 | |||
| 184 | $self->debug("Start communication process (pid $$)..."); | ||
| 185 | |||
| 186 | my $tries = 20; | ||
| 187 | my $pipe_err; | ||
| 188 | my $pipe = Doit::RPC::gentle_retry( | ||
| 189 | code => sub { | ||
| 190 | my(%opts) = @_; | ||
| 191 | my $pipe = Win32::Pipe->new($full_client_pipename); | ||
| 192 | return $pipe if $pipe; | ||
| 193 | ${$opts{fail_info_ref}} = "(peer=$full_client_pipename)"; | ||
| 194 | undef; | ||
| 195 | }, | ||
| 196 | retry_msg_code => sub { | ||
| 197 | my($seconds) = @_; | ||
| 198 | $self->debug("can't connect, sleep for $seconds seconds"); | ||
| 199 | }, | ||
| 200 | fail_info_ref => \$pipe_err, | ||
| 201 | ); | ||
| 202 | if (!$pipe) { | ||
| 203 | die "COMM: Can't connect to named pipe (after $tries retries) $pipe_err"; | ||
| 204 | } | ||
| 205 | $self->debug("named pipe to worker was created"); | ||
| 206 | $self->{pipe} = $pipe; | ||
| 207 | |||
| 208 | my $get_and_send_to_pipe = sub ($$$) { | ||
| 209 | my($infh, $inname, $outname) = @_; | ||
| 210 | |||
| 211 | my $length_buf; | ||
| 212 | read $infh, $length_buf, 4 or die "COMM: reading data from $inname failed (getting length): $!"; | ||
| 213 | my $length = unpack("N", $length_buf); | ||
| 214 | $self->debug("starting getting data from $inname, length is $length"); | ||
| 215 | my $buf = ''; | ||
| 216 | while (1) { | ||
| 217 | my $got = read($infh, $buf, $length, length($buf)); | ||
| 218 | last if $got == $length; | ||
| 219 | die "COMM: Unexpected error $got > $length" if $got > $length; | ||
| 220 | $length -= $got; | ||
| 221 | } | ||
| 222 | $self->debug("finished reading data from $inname"); | ||
| 223 | |||
| 224 | $pipe->Write($length_buf) | ||
| 225 | or die "Error writing to pipe (length data)"; | ||
| 226 | $pipe->Write($buf) | ||
| 227 | or die "Error writing to pipe (body data)"; | ||
| 228 | $self->debug("finished sending data to $outname"); | ||
| 229 | }; | ||
| 230 | |||
| 231 | my $get_from_pipe_and_send = sub ($$$) { | ||
| 232 | my($outfh, $inname, $outname) = @_; | ||
| 233 | |||
| 234 | my $buf = $self->receive_data; | ||
| 235 | die "COMM: Unexpected error (lost connection?)" if !defined $buf; | ||
| 236 | $self->debug("finished reading data from $inname"); | ||
| 237 | |||
| 238 | print $outfh pack("N", length($buf)); | ||
| 239 | print $outfh $buf; | ||
| 240 | $self->debug("finished sending data to $outname"); | ||
| 241 | }; | ||
| 242 | |||
| 243 | $outfh->autoflush(1); | ||
| 244 | $self->debug("about to enter loop"); | ||
| 245 | $self->start_receive_data; | ||
| 246 | while () { | ||
| 247 | $self->debug("seen eof from local"), last if eof($infh); | ||
| 248 | $get_and_send_to_pipe->($infh, "local", "worker"); | ||
| 249 | $get_from_pipe_and_send->($outfh, "worker", "local"); | ||
| 250 | } | ||
| 251 | $self->debug("exited loop"); | ||
| 252 | } | ||
| 253 | |||
| 254 | } | ||
| 255 | |||
| 256 | 1; | ||
| 257 | |||
| 258 | __END__ |