C++实例讲解Binder通信

时间:2022-04-15 06:33:18

  binder是android里面的通信机制,这就不说它如何如何好了,Goog已经说过了,这里不多说。binder是一个面向对象的编程方法,大量使用虚函数类。最近研究binder看到一网友写的,就借鉴一下。这个例子很好的解释里binder通信关系。原文:http://blog.csdn.net/new_abc/article/details/8097775 例子不错不过就是没运行起来,不过这都不是问题,关键是很容易理解。

  我将他的源码整理类图看看,不过这个是简单的继承关系。

  C++实例讲解Binder通信

  基本上使用binder就这个关系,从中间一分为二,左边客户端使用,右边服务端。不管是客户端还是服务端都继承子IXXXService这个类,这个类可以裂解为客户端和服务端的“爷爷”,而“爷爷”继承IInterface,所有自定义的binder都必须继承这个类,这个是android强指针实现计数的方法。先看看源码后再理解这个图。

首先看下目录结构:

  TestBinderClient目录:  Android.mk  ITestBinderService.cpp

  TestBinderServer目录: Android.mk  ITestBinderService.h  main_testBinder.cpp  testBinder.cpp  TestBinderService.cpp  TestBinderService.h

TestBinderClient下面是Binder的客户端,TestBinderServer是binder的服务端

我们先来看下biner服务端代码

1、ITestBinderService.h

     #ifndef ANDROID_ITESTBINDERSERVICE_H_
#define ANDROID_ITESTBINDERSERVICE_H_ #include <utils/RefBase.h>
#include <binder/IInterface.h>
#include <binder/Parcel.h> namespace android { class Parcel; class ITestBinderService: public IInterface {
public:
DECLARE_META_INTERFACE(TestBinderService); virtual int add(int a, int b) = ;
}; class BnTestBinderService: public BnInterface<ITestBinderService> {
public:
virtual status_t onTransact(uint32_t code, const Parcel& data,
Parcel* reply, uint32_t flags = );
}; } #endif /* ANDROID_ITESTBINDERSERVICE_H_ */

ITestBinderService.h

这里主要是定义了两个类ITestBinderService 和 BnTestBinderService,ITestBinderService 是TestBinderService 的基类,这里主要是DECLARE_META_INTERFACE 这个宏,定义在frameworks\base\include\binder\IInterface.h文件中。

 #define DECLARE_META_INTERFACE(INTERFACE)                               \
static const android::String16 descriptor; \
static android::sp<I##INTERFACE> asInterface( \
const android::sp<android::IBinder>& obj); \
virtual const android::String16& getInterfaceDescriptor() const; \
I##INTERFACE(); \
virtual ~I##INTERFACE();

DECLARE_META_INTERFACE 宏

把TestBinderService代入进去

 #define DECLARE_META_INTERFACE(TestBinderService)                               \
static const android::String16 descriptor; \
static android::sp<ITestBinderService> asInterface( \
const android::sp<android::IBinder>& obj); \
virtual const android::String16& getInterfaceDescriptor() const; \
ITestBinderService(); \
virtual ~I##TestBinderService();

带入宏后

其中封装了实现binder所需要的一些类成员变量和成员函数,通过这些成员函数可以为一个binder实现创建proxy(代理)

2、TestBinderService.h

     #ifndef ANDROID_TESTBINDERSERVICE_H_
#define ANDROID_TESTBINDERSERVICE_H_ #include <utils/KeyedVector.h>
#include "ITestBinderService.h" namespace android { class TestBinderService: public BnTestBinderService {
public:
static void instantiate();
int add(int a,int b);
private:
TestBinderService();
virtual ~TestBinderService();
}; } #endif /* ANDROID_TESTBINDERSERVICE_H_ */

TestBinderService.h

这个文件比较简单,主要就是定义了一个类TestBinderService,继承于前面 的BnTestBinderService,并定义了一个方法add函数和instantiate

3、TestBinderService.cpp

     #define LOG_TAG "TestBinderService"
#include <utils/Log.h>
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h> #include "TestBinderService.h"
static int debug_flag = ;
namespace android { void TestBinderService::instantiate() {
LOGI("Enter TestBinderService::instantiate");
status_t st = defaultServiceManager()->addService(
String16("my.test.binder"), new TestBinderService());
LOGD("ServiceManager addService ret=%d", st);
LOGD("instantiate> end");
} TestBinderService::TestBinderService() {
LOGD(" TestBinderServicet");
} TestBinderService::~TestBinderService() {
LOGD("TestBinderService destroyed,never destroy normally");
} int TestBinderService::add(int a,int b) { LOGI("TestBinderService::add a = %d, b = %d.", a , b);
return a+b;
} }

TestBinderService.cpp

在instantiate函数中,将TestBinderService注册到系统的binder service列表中,这样以后就可以使用这个service提供的方法,该service提供了一个add 方法,返回两个数的和。

再来看下clinet端 的代码

