lib/em-synchrony/tcpsocket.rb in em-synchrony-1.0.3 vs lib/em-synchrony/tcpsocket.rb in em-synchrony-1.0.4

- old
+ new

@@ -83,12 +83,18 @@ end def read(num_bytes = nil, dest = nil) handle_read(:read, num_bytes, dest) end - alias_method :read_nonblock, :read + def read_nonblock(maxlen, dest = nil) + raise ArgumentError, "maxlen must be > 0" if !maxlen || maxlen <= 0 + read_bytes = handle_read(:read_nonblock, maxlen, dest) + raise EOFError if read_bytes.nil? + read_bytes + end + def recv(num_bytes, flags = 0) raise "Unknown flags in recv(): #{flags}" if flags.nonzero? handle_read(:recv, num_bytes) end @@ -157,14 +163,17 @@ (data = read_data) != :block ? data : sync(:in) end def try_read_data - if @read_type == :read + if @read_type == :read || @read_type == :read_nonblock + nonblocking = @read_type == :read_nonblock unless @remote_closed if @read_bytes # read(n) on an open socket, with >= than n buffered data, returns n data - if @in_buff.size >= @read_bytes then @in_buff.slice!(0, @read_bytes) + if (@in_buff.size >= @read_bytes || + (nonblocking && @in_buff.size > 0)) then + @in_buff.slice!(0, @read_bytes) # read(n) on an open socket, with < than n buffered data, blocks else :block end else # read() on an open socket, blocks until a remote close and returns all the data sent :block