ارسال فایل‌ها

فایل‌ها را می‌توان با استفاده از نوع ParcelFileDescriptor بین کلاینت‌ها/سرورهای Binder ارسال کرد:

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

interface IBirthdayService { /** The same thing, but loads info from a file. */ String wishFromFile(in ParcelFileDescriptor infoFile); }

birthday_service/src/client.rs:

fn main() { binder::ProcessState::start_thread_pool(); let service = connect().expect("Failed to connect to BirthdayService"); // Open a file and put the birthday info in it. let mut file = File::create("/data/local/tmp/birthday.info").unwrap(); writeln!(file, "{name}")?; writeln!(file, "{years}")?; // Create a `ParcelFileDescriptor` from the file and send it. 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> { // Convert the file descriptor to a `File`. `ParcelFileDescriptor` wraps // an `OwnedFd`, which can be cloned and then used to create a `File` // object. let mut info_file = info_file .as_ref() .try_clone() .map(File::from) .expect("کنترل فایل نامعتبر"); 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!("Happy Birthday {name}, congratulations with the {years} years!")) } }

Speaker Notes

  • ParcelFileDescriptor یک OwnedFd را احاطه می‌کند و بنابراین می‌تواند از یک File (یا هر نوع دیگری که یک OwnedFd را احاطه می‌کند) ایجاد کند و می‌تواند برای ایجاد یک دسته File جدید در طرف دیگر استفاده شود.
  • انواع دیگر توصیف‌گرهای فایل را می توان بسته‌بندی و ارسال کرد، به‌عنوان‌مثال. سوکت های TCP، UDP و UNIX.