<div dir="ltr">And here&#39;s another one I&#39;m banging my head on. If I open 2 named pipes like so:<div><br></div><span style="white-space:pre">        </span><span style="white-space:pre">pipeIn := FileStream fileNamed: &#39;\\.\pipe\in&#39;.
</span><div><span style="white-space:pre">        </span><span style="white-space:pre">pipeOut := FileStream fileNamed: &#39;\\.\pipe\out&#39;.
</span><div><span style="white-space:pre"><br></span></div></div><div><span style="white-space:pre">And test them like this:</span></div><div><ol><li>Write &#39;Here&#39;s Johnny!!!!!&#39; to pipeOut</li><li>Wait for the md5 ack from the pipe server and verify</li>

<li>Wait for a message from the pipe server on pipeIn</li><li>Computer md5 ack and send it back</li></ol>all works fine (assuming the pipe server at the other end has a mirror image of this flow, which it does).</div><div>

<br></div><div>However, if I do this (simulating having several messages waiting in the &#39;out&#39; pipe):</div><div><ol><li>Write &#39;Here&#39;s Johnny!!!!!&#39; to pipeOut (call this message 1)</li><li>Write &#39;Another message&#39; to pipeOut (call this message 2)</li>

<li>Wait for the md5 ack for message 1 and verify (pipe server only reads message 1)</li><li>Wait for a message from the pipe server on pipeIn (pipe server initiates this once it has sent the ack above)</li><li>Computer md5 and send it back via pipeIn</li>

</ol></div><div>I get a &#39;write failed&#39; at step 5, trying to send the md5 back on pipeIn. This seems strange, since the only difference I can see is the extra write to pipeOut at step 2 - nothing to do with pipeIn.</div>

<div><br></div><div>What&#39;s even more frustrating is that by step 3, I can see on the pipe server (via PeekNamedPipe) that there are another 15 bytes available (message 2), and if I actually read them here before sending the ack back to the Squeak app, then step 5 doesn&#39;t fail. Gah... Can a nextPutAll: on one StandardFileStream fail because a write on another hasn&#39;t been read? The full code looks like this:</div>

<div><br></div><div><div><span class="" style="white-space:pre">        </span>pipeIn := FileStream fileNamed: &#39;\\.\pipe\in&#39;.</div><div><span class="" style="white-space:pre">        </span>pipeOut := FileStream fileNamed: &#39;\\.\pipe\out&#39;.</div>

<div><br></div><div><span class="" style="white-space:pre">        </span>msg := &#39;Here&#39;&#39;s Johnny!!!!&#39;.</div><div><span class="" style="white-space:pre">        </span>msgHash := ((CMD5Hasher hashMessage: msg) printStringBase: 16 length: 32 padded: true) asLowercase.</div>

<div><span class="" style="white-space:pre">        </span>pipeOut nextPutAll: msg; flush.</div><div><br></div><span class="" style="white-space:pre">        </span>&quot;Queue another message - this will cause the later call to pipeIn nextPutAll: to fail, unless the pipe server has already read this message...&quot; <div>

<span class="" style="white-space:pre">        </span>msg := &#39;Another message&#39;.<br></div><div><span class="" style="white-space:pre">        </span>pipeOut nextPutAll: msg; flush.</div><div><br></div><div><span class="" style="white-space:pre">        </span>start := DateAndTime millisecondClockValue.</div>

<div><span class="" style="white-space:pre">        </span>[(pipeOut size &lt; 32) &amp; ((DateAndTime millisecondClockValue) - start &lt; 3000)] whileTrue: [</div><div><span class="" style="white-space:pre">                </span>(Delay forMilliseconds: 200) wait.</div>

<div><span class="" style="white-space:pre">        </span>].</div><div><span class="" style="white-space:pre">        </span>pipeOut size = 32 ifTrue: [</div><div><span class="" style="white-space:pre">                </span>ack := pipeOut next: 32.</div>

<div><span class="" style="white-space:pre">        </span>] ifFalse: [</div><div><span class="" style="white-space:pre">                </span>ack := &#39;timeout&#39;.</div><div><span class="" style="white-space:pre">        </span>].<span class="" style="white-space:pre">                </span></div>

<div><span class="" style="white-space:pre">        </span>ack = msgHash ifFalse: [</div><div><span class="" style="white-space:pre">                </span>^ false.</div><div><span class="" style="white-space:pre">        </span>].</div><div><br></div>

<div><span class="" style="white-space:pre">        </span>&quot;Wait up to 3 seconds for the return connection check from pipe server&quot;</div><div><span class="" style="white-space:pre">        </span>start := DateAndTime millisecondClockValue.</div>

<div><span class="" style="white-space:pre">        </span>[(pipeIn size = 0) &amp; ((DateAndTime millisecondClockValue) - start &lt; 3000)] whileTrue: [</div><div><span class="" style="white-space:pre">                </span>(Delay forMilliseconds: 200) wait.</div>

<div><span class="" style="white-space:pre">        </span>].</div><div><span class="" style="white-space:pre">        </span>msg := pipeIn next: (pipeIn size).</div><div><span class="" style="white-space:pre">        </span>msgHash := ((CMD5Hasher hashMessage: msg) printStringBase: 16 length: 32 padded: true) asLowercase.</div>

<div><span class="" style="white-space:pre">        </span>&quot;The next line will fail unless the pipe server has read our earlier message on pipeOut&quot;</div><div><span class="" style="white-space:pre">        </span>pipeIn nextPutAll: msgHash; flush.</div>

<div><span class="" style="white-space:pre">        </span></div><div><span class="" style="white-space:pre">        </span>^ true.</div></div><div><br></div><div>BTW - the reason I&#39;m using 2 pipes is that both ends (the squeak app and the <a href="http://vb.net" target="_top" rel="nofollow" link="external">vb.net</a> app) could generate events / messages at any time, and so I need both ends to be &#39;waiting&#39; for a message from the other. This seemed the simplest way to achieve this and be able to send an ack without the risk of the ack getting mangled up with new messages. However, it seems like I might have been wrong...</div>

</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Jan 10, 2014 at 4:25 PM, David Lomas <span dir="ltr">&lt;<a href="/user/SendEmail.jtp?type=node&node=4735778&i=0" target="_top" rel="nofollow" link="external">[hidden email]</a>&gt;</span> wrote:<br>

<blockquote style='border-left:2px solid #CCCCCC;padding:0 1em' class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Thanks - I&#39;m persevering with the busy wait for now, but may look at async. I also thought it looked much trickier, but your example code is great so thanks for that.<div>

<br></div><div>I did originally try using sockets, but it triggers all kinds of security warnings on a PC when a socket server is set up. I didn&#39;t want to worry end users, or required them to put exceptions in their firewalls, etc., when the communication is entirely local. I have also tried using plain files (which works), but watching for changes on the filesystem is ugly and prone to missing things.</div>


<div><br></div><div>Ideally, I&#39;d like to use messages (e.g. via WndProc on Win32). In fact, I&#39;ve been able to <i>send</i> windows messages from Squeak to a <a href="http://vb.net" target="_blank" rel="nofollow" link="external">vb.net</a> app by calling RegisterWindowMessage / SendMessage in user32.dll, but I couldn&#39;t find any way of receiving messages using this system, and I need bi-directional. I presume the VM would need to capture and pass those on to the Squeak image, which it doesn&#39;t currently do. If there is some kind of &#39;user&#39; event channel I could use, please do suggest something...</div>


<div><br></div><div>Hence pipes was the least worst option so far.</div><div><br></div><div>Thanks again,</div><div><br></div><div>Dave</div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">

On Fri, Jan 10, 2014 at 3:37 PM, Levente Uzonyi-2 [via Smalltalk] <span dir="ltr">&lt;<a href="/user/SendEmail.jtp?type=node&node=4735778&i=1" target="_top" rel="nofollow" link="external">[hidden email]</a>&gt;</span> wrote:<br>


<blockquote style='border-left:2px solid #CCCCCC;padding:0 1em' class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>

        On Fri, 10 Jan 2014, dsl101 wrote:
<br><br>&gt; Hi Ron,
<br>&gt; Thanks for that - but I can only see #dataAvailable for Sockets, not for FileStream (named pipes). I think the same kind of thing is available for pipes (you can do `pipe size` to see how much data is there),
<br>&gt; but it still doesn&#39;t wait. I&#39;m trying to avoid a busy loop waiting for the data - like this:
<br><br></div>FileStreams don&#39;t have semaphores, so you can only use busy waiting with 
<br>them. However there&#39;s AsyncFile, which can do what you want. But its 
<br>interface is a bit cumbersome, and it&#39;s hardly tested/used at all. Here&#39;s 
<br>how it could work:
<br><br>| syncSemaphore file message |
<br>syncSemaphore := Semaphore new.
<br>file := AsyncFile new.
<br>file open: &#39;your_pipe&#39; forWrite: true.
<br>message := &#39;Here&#39;&#39;s Johnny!!!!&#39;.
<br>file writeBuffer: message atFilePosition: 0 onCompletionDo: [
<br>         syncSemaphore signal ].
<br>(syncSemaphore waitTimeoutMSecs: 3000) ifTrue: [
<br>         &quot;handle timeout&quot; ].
<br>file readByteCount: 32 fromFilePosition: 0 onCompletionDo: [ :response |
<br>         message := response.
<br>         syncSemaphore signal ].
<br>(syncSemaphore waitTimeoutMSecs: 3000) ifTrue: [
<br>         &quot;handle timeout&quot; ].
<br>Transcript show: &#39;Received: &#39;, message; cr.
<br>file close.
<br><br>Using syncSemaphore is a must, because the callbacks are evaluated from 
<br>another process.
<br><br><br>Levente
<br><br>P.S.: If you want to communicate with another program from Squeak, then 
<br>you should use Sockets if possible, since those are versatile and well 
<br>tested.
<br><div><div><div class='shrinkable-quote'><br>&gt; 
<br>&gt; start := DateAndTime millisecondClockValue.
<br>&gt; (pipe size &lt; 32) &amp; (DateAndTime millisecondClockValue - start &lt; 3000) ifTrue: [
<br>&gt; (Delay forMilliseconds 50) wait.
<br>&gt; ]
<br>&gt; pipe size = 32 ifTrue: [
<br>&gt; &quot;Get data&quot;
<br>&gt; ] ifFalse: [
<br>&gt; &quot;Deal with timeout&quot;
<br>&gt; ]
<br>&gt; 
<br>&gt; The shorter the &#39;wait&#39;, the more responsive the code is to data arriving on the pipe, but the more CPU it will use as it spins round the loop. The longer the &#39;wait&#39;, the more lag it has for data coming back.
<br>&gt; That&#39;s what I&#39;m trying to avoid by blocking on the read, but with a way to escape after some timeout.
<br>&gt; 
<br>&gt; I&#39;m guessing the call to &#39;pipe next:&#39; is a primitive, and blocks there, which is why valueWithin:onTimeout: doesn&#39;t return after the timeout, but does eventually return the correct answer. So, I&#39;m guessing
<br>&gt; I&#39;ll have to do something like this:
</div></div>&gt;  *  Set up a semaphore
<br>&gt;  *  Fork the blocking read process, which will signal the semaphore if it ever returns its 32 bytes
<br>&gt;  *  In the main thread, wait for up to 3 seconds for the semaphore to be signalled
<br>&gt;  *  If the semaphore times out, kill the forked process
<br><div>&gt; Obviously there&#39;s a potential race at the end there, but the worst case is we throw away data which was returned at the last moment. Is there anything else you can see wrong with this approach?
<br>&gt; 
<br>&gt; Thanks,
<br>&gt; 
<br>&gt; Dave
<br>&gt; 
<br>&gt; 
<br></div><div>&gt; On Thu, Jan 9, 2014 at 9:16 PM, Ron Teitelbaum [via Smalltalk] &lt;[hidden email]&gt; wrote:
<br>&gt;       Hi Dave,
<br>&gt;
<br></div>&gt;       See #dataAvailable ??and #recieveAvailableData.
<br>&gt;
<br>&gt;       It&#39;s never good to call for data if you don&#39;t know you have any. ??Better to
<br>&gt;       setup a wait for data until call instead. ??
<br><div>&gt;
<div class='shrinkable-quote'><br>&gt;       All the best,
<br>&gt;
<br>&gt;       Ron Teitelbaum
<br>&gt;       Head Of Engineering
<br>&gt;       3d Immersive Collaboration Consulting
<br>&gt;       [hidden email]
<br>&gt;       Follow Me On Twitter: @RonTeitelbaum
<br>&gt;       <a href="http://www.3dicc.com" target="_blank" rel="nofollow" link="external">www.3dicc.com</a>
<br>&gt;       <a href="https://www.google.com/+3dicc" rel="nofollow" link="external" target="_blank">https://www.google.com/+3dicc</a><br>&gt; 
<br>&gt;
<br>&gt;       &gt; -----Original Message-----
</div></div><div><div>&gt;       &gt; From: [hidden email] [mailto:[hidden email]
<div class='shrinkable-quote'><br>&gt;       &gt; [hidden email]] On Behalf Of dsl101
<br>&gt;       &gt; Sent: Thursday, January 09, 2014 10:16 AM
<br>&gt;       &gt; To: [hidden email]
<br>&gt;       &gt; Subject: [Newbies] Read a filestream (named pipe) with a timeout
<br>&gt;       &gt;
<br>&gt;       &gt; I&#39;m using Squeak 4.2 and working on the smalltalk end of a named pipe
<br>&gt;       &gt; connection, which sends a message to the named pipe server with:
<br>&gt;       &gt;
<br>&gt;       &gt; msg := &#39;Here&#39;&#39;s Johnny!!!!&#39;.
<br>&gt;       &gt; pipe nextPutAll: msg; flush.
<br>&gt;       &gt;
<br>&gt;       &gt; It should then receive an acknowledgement, which will be a 32-byte md5
<br>&gt; hash of
<br>&gt; &gt; the received message (which the smalltalk app can then verify). It&#39;s
<br>&gt; possible the
<br>&gt; &gt; named pipe server may have gone away or otherwise been unable to deal with
<br>&gt; &gt; the request, and so I&#39;d like to set a timeout on reading the
<br>&gt; acknowledgement.
<br>&gt; &gt; I&#39;ve tried using this:
<br>&gt; &gt;
<br>&gt; &gt; ack := [ pipe next: 32 ] valueWithin: (Duration seconds: 3)
<br>&gt; onTimeout: [
<br>&gt; &gt; &#39;timeout&#39;. ].
<br>&gt; &gt;
<br>&gt; &gt; and then made the pipe server pause artificially to test the code. But the
<br>&gt; &gt; smalltalk thread blocks on the read and doesn&#39;t carry on (even after the
<br>&gt; &gt; timeout), although if I then get the pipe server to send the correct
<br>&gt; response
<br>&gt; &gt; (after a 5 second delay, for example), the value of &#39;ack&#39; is &#39;timeout&#39;.
<br>&gt; Obviously
<br>&gt; &gt; the timeout did what it&#39;s supposed to do, but couldn&#39;t &#39;unblock&#39; the
<br>&gt; blocking
<br>&gt; &gt; read on the pipe.
<br>&gt; &gt;
<br>&gt; &gt; Is there a way to accomplish this even with a blocking FileStream read?
<br>&gt; I&#39;d rather
<br>&gt; &gt; avoid a busy wait on there being 32 characters available if at all
<br>&gt; possible.
<br>&gt; &gt;
<br>&gt; &gt; Thanks,
<br>&gt; &gt;
<br>&gt; &gt; Dave
<br>&gt; &gt;
<br>&gt; &gt;
<br>&gt; &gt;
<br>&gt; &gt; --
<br>&gt; &gt; View this message in context:
<br>&gt; <a href="http://forum.world.st/Read-a-filestream-named-" rel="nofollow" link="external" target="_blank">http://forum.world.st/Read-a-filestream-named-</a><br>&gt; &gt; pipe-with-a-timeout-tp4735456.html
<br>&gt; &gt; Sent from the Squeak - Beginners mailing list archive at Nabble.com.
<br>&gt; &gt; _______________________________________________
<br>&gt; &gt; Beginners mailing list
<br>&gt; &gt; [hidden email]
<br>&gt; &gt; <a href="http://lists.squeakfoundation.org/mailman/listinfo/beginners" rel="nofollow" link="external" target="_blank">http://lists.squeakfoundation.org/mailman/listinfo/beginners</a><br>&gt; 
<br>&gt; 
<br>&gt; _______________________________________________
<br>&gt; Beginners mailing list
<br>&gt; [hidden email]
<br>&gt; <a href="http://lists.squeakfoundation.org/mailman/listinfo/beginners" rel="nofollow" link="external" target="_blank">http://lists.squeakfoundation.org/mailman/listinfo/beginners</a><br>&gt; 
<br>&gt; 
</div></div></div>&gt; ________________________________________________________________________________________________________________________________________________________________________________________________________________
<br><div>&gt; If you reply to this email, your message will be added to the discussion below:
<br>&gt; <a href="http://forum.world.st/Read-a-filestream-named-pipe-with-a-timeout-tp4735456p4735547.html" rel="nofollow" link="external" target="_blank">http://forum.world.st/Read-a-filestream-named-pipe-with-a-timeout-tp4735456p4735547.html</a><br>


&gt; To unsubscribe from Read a filestream (named pipe) with a timeout, click here.
<br>&gt; NAML
<br>&gt; 
<br>&gt; 
<br></div>&gt; ________________________________________________________________________________________________________________________________________________________________________________________________________________
<br>&gt; View this message in context: Re: Read a filestream (named pipe) with a timeout Sent from the Squeak - Beginners mailing list archive at Nabble.com.
<br>&gt;
</div><div>_______________________________________________
<br>Beginners mailing list
<br><a href="http://user/SendEmail.jtp?type=node&amp;node=4735726&amp;i=0" rel="nofollow" link="external" target="_blank">[hidden email]</a>
<br><a href="http://lists.squeakfoundation.org/mailman/listinfo/beginners" rel="nofollow" link="external" target="_blank">http://lists.squeakfoundation.org/mailman/listinfo/beginners</a><br>

        
        
        
        <br>
        <br>
        </div><hr noshade size="1" color="#cccccc">
        <div style="color:#444;font:12px tahoma,geneva,helvetica,arial,sans-serif"><div>
                <div style="font-weight:bold">If you reply to this email, your message will be added to the discussion below:</div>
                </div><a href="http://forum.world.st/Read-a-filestream-named-pipe-with-a-timeout-tp4735456p4735726.html" target="_blank" rel="nofollow" link="external">http://forum.world.st/Read-a-filestream-named-pipe-with-a-timeout-tp4735456p4735726.html</a>
        </div><div><div>
        <div style="color:#666;font:11px tahoma,geneva,helvetica,arial,sans-serif;margin-top:.4em;line-height:1.5em">
                
                To unsubscribe from Read a filestream (named pipe) with a timeout, <a href="" target="_blank" rel="nofollow" link="external">click here</a>.<br>



                <a href="http://forum.world.st/template/NamlServlet.jtp?macro=macro_viewer&amp;id=instant_html%21nabble%3Aemail.naml&amp;base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&amp;breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml" rel="nofollow" style="font:9px serif" target="_blank" link="external">NAML</a>
        </div></div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>


        
        
        
<br/><hr align="left" width="300" />
View this message in context: <a href="http://forum.world.st/Read-a-filestream-named-pipe-with-a-timeout-tp4735456p4735778.html">Re: Read a filestream (named pipe) with a timeout</a><br/>
Sent from the <a href="http://forum.world.st/Squeak-Beginners-f107673.html">Squeak - Beginners mailing list archive</a> at Nabble.com.<br/>