Class: Protobuf::Rpc::SocketServer::Worker

Inherits:
Object
  • Object
show all
Includes:
Logger::LogMethods, Protobuf::Rpc::Server
Defined in:
lib/protobuf/rpc/servers/socket_server.rb

Instance Method Summary collapse

Methods included from Protobuf::Rpc::Server

#handle_client, #handle_error, #invoke_rpc_method, #parse_request_from_buffer, #parse_response_from_service, #parse_service_info, #send_response, #serialize_response

Constructor Details

#initialize(sock, &complete_cb) ⇒ Worker

Returns a new instance of Worker.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/protobuf/rpc/servers/socket_server.rb', line 106

def initialize(sock, &complete_cb)
  @did_response = false
  @socket = sock
  @request = Protobuf::Socketrpc::Request.new
  @response = Protobuf::Socketrpc::Response.new
  @buffer = Protobuf::Rpc::Buffer.new(:read)
  @stats = Protobuf::Rpc::Stat.new(:SERVER, true)
  @complete_cb = complete_cb
  log_debug "[#{log_signature}] Post init, new read buffer created"

  @stats.client = Socket.unpack_sockaddr_in(@socket.getpeername)
  @buffer << read_data 
  log_debug "[#{log_signature}] handling request"
  handle_client if @buffer.flushed?
end

Instance Method Details

#log_signatureObject



122
123
124
# File 'lib/protobuf/rpc/servers/socket_server.rb', line 122

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

#read_dataObject



126
127
128
129
130
131
132
133
134
135
# File 'lib/protobuf/rpc/servers/socket_server.rb', line 126

def read_data
  size_io = StringIO.new

  while((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

#send_data(data) ⇒ Object



137
138
139
140
141
142
# File 'lib/protobuf/rpc/servers/socket_server.rb', line 137

def send_data(data)
  log_debug "[#{log_signature}] sending data : %s" % data
  @socket.write(data)
  @socket.flush
  @complete_cb.call(@socket)
end