Class: Fastdfs::Client::Socket

Inherits:
Object
  • Object
show all
Defined in:
lib/fastdfs-client/socket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, options) ⇒ Socket

Returns a new instance of Socket.



12
13
14
15
16
17
18
# File 'lib/fastdfs-client/socket.rb', line 12

def initialize(host, port, options)
  @host, @port = host, port
  @header_len = ProtoCommon::HEAD_LEN
  @options = options || {}
  @connection_timeout = @options[:connection_timeout]
  @recv_timeout = @options[:recv_timeout]
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



10
11
12
# File 'lib/fastdfs-client/socket.rb', line 10

def host
  @host
end

#portObject

Returns the value of attribute port.



10
11
12
# File 'lib/fastdfs-client/socket.rb', line 10

def port
  @port
end

#socketObject

Returns the value of attribute socket.



10
11
12
# File 'lib/fastdfs-client/socket.rb', line 10

def socket
  @socket
end

Instance Method Details

#closeObject



28
29
30
# File 'lib/fastdfs-client/socket.rb', line 28

def close 
  @socket.close if connected
end

#connectedObject



41
42
43
# File 'lib/fastdfs-client/socket.rb', line 41

def connected
  @socket.nil? ? false : !@socket.closed?
end

#connection(&block) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/fastdfs-client/socket.rb', line 32

def connection(&block)
  if @socket.nil? || !connected
    @socket = Timeout.timeout(@connection_timeout) do
      TCPSocket.new(@host, @port)  
    end
  end
  yield if block_given?
end

#receive(&block) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fastdfs-client/socket.rb', line 45

def receive(&block)
  @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?
    res_header[:result] = res unless res.nil?
  end
  res_header
end

#response_objObject



58
59
60
# File 'lib/fastdfs-client/socket.rb', line 58

def response_obj
  Hash[status: true, err_msg: nil, result: nil]
end

#write(*args) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/fastdfs-client/socket.rb', line 20

def write(*args)
  @cmd = args.shift
  pkg = args.shift

  pkg = pkg.pack("C*") if pkg.is_a?(Array)
  @socket.write pkg
end