AndroidBinder6—Native应用—双向通信

mkdir testbinder  //创建testbinder目录

Android.mk

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. include $(call all-subdir-makefiles)   

一、接口类

1.正向调用—Itestbinder

Itestbinder.h

  1. #ifndef Itestbinder_H    
  2. #define Itestbinder_H   
  3. #include <binder/IInterface.h>  
  4. #include "Icallback.h"  
  5. namespace android{  
  6.   class Itestbinder : public IInterface{  
  7.     public:  
  8.       DECLARE_META_INTERFACE(testbinder);  
  9.       virtual int testinterface(int a) = 0;  
  10.       virtual int setcallback(const sp<Icallback>& callback) = 0;  
  11.   };  
  12.   class Bntestbinder : public BnInterface<Itestbinder>{  
  13.   public:  
  14.     virtual status_t    onTransact( uint32_t code,  
  15.                                     const Parcel& data,  
  16.                                     Parcel* reply,  
  17.                                     uint32_t flags = 0);  
  18.   };  
  19. }  
  20. #endif  

Itestbinder.cpp

  1. #include "Itestbinder.h"  
  2. #include <binder/Parcel.h>  
  3. #include <binder/IInterface.h>  
  4. #include "Icallback.h"  
  5. namespace android{  
  6.   enum {  
  7.     TEST_INTERFACE,  
  8.     SET_CALLBACK  
  9.   };  
  10. //////////////////客户端  
  11.   class Bptestbinder : public BpInterface<Itestbinder>{  
  12.     public:  
  13.       Bptestbinder(const sp<IBinder>& impl) : BpInterface<Itestbinder>(impl){  
  14.       }  
  15.       virtual int testinterface(int a){  
  16.         LOGD("==========================================================\n");  
  17.         LOGD("TK---->>>>>>Itestbinder.cpp>>>>Bptestbinder::testinterface\n");  
  18.         Parcel data,reply;  
  19.         data.writeInt32(a);  
  20.         remote()->transact(TEST_INTERFACE,data,&reply);  
  21.         return reply.readInt32();  
  22.       }  
  23.       virtual int setcallback(const sp<Icallback>& callback){  
  24.         LOGD("==========================================================\n");  
  25.         LOGD("TK---->>>>>>Itestbinder.cpp>>>>Bptestbinder::setcallback\n");  
  26.         Parcel data, reply;  
  27.         data.writeStrongBinder(callback->asBinder());  
  28.         remote()->transact(SET_CALLBACK, data, &reply);  
  29.         return reply.readInt32();  
  30.       }  
  31.   };  
  32.   
  33.   IMPLEMENT_META_INTERFACE(testbinder, "android.test.Itestbinder");  
  34. /////////////////服务端  
  35.   status_t Bntestbinder::onTransact(  
  36.       uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags){  
  37.     LOGD("TK---->>>>>>Itestbinder.cpp>>>>Bntestbinder::onTransact\n");  
  38.     switch (code) {  
  39.       case TEST_INTERFACE:{  
  40.         //CHECK_INTERFACE(Itestbinder, data, reply);  
  41.         LOGD("TK---->>>>>>Itestbinder.cpp>>>>Bntestbinder::onTransact>>TEST_INTERFACE\n");  
  42.         reply->writeInt32(testinterface((int) data.readInt32()) );  
  43.         return NO_ERROR;  
  44.       } break;  
  45.       case SET_CALLBACK:{  
  46.         LOGD("TK---->>>>>>Itestbinder.cpp>>>>Bntestbinder::onTransact>>SET_CALLBACK\n");  
  47.         sp<Icallback> callback = interface_cast<Icallback>(data.readStrongBinder());  
  48.         //int a = connect(Client);  
  49.         reply->writeInt32(setcallback(callback));      
  50.         return NO_ERROR;      
  51.       }  
  52.       default:{  
  53.         LOGD("TK---->>>>>>Itestbinder.cpp>>>>Bntestbinder::onTransact>>333\n");  
  54.         return BBinder::onTransact(code, data, reply, flags);  
  55.       }  
  56.     }  
  57.   }  
  58. }  

2.反向调用—Icallback

