<div dir="ltr"><span style="font-family:arial,sans-serif;font-size:13px">Levente,</span><br><div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">I gave it a try with the AsyncFile method you outlined, and I hit 2 problems:</span></div>

<div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">1. The named pipe is created in Message mode, but repeated calls to writeBuffer:atFilePosition:onCompletionDo: seem to concatenate all the messages together - I couldn&#39;t see the equivalent of &#39;flush&#39; for FileStream which separates out the messages at the pipe server end.</span></div>

<div><span style="font-family:arial,sans-serif;font-size:13px">2. I couldn&#39;t find the equivalent of &#39;size&#39; from FileStream to find out how much data was available in the next message when reading - </span><span style="font-family:arial,sans-serif;font-size:13px">readByteCount:fromFilePosition:onCompletionDo: seems to want me to pass this value in. Given that the messages vary in size, this is a little awkward (although I guess I could make it send a fixed 256-byte message each time, that is bound to bite me later (640kb is enough for anyone, etc.)).</span></div>

<div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">Anyway, if you have any other thoughts on using AsyncFile, or an alternative, I&#39;d be most grateful.</span></div>

<div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><font face="arial, sans-serif">Many thanks,</font></div><div><font face="arial, sans-serif"><br></font></div><div><font face="arial, sans-serif">David.</font></div>

</div><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=4736412&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 class="im">

        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 class="im"><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 class="im">&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 class="im">&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 class="im">&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 class="h5">&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 class="im">&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 class="im">_______________________________________________
<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 class="im">
                <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 class="HOEnZb"><div class="h5">
        <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>


        
        
        
<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-tp4735456p4736412.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/>