คิวข้อความด่วนด้วย AIDL

ตั้งแต่ Android 12 เป็นต้นไป Fast Message Queue สามารถใช้ได้กับอินเทอร์เฟซ AIDL โดยใช้แบ็กเอนด์ NDK ซึ่งช่วยให้กระบวนการสื่อสารได้โดยไม่มีค่าใช้จ่ายและข้อจำกัดของธุรกรรม Binder หลังจากการตั้งค่าสั้นๆ การใช้ Stable AIDL ช่วยให้สามารถสื่อสารระหว่างระบบและกระบวนการของผู้ขายได้

ประเภทเพย์โหลดที่รองรับ

ข้อความที่ส่งระหว่างกระบวนการในคิวข้อความหน่วยความจำแบบแบ่งใช้จะต้องมีโครงร่างหน่วยความจำเดียวกันข้ามขอบเขตกระบวนการ และต้องไม่มีตัวชี้ การพยายามสร้าง AidlMessageQueue ด้วยประเภทที่ไม่รองรับจะทำให้เกิดข้อผิดพลาดในการคอมไพล์

ประเภทคิวที่รองรับ

AIDL รองรับ คิวประเภท เดียวกันซึ่งมักเรียกว่ารสชาติ สิ่งเหล่านี้ถูกใช้เป็นอาร์กิวเมนต์เทมเพลตสำหรับคิวและตัวอธิบาย

ประเภท HIDL ประเภทโรคเอดส์
android::hardware::kSynchronizedReadWrite android.hardware.common.fmq.SynchronizedReadWrite
android::hardware::kUnsynchronizedWrite android.hardware.common.fmq.UnsynchronizedWrite

วิธีใช้

กำหนดอินเทอร์เฟซ AIDL ที่จะส่งต่อ MQDescriptor ไปยังกระบวนการอื่น MQDescriptor สามารถใช้ได้ทุกที่ที่สามารถแบ่งพัสดุได้

อาร์กิวเมนต์เทมเพลตที่จำเป็นสำหรับ MQDescriptor คือประเภทเพย์โหลดและรสชาติของคิว

import android.hardware.common.fmq.MQDescriptor
import android.hardware.common.fmq.SynchronizedReadWrite

void getQueue(out MQDescriptor<int, SynchronizedReadWrite> mqDesc);

กระบวนการตั้งค่าคิวข้อความแต่ละด้านเกือบจะเหมือนกันกับ กระบวนการที่ใช้ HIDL เพียงใช้ประเภท AIDL

#include <fmq/AidlMessageQueue.h>
...
using ::android::AidlMessageQueue;
using ::aidl::android::hardware::common::fmq::MQDescriptor;
using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
...
ndk::ScopedAStatus MyInterface::getQueue(MQDescriptor<int32_t, SynchronizedReadWrite>* mqDesc) {
    *mqDesc = mFmqSynchronized->dupeDesc();
    return ndk::ScopedAStatus::ok();
}
...
// Create the first side of the queue before servicing getQueue() in this example
mFmqSynchronized =
  new AidlMessageQueue<int32_t, SynchronizedReadWrite>(kNumElementsInQueue);

กระบวนการรับจะสร้างอีกด้านหนึ่งของคิวโดยมีคำอธิบายที่ได้รับจากอินเทอร์เฟซ AIDL

MQDescriptor<int32_t, SynchronizedReadWrite> desc;
auto ret = service->getQueue(true, &desc);
if (!ret.isOk()) {
   ...
}
// By default the constructor will reset the read and write pointers of the queue.
// Add a second `false` argument to avoid resetting the pointers.
mQueue = new (std::nothrow) AidlMessageQueue<int32_t, SynchronizedReadWrite>(desc);
if (!mQueue->isValid()) {
   ...
}

การใช้ AidlMessageQueue หลังจากการตั้งค่าจะเหมือนกับ HIDL MessageQueue API ทั้งหมดที่อธิบายไว้ใน การใช้ MessageQueue ได้รับการรองรับ AidlMessageQueue อย่างสมบูรณ์ โดยมีข้อยกเว้นประการหนึ่ง:

const MQDescriptor<T, flavor>* getDesc() ถูกแทนที่ด้วย MQDescriptor<T, U> dupeDesc() ซึ่งส่งคืน AIDL MQDescriptor