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


    继承关系:

      

    文件关系

    IHelloService.h

     1 /* 参考: frameworksavincludemediaIMediaPlayerService.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 /* 参考: frameworksavmedialibmediaIMediaPlayerService.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
    ", cnt++);
    38 }
    39 
    40 int BnHelloService::sayhello_to(const char *name)
    41 {
    42     static int cnt = 0;
    43     printf("say hello to %s : %d
    ", name, cnt++);
    44     return cnt;
    45 }
    46 }
    View Code

    BpHelloService.cpp

     1 /* 参考: frameworksavmedialibmediaIMediaPlayerService.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 /* 参考: frameworksavmediamediaserverMain_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:
    ");
    23         printf("%s hello
    ", argv[0]);
    24         printf("%s hello <name>
    ", 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
    ");
    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

  • 相关阅读:
    HDU 5409 CRB and Graph (边双连通+DFS)
    HDU 3749 Financial Crisis (点双连通+并查集)
    POJ 1523 SPF (无向图割点)
    HDU 3639 Hawk-and-Chicken (强连通缩点+DFS)
    UVA11324 The Largest Clique (强连通缩点+DP最长路)
    HDU 3861 The King’s Problem (强连通缩点+DAG最小路径覆盖)
    ZOJ 3795 Grouping (强连通缩点+DP最长路)
    POJ 2455 Secret Milking Machine 【二分】+【最大流】
    POJ 2112 Optimal Milking (二分+最短路+最大流)
    POJ 1094 Sorting It All Out 【拓扑排序】
  • 原文地址:https://www.cnblogs.com/winfu/p/7513659.html
Copyright © 2020-2023  润新知