Frank Preiswerk wrote:
> Yes, I understand that socket.recv() reads UP TO size bytes, but doesn't
> necessarily wait.
> How would you buffer the data?

Here's an idea that's a little different from what you're asking for (it 
assumes the data stream is chunked using 4 byte length fields, rather 
than knowing the length in advance), but it shows a working buffer 
implementation. (See also 
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/177704)


module Messageable
   LEN_LEN = 4

   def recv_message
     if (data = recv(LEN_LEN))
       if data.empty?
         nil
       else
         len = Message::Length.new(data).value
# for extra safety:
#        if len > MAXLEN
#          raise MessageLengthError, "MAXLEN exceeded: #{len} > #{MAXLEN}"
#        end

         msg = ""
         part = nil
         while (delta = len - msg.length) > 0 and
               (part = recv(delta)) and
               not part.empty?
           yield part if block_given?
           msg << part
         end
         msg.empty? ? nil : msg
       end
     end
   end
end


-- 
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407