HarmonyOS IPC Kit Advanced: Basic Communication between Client and Server

RMAG news

This article aims to deeply explore the technical details of the Huawei HarmonyOS Next system (up to API 12 as of now), and is summarized based on actual development practices. It mainly serves as a carrier for technical sharing and communication. Mistakes and omissions are inevitable. Colleagues are welcome to put forward valuable opinions and questions so that we can make progress together. This article is original content, and any form of reprint must indicate the source and the original author.
This article will deeply explore how to use the IPC Kit to achieve basic communication between the client (Client) and the server (Server), and analyze the roles of Proxy and Stub in the communication process.

Communication Process between Client and Server

Server Registers Capability: The Server side first needs to register its own capabilities (System Ability) with the System Ability Manager (SAMgr).

Client Obtains Proxy: The Client side obtains the Proxy object of the corresponding capability from the SAMgr.

Client Sends Request: The Client side sends a request to the Server side through the Proxy object.

Server Processes Request: The Stub object on the Server side receives and processes the request from the Client side.

Server Returns Result: The Server side returns the processing result to the Client side.

Client Receives Result: The Client side receives the result returned by the Server side.
### Roles of Proxy and Stub

Proxy: The proxy object on the Client side is used to forward the Client’s request to the Server side. The Proxy object has the same method interface as the Server side, and the Client side sends requests by calling the methods of the Proxy object.

Stub: The proxy object on the Server side is used to receive the request from the Client side and call the methods of the Server. The Stub object implements the method interface of the Server side and is responsible for handling the request from the Client side.
### Implementation of IPC Client Proxy and Server Stub

Server Side:
    – Create an OHIPCRemoteStub object.
    – Implement the methods of the server side and handle the request from the Client side in the OnRemoteRequest callback function.
    – Register the service with the SAMgr.

Client Side:
    – Obtain the Proxy object of the corresponding capability.
    – Send a request through the Proxy object.
    – Receive the result returned by the Server side.
### Using IPC Kit to Create Remote Proxy and Stub

// Server side
OHIPCRemoteStub *stub = OH_IPCRemoteStub_Create(“com.example.service”, &MyService::OnRemoteRequest, nullptr, this);
OH_IPCRemoteStub_RegisterSystemAbility(stub, MY_SERVICE_ID);
// Client side
OHIPCRemoteProxy *proxy = OH_IPCRemoteProxy_Create(MY_SERVICE_ID, “com.example.service”);

Implementation of Asynchronous Communication Mode

The IPC Kit supports the asynchronous communication mode. The Client side can send a request and return immediately without waiting for the Server side to complete the processing. After the Server side completes the processing, it will return the result to the Client side through a callback function.

// Client side
OH_IPC_MessageOption option = { OH_IPC_REQUEST_MODE_ASYNC, 0 };
OH_IPCRemoteProxy_SendRequest(proxy, MY_METHOD_ID, data, nullptr, &option);
// Server side
int MyService::OnRemoteRequest(uint32_t code, const OHIPCParcel *data, OHIPCParcel *reply, void *userData) {
    // Process the request
    //…
    // Return the result
    OH_IPCRemoteProxy_SendRequest(proxy, MY_METHOD_ID, data, reply, &option);
}

Code Example: Basic Communication Code between IPC Client and Server

// Server side
class MyService : public OHIPCRemoteStub {
public:
    MyService() : OHIPCRemoteStub(“com.example.service”, &MyService::OnRemoteRequest) {}
    ~MyService() override {
        OH_IPCRemoteStub_Destroy(this);
    }
    int Add(int a, int b, int *result) {
        *result = a + b;
        return OH_IPC_SUCCESS;
    }
    static int OnRemoteRequest(uint32_t code, const OHIPCParcel *data, OHIPCParcel *reply, void *userData) {
        MyService *service = reinterpret_cast<MyService *>(userData);
        if (service == nullptr) {
            return OH_IPC_CHECK_PARAM_ERROR;
        }
        int a = 0, b = 0;
        OH_IPCParcel_ReadInt32(data, &a);
        OH_IPCParcel_ReadInt32(data, &b);
        int result = 0;
        service->Add(a, b, &result);
        OH_IPCParcel_WriteInt32(reply, result);
        return OH_IPC_SUCCESS;
    }
private:
    int MY_METHOD_ID = 1;
};
// Client side
OHIPCRemoteProxy *proxy = OH_IPCRemoteProxy_Create(MY_SERVICE_ID, “com.example.service”);
OH_IPC_MessageOption option = { OH_IPC_REQUEST_MODE_SYNC, 0 };
OH_IPCParcel data, reply;
OH_IPCParcel_WriteInt32(&data, 1);
OH_IPCParcel_WriteInt32(&data, 2);
OH_IPCRemoteProxy_SendRequest(proxy, MY_METHOD_ID, &data, &reply, &option);
int result = 0;
OH_IPCParcel_ReadInt32(&reply, &result);
printf(“Result: %dn, result);

Summary

This article introduced in detail how to use the IPC Kit to achieve basic communication between the client and the server, and analyzed the roles of Proxy and Stub in the communication process. By understanding these concepts and code examples, we can easily build our own client – server applications and use the IPC Kit to achieve efficient inter – process communication.

Please follow and like us:
Pin Share