Sha256: 98607b5158069c8a1fd2f5ffd06f27442414b352937365b2aff4492d44d01919

Contents?: true

Size: 1.64 KB

Versions: 66

Compression:

Stored size: 1.64 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

          @stats.request_size = request_buffer.size

          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

66 entries across 66 versions & 1 rubygems

Version Path
protobuf-2.7.12 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.0 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.0.beta9-java lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.0.beta9 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.0.beta8-java lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.0.beta8 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.0.beta6-java lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.0.beta6 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.0.beta5-java lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.0.beta5 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.0.beta4-java lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.0.beta4 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.0.beta3-java lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.0.beta3 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.0.beta2-java lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.0.beta2 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.7.11-java lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.7.11 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.0.beta1-java lib/protobuf/rpc/servers/socket/worker.rb
protobuf-2.8.0.beta1 lib/protobuf/rpc/servers/socket/worker.rb