Sha256: 0ac86fe3b1f6f9bee63cb8bc2c37b6408a1aaaba1f8cf6fff38561434cc39bf2

Contents?: true

Size: 1.59 KB

Versions: 13

Compression:

Stored size: 1.59 KB

Contents

require 'protobuf/rpc/server'
require 'protobuf/logger'

module Protobuf
  module Rpc
    module Socket

      class Worker
        include ::Protobuf::Rpc::Server
        include ::Protobuf::Logger::LogMethods

        def initialize(sock, &complete_cb)
          @socket = sock
          initialize_request!

          request_buffer = Protobuf::Rpc::Buffer.new(:read)
          @complete_cb = complete_cb

          log_debug { sign_message("stats are #{@stats.to_s}") }
          request_buffer << read_data
          @request_data = request_buffer.data

          log_debug { sign_message("handling request") }
          handle_client if request_buffer.flushed?
        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 send_data
          raise 'Socket closed unexpectedly' unless socket_writable?
          response_buffer = Protobuf::Rpc::Buffer.new(:write)
          response_buffer.set_data(@response)
          @stats.response_size = response_buffer.size
          log_debug { sign_message("sending data : #{response_buffer.data}") }
          @socket.write(response_buffer.write)
          @socket.flush
          @complete_cb.call(@socket)
        end

        def log_signature
          @_log_signature ||= "server-#{self.class}-#{object_id}"
        end

        def socket_writable?
          ! @socket.nil? && ! @socket.closed?
        end
      end

    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
protobuf-2.8.13 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.12 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.11 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.10 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.9 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.8 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.7 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.6 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.5 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.4 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.3 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.2 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.1 lib/protobuf/rpc/servers/socket/worker.rb