binder通信问题求大神帮忙解答

    xiaoxiao2022-07-03  102

    写了个测试的binder通信的代码,可是运行的时候显示transact函数返回数据错误Not a data message。代码如下

    IServiceTest.h

    ```#ifndef __ISERVICETEST_H__ #define __ISERVICETEST_H__

    #include <binder/IInterface.h> #include <binder/Parcel.h>

    using android::status_t;

    namespace android {

    class IServiceTest:public IInterface{ public:     DECLARE_META_INTERFACE(ServiceTest);     virtual int setTest()= 0;     virtual int getTest()= 0; };

    class BnServiceTest:public BnInterface<IServiceTest>{ public:     virtual  status_t OnTransact(int code,         const Parcel &data,         Parcel *replay,         int flag=0);         };

    };

    #endif

    ```

    IServiceTest.cpp #include <IServiceTest.h> #include <log.h> #include <utils/StrongPointer.h> #include <errno.h>

    namespace android{

    enum{     CODE_SET_TEST = IBinder::FIRST_CALL_TRANSACTION,     CODE_GET_TEST, };

    class BpServiceTest:public BpInterface<IServiceTest>{ public:     BpServiceTest(const sp<IBinder> &impl)         :BpInterface<IServiceTest>(impl)     {     }

        virtual int setTest()     {

            Parcel data,reply;         data.writeInterfaceToken(IServiceTest::getInterfaceDescriptor());         status_t status = remote()->transact(CODE_SET_TEST,data,&reply);         if(status != NO_ERROR){             BINDER_LOG_ERROR("transact error,%d,%s\n",status,strerror(-status));         }         return (int)status;     }

        virtual int getTest()     {         Parcel data,reply;         data.writeInterfaceToken(IServiceTest::getInterfaceDescriptor());         status_t status = remote()->transact(CODE_GET_TEST,data,&reply);         if(status != NO_ERROR){             BINDER_LOG_ERROR("getTest transact error,%d\n",status);         }         return (int)status;     } };

    IMPLEMENT_META_INTERFACE(ServiceTest,"android.test.IServiceTest");

    status_t BnServiceTest::OnTransact(int code,const Parcel & data,Parcel * replay,int flag) {     BINDER_LOG_DEBUG("code =%d\n",code);     switch(code){     case CODE_SET_TEST:         CHECK_INTERFACE(IServiceTest,data,replay)         setTest();         break;     case CODE_GET_TEST:         CHECK_INTERFACE(IServiceTest,data,replay)         getTest();         break;     }     return NO_ERROR; } };

    ```

    ``` Client端 main函数 #include <IServiceTest.h> #include <binder/IServiceManager.h> #include <utils/StrongPointer.h> #include <log.h>

    using namespace android;

    int main(int argc,char **argv) {     sp<IBinder> test = defaultServiceManager()->getService(String16("service.test"));     if(test ==  NULL){             BINDER_LOG_ERROR("can not get service\n");             return -1;     }     sp<IServiceTest> ptest = IServiceTest::asInterface(test);     if(ptest == NULL){         BINDER_LOG_ERROR("can not get proxy class ptest");         return -1;     }     ptest->setTest();     ptest->getTest(); }

    ```

    ```

    Server端 ServiceTest.h #ifndef __SERVICETEST_H__ #define __SERVICETEST_H__

    #include <IServiceTest.h>

    namespace android{

    using android::status_t;

    class ServiceTest:public BnServiceTest{     public:         static void instance();         virtual int setTest();         virtual int getTest();         virtual status_t OnTransact(int code,const Parcel & data,Parcel* replay,int flag = 0); };

    }; #endif

    ```

    ``` ServiceTest.cpp #include <ServiceTest.h> #include <binder/IServiceManager.h> #include <log.h>            #define INTERFACE_DESCRiPTOR "service.test" namespace android{

    void ServiceTest::instance() {     defaultServiceManager()->addService((String16)INTERFACE_DESCRiPTOR,new ServiceTest()); } int ServiceTest::setTest() {     BINDER_LOG_DEBUG("%s\n",__FUNCTION__);     return 0; }

    int ServiceTest::getTest() {     BINDER_LOG_DEBUG("%s\n",__FUNCTION__);     return 0; } status_t ServiceTest::OnTransact(int code,const Parcel & data,Parcel * replay,int flag) {     return BnServiceTest::OnTransact(code,data,replay,flag); }

    };

    main函数 #include <ServiceTest.h> #include <utils/StrongPointer.h> #include <binder/ProcessState.h> #include <binder/IPCThreadState.h>

    using namespace android;

    int main(int argc,char **argv) {     ServiceTest::instance();     ProcessState::self()->startThreadPool();     IPCThreadState::self()->joinThreadPool();     return 0; }

    ```

    ``` 麻烦哪位大神帮小弟看看这里有什么问题 错误是05-22 13:53:59.741  1685  1685 E binder_test: transact error,-74,Not a data message 05-22 13:53:59.741  1685  1685 E binder_test: getTest transact error,-74

    多谢

    最新回复(0)