Class: Fastdfs::Client::Socket
Instance Attribute Summary collapse
-
#cmd ⇒ Object
Returns the value of attribute cmd.
-
#content ⇒ Object
Returns the value of attribute content.
-
#header ⇒ Object
Returns the value of attribute header.
-
#header_len ⇒ Object
Returns the value of attribute header_len.
-
#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 ⇒ Object
-
#initialize(host, port, options = {}) ⇒ Socket
constructor
A new instance of Socket.
- #receive {|@content| ... } ⇒ Object
- #write(*args) ⇒ Object
Constructor Details
#initialize(host, port, options = {}) ⇒ Socket
12 13 14 15 16 17 18 19 20 |
# File 'lib/fastdfs-client/socket.rb', line 12 def initialize(host, port, = {}) @host = host @port = port connection @header_len = ProtoCommon::HEAD_LEN = || {} @connection_timeout = [:connection_timeout] || 3 @recv_timeout = [:recv_timeout] || 3 end |
Instance Attribute Details
#cmd ⇒ Object
Returns the value of attribute cmd.
10 11 12 |
# File 'lib/fastdfs-client/socket.rb', line 10 def cmd @cmd end |
#content ⇒ Object
Returns the value of attribute content.
10 11 12 |
# File 'lib/fastdfs-client/socket.rb', line 10 def content @content end |
#header ⇒ Object
Returns the value of attribute header.
10 11 12 |
# File 'lib/fastdfs-client/socket.rb', line 10 def header @header end |
#header_len ⇒ Object
Returns the value of attribute header_len.
10 11 12 |
# File 'lib/fastdfs-client/socket.rb', line 10 def header_len @header_len end |
#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
29 30 31 |
# File 'lib/fastdfs-client/socket.rb', line 29 def close @socket.close if connected end |
#connected ⇒ Object
41 42 43 |
# File 'lib/fastdfs-client/socket.rb', line 41 def connected !@socket.closed? end |
#connection ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/fastdfs-client/socket.rb', line 33 def connection if @socket.nil? || !connected Timeout.timeout(@connection_timeout) do @socket = TCPSocket.new(@host, @port) end end end |
#receive {|@content| ... } ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/fastdfs-client/socket.rb', line 45 def receive @content = nil Timeout.timeout(@recv_timeout) do @header = @socket.recv(@header_len).unpack("C*") end res_header = parseHeader if res_header[:body_length] > 0 Timeout.timeout(@recv_timeout) do @content = @socket.recv(@header.to_pack_long) end end yield @content if block_given? end |
#write(*args) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/fastdfs-client/socket.rb', line 22 def write(*args) @cmd = args.shift pkg = args.shift pkg = pkg.pack("C*") if pkg.is_a?(Array) @socket.write pkg end |