Icallback.h

  1. #ifndef Icallback_H    
  2. #define Icallback_H   
  3. #include <binder/IInterface.h>  
  4. namespace android{  
  5.   class Icallback : public IInterface{  
  6.     public:  
  7.       DECLARE_META_INTERFACE(callback);  
  8.       virtual int notifyCallback(int a) = 0;  
  9.   };  
  10.   class Bncallback : public BnInterface<Icallback>{  
  11.   public:  
  12.     virtual status_t    onTransact( uint32_t code,  
  13.                                     const Parcel& data,  
  14.                                     Parcel* reply,  
  15.                                     uint32_t flags = 0);  
  16.   };  
  17. }  
  18. #endif  

Icallback.cpp

  1. #include "Itestbinder.h"  
  2. #include <binder/Parcel.h>  
  3. #include <binder/IInterface.h>  
  4. namespace android{  
  5.   enum {  
  6.     NOTIFY_CALLBACK,  
  7.   };  
  8. //////////////////客户端  
  9.   class Bpcallback : public BpInterface<Icallback>{  
  10.     public:  
  11.       Bpcallback(const sp<IBinder>& impl) : BpInterface<Icallback>(impl){  
  12.       }  
  13.       virtual int notifyCallback(int a){  
  14.         LOGD("==========================================================\n");  
  15.         LOGD("TK---->>>>>>Icallback.cpp>>>>Bpcallback::notifyCallback\n");  
  16.         Parcel data,reply;  
  17.         data.writeInt32(a);  
  18.         remote()->transact(NOTIFY_CALLBACK,data,&reply);  
  19.         return reply.readInt32();  
  20.       }  
  21.   };  
  22.   
  23.   IMPLEMENT_META_INTERFACE(callback, "android.test.Icallback");  
  24. /////////////////服务端  
  25.   status_t Bncallback::onTransact(  
  26.       uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags){  
  27.     LOGD("TK---->>>>>>Icallback.cpp>>>>Bncallback::onTransact\n");  
  28.     switch (code) {  
  29.       case NOTIFY_CALLBACK:{  
  30.         //CHECK_INTERFACE(Itestbinder, data, reply);  
  31.         LOGD("TK---->>>>>>Icallback.cpp>>>>Bncallback::onTransact>>NOTIFY_CALLBACK\n");  
  32.         reply->writeInt32(notifyCallback((int) data.readInt32()) );  
  33.         return NO_ERROR;  
  34.       } break;  
  35.       default:{  
  36.         LOGD("TK---->>>>>>Icallback.cpp>>>>Bncallback::onTransact>>222\n");  
  37.         return BBinder::onTransact(code, data, reply, flags);  
  38.       }  
  39.     }  
  40.   }  
  41. }  

二、服务端

mkdir server  //创建server目录,这个是服务端实现

1.Bntestbinder实现

testbinder.h

  1. #include "../interface/Itestbinder.h"  
  2. #include "../interface/Icallback.h"  
  3. #include <binder/BinderService.h>  
  4. namespace android{  
  5.   class testbinder:   
  6.       public BinderService<testbinder>,  
  7.       public Bntestbinder{  
  8.     friend class BinderService<testbinder>;  
  9.     public:  
  10.       static const char* getServiceName() { return "test.Itestbinder"; }  
  11.       virtual int testinterface(int a);  
  12.       virtual int setcallback(const sp<Icallback>& callback);  
  13.       virtual     status_t    onTransact(  
  14.                                 uint32_t code,  
  15.                                 const Parcel& data,  
  16.                                 Parcel* reply,  
  17.                                 uint32_t flags);  
  18.     protected:  
  19.       sp<Icallback> mcallback;  
  20.   };  
  21. }  

testbinder.cpp

  1. #include <binder/IPCThreadState.h>  
  2. #include <binder/IServiceManager.h>  
  3. #include <utils/Log.h>  
  4. //#include <utils/Trace.h>  
  5. #include <binder/Parcel.h>  
  6. #include <binder/IPCThreadState.h>  
  7. #include <utils/String16.h>  
  8. #include <utils/threads.h>  
  9. #include <utils/Atomic.h>  
  10.   
  11. //#include <cutils/bitops.h>  
  12. #include <cutils/properties.h>  
  13. #include <cutils/compiler.h>  
  14. #include "testbinder.h"  
  15.   
  16. namespace android{  
  17.   int testbinder::testinterface(int a){  
  18.     LOGD("TK---->>>>>>testbinder.cpp>>>>testbinder::testinterface\n");  
  19.     sp<Icallback> c = mcallback;  
  20.     c->notifyCallback(2);  
  21.     return a+2;  
  22.   }  
  23.   int testbinder::setcallback(const sp<Icallback>& callback){  
  24.     LOGD("TK---->>>>>>testbinder.cpp>>>>testbinder::setcallback\n");  
  25.     mcallback = callback;  
  26.     return 1;  
  27.   }  
  28.   status_t testbinder::onTransact(  
  29.         uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags){  
  30.     LOGD("TK---->>>>>>testbinder.cpp>>>>testbinder::onTransact\n");  
  31.     return Bntestbinder::onTransact(code, data, reply, flags);  
  32.   }  
  33. }  

