lib/protobuf/rpc/connectors/socket.rb in protobuffy-3.6.0 vs lib/protobuf/rpc/connectors/socket.rb in protobuffy-4.0.0
- old
+ new
@@ -2,12 +2,11 @@
module Protobuf
module Rpc
module Connectors
class Socket < Base
- include Protobuf::Rpc::Connectors::Common
- include Protobuf::Logger::LogMethods
+ include Protobuf::Logging
def send_request
timeout_wrap do
setup_connection
connect_to_rpc_server
@@ -22,25 +21,31 @@
private
def close_connection
@socket.close
- log_debug { sign_message('Connector closed') }
+ logger.debug { sign_message('Connector closed') }
end
def connect_to_rpc_server
- @socket = TCPSocket.new(options[:host], options[:port])
- log_debug { sign_message("Connection established #{options[:host]}:#{options[:port]}") }
+ @socket ||= TCPSocket.new(options[:host], options[:port])
+ logger.debug { sign_message("Connection established #{options[:host]}:#{options[:port]}") }
end
# Method to determine error state, must be used with Connector api
def error?
return true if @error
- log_debug { sign_message("Error state : #{@socket.closed?}") }
+ logger.debug { sign_message("Error state : #{@socket.closed?}") }
@socket.closed?
end
+ def post_init
+ send_data unless error?
+ rescue => e
+ failure(:RPC_ERROR, "Connection error: #{e.message}")
+ end
+
def read_data
size_io = StringIO.new
until (size_reader = @socket.getc) == "-"
size_io << size_reader
@@ -49,11 +54,11 @@
"#{str_size_io}-#{@socket.read(str_size_io.to_i)}"
end
def read_response
- log_debug { sign_message("error? is #{error?}") }
+ logger.debug { sign_message("error? is #{error?}") }
return if error?
response_buffer = ::Protobuf::Rpc::Buffer.new(:read)
response_buffer << read_data
@response_data = response_buffer.data
parse_response if response_buffer.flushed?
@@ -63,10 +68,10 @@
return if error?
request_buffer = ::Protobuf::Rpc::Buffer.new(:write)
request_buffer.set_data(@request_data)
@socket.write(request_buffer.write)
@socket.flush
- log_debug { sign_message("write closed") }
+ logger.debug { sign_message("write closed") }
end
end
end
end
end