Native Binder通讯

转载请注明出处:http://blog.csdn.net/zhaodai11?viewmode=contents

Binder是Android系统独有的一种IPC通信机制,贯穿在整个Android系统中。

Binder通信使用C/S架构,除了C/S架构所包括的Client端和Server端外,Android还有一个ServiceManager端,用来注册和查询服务。(注意这里的ServiceManager是指底层和驱动交互实现服务的注册和查询,并非Java类中的ServiceManager,这点很容易搞混)下面这张来自邓凡平老师博客的图片可以形象描绘出他们三者之间的关系。

Client、Server和ServiceManager三者之间的交互关系

根据上面的图,可以看出:

  1. Server进程注册服务到ServiceManager,此时Server是ServiceManager的客户端,ServiceManager是服务端。
  2. Client进程使用服务,必须先要通过ServerManager获取相应的服务信息。此时Client是客户端,ServiceManager是服务端。
  3. Client根据得到的服务信息建立与服务所在的Server进程通信的通路,然后就可以直接与Service交互了,此时Client是客户端,Server是服务端。

上面提到的ServiceManager很容易被大家误以为是Java中ServiceManager,它真正的实现是在 source/android-6.0.1_r17/frameworks/native/cmds/servicemanager/service_manager.c中

//...

//svcmgr_handler是真正负责查找和添加服务信息的函数
int svcmgr_handler(struct binder_state *bs,
                   struct binder_transaction_data *txn,
                   struct binder_io *msg,
                   struct binder_io *reply)
{
    struct svcinfo *si;
    uint16_t *s;
    size_t len;
    uint32_t handle;
    uint32_t strict_policy;
    int allow_isolated;

    //ALOGI("target=%p code=%d pid=%d uid=%d\n",
    //      (void*) txn->target.ptr, txn->code, txn->sender_pid, txn->sender_euid);

    if (txn->target.ptr != BINDER_SERVICE_MANAGER)
        return -1;

    if (txn->code == PING_TRANSACTION)
        return 0;

    // Equivalent to Parcel::enforceInterface(), reading the RPC
    // header with the strict mode policy mask and the interface name.
    // Note that we ignore the strict_policy and don't propagate it
    // further (since we do no outbound RPCs anyway).
    strict_policy = bio_get_uint32(msg);
    s = bio_get_string16(msg, &len);
    if (s == NULL) {
        return -1;
    }

    if ((len != (sizeof(svcmgr_id) / 2)) ||
        memcmp(svcmgr_id, s, sizeof(svcmgr_id))) {
        fprintf(stderr,"invalid id %s\n", str8(s, len));
        return -1;
    }

    if (sehandle && selinux_status_updated() > 0) {
        struct selabel_handle *tmp_sehandle = selinux_android_service_context_handle();
        if (tmp_sehandle) {
            selabel_close(sehandle);
            sehandle = tmp_sehandle;
        }
    }

    switch(txn->code) {
    //获取某个Service信息
    case SVC_MGR_GET_SERVICE:
    case SVC_MGR_CHECK_SERVICE:
        s = bio_get_string16(msg, &len);
        if (s == NULL) {
            return -1;
        }
        handle = do_find_service(bs, s, len, txn->sender_euid, txn->sender_pid);
        if (!handle)
            break;
        bio_put_ref(reply, handle);
        return 0;
//添加service到servicemanager
    case SVC_MGR_ADD_SERVICE:
        s = bio_get_string16(msg, &len);
        if (s == NULL) {
            return -1;
        }
        handle = bio_get_ref(msg);
        allow_isolated = bio_get_uint32(msg) ? 1 : 0;
        if (do_add_service(bs, s, len, handle, txn->sender_euid,
            allow_isolated, txn->sender_pid))
            return -1;
        break;
//获取当前系统已经注册的所有service名称
    case SVC_MGR_LIST_SERVICES: {
        uint32_t n = bio_get_uint32(msg);

        if (!svc_can_list(txn->sender_pid)) {
            ALOGE("list_service() uid=%d - PERMISSION DENIED\n",
                    txn->sender_euid);
            return -1;
        }
        si = svclist;
        while ((n-- > 0) && si)
            si = si->next;
        if (si) {
            bio_put_string16(reply, si->name);
            return 0;
        }
        return -1;
    }
    default:
        ALOGE("unknown code %d\n", txn->code);
        return -1;
    }

    bio_put_uint32(reply, 0);
    return 0;
}
//.....

以上是简单介绍,详细原理,给大家推荐两个讲解Binder比较全面细致的博客:
邓凡平老师:http://blog.csdn.net/innost/article/details/47208049
罗升阳老师:http://blog.csdn.net/Luoshengyang/article/list/4

