下面创建了一个名称为"test.binderLib.0921“的 service,并通过defaultServiceManager增加到系统的服务中。我们这个服务的功能是读取客户发过来的2个整型值然后计算出和后返回给客户端。
#include <ui/DisplayConfig.h>
#include <gui/IRegionSamplingListener.h>
#include <gui/ISurfaceComposer.h>
#include <gui/SurfaceControl.h>
#include <gui/Surface.h>
#include <private/gui/ComposerService.h>
#include <gui/SurfaceComposerClient.h>
#include <android/native_window.h>
#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 <private/binder/binder_module.h>
#include <sys/epoll.h>
#include <sys/prctl.h>
using namespace android;
using namespace std;
enum BinderLibTestTranscationCode {
BINDER_LIB_TEST_SUM_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
};
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);
(void) flags;
if (getuid() != (uid_t) IPCThreadState::self()->getCallingUid()) {
return PERMISSION_DENIED;
}
switch (code) {
case BINDER_LIB_TEST_SUM_TRANSACTION:
cout
<< "BinderLibTestService onTransact BINDER_LIB_TEST_NOP_TRANSACTION"
<< endl;
int32_t a,b;
a = data.readInt32();
b = data.readInt32();
cout<<"a : " <<a<< endl ;
cout<<"b : " <<b<< endl ;
reply->writeInt32(a+b);
return NO_ERROR;
default:
return UNKNOWN_TRANSACTION;
};
}
};
static String16 binderLibTestServiceName = String16("test.binderLib.0921");
int main(int /*argc*/, char** /*argv*/) {
sp < IBinder > m_server;
status_t ret;
sp < IServiceManager > sm = defaultServiceManager();
ProcessState::self()->startThreadPool();
sp<BinderLibTestService> testService = new BinderLibTestService(0);
ret = sm->addService(binderLibTestServiceName, testService);
// m_server = sm->getService(binderLibTestServiceName);
//
// Parcel data, reply;
//
// data.writeInt32(40);
// data.writeInt32(50);
// ret = m_server->transact(BINDER_LIB_TEST_SUM_TRANSACTION, data, &reply);
//
// cout << ret << endl;
// cout << reply.readInt32() << endl;
printf("end\n");
IPCThreadState::self()->joinThreadPool();
return 0;
}
下面是另外的独立的一个应用程序作为客户端,,这个客户端从系统的defaultServiceManager中获取名称为"test.binderLib.0921“的服务,然后发送2个整型,并获取返回结果。
#include <ui/DisplayConfig.h>
#include <gui/IRegionSamplingListener.h>
#include <gui/ISurfaceComposer.h>
#include <gui/SurfaceControl.h>
#include <gui/Surface.h>
#include <private/gui/ComposerService.h>
#include <gui/SurfaceComposerClient.h>
#include <android/native_window.h>
#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 <private/binder/binder_module.h>
#include <sys/epoll.h>
#include <sys/prctl.h>
using namespace android;
using namespace std;
enum BinderLibTestTranscationCode {
BINDER_LIB_TEST_SUM_TRANSACTION = IBinder::FIRST_CALL_TRANSACTION,
};
static String16 binderLibTestServiceName = String16("test.binderLib.0921");
int main(int /*argc*/, char** /*argv*/) {
sp < IBinder > m_server;
status_t ret;
sp <IServiceManager> sm = defaultServiceManager();
ProcessState::self()->startThreadPool();
m_server = sm->getService(binderLibTestServiceName);
Parcel data, reply;
data.writeInt32(40);
data.writeInt32(50);
ret = m_server->transact(BINDER_LIB_TEST_SUM_TRANSACTION, data, &reply);
cout << ret << endl;
cout << reply.readInt32() << endl;
printf("end\n");
IPCThreadState::self()->joinThreadPool();
return 0;
}
运行之后会打印90,说明是成功的.