2.向ServiceManager注册testbinder服务

main.cpp

  1. #include <binder/IPCThreadState.h>  
  2. #include <binder/ProcessState.h>  
  3. #include <binder/IServiceManager.h>  
  4. #include <utils/Log.h>  
  5.   
  6. #include "testbinder.h"  
  7.   
  8.   
  9. using namespace android;  
  10.   
  11. int main(int argc, char** argv)  
  12. {  
  13.   sp<ProcessState> proc(ProcessState::self());  
  14.   sp<IServiceManager> sm = defaultServiceManager();  
  15.   LOGI("ServiceManager: %p", sm.get());  
  16.   testbinder::instantiate();  
  17.   ProcessState::self()->startThreadPool();  
  18.   IPCThreadState::self()->joinThreadPool();  
  19.   return 0;  
  20. }  

Android.mk

  1. LOCAL_PATH:= $(call my-dir)  
  2. #LOCAL_CFLAGS_ALL :=-I. -I$(LOCAL_PATH)/..  
  3.   
  4. include $(CLEAR_VARS)  
  5.   
  6. LOCAL_SRC_FILES:= \  
  7.     main.cpp \  
  8.     testbinder.cpp \  
  9.     ../interface/Itestbinder.cpp \  
  10.     ../interface/Icallback.cpp  
  11.   
  12. LOCAL_SHARED_LIBRARIES := \  
  13.         libui libcutils libutils libbinder libsonivox libicuuc libexpat \  
  14.     libdl  
  15.   
  16.   
  17. LOCAL_MODULE:= server  
  18. LOCAL_MODULE_TAGS := optional  

三、客户端

1.通过ServiceManager获得Itestbinder远程接口

mkdir client  //创建client目录,这个是client的实现

client.h

  1. #include "../interface/Itestbinder.h"  
  2.   
  3. namespace android{  
  4.   class client{  
  5.     public:  
  6.       static const sp<Itestbinder>& get_test_binder();  
  7.       static sp<Itestbinder> gtestbinder;  
  8.   };  
  9. }  

client.cpp

  1. #include "client.h"  
  2. #include <binder/IServiceManager.h>  
  3. #include <utils/Log.h>  
  4. #include <stdio.h>  
  5. namespace android{  
  6.   sp<Itestbinder> client::gtestbinder;  
  7.   const sp<Itestbinder>& client::get_test_binder(){  
  8.     if (gtestbinder == 0) {  
  9.         sp<IServiceManager> sm = defaultServiceManager();  
  10.         sp<IBinder> binder;  
  11.         do {  
  12.             binder = sm->getService(String16("test.Itestbinder"));  
  13.             if (binder != 0)  
  14.                 break;  
  15.             printf("testbinder not published, waiting...");  
  16.             usleep(500000); // 0.5 s  
  17.         } while (true);  
  18.         gtestbinder = interface_cast<Itestbinder>(binder);  
  19.     }  
  20.     if(gtestbinder==0) printf("no testbinder!?");  
  21.     return gtestbinder;  
  22.   }  
  23. }  

2.Bncallback的实现,反向调用

callback.h

  1. #include "../interface/Itestbinder.h"  
  2. #include "../interface/Icallback.h"  
  3. #include <binder/BinderService.h>  
  4. namespace android{  
  5.   class callback: public Bncallback{  
  6.     friend class BinderService<callback>;  
  7.     public:  
  8.       virtual int notifyCallback(int a);  
  9.       virtual     status_t    onTransact(  
  10.                                 uint32_t code,  
  11.                                 const Parcel& data,  
  12.                                 Parcel* reply,  
  13.                                 uint32_t flags);  
  14.   };  
  15. }  