在Android系统源码中,使用Binder时用了很多的代理,包括在很多博客的示例中也一样,让人感觉眼花缭乱。其实弄懂原理不用写代理那些也能实现。

服务端:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <binder/IPCThreadState.h>
#include "binder/IPCThreadState.h"
#include "binder/IServiceManager.h"
#include "TestBinderService.h"

using namespace android;

int main()
{
    printf("-------服务端启动---------\n");
    // 获得一个ProcessState实例
    sp<ProcessState> proc(ProcessState::self());
    //调用defaultServiceManager获取ServiceManger 
    sp<IServiceManager> sm = defaultServiceManager();
    
    //创建TestBinderService服务 将服务注册到ServiceManager中
    TestBinderService::Instance();
    //创建一个线程池
    ProcessState::self()->startThreadPool();
    //
    IPCThreadState::self()->joinThreadPool(true);
    return 0;
}

TestBinderService::Instance()实现


#define BINDER_TESTSERVICE "TestBinderService"

namespace android
{
    
    TestBinderService* TestBinderService::gScrService = NULL;
    
    int TestBinderService::Instance()
    {
        if(!gScrService)
        {
            //创建服务
            gScrService = new TestBinderService();
            //将服务注册到serviceManager中
            int ret = defaultServiceManager()->addService(String16(BINDER_TESTSERVICE), gScrService);
            return ret;
        }
        return 0;
    }
    //.......
    //与客户端进行通信
    status_t TestBinderService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
    {
        printf("命令码 code=%d\n",code);
        switch (code)
        {
            case BINDER_send:
            {
                int ivalue = data.readInt32();
                String8 s = data.readString8();
                // string str(s);
                const char *cptr = s; 
                printf("服务端接收信息:数字 = %d 字符串 = %s\n", ivalue,cptr);
                // reply->writeInt32(200);
                String8 msg("收到了 谢谢!!!");
                reply->writeString8(msg);
            }
                break;
                
            case BINDER_get:
            {
                // int ivalue = data.readInt32();
                String8 s = data.readString8();
                const char *cptr = s; 
                printf("服务端接收信息:信息 = %s\n",cptr);
                reply->writeInt32(600);
                String8 msg("给你发个信息");
                reply->writeString8(msg);

            }
                break;
                
            default:
                break;
        }
        return 0;
    }


}

客户端:

    //获取相关服务信息 
    TestBinderClient* client = new TestBinderClient();
    //与服务端通信
    client->sendMsg();
    client->getMsg();
    return (int)client;

TestBinderClient.cpp

    sp<IBinder> binder;
    
    TestBinderClient::TestBinderClient()
    {
        // printf("TestBinderClient::%s\n", __FUNCTION__);
        getScrService();
    }
    
    
    TestBinderClient::~TestBinderClient()
    {
        // printf("TestBinderClient::%s\n", __FUNCTION__);
        binder = 0;
    }
    
    void TestBinderClient::getScrService()
    {
        //获取serviceManager
        sp<IServiceManager> sm = defaultServiceManager();
        //查找相关服务信息
        binder = sm->getService(String16(BINDER_TESTSERVICE));
        if(binder == 0)
        {
            printf("getScrService failed\n");
            return;
        }
    }
    
    //使用服务进行进程间通信
    int TestBinderClient::sendMsg()
    {
        // printf("TestBinderClient::%s\n", __FUNCTION__);
        Parcel data, reply;
        data.writeInt32(100);
        String8 msg("给你发个信息");
        data.writeString8(msg);
        binder->transact(BINDER_send, data, &reply);
        // int ret = reply.readInt32();
        String8 s = reply.readString8();
        const char *cptr = s; 
        printf("客户端接收回复信息:信息 = %s\n",cptr);
        return 0;
    }

    int TestBinderClient::getMsg()
    {
        // printf("TestBinderClient::%s\n", __FUNCTION__);
        Parcel data, reply;
        // data.writeInt32(0);
        String8 msg("给我来个信息");
        data.writeString8(msg);
        binder->transact(BINDER_get, data, &reply);
        int ret = reply.readInt32();
        String8 s = reply.readString8();
        const char *cptr = s; 
        printf("客户端接收回复信息:数字 = %d 字符串 = %s\n",ret,cptr);
        return ret;
    }

运行效果图:
服务端:


服务端启动

客户端:


客户端启动

编译代码需要在源码环境下编译,将服务端和客户端编译成两个可执行文件,push到手机中执行。

当然也可以增加编写JNI接口,编译成静态库文件,在APP中进行调用。不过这样需要APP获取ROOT权限或者将APP变成系统应用。

相关代码已经传到github: https://github.com/zhaodaizheng/BinderTest

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容