Binder
Binder as a mechanism for IPC in Android, is used everywhere in Android. Contains three layers (Java, c++/c, driver).
The purpose of binder is to implement communication between multiple processes, with data passed between the basic data type, binder type, and this wrapper in parcel.
In the C + + tier, use multiple classes to encapsulate:
The main classes are:
RefBaseAbout reference counting
ParcelThis is the data container for the transfer and also supports the ##### #IBinder对象 ##### #的引用.
IBinderBinder object interface with transact pure virtual function
BpBinderBinder Reference Object, present BpRefBase in, generated by binder driver
BBinderBinder's Entity Object
IInterfaceInterface
BnInterfaceTemplate class, inherited by Ixx and Bbinder
BpInterfaceTemplate class, inherited by Ixx and Bprefbase
IPCThreadState
ProcessStateAfter the class is generated, the binder driver opens.
In C + + to write binder applications, to have a client code and server-side code, one of my applications, the main code is:
Client
int main(int argc, char** argv)
{
LOGI("binderclient");
printf("binderclient --\n");
int sum = 0;
sp mTestBinserService;
if (mTestBinserService.get() == 0) {
sp sm = defaultServiceManager();
sp binder;
do {
binder = sm->getService(String16("my.test.binder"));
if (binder != 0)
break;
LOGI("getService fail");
usleep(500000); // 0.5 s
} while (true);
mTestBinserService = interface_cast (binder);
LOGE_IF(mTestBinserService == 0, "no ITestBinserService!?");
}
sum = mTestBinserService->add(3, 4);
LOGI("sum = %d", sum);
return 0;
}
Service side
int main(int argc, char** argv)
{
sp proc(ProcessState::self());
sp sm = defaultServiceManager();
LOGI("TestBinderService before");
//TestBinderService::instantiate();
sm->addService(String16("my.test.binder", new TestBinderService());//TestBinderService继承于B你Interface, 也就是继承于ITestBinderService和BBinder.
LOGI("TestBinderService End");
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool();
LOGI("TestBinderService End");
return 0;
}
Inter-process callbacks
Using binders allows you to implement callbacks between processes. The IBinder interface is available as a function parameter.
From for notes (Wiz)
Binder C + + class