Enviando Arquivos

Arquivos podem ser enviados entre clientes/servidores Binder usando o tipo ParcelFileDescriptor:

birthday_service/aidl/com/example/birthdayservice/IBirthdayService.aidl:

interface IBirthdayService { /** A mesma coisa, mas carrega informaçÔes de um arquivo. */ String wishFromFile(in ParcelFileDescriptor infoFile); }

birthday_service/src/client.rs:

fn main() { binder::ProcessState::start_thread_pool(); let service = connect().expect("Falha ao conectar-se a BirthdayService"); // Abre um arquivo e coloca as informaçÔes de aniversårio nele. let mut file = File::create("/data/local/tmp/birthday.info").unwrap(); writeln!(file, "{name}")?; writeln!(file, "{years}")?; // Cria um `ParcelFileDescriptor` a partir do arquivo e o envia. let file = ParcelFileDescriptor::new(file); service.wishFromFile(&file)?; }

birthday_service/src/lib.rs:

impl IBirthdayService for BirthdayService { fn wishFromFile( &self, info_file: &ParcelFileDescriptor, ) -> binder::Result<String> { // Converte o descritor de arquivo para um `File`. `ParcelFileDescriptor` envolve // um `OwnedFd`, que pode ser clonado e então usado para criar um objeto `File`. let mut info_file = info_file .as_ref() .try_clone() .map(File::from) .expect("Identificador de arquivo invålido"); let mut contents = String::new(); info_file.read_to_string(&mut contents).unwrap(); let mut lines = contents.lines(); let name = lines.next().unwrap(); let years: i32 = lines.next().unwrap().parse().unwrap(); Ok(format!("Feliz aniversårio {name}, parabéns pelos seus {years} anos!")) } }

Speaker Notes

  • ParcelFileDescriptor envolve um OwnedFd, e assim pode ser criado a partir de um File (ou qualquer outro tipo que envolva um OwnedFd), e pode ser usado para criar um novo identificador de File no outro lado.
  • Outros tipos de descritores de arquivo podem ser envolvidos e enviados, por exemplo, soquetes TCP, UDP e UNIX.