Binder通信实例

服务端:

#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>

#include <utils/RefBase.h>
#include <binder/IInterface.h>
#include <binder/Parcel.h>
#include <utils/threads.h>

#include <cutils/log.h>
#include <cutils/properties.h>
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>


using namespace android;


namespace android
{

enum{
    CALCULATE_MUL_NUM=0,
    CALCULATE_DIV_NUM
};


class MyServer:public BBinder
{
public:
    MyServer();
    virtual ~MyServer();
    static int instantiate();
    virtual status_t onTransact(uint32_t, const Parcel&, Parcel*, uint32_t);

    void parse_edid(void);
    int DeviceInit(void);
    void UpdateSurface(void);

private:
    mutable Mutex mLock;
};

int MyServer::instantiate()
{
    int tTempRet=defaultServiceManager()->addService(String16("service.damon"), new MyServer());

    ALOGD("damon ===> server instantiate : %d \n", tTempRet);

    return tTempRet;
}

MyServer::MyServer()
{
    ALOGD("damon ===> create server !\n");
}

MyServer::~MyServer()
{
    ALOGD("damon ===> destroy server !\n");
}


status_t MyServer::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
//  ALOGD("damon ===> server onTransact code = %d ", code);

    Mutex::Autolock _l(mLock);
    switch(code)
    {
    case CALCULATE_MUL_NUM:
        {
            int a=data.readInt32();
            int b=data.readInt32();
            int tTempSum=a*b;
            ALOGD("damon ===> server mul = %d %d %d \n", a, b, tTempSum);
            reply->writeInt32(tTempSum);
            return NO_ERROR;
        }
        break;
    case CALCULATE_DIV_NUM:
        {
            int a=data.readInt32();
                        int b=data.readInt32();
                        int tTempSum=a/b;
                        ALOGD("damon ===> server div = %d %d %d \n", a, b, tTempSum);
                        reply->writeInt32(tTempSum);
                        return NO_ERROR;
        }
        break;
    case 0x10:
        {

        }
        break;
    default:
        return BBinder::onTransact(code, data, reply, flags);
    }

    return 0;
}
}

int main(int argc, char *argv[])
{
    srand(time(NULL));

    sp<ProcessState> proc(ProcessState::self());
    sp<IServiceManager> sm=defaultServiceManager();
    ALOGD("damon ===> ServiceManager: %p", sm.get());
    MyServer::instantiate();
    ProcessState::self()->startThreadPool();
    IPCThreadState::self()->joinThreadPool();

    return 0;
}


Android.mk文件

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS) 
LOCAL_PRELINK_MODULE := false 
LOCAL_CFLAGS += -O2 -g 
LOCAL_SRC_FILES := server.cpp

LOCAL_SHARED_LIBRARIES = libdl libcutils libutils libbinder 
LOCAL_C_INCLUDE := $(TOP)/frameworks/base/include

LOCAL_MODULE := server_test

LOCAL_MODULE_TAGS:= optional

LOCAL_CFLAGS += -Wno-unused-parameter -Werror

include $(BUILD_EXECUTABLE)
 

客户端:

#include <cutils/log.h>
#include <cutils/properties.h>
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>

using namespace android;

enum{
        CALCULATE_ADD_NUM=0,
        CALCULATE_SUB_NUM
};

enum{
        CALCULATE_MUL_NUM=0,
        CALCULATE_DIV_NUM
};

int main(int argc, char *argv[])
{
        sp<IBinder> tTempServerBinder;
        Parcel data, reply;
        int tTempSum=0;

        ALOGD("damon ===> client main is call .....");

        sp<IServiceManager> sm=defaultServiceManager();

        while(1)
        {
                tTempServerBinder=sm->getService(String16("service.damon"));
                ALOGD("damon ===> client get service : %p", sm.get());
                if(tTempServerBinder==0)
                {
                        ALOGD("damon ===> test service not published, waiting . . .");
                        usleep(1000*1000);
                        continue;
                }else
                {
                        ALOGD("damon ===> get service success! ");
                        break;
                }
        }


        data.writeInt32(3);
        data.writeInt32(5);
        ALOGD("damon ===> create remote !\n");
        tTempServerBinder->transact(CALCULATE_MUL_NUM, data, &reply);
        tTempSum=reply.readInt32();
        ALOGD("damon ===> client calculate mul num : %d ", tTempSum);


        data.freeData();
        data.writeInt32(5);
        data.writeInt32(3);
        tTempServerBinder->transact(CALCULATE_DIV_NUM, data, &reply);
        tTempSum=reply.readInt32();
        ALOGD("damon ===> client calculate div num : %d \n", tTempSum);

        return 0;
}
 

Android.mk文件:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS) 
LOCAL_PRELINK_MODULE := false 
LOCAL_CFLAGS += -O2 -g 
LOCAL_SRC_FILES := client.cpp

LOCAL_SHARED_LIBRARIES = libdl libcutils libutils libbinder 
LOCAL_C_INCLUDE := $(TOP)/frameworks/base/include

LOCAL_MODULE := client_test

LOCAL_MODULE_TAGS:= optional

LOCAL_CFLAGS += -Wno-unused-parameter -Werror

include $(BUILD_EXECUTABLE)
 

转载:https://blog.csdn.net/u013470224/article/details/68065982 

binder原理深入理解:https://blog.csdn.net/bob_fly1984/article/details/79836848

Android系统全覆盖:http://gityuan.com/page23/

                                  https://zhuanlan.zhihu.com/p/20708611

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页