lib/bertrpc/action.rb in bertrpc-0.4.1 vs lib/bertrpc/action.rb in bertrpc-0.4.2
- old
+ new
@@ -42,11 +42,11 @@
raise ProtocolError.new(ProtocolError::NO_DATA) unless bert_response
sock.close
bert_response
rescue Errno::ECONNREFUSED
raise ConnectionError.new("Unable to connect to #{@svc.host}:#{@svc.port}")
- rescue Timeout::Error
+ rescue Errno::EAGAIN
raise ReadTimeoutError.new(@svc.host, @svc.port, @svc.timeout)
end
# Creates a socket object which does speedy, non-blocking reads
# and can perform reliable read timeouts.
@@ -55,11 +55,20 @@
#
# +host+ String address of the target TCP server
# +port+ Integer port of the target TCP server
# +timeout+ Optional Integer (in seconds) of the read timeout
def connect_to(host, port, timeout = nil)
- io = BufferedIO.new(TCPSocket.new(host, port))
- io.read_timeout = timeout
- io
+ sock = TCPSocket.new(host, port)
+ sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1
+
+ if timeout
+ secs = Integer(timeout)
+ usecs = Integer((timeout - secs) * 1_000_000)
+ optval = [secs, usecs].pack("l_2")
+ sock.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval
+ sock.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval
+ end
+
+ sock
end
end
end