lib/fastdfs-client/socket.rb in fastdfs-client-1.0.0 vs lib/fastdfs-client/socket.rb in fastdfs-client-1.0.1
- old
+ new
@@ -29,23 +29,23 @@
@socket.close if connected
end
def connection
if @socket.nil? || !connected
- Timeout.timeout(@connection_timeout) do
- @socket = TCPSocket.new(@host, @port)
+ @socket = Timeout.timeout(@connection_timeout) do
+ TCPSocket.new(@host, @port)
end
end
end
def connected
!@socket.closed?
end
def receive(&block)
- timeout_recv do
- @header = @socket.recv(@header_len).unpack("C*")
+ @header = timeout_recv do
+ @socket.recv(@header_len).unpack("C*")
end
res_header = parseHeader
if res_header[:status]
recv_body if is_recv?
res = yield(@content) if block_given?
@@ -54,15 +54,15 @@
res_header
end
private
def parseHeader
- err_msg = ""
- err_msg = "recv package size #{@header} is not equal #{@header_len}, cmd: #{@cmd}" unless @header.length == @header_len
- err_msg = "recv cmd: #{@header[8]} is not correct, expect cmd: #{CMD::RESP_CODE}, cmd: #{@cmd}" unless @header[8] == CMD::RESP_CODE
- err_msg = "recv erron #{@header[9]}, 0 is correct cmd: #{@cmd}" unless @header[9] == 0
- {status: err_msg.blank?, err_msg: err_msg}
+ err_msg = nil
+ err_msg = "recv package size #{@header} is not equal #{@header_len}, cmd: #{@cmd}" unless @header.length == @header_len || err_msg
+ err_msg = "recv cmd: #{@header[8]} is not correct, expect cmd: #{CMD::RESP_CODE}, cmd: #{@cmd}" unless @header[8] == CMD::RESP_CODE || err_msg
+ err_msg = "recv erron #{@header[9]}, 0 is correct cmd: #{@cmd}" unless @header[9] == 0 || err_msg
+ {status: err_msg.nil?, err_msg: err_msg}
end
def timeout_recv
Timeout.timeout(@recv_timeout) do
yield if block_given?
@@ -86,10 +86,10 @@
len = [body_len, max_len].min
@content << @socket.recv(len)
body_len -= len
end
end
- @content = nil if @content.blank?
+ @content = nil if Utils.is_blank? @content
end
end
end
end
\ No newline at end of file