纯C++binder服务和客户端实例

继承关系:

  

文件关系

IHelloService.h

 1 /* 参考: frameworks\av\include\media\IMediaPlayerService.h */
 2 
 3 #ifndef ANDROID_IHELLOERVICE_H
 4 #define ANDROID_IHELLOERVICE_H
 5 
 6 #include <utils/Errors.h>  // for status_t
 7 #include <utils/KeyedVector.h>
 8 #include <utils/RefBase.h>
 9 #include <utils/String8.h>
10 #include <binder/IInterface.h>
11 #include <binder/Parcel.h>
12 
13 #define HELLO_SVR_CMD_SAYHELLO     0
14 #define HELLO_SVR_CMD_SAYHELLO_TO  1
15 
16 namespace android {
17 
18 class IHelloService: public IInterface
19 {
20 public:
21     DECLARE_META_INTERFACE(HelloService);
22     virtual void sayhello(void) = 0;
23     virtual int sayhello_to(const char *name) = 0;
24 };
25 
26 class BnHelloService: public BnInterface<IHelloService>
27 {
28 public:
29     virtual status_t    onTransact( uint32_t code,
30                                     const Parcel& data,
31                                     Parcel* reply,
32                                     uint32_t flags = 0);
33 
34     virtual void sayhello(void);
35     virtual int sayhello_to(const char *name);
36 };
37 }
38 
39 #endif
View Code

BnHelloService.cpp

 1 /* 参考: frameworks\av\media\libmedia\IMediaPlayerService.cpp */
 2 
 3 #define LOG_TAG "HelloService"
 4 #include "IHelloService.h"
 5 
 6 namespace android {
 7 
 8 status_t BnHelloService::onTransact( uint32_t code, const Parcel& data,
 9                                 Parcel* reply, uint32_t flags)
10 {
11     /* 解析数据,调用sayhello/sayhello_to */
12 
13     switch (code) {
14         case HELLO_SVR_CMD_SAYHELLO: {
15             sayhello();
16             return NO_ERROR;
17         } break;
18         
19         case HELLO_SVR_CMD_SAYHELLO_TO: {
20             /* 从data中取出参数 */
21             int32_t policy =  data.readInt32(); //多余的0,客户端有多发送一个0
22             String16 name16 = data.readString16(); //获取客户端发来的name
23             String8 name8(name16); //讲16位字符传化位8位的字符
24             int cnt = sayhello_to(name8.string()); //const char*
25             /* 把返回值写入reply传回去 */
26             reply->writeInt32(cnt);
27             return NO_ERROR;
28         } break;
29         default:
30             return BBinder::onTransact(code, data, reply, flags);
31     }
32 }
33 
34 void BnHelloService::sayhello(void)
35 {
36     static int cnt = 0;
37     printf("say hello : %d\n", cnt++);
38 }
39 
40 int BnHelloService::sayhello_to(const char *name)
41 {
42     static int cnt = 0;
43     printf("say hello to %s : %d\n", name, cnt++);
44     return cnt;
45 }
46 }
View Code

BpHelloService.cpp

 1 /* 参考: frameworks\av\media\libmedia\IMediaPlayerService.cpp */
 2 #include "IHelloService.h"
 3 namespace android {
 4 
 5 class BpHelloService: public BpInterface<IHelloService>
 6 {
 7 public:
 8     BpHelloService(const sp<IBinder>& impl)
 9         : BpInterface<IHelloService>(impl)
10     {
11     }
12 
13     void sayhello(void)
14     {
15         /* 构造/发送数据 */
16         Parcel data, reply;
17         data.writeInt32(0);
18         remote()->transact(HELLO_SVR_CMD_SAYHELLO, data, &reply);
19     }
20     
21     int sayhello_to(const char *name)
22     {
23         /* 构造/发送数据 */
24         Parcel data, reply;
25 
26         data.writeInt32(0); //多发一个0
27         data.writeString16(String16(name));
28 
29         remote()->transact(HELLO_SVR_CMD_SAYHELLO_TO, data, &reply);
30 
31         return reply.readInt32();
32     }
33 
34 };
35 
36 IMPLEMENT_META_INTERFACE(HelloService, "android.media.IHelloService");
37 
38 }
View Code

