BpBinder and PPBinder call process - Android development Binder IPC communication technology

In the Android system, inter-process communication (IPC) is a very important topic. The Android system implements inter-process communication through the Binder IPC mechanism, and the Binder IPC communication technology is one of the most important inter-process communication technologies in the Android system. This article will introduce the principle of Binder IPC communication technology, and analyze the use and precautions of the BpBinder and PPBinder calling process in detail.

1. Principle of Binder IPC communication technology:

Binder IPC communication technology is an efficient cross-process communication technology in the Android system, which implements inter-process communication through the underlying mechanism provided by the Binder driver. The principle of Binder IPC communication technology is as follows:

  1. Binder driver: The Binder driver is the core of the Binder IPC communication technology. It is a module located in the kernel space and is responsible for processing inter-process communication requests. The Binder driver provides a set of system call interfaces for creating, registering, finding and destroying Binder objects, and handling inter-process communication requests.
  2. Binder object: In Binder IPC communication technology, the basic unit of inter-process communication is the Binder object. Each Binder object has a unique identifier called a Binder reference. The Binder object can be a Binder object on the server side or a Binder object on the client side. The Binder object on the server side is responsible for providing certain services, while the Binder object on the client side is responsible for invoking the services provided by the server side.
  3. Binder communication mechanism: In Binder IPC communication technology, the communication process can be briefly described as the following steps: (1) The server creates a Binder object and registers it in the Binder driver. (2) The client finds the Binder object on the server through the Binder reference. (3) The client invokes the corresponding service through the Binder object of the server. (4) The server receives the call request from the client and processes the request. (5) The server returns the processing result to the client.

Two, BpBinder and PPBinder call process:

BpBinder and PPBinder are two important concepts in Binder IPC communication technology. BpBinder is the client's Binder proxy object, used to send call requests to the server; PPBinder is the server's Binder proxy object, used to receive and process the client's call requests.

  1. BpBinder calling process: (1) The client finds the Binder object of the server through the Binder reference. (2) The client sends a call request to the server through the proxy method of BpBinder. (3) BpBinder encapsulates the call request into a Parcel object, and sends the Parcel object to the server through the Binder driver. (4) After the PPBinder on the server side receives the Parcel object, it parses it into a call request and calls the corresponding service. (5) The server encapsulates the processing result into a Parcel object, and sends the Parcel object to the client through the Binder driver. (6) After receiving the Parcel object, BpBinder parses it into a processing result and returns it to the client.
  2. PPBinder calling process:

(1) The server creates a subclass inherited from the Binder class and overrides its onTransact() method. In this method, the server performs corresponding processing according to the received call request, and encapsulates the processing result into a Parcel object and returns it to the client. (2) The server registers the created Binder object to the Binder driver, so that the client can find the Binder object through the Binder reference. (3) The client finds the Binder object on the server through the Binder reference. (4) The client sends a call request to the server through the proxy method of BpBinder. (5) BpBinder encapsulates the call request into a Parcel object, and sends the Parcel object to the server through the Binder driver. (6) After the PPBinder on the server side receives the Parcel object, it parses it into a call request and calls the corresponding service. (7) The server encapsulates the processing result into a Parcel object, and sends the Parcel object to the client through the Binder driver. (8) After receiving the Parcel object, BpBinder parses it into a processing result and returns it to the client.

Three, BpBinder and PPBinder combat analysis

When using BpBinder and PPBinder to call the process, you need to pay attention to the following points:

  1. When using BpBinder and PPBinder to call the process, it is necessary to ensure that the Binder object on the server side has been registered in the Binder driver, and the client can find the Binder object through the Binder reference.
  2. BpBinder is the client's Binder proxy object, which can send call requests to the server through its proxy method. When using BpBinder to make a call, you need to pay attention to the type and order of the passed parameters to be consistent with the method definition on the server side.
  3. PPBinder is the Binder proxy object on the server side, which is used to receive and process the call request from the client. When using PPBinder to call, you need to rewrite the onTransact() method, and perform corresponding processing according to the received call request.
  4. During the call, BpBinder encapsulates the call request into a Parcel object, and sends the Parcel object to the server through the Binder driver. After receiving the Parcel object, PPBinder parses it into a call request and calls the corresponding service. Therefore, it is necessary to ensure that the call request and the processing result can be correctly encapsulated into a Parcel object, and that the Parcel object can be correctly transmitted between the client and the server.
  5. In the process of calling BpBinder and PPBinder, you need to pay attention to handling abnormal situations. For example, when the server cannot handle the client's call request, it needs to throw a corresponding exception and return it to the client.

The sample code is as follows:

Server code:

public class MyService extends Service {
    private final Binder mBinder = new MyBinder();
​
    private class MyBinder extends Binder {
        @Override
        protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
            switch (code) {
                case 1: // 调用请求的标识符                    int arg1 = data.readInt(); // 解析调用请求的参数                    int result = processRequest(arg1); // 处理调用请求                    reply.writeInt(result); // 将处理结果封装成Parcel对象返回给客户端                    return true;
                default:
                    return super.onTransact(code, data, reply, flags);
      }
    }
  }private int processRequest(int arg1) {
    // 处理调用请求的逻辑 return arg1 * 2;
  }@Nullable
  @Override
  public IBinder onBind(Intent intent) {
    return mBinder;
  }
}
​
客户端代码:
```javapublic class MyClientActivity extends Activity {
    private IService mService;
​
    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            mService = IService.Stub.asInterface(iBinder);
        }
​
        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mService = null;
        }
    };
​
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
​
        Intent intent = new Intent(this, MyService.class);
        bindService(intent, mConnection, BIND_AUTO_CREATE);
​
        // 调用服务端的方法
        try {
            int result = mService.processRequest(10);
            Log.d(TAG, "Result: " + result);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
​
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mConnection);
    }
}

This article mainly analyzes the important technical points in the Binder IPC communication developed by Android, the analysis of the calling process of BpBinder and PPBinder, and more about Android development technology. For an in-depth understanding of Binder technology, please refer to the "Binder Manual" to view the detailed categories .

Precautions:

  1. In the MyBinder class on the server side, the onTransact() method needs to be rewritten, and corresponding processing should be performed according to the received call request. In the sample code, we use the code parameter to determine the identifier of the call request, then parse the call request parameters, and call the processRequest() method to process the request.
  2. In the ServiceConnection of the client, we convert the IBinder object to an IService object through the asInterface() method, so that we can call the method of the server.
  3. In the onCreate() method of the client, we first bind the Service of the server, then call the processRequest() method of the server through the mService object, and print the processing result.

Summarize:

This article introduces the principle of Binder IPC communication technology, and analyzes the use and precautions of the BpBinder and PPBinder calling process in detail. By using BpBinder and PPBinder, we can achieve cross-process communication, and can easily call the method of the server. During use, you need to pay attention to details such as registering Binder objects, passing parameters, and handling exceptions. Binder IPC communication technology is a very important inter-process communication technology in the Android system, which is of great significance for realizing cross-process communication and improving system performance.

Guess you like

Origin blog.csdn.net/m0_70748845/article/details/132171815