PowerShell Read Form Pipe - not blocking one or even both sides? -
the powershell-script start namedpipe server:
$enc = [system.text.encoding]::ascii $buf = new-object byte[] 4096 $basename = "testpipe" $n = 0; { $n++ $pipename = "$basename$n" $delpipe = [system.io.directory]::getfiles("\\.\pipe\")|where{$_ -match $pipename} } while ( $delpipe.length -gt 5 ) $pipedir = [system.io.pipes.pipedirection]::inout $pipemsg = [system.io.pipes.pipetransmissionmode]::message $pipeopti = [system.io.pipes.pipeoptions]::asynchronous $pipe = new-object system.io.pipes.namedpipeserverstream( $pipename, $pipedir, 1, $pipemsg, $pipeopti ) if ( !$pipe.isconnected ) { $pipe.waitforconnection() } echo "connected pipe: $pipename" $pw = new-object system.io.streamwriter $pipe $pw.autoflush = $true $pr = new-object system.io.streamreader $pipe
the pipeclient sends message:
writeline(pipehandel,"hi message"); fileflush(pipehandel);
now want read pipe , have found 2 ways 1) client blocked 2) server blocked?
1) makes pipeclient hang pipeserver doesn't :(
function readmsg ($r, $e, $b) { try { $ntry=2 $got = "" $loops = 0 { # read data available $foundmore = $false { try { $read = $r.readasync($b, 0, $b.length) if($read -gt 0) { $foundmore = $true $got += ($e.getstring($b, 0, $read)) $ntry = -1 } } catch { $foundmore = $false; $read = 0; } } while($read -gt 0) if ($ntry -gt 0) {start-sleep -m 5; $ntry--;} } while($foundmore -or $ntry -gt 0) } { return $got } } readmsg pr $enc $buf
2) let client send message server waits until client closes pipe in mean time server blocked??
function rdpipe ($r, $e, $b) { return $r.readtoend() # readline() } rdpipe pr $enc $buf
is there way both sides send , receives asynchronous messages? help, gooly
$read
above task<int>
, not int
. if want use async following if
should changed to:
if ($read.result -gt 0) { ... }
Comments
Post a Comment