module Protobuf::Rpc::Connectors::Common

def parse_response

def parse_response
  # Close up the connection as we no longer need it
  close_connection
  log_debug "[#{log_signature}] Parsing response from server (connection closed)"
  @stats.response_size = @buffer.size
  # Parse out the raw response
  response_wrapper = Protobuf::Socketrpc::Response.new
  response_wrapper.parse_from_string(@buffer.data)
  # Determine success or failure based on parsed data
  if response_wrapper.has_field?(:error_reason)
    log_debug "[#{log_signature}] Error response parsed"
    # fail the call if we already know the client is failed
    # (don't try to parse out the response payload)
    fail(response_wrapper.error_reason, response_wrapper.error)
  else
    log_debug "[#{log_signature}] Successful response parsed"
    # Ensure client_response is an instance
    response_type = @options[:response_type].new
    parsed = response_type.parse_from_string(response_wrapper.response_proto.to_s)
    if parsed.nil? and not response_wrapper.has_field?(:error_reason)
      fail(:BAD_RESPONSE_PROTO, 'Unable to parse response from server')
    else
      verify_callbacks
      succeed(parsed)
      return @data if @used_data_callback
    end
  end
end