Sha256: b62bca904aff8ed5b176d15970c2bcaf89bbf210a3383fb9869fe26ff22491e9

Contents?: true

Size: 1.97 KB

Versions: 13

Compression:

Stored size: 1.97 KB

Contents

require 'protobuf/rpc/connectors/base'

module Protobuf
  module Rpc
    module Connectors
      class Socket < Base
        include Protobuf::Rpc::Connectors::Common
        include Protobuf::Logging

        def send_request
          timeout_wrap do
            setup_connection
            connect_to_rpc_server
            post_init
            read_response
          end
        end

        def log_signature
          @_log_signature ||= "[client-#{self.class}]"
        end

        private

        def close_connection
          @socket.close
          logger.debug { sign_message('Connector closed') }
        end

        def connect_to_rpc_server
          @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
          logger.debug { sign_message("Error state : #{@socket.closed?}") }
          @socket.closed?
        end

        def read_data
          size_io = StringIO.new

          until (size_reader = @socket.getc) == "-"
            size_io << size_reader
          end
          str_size_io = size_io.string

          "#{str_size_io}-#{@socket.read(str_size_io.to_i)}"
        end

        def read_response
          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?
        end

        def send_data
          return if error?
          request_buffer = ::Protobuf::Rpc::Buffer.new(:write)
          request_buffer.set_data(@request_data)
          @socket.write(request_buffer.write)
          @socket.flush
          logger.debug { sign_message("write closed") }
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
protobuf-3.6.12 lib/protobuf/rpc/connectors/socket.rb
protobuf-3.6.11 lib/protobuf/rpc/connectors/socket.rb
protobuf-3.6.10 lib/protobuf/rpc/connectors/socket.rb
protobuf-3.7.0.pre2 lib/protobuf/rpc/connectors/socket.rb
protobuf-3.6.9 lib/protobuf/rpc/connectors/socket.rb
protobuf-3.7.0.pre1 lib/protobuf/rpc/connectors/socket.rb
protobuf-3.7.0.pre0 lib/protobuf/rpc/connectors/socket.rb
protobuf-3.6.7 lib/protobuf/rpc/connectors/socket.rb
protobuf-3.6.6 lib/protobuf/rpc/connectors/socket.rb
protobuf-3.6.2 lib/protobuf/rpc/connectors/socket.rb
protobuf-3.6.1 lib/protobuf/rpc/connectors/socket.rb
protobuf-3.6.0 lib/protobuf/rpc/connectors/socket.rb
protobuf-3.5.5 lib/protobuf/rpc/connectors/socket.rb