Class: Fastdfs::Client::Socket
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
-
#socket ⇒ Object
Returns the value of attribute socket.
Instance Method Summary collapse
- #close ⇒ Object
- #connected ⇒ Object
- #connection(&block) ⇒ Object
-
#initialize(host, port, options) ⇒ Socket
constructor
A new instance of Socket.
- #receive(&block) ⇒ Object
- #response_obj ⇒ Object
- #write(*args) ⇒ Object
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, ) @host, @port = host, port @header_len = ProtoCommon::HEAD_LEN @options = || {} @connection_timeout = @options[:connection_timeout] @recv_timeout = @options[:recv_timeout] end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
10 11 12 |
# File 'lib/fastdfs-client/socket.rb', line 10 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
10 11 12 |
# File 'lib/fastdfs-client/socket.rb', line 10 def port @port end |
#socket ⇒ Object
Returns the value of attribute socket.
10 11 12 |
# File 'lib/fastdfs-client/socket.rb', line 10 def socket @socket end |
Instance Method Details
#close ⇒ Object
28 29 30 |
# File 'lib/fastdfs-client/socket.rb', line 28 def close @socket.close if connected end |
#connected ⇒ Object
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 |