现在的位置: 首页 > 综合 > 正文

Binder机制之Server端—预备知识

2018年02月10日 ⁄ 综合 ⁄ 共 1247字 ⁄ 字号 评论关闭

以MediaPlayerService为例研究server端

MediaPlayerService类图:

  1、RefBase:所有类的基类,相当于C++的CObject,该类可以通过引用计数的方式类控制对象的声明周期。

  2、IBinder:binder接口的基类,接口定义了进程间交互的协议,通过transact方法向远端的IBinder对象发出调用,远端对象通过Binder.OnTransact()方法响应接收到的调用

  3、IBinder、BBinder、BpBinder:

      IBinder是对binder的抽象,即binder接口,它有BBinder、BpBinder两个子类,

      BBinder是服务器中的binder实体,负责接收远程函数的调用和数据,并在Binder 驱动内部生成binder节点,即内核中的binder实体。

      BpBinder是客户端中的binder引用,保存着目标服务的handle信息,即服务端的binder实体的引用信息,用于查询内核中的binder节点,并同binder实体通信。

 4、IInterface、BnInterface、BpInterface:

       IInterface提供asBinder()方法,返回一个IBinder引用,提供类型转换功能,将服务或服务代理类转换为IBinder类型。实际的类型转换有BnInterface、BpInterface完成

       BnInterface用在服务端,将服务类转换为IBinder

       BpInterface用在客户端,将服务代理类转换为IBinder。

 5、Parcel:在服务与服务代理间进行binder IPC通信时,Parcel类负责保存binder IPC通信数据,包括:C语言基本数据类型和binder对象。

 6、IMediaPlayerService:是媒体播放服务MediaPlayerService的业务逻辑接口,客户端远程调用的就是它提供的接口函数。其主要功能是根据

     MediaPlayer::setDataSource输入的URL调用create函数创建对应的Player

 7、BnMediaPlayerService:类继承自BnInterface模板类,约束类型为IMediaPlayerService,BnMediaPlayerService间接继承了IMediaPlayerService接口类。不过 BnInterface类并没有实现IMediaPlayerService所定义的6个接口函数,因此BnInterface还是一个纯虚类。这些接口 需要在BnMediaPlayerService的子类中真正实现,这个子类就是MediaPlayerService

8、ProcessState:每个binder通信的进程,都有一个ProcessState,用来打开binder 驱动,建立连接。

9、IPCThreadState:负责传递数据。

抱歉!评论已关闭.