test_service.cpp

 1 /* 参考: frameworks\av\media\mediaserver\Main_mediaserver.cpp */
 2 
 3 #define LOG_TAG "HelloService"
 4 //#define LOG_NDEBUG 0
 5 
 6 #include <fcntl.h>
 7 #include <sys/prctl.h>
 8 #include <sys/wait.h>
 9 #include <binder/IPCThreadState.h>
10 #include <binder/ProcessState.h>
11 #include <binder/IServiceManager.h>
12 #include <cutils/properties.h>
13 #include <utils/Log.h>
14 #include "IHelloService.h"
15 using namespace android;
16 
17 int main(void)
18 {
19     /* addService */
20 
21     /* while(1){ read data, 解析数据, 调用服务函数 } */
22 
23     /* 打开驱动, mmap */
24     sp<ProcessState> proc(ProcessState::self());
25 
26     /* 获得BpServiceManager */
27     sp<IServiceManager> sm = defaultServiceManager();
28 
29     sm->addService(String16("hello"), new BnHelloService());
30 
31     /* 循环体 */
32     ProcessState::self()->startThreadPool();
33     IPCThreadState::self()->joinThreadPool();
34 
35     return 0;
36 }
View Code

test_client.cpp

 1 #define LOG_TAG "HelloService"
 2 #include <fcntl.h>
 3 #include <sys/prctl.h>
 4 #include <sys/wait.h>
 5 #include <binder/IPCThreadState.h>
 6 #include <binder/ProcessState.h>
 7 #include <binder/IServiceManager.h>
 8 #include <cutils/properties.h>
 9 #include <utils/Log.h>
10 #include "IHelloService.h"
11 
12 using namespace android;
13 
14 /* ./test_client hello
15  * ./test_client hello <name>
16  */
17 int main(int argc, char **argv)
18 {
19     int cnt;
20     
21     if (argc < 2){
22         printf("Usage:\n");
23         printf("%s hello\n", argv[0]);
24         printf("%s hello <name>\n", argv[0]);
25         return -1;
26     }
27 
28     /* getService */
29     /* 打开驱动, mmap */
30     sp<ProcessState> proc(ProcessState::self());
31 
32     /* 获得BpServiceManager */
33     sp<IServiceManager> sm = defaultServiceManager();
34     //获取hello服务
35     sp<IBinder> binder = sm->getService(String16("hello"));
36 
37     if (binder == 0)
38     {
39         printf("can't get hello service\n");
40         return -1;
41     }
42 
43     /* service肯定是BpHelloServie指针 */
44     sp<IHelloService> service = interface_cast<IHelloService>(binder);
45 
46     /* 调用Service的函数 */
47     if (argc < 3) {
48         service->sayhello();
49         printf("client call sayhello");
50     }
51     else {
52         cnt = service->sayhello_to(argv[2]);
53         printf("client call sayhello_to, cnt = %d", cnt);
54     }
55     
56     return 0;
57 }
View Code

Andriod.mk

 1 LOCAL_PATH:= $(call my-dir)
 2 
 3 include $(CLEAR_VARS)
 4 
 5 LOCAL_SRC_FILES:= \
 6     BnHelloService.cpp \
 7     BpHelloService.cpp \
 8     test_server.cpp
 9 
10 LOCAL_SHARED_LIBRARIES := \
11     libcutils \
12     libutils \
13     liblog \
14     libbinder 
15 
16 LOCAL_MODULE:= test_server
17 LOCAL_32_BIT_ONLY := true
18 
19 include $(BUILD_EXECUTABLE)
20 
21 include $(CLEAR_VARS)
22 
23 LOCAL_SRC_FILES:= \
24     BpHelloService.cpp \
25     test_client.cpp
26 
27 LOCAL_SHARED_LIBRARIES := \
28     libcutils \
29     libutils \
30     liblog \
31     libbinder 
32     
33 LOCAL_MODULE:= test_client
34 LOCAL_32_BIT_ONLY := true
35 
36 include $(BUILD_EXECUTABLE)
View Code

 

转载于:https://www.cnblogs.com/winfu/p/7513659.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值