純C++binder服務和客戶端實例

繼承關係:android

  

文件關係函數

IHelloService.hui

複製代碼
 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
複製代碼

BnHelloService.cppspa

複製代碼
 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 }
複製代碼

BpHelloService.cpp3d

複製代碼
 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 }
複製代碼

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 }
複製代碼

test_client.cppcode

複製代碼
 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 }
複製代碼

Andriod.mkserver

複製代碼
 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)
複製代碼

相關文章
相關標籤/搜索