Service代理对象的获取过程

2018-07-18  本文已影响0人  ColdWave

Service 组件将自己注册到 ServiceManager 后,就在 Server 进程中等待 Client 进程发送进程间通信数据.Client 进程要和 Service 组件通信,需要首先获取一个代理对象,这是通过 ServiceManager 提供的 Service 组件查询服务来实现的.

//#define LOG_NDEBUG 0
#define LOG_TAG "FregClient"
#include <utils/Log.h>

#include <utils/Log.h>
#include <utils/RefBase.h>
#include <binder/IServiceManager.h>

#include "../common/IFregService.h"

using namespace android;

int main()
{
   sp<IBinder> binder = defaultServiceManager()->getService(String16(FREG_SERVICE));
   if (binder == NULL) {
       printf("%s: Failed to get freg service: %s.\n", __func__, FREG_SERVICE);
       return -1;
   }

   sp<IFregService> service = IFregService::asInterface(binder);
   if (service == NULL) {
       ALOGE("Failed to get freg service interface.");
       return -1;
   }

   printf("Read original value from FregService:\n");

   int32_t val = service->getVal();
   printf(" %d .\n", val);

   printf("Add value 1 to FregService.");

   val +=1 ;

   service->setVal(val);

   printf("Read the value from FregService anain:\n");

   val = service->getVal();

   printf(" %d .\n", val);

   return 0;
}

getService

defaultServiceManager() 已经分析过,拿到的是 BpBinder -> BpServiceManager -> IServiceManager.

class BpServiceManager : public BpInterface<IServiceManager>
{
public:
    explicit BpServiceManager(const sp<IBinder>& impl)
        : BpInterface<IServiceManager>(impl)
    {
    }

    virtual sp<IBinder> getService(const String16& name) const
    {
        unsigned n;
        for (n = 0; n < 5; n++){
            if (n > 0) {
                if (!strcmp(ProcessState::self()->getDriverName().c_str(), "/dev/vndbinder")) {
                    ALOGI("Waiting for vendor service %s...", String8(name).string());
                    CallStack stack(LOG_TAG);
                } else {
                    ALOGI("Waiting for service %s...", String8(name).string());
                }
                sleep(1);
            }
            sp<IBinder> svc = checkService(name);
            if (svc != NULL) return svc;
        }
        return NULL;
    }

    virtual sp<IBinder> checkService( const String16& name) const
    {
        Parcel data, reply;
        data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
        data.writeString16(name);
        remote()->transact(CHECK_SERVICE_TRANSACTION, data, &reply);
        return reply.readStrongBinder();
    }

    virtual status_t addService(const String16& name, const sp<IBinder>& service,
            bool allowIsolated)
    {
        Parcel data, reply;
        data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
        data.writeString16(name);
        data.writeStrongBinder(service);
        data.writeInt32(allowIsolated ? 1 : 0);
        status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply);
        return err == NO_ERROR ? reply.readExceptionCode() : err;
    }

    virtual Vector<String16> listServices()
    {
        Vector<String16> res;
        int n = 0;

        for (;;) {
            Parcel data, reply;
            data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
            data.writeInt32(n++);
            status_t err = remote()->transact(LIST_SERVICES_TRANSACTION, data, &reply);
            if (err != NO_ERROR)
                break;
            res.add(reply.readString16());
        }
        return res;
    }
};
上一篇 下一篇

猜你喜欢

热点阅读