callback.cpp

  1. #include <binder/Parcel.h>  
  2. #include <binder/IPCThreadState.h>  
  3. #include <utils/String16.h>  
  4. #include <utils/threads.h>  
  5. #include <utils/Atomic.h>  
  6.   
  7. //#include <cutils/bitops.h>  
  8. #include <cutils/properties.h>  
  9. #include <cutils/compiler.h>  
  10. #include "callback.h"  
  11.   
  12. namespace android{  
  13.   int callback::notifyCallback(int a){  
  14.     LOGD("TK---->>>>>>callback.cpp>>>>callback::notifyCallback\n");  
  15.     return 1;  
  16.   }  
  17.   status_t callback::onTransact(  
  18.         uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags){  
  19.     LOGD("TK---->>>>>>callback.cpp>>>>callback::onTransact\n");  
  20.     return Bncallback::onTransact(code, data, reply, flags);  
  21.   }  
  22. }  

3.主函数

main.cpp

  1. #include <stdio.h>  
  2. #include "client.h"  
  3. #include "callback.h"  
  4.   
  5. using namespace android;  
  6.   
  7. int main(int argc, char* argv[]){  
  8.   client* myclient = new client();  
  9.   if(myclient == NULL) return 0;  
  10.   const sp<Itestbinder>& tb = myclient->get_test_binder();  
  11.   if(tb == NULL) return 0;  
  12.   sp<callback> c = new callback();  
  13.   ProcessState::self()->startThreadPool();  //add by tank  
  14.   int a = tb->setcallback(c);  
  15.   a = tb->testinterface(3);  
  16.   LOGD("TK-------->>>result is %d\n",a);  
  17.   delete myclient;  
  18.   return 0;  
  19. }  

Android.mk

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. LOCAL_PATH:= $(call my-dir)  
  2. #LOCAL_CFLAGS_ALL :=-I. -I$(LOCAL_PATH)/..  
  3.   
  4. include $(CLEAR_VARS)  
  5.   
  6. LOCAL_SRC_FILES:= \  
  7.     client.cpp \  
  8.     main.cpp \  
  9.     ../interface/Itestbinder.cpp \  
  10.     callback.cpp \  
  11.     ../interface/Icallback.cpp \  
  12.   
  13. LOCAL_SHARED_LIBRARIES := \  
  14.         libui libcutils libutils libbinder libsonivox libicuuc libexpat \  
  15.     libdl  
  16.   
  17.   
  18. LOCAL_MODULE:= client  
  19. LOCAL_MODULE_TAGS := optional  
  20.   
  21. include $(BUILD_EXECUTABLE)  

四、运行

./server

./client

结果:

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
    1. result:  
    2.   
    3. I/        (  563): ServiceManager: 0xd750  
    4. D/        (  565): ==========================================================  
    5. D/        (  565): TK---->>>>>>Itestbinder.cpp>>>>Bptestbinder::setcallback  
    6. D/        (  563): TK---->>>>>>testbinder.cpp>>>>testbinder::onTransact  
    7. D/        (  563): TK---->>>>>>Itestbinder.cpp>>>>Bntestbinder::onTransact  
    8. D/        (  563): TK---->>>>>>Itestbinder.cpp>>>>Bntestbinder::onTransact>>SET_CALLBACK  
    9. D/        (  563): TK---->>>>>>testbinder.cpp>>>>testbinder::setcallback  
    10. D/        (  565): ==========================================================  
    11. D/        (  565): TK---->>>>>>Itestbinder.cpp>>>>Bptestbinder::testinterface  
    12. D/        (  563): TK---->>>>>>testbinder.cpp>>>>testbinder::onTransact  
    13. D/        (  563): TK---->>>>>>Itestbinder.cpp>>>>Bntestbinder::onTransact  
    14. D/        (  563): TK---->>>>>>Itestbinder.cpp>>>>Bntestbinder::onTransact>>TEST_INTERFACE  
    15. D/        (  563): TK---->>>>>>testbinder.cpp>>>>testbinder::testinterface  
    16. D/        (  563): ==========================================================  
    17. D/        (  563): TK---->>>>>>Icallback.cpp>>>>Bpcallback::notifyCallback  
    18. D/        (  565): TK---->>>>>>callback.cpp>>>>callback::onTransact  
    19. D/        (  565): TK---->>>>>>Icallback.cpp>>>>Bncallback::onTransact  
    20. D/        (  565): TK---->>>>>>Icallback.cpp>>>>Bncallback::onTransact>>NOTIFY_CALLBACK  
    21. D/        (  565): TK---->>>>>>callback.cpp>>>>callback::notifyCallback  
    22. D/        (  565): TK-------->>>result is 5 
posted @ 2016-02-17 14:14  何人之名  阅读(484)  评论(0)    收藏  举报