android11 binder服务callback使用说明
android11 binder服务callback使用说明
·
binder中的callback可以用在客户端请求服务one way的情况下,服务器处理完毕之后主动通知客户端处理结果,对客户端来说,就是回调。
以下是演示代码。首先是服务端代码,在服务端处理完毕之后,会通过readStrongBinder方法获取到一个IBinder对象,然后设置一个8888的处理码。
#include <utils/Trace.h>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>
#include <iostream>
#include <stdint.h>
#include <sys/types.h>
#include <set>
#include <thread>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <inttypes.h>
#include <cutils/properties.h>
#include <utils/Log.h>
#include <utils/SystemClock.h>
#include <android-base/properties.h>
#include <errno.h>
#include <fcntl.h>
#include <fstream>
#include <poll.h>
#include <pthread.h>
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/Binder.h>
#include <binder/IBinder.h>
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <binder/IServiceManager.h>
#include <sys/wait.h>
#include <private/binder/binder_module.h>
#include <sys/epoll.h>
#include <sys/prctl.h>
using namespace android;
using namespace std;
enum BinderLibTestTranscationCode {
BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
BINDER_LIB_TEST_REGISTER_SERVER,
BINDER_LIB_TEST_ADD_SERVER,
BINDER_LIB_TEST_ADD_POLL_SERVER,
BINDER_LIB_TEST_CALL_BACK,
BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
BINDER_LIB_TEST_DELAYED_CALL_BACK,
BINDER_LIB_TEST_NOP_CALL_BACK,
BINDER_LIB_TEST_GET_SELF_TRANSACTION,
BINDER_LIB_TEST_GET_ID_TRANSACTION,
BINDER_LIB_TEST_INDIRECT_TRANSACTION,
BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
BINDER_LIB_TEST_EXIT_TRANSACTION,
BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
BINDER_LIB_TEST_GET_SCHEDULING_POLICY,
BINDER_LIB_TEST_NOP_TRANSACTION_WAIT,
BINDER_LIB_TEST_GETPID,
BINDER_LIB_TEST_ECHO_VECTOR,
BINDER_LIB_TEST_REJECT_BUF,
};
static String16 binderLibTestServiceName = String16("test.binderLib.0921");
class BinderLibTestService: public BBinder {
public:
explicit BinderLibTestService(int32_t id)
{
}
~BinderLibTestService() {
exit(EXIT_SUCCESS);
}
virtual status_t onTransact(uint32_t code, const Parcel &data,
Parcel *reply, uint32_t flags = 0) override {
printf("%s: code %d\n", __func__, code);
switch (code) {
case BINDER_LIB_TEST_NOP_CALL_BACK: {
Parcel data2, reply2;
sp<IBinder> binder;
binder = data.readStrongBinder();
if (binder == nullptr) {
return BAD_VALUE;
}
data2.writeInt32(8888);
binder->transact(BINDER_LIB_TEST_CALL_BACK, data2, &reply2);
return NO_ERROR;
}
default:
return UNKNOWN_TRANSACTION;
};
}
private:
};
int main(int argc, char **argv) {
sp<IBinder> m_server;
ProcessState::self()->startThreadPool();
printf("10\n");
status_t ret;
sp<IServiceManager> sm = defaultServiceManager();
BinderLibTestService *testServicePtr;
int index = 0;
sp<BinderLibTestService> testService = new BinderLibTestService(index);
printf("addService\n");
ret = sm->addService(binderLibTestServiceName, testService);
printf("end\n");
IPCThreadState::self()->joinThreadPool();
return 0;
}
下面是客户端的代码:,客户端获取到服务的BInder对象之后,调用 writeStrongBinder注册本地的一个回调。
#include <utils/Trace.h>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>
#include <iostream>
#include <stdint.h>
#include <sys/types.h>
#include <set>
#include <thread>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <inttypes.h>
#include <cutils/properties.h>
#include <utils/Log.h>
#include <utils/SystemClock.h>
#include <android-base/properties.h>
#include <errno.h>
#include <fcntl.h>
#include <fstream>
#include <poll.h>
#include <pthread.h>
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/Binder.h>
#include <binder/IBinder.h>
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <binder/IServiceManager.h>
#include <sys/wait.h>
#include <private/binder/binder_module.h>
#include <sys/epoll.h>
#include <sys/prctl.h>
using namespace android;
using namespace std;
enum BinderLibTestTranscationCode {
BINDER_LIB_TEST_NOP_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
BINDER_LIB_TEST_REGISTER_SERVER,
BINDER_LIB_TEST_ADD_SERVER,
BINDER_LIB_TEST_ADD_POLL_SERVER,
BINDER_LIB_TEST_CALL_BACK,
BINDER_LIB_TEST_CALL_BACK_VERIFY_BUF,
BINDER_LIB_TEST_DELAYED_CALL_BACK,
BINDER_LIB_TEST_NOP_CALL_BACK,
BINDER_LIB_TEST_GET_SELF_TRANSACTION,
BINDER_LIB_TEST_GET_ID_TRANSACTION,
BINDER_LIB_TEST_INDIRECT_TRANSACTION,
BINDER_LIB_TEST_SET_ERROR_TRANSACTION,
BINDER_LIB_TEST_GET_STATUS_TRANSACTION,
BINDER_LIB_TEST_ADD_STRONG_REF_TRANSACTION,
BINDER_LIB_TEST_LINK_DEATH_TRANSACTION,
BINDER_LIB_TEST_WRITE_FILE_TRANSACTION,
BINDER_LIB_TEST_WRITE_PARCEL_FILE_DESCRIPTOR_TRANSACTION,
BINDER_LIB_TEST_EXIT_TRANSACTION,
BINDER_LIB_TEST_DELAYED_EXIT_TRANSACTION,
BINDER_LIB_TEST_GET_PTR_SIZE_TRANSACTION,
BINDER_LIB_TEST_CREATE_BINDER_TRANSACTION,
BINDER_LIB_TEST_GET_WORK_SOURCE_TRANSACTION,
BINDER_LIB_TEST_GET_SCHEDULING_POLICY,
BINDER_LIB_TEST_NOP_TRANSACTION_WAIT,
BINDER_LIB_TEST_GETPID,
BINDER_LIB_TEST_ECHO_VECTOR,
BINDER_LIB_TEST_REJECT_BUF,
};
static String16 binderLibTestServiceName = String16("test.binderLib.0921");
class BinderLibTestCallBack: public BBinder {
public:
BinderLibTestCallBack() :
m_result(0) {
}
status_t getResult(void) {
return m_result;
}
private:
virtual status_t onTransact(uint32_t code, const Parcel &data,
Parcel *reply, uint32_t flags = 0) {
(void) reply;
(void) flags;
switch (code) {
case BINDER_LIB_TEST_CALL_BACK: {
status_t status = data.readInt32();
if (status != NO_ERROR) {
m_result = status;
}
printf("cb: callback called\n");
std::cout << "cb: " << m_result << std::endl;
// triggerEvent();
return NO_ERROR;
}
default:
return UNKNOWN_TRANSACTION;
}
}
status_t m_result;
const uint8_t *m_prev_end;
};
int main(int argc, char **argv) {
sp < IBinder > m_server;
ProcessState::self()->startThreadPool();
printf("10\n");
status_t ret;
sp < IServiceManager > sm = defaultServiceManager();
m_server = sm->getService(binderLibTestServiceName);
Parcel data, reply;
sp<BinderLibTestCallBack> callBack = new BinderLibTestCallBack();
data.writeStrongBinder(callBack);
ret = m_server->transact(BINDER_LIB_TEST_NOP_CALL_BACK, data, &reply,
TF_ONE_WAY);
std::cout << ret << std::endl;
printf("end\n");
IPCThreadState::self()->joinThreadPool();
return 0;
}
编译运行,先运行服务端,再运行客户端,如果打印8888,则是成功的。
更多推荐
已为社区贡献49条内容
所有评论(0)