Binder理解

来源:互联网 发布:软件行业ppt 编辑:程序博客网 时间:2024/06/06 03:47
Binder机制

    Bp接受客户端请求,并将客户端请求通过IpcThreadState写入Binder里,相对的Bn从Binder里读取信息,并处理后再将结果写入Binder如下图


Bp(Binder Proxy)和Bn(Binder Native)






mediaserivce    
1.sp<ProcessState> proc(ProcessState::self());  \\sp阔以当成指针 sp<ProcessState> 就相当于  ProcessState*
procegress->self()   生成了gProcess   
     在gProcess=new ProcessState();
     在创建的时候mDriverFD(open_driver())    open_driver返回fd(文件标识符)   int fd = open("/dev/binder", O_RDWR);
   
2.gDefaultServiceManager = interface_cast<IServiceManager>(ProcessState::self()->getContextObject(NULL));
a.getContextObject(NULL)   ->  getStrongProxyForHandle(0) {  return  new BpBinder(0); }
b.interface_cast<IServiceManager>(ProcessState::self()->getContextObject(NULL));相当于interface_cast<IServiceManager>(new BpBinder(0));
然后看到inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj){return INTERFACE::asInterface(obj);} 相当于IServiceManager::asInterface(new 


BpBinder(0))  
c.去IServiceManager里面看到 DECLARE_META_INTERFACE(ServiceManager);
#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();                                            \




#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() { }    
替换行成
    android::sp<IServiceManager> IServiceManager::asInterface(                
            const android::sp<android::IBinder>& obj)                   
    {                                                                   
        android::sp<IServiceManager> intr;                                 
        if (obj != NULL) {                                              
            intr = static_cast<IServiceManager*>(obj->queryLocalInterface(IServiceManager::descriptor).get());
            if (intr == NULL) {                                         
                intr = new BpServiceManager(obj);                          
            }                                                           
        }                                                               
        return intr;                                                    
    }  
执行结果就是 new BpServiceManager(new BpBinder(0));实际上就是创建了一个BpServiceManager
3.MediaPlayerService::instantiate();
a.new MediaPlayerService()相当于生成了BnMediaPlayerService MediaPlayerService继承BnMediaPlayerService 
b.defaultServiceManager()->addService(String16("media.player"),new MediaPlayerService());相当于BpServiceManager->addService(String16("media.player"),new 
MediaPlayerService());
c.remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply);发现remote()在BpRefBase里直接返回mRemote,不知道mRemote在哪赋值滴,然后在BpRefBase构造方法中  
BpRefBase::BpRefBase(const sp<IBinder>& o): mRemote(o.get()), mRefs(NULL), mState(0)  发现mRemote值其实是o.get()
      BpServiceManager继承自BpInterface,而BpInterface继承自BpRefBase,所以在BpSerivceManagere
而且构造函数BpServiceManager(const sp<IBinder>& impl): BpInterface<IServiceManager>(impl){},
而inline BpInterface<INTERFACE>::BpInterface(const sp<IBinder>& remote): BpRefBase(remote)
最终可以看成其实当时impl=new BpBinder(0);所以mRemote=(new BpBinder(0)).get(),阔以最终看成是BpBinder->transact(ADD_SERVICE_TRANSACTION, data,&reply)
d.  status_t status = IPCThreadState::self()->transact(mHandle, code, data, reply, flags);其实调用IpcThreadState里的transact方法
err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, NULL);  向Binder里面写数据,实际上是写到mOut里了
  err = waitForResponse(reply);里面在talkWithDriver通过ioctl向Binder里面读数据
4.    ProcessState::self()->startThreadPool();  最终也是创建一个线程并加入线程池
    IPCThreadState::self()->joinThreadPool();




问题1.Bn处理后结果如何被客户端知道,是否直接从Binder里直接读取

2.Binder机制快的原因是客户端直接获取Binder内存地址,请问多个客户端是访问服务是否生成多个Binder内存区间,还是只是在服务注册时候分配一块Binder,或者Binder机制快的原因都是错误滴


0 0
原创粉丝点击