Class: Protobuf::Rpc::Socket::Worker

Inherits:
Object
  • Object
show all
Includes:
Logging, Protobuf::Rpc::Server
Defined in:
lib/protobuf/rpc/servers/socket/worker.rb

Instance Method Summary collapse

Methods included from Logging

initialize_logger, #log_exception, #logger, #sign_message

Methods included from Protobuf::Rpc::Server

#gc_pause, #handle_request

Constructor Details

#initialize(sock, &complete_cb) ⇒ Worker

Returns a new instance of Worker.


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/protobuf/rpc/servers/socket/worker.rb', line 11

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

Instance Method Details

#log_signatureObject


46
47
48
# File 'lib/protobuf/rpc/servers/socket/worker.rb', line 46

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

#read_dataObject


24
25
26
27
28
29
30
31
32
33
# File 'lib/protobuf/rpc/servers/socket/worker.rb', line 24

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

#send_data(data) ⇒ Object


35
36
37
38
39
40
41
42
43
44
# File 'lib/protobuf/rpc/servers/socket/worker.rb', line 35

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

#socket_writable?Boolean

Returns:

  • (Boolean)

50
51
52
# File 'lib/protobuf/rpc/servers/socket/worker.rb', line 50

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