1、ITestBinderService.cpp

     #define LOG_TAG "ITeeveePlayerService"  

     #include <utils/Log.h>  

     #include "../TestBinderServer/ITestBinderService.h"  

     namespace android {  

     enum {
TEST_ADD = IBinder::FIRST_CALL_TRANSACTION,
}; class BpTestBinderService: public BpInterface<ITestBinderService> {
public:
BpTestBinderService(const sp<IBinder>& impl) :
BpInterface<ITestBinderService> (impl) {
} int add(int a, int b) { Parcel data, reply;
LOGI("Enter BpTestBinderService add,a = %d , b = %d", a, b);
data.writeInterfaceToken(ITestBinderService::getInterfaceDescriptor());
data.writeInt32(a);
data.writeInt32(b);
remote()->transact(TEST_ADD, data, &reply);
int sum = reply.readInt32();
LOGI("BpTestBinderService sum = %d", sum);
return sum;
}
}; IMPLEMENT_META_INTERFACE(TestBinderService, "android.test.ITestBinderService"); // ---------------------------------------------------------------------- status_t BnTestBinderService::onTransact(uint32_t code, const Parcel& data,
Parcel* reply, uint32_t flags) {
switch (code) {
case TEST_ADD: { CHECK_INTERFACE(ITestBinderService, data, reply);
int a = data.readInt32();
int b = data.readInt32();
LOGI("Enter BnTestBinderService add,a = %d , b = %d", a, b);
int sum = ;
sum = add(a, b);
LOGI("BnTestBinderService sum = %d", sum);
reply->writeInt32(sum);
return sum;
}
default:
return BBinder::onTransact(code, data, reply, flags);
}
} }

ITestBinderService.cpp

定义了一个类BpTestBinderService,提供add方法,该方法通过调用远端的binder service提供的服务返回两个数的和重载了BnTestBinderService的onTransact方法,使其在TEST_ADD时调用add方法

这个文件里面也使用了一个宏IMPLEMENT_META_INTERFACE,也是定义在frameworks\base\include\binder\IInterface.h文件中

     #define IMPLEMENT_META_INTERFACE(INTERFACE, NAME)                       \
const android::String16 I##INTERFACE::descriptor(NAME); \
const android::String16& \
I##INTERFACE::getInterfaceDescriptor() const { \
return I##INTERFACE::descriptor; \
} \
android::sp<I##INTERFACE> I##INTERFACE::asInterface( \
const android::sp<android::IBinder>& obj) \
{ \
android::sp<I##INTERFACE> intr; \
if (obj != NULL) { \
intr = static_cast<I##INTERFACE*>( \
obj->queryLocalInterface( \
I##INTERFACE::descriptor).get()); \
if (intr == NULL) { \
intr = new Bp##INTERFACE(obj); \
} \
} \
return intr; \
} \
I##INTERFACE::I##INTERFACE() { } \
I##INTERFACE::~I##INTERFACE() { }

IMPLEMENT_META_INTERFACE宏

代入展开后:

 #define IMPLEMENT_META_INTERFACE(TestBinderService, "android.test.ITestBinderService")                       \
const android::String16 ITestBinderService::descriptor("android.test.ITestBinderService"); \
const android::String16& \
ITestBinderService::getInterfaceDescriptor() const { \
return ITestBinderService::descriptor; \
} \
android::sp<ITestBinderService> ITestBinderService::asInterface( \
const android::sp<android::IBinder>& obj) \
{ \
android::sp<ITestBinderService> intr; \
if (obj != NULL) { \
intr = static_cast<ITestBinderService*>( \
obj->queryLocalInterface( \
ITestBinderService::descriptor).get()); \
if (intr == NULL) { \
intr = new BpTestBinderService(obj); \
} \
} \
return intr; \
} \
ITestBinderService::ITestBinderService() { } \
ITestBinderService::~ITestBinderService() { }

带入到宏后

这样,server和client端的binder代码主写好了,接着就需要把binder service加入到binder中

这里有两种方法:

1、在system_init.cpp中添加

TestBinderService::instantiate();

如果是在这里加的话可以去掉TestBinderService中实现的instantiate方法,同时将TestBinderService继
承自BinderService,因为在BinderService实现了这一方法,同时将其添加到binder service

2、以单独的程序启动

main_testBinder.cpp

     #include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <utils/Log.h> #include "TestBinderService.h" using namespace android; int main(int argc, char** argv)
{ sp<ProcessState> proc(ProcessState::self());
sp<IServiceManager> sm = defaultServiceManager();
LOGI("TestBinderService before");
TestBinderService::instantiate();
LOGI("TestBinderService End");
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool();
return ; }

将server添加到servermanage里面

这里调用的是TestBinderService自己的instantiate来添加的

再来看下测试testBinder.cpp

     #define LOG_TAG "TestBinserService"  

     #include <utils/Log.h>
#include <nativehelper/jni.h>
#include <nativehelper/JNIHelp.h>
#include <android_runtime/AndroidRuntime.h>
#include <binder/IServiceManager.h>
#include "../TestBinderServer/ITestBinderService.h" #include "TestBinderService.h" using namespace android; int main(int argc, char** argv)
{
int sum = ;
sp<ITestBinderService> mTestBinserService;
if (mTestBinserService.get() == ) {
sp<IServiceManager> sm = defaultServiceManager();
sp<IBinder> binder;
do {
binder = sm->getService(String16("my.test.binder"));
if (binder != )
break;
LOGI("getService fail");
usleep(); // 0.5 s
} while (true);
mTestBinserService = interface_cast<ITestBinderService> (binder);
LOGE_IF(mTestBinserService == , "no ITestBinserService!?");
}
sum = mTestBinserService->add(, );
LOGI("sum = %d", sum);
return ; }

testBinder.cpp

以上就是测试代码。