lib/fastdfs-client/client_proxy.rb in fastdfs-client-1.0.2 vs lib/fastdfs-client/client_proxy.rb in fastdfs-client-1.2.0

- old
+ new

@@ -1,29 +1,34 @@ +require 'monitor' + module Fastdfs module Client class ClientProxy - attr_accessor :data, :header, :content, :socket + include MonitorMixin - def initialize(cmd, socket, content_len, header = []) - @cmd = cmd - socket.connection - @socket = socket - @header = ProtoCommon.header_bytes(cmd, content_len) + header - @content = [] - end + attr_accessor :host, :port, :socket - def push_content - raise "argument not block!" unless block_given? - @content << yield + def initialize(host, port, options = {}) + super() + options ||= {} + @host = host + @port = port + + @socket = Socket.new(host, port, options[:socket]) end - def dispose(&block) - @socket.write(@cmd, @header) - @content.each do |c| - @socket.write(@cmd, c) + def dispose(cmd, content_len, header = [], content = [], &block) + synchronize do + @socket.connection do + full_header = ProtoCommon.header_bytes(cmd, content_len) + header + @socket.write(cmd, full_header) + Array(@content).each do |c| + @socket.write(cmd, c) + end + @socket.receive &block + end end - @socket.receive &block ensure @socket.close end end \ No newline at end of file