Sha256: 0d5b71c51a95a0f95622ffd90b28bed907ba874b57417ed9795f6e1f38d9bd0d

Contents?: true

Size: 1.27 KB

Versions: 62

Compression:

Stored size: 1.27 KB

Contents

require 'protobuf/rpc/server'
require 'protobuf/logging'

module Protobuf
  module Rpc
    module Socket
      class Worker
        include ::Protobuf::Rpc::Server
        include ::Protobuf::Logging

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

          data = read_data
          return unless data

          gc_pause do
            encoded_response = handle_request(data)
            send_data(encoded_response)
          end
        end

        def read_data
          size_io = StringIO.new

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

          @socket.read(str_size_io.to_i)
        end

        def send_data(data)
          fail 'Socket closed unexpectedly' unless socket_writable?
          response_buffer = Protobuf::Rpc::Buffer.new(:write)
          response_buffer.set_data(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

62 entries across 62 versions & 4 rubygems

Version Path
protobuf-3.8.5.pre0 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-3.8.4 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-3.8.3 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-3.8.2 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-3.8.1 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-3.8.0 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-3.8.0.pre1 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-3.7.5 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-3.7.4 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-3.7.3 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-3.7.2 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-3.7.2.pre1 lib/protobuf/rpc/servers/socket/worker.rb
protobuffy-4.0.1 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-3.7.1 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-3.7.0 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-3.7.0.pre3 lib/protobuf/rpc/servers/socket/worker.rb
protobuffy-4.0.0 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-3.6.12 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-3.6.11 lib/protobuf/rpc/servers/socket/worker.rb
protobuf-3.6.10 lib/protobuf/rpc/servers/socket/worker.rb