lib/oxblood/rsocket.rb in oxblood-0.1.0.dev9 vs lib/oxblood/rsocket.rb in oxblood-0.1.0.dev10
- old
+ new
@@ -6,13 +6,14 @@
# automatically recreated in case of any errors (including timeout errors)
# in order to avoid inconsistent state.
class RSocket
TimeoutError = Class.new(RuntimeError)
- # JRuby don't properly support SO_LINGER setting
+ # JRuby before 9.1.6.0 don't properly support SO_LINGER setting
# @see https://github.com/jruby/jruby/issues/4040
- LINGER_OPTION = if RUBY_ENGINE == 'jruby'
+ LINGER_OPTION = if RUBY_ENGINE == 'jruby' &&
+ Gem::Version.new(JRUBY_VERSION) < Gem::Version.new('9.1.6.0')
[Socket::SOL_SOCKET, :LINGER, 0].freeze
else
Socket::Option.linger(true, 0)
end
private_constant :LINGER_OPTION
@@ -40,42 +41,42 @@
end
# Read number of bytes
# @param [Integer] nbytes number of bytes to read
# @return [String] read result
- def read(nbytes)
+ def read(nbytes, timeout = @timeout)
result = @buffer.slice!(0, nbytes)
while result.bytesize < nbytes
- result << readpartial(nbytes - result.bytesize)
+ result << readpartial(nbytes - result.bytesize, timeout)
end
result
end
# Read until separator
# @param [String] separator separator
# @return [String] read result
- def gets(separator)
+ def gets(separator, timeout = @timeout)
while (crlf = @buffer.index(separator)).nil?
- @buffer << readpartial(1024)
+ @buffer << readpartial(1024, timeout)
end
@buffer.slice!(0, crlf + separator.bytesize)
end
# Write data to socket
# @param [String] data given
# @return [Integer] the number of bytes written
- def write(data)
+ def write(data, timeout = @timeout)
full_size = data.bytesize
while data.bytesize > 0
written = socket.write_nonblock(data, exception: false)
if written == :wait_writable
- socket.wait_writable(@timeout) or fail_with_timeout!
+ socket.wait_writable(timeout) or fail_with_timeout!
else
data = data.byteslice(written..-1)
end
end
@@ -117,15 +118,15 @@
sock.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
end
end
end
- def readpartial(nbytes)
+ def readpartial(nbytes, timeout)
case data = socket.read_nonblock(nbytes, exception: false)
when String
return data
when :wait_readable
- socket.wait_readable(@timeout) or fail_with_timeout!
+ socket.wait_readable(timeout) or fail_with_timeout!
when nil
close
raise Errno::ECONNRESET
end while true
end