纯C++binder服务和客户端实例
继承关系:
  
文件关系

IHelloService.h
/* 参考: frameworks\av\include\media\IMediaPlayerService.h */ #ifndef ANDROID_IHELLOERVICE_H
#define ANDROID_IHELLOERVICE_H #include <utils/Errors.h> // for status_t
#include <utils/KeyedVector.h>
#include <utils/RefBase.h>
#include <utils/String8.h>
#include <binder/IInterface.h>
#include <binder/Parcel.h> #define HELLO_SVR_CMD_SAYHELLO 0
#define HELLO_SVR_CMD_SAYHELLO_TO 1 namespace android { class IHelloService: public IInterface
{
public:
DECLARE_META_INTERFACE(HelloService);
virtual void sayhello(void) = ;
virtual int sayhello_to(const char *name) = ;
}; class BnHelloService: public BnInterface<IHelloService>
{
public:
virtual status_t onTransact( uint32_t code,
const Parcel& data,
Parcel* reply,
uint32_t flags = ); virtual void sayhello(void);
virtual int sayhello_to(const char *name);
};
} #endif
BnHelloService.cpp
/* 参考: frameworks\av\media\libmedia\IMediaPlayerService.cpp */ #define LOG_TAG "HelloService"
#include "IHelloService.h" namespace android { status_t BnHelloService::onTransact( uint32_t code, const Parcel& data,
Parcel* reply, uint32_t flags)
{
/* 解析数据,调用sayhello/sayhello_to */ switch (code) {
case HELLO_SVR_CMD_SAYHELLO: {
sayhello();
return NO_ERROR;
} break; case HELLO_SVR_CMD_SAYHELLO_TO: {
/* 从data中取出参数 */
int32_t policy = data.readInt32(); //多余的0,客户端有多发送一个0
String16 name16 = data.readString16(); //获取客户端发来的name
String8 name8(name16); //讲16位字符传化位8位的字符
int cnt = sayhello_to(name8.string()); //const char*
/* 把返回值写入reply传回去 */
reply->writeInt32(cnt);
return NO_ERROR;
} break;
default:
return BBinder::onTransact(code, data, reply, flags);
}
} void BnHelloService::sayhello(void)
{
static int cnt = ;
printf("say hello : %d\n", cnt++);
} int BnHelloService::sayhello_to(const char *name)
{
static int cnt = ;
printf("say hello to %s : %d\n", name, cnt++);
return cnt;
}
}
BpHelloService.cpp
/* 参考: frameworks\av\media\libmedia\IMediaPlayerService.cpp */
#include "IHelloService.h"
namespace android { class BpHelloService: public BpInterface<IHelloService>
{
public:
BpHelloService(const sp<IBinder>& impl)
: BpInterface<IHelloService>(impl)
{
} void sayhello(void)
{
/* 构造/发送数据 */
Parcel data, reply;
data.writeInt32();
remote()->transact(HELLO_SVR_CMD_SAYHELLO, data, &reply);
} int sayhello_to(const char *name)
{
/* 构造/发送数据 */
Parcel data, reply; data.writeInt32(); //多发一个0
data.writeString16(String16(name)); remote()->transact(HELLO_SVR_CMD_SAYHELLO_TO, data, &reply); return reply.readInt32();
} }; IMPLEMENT_META_INTERFACE(HelloService, "android.media.IHelloService"); }
test_service.cpp
/* 参考: frameworks\av\media\mediaserver\Main_mediaserver.cpp */ #define LOG_TAG "HelloService"
//#define LOG_NDEBUG 0 #include <fcntl.h>
#include <sys/prctl.h>
#include <sys/wait.h>
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <cutils/properties.h>
#include <utils/Log.h>
#include "IHelloService.h"
using namespace android; int main(void)
{
/* addService */ /* while(1){ read data, 解析数据, 调用服务函数 } */ /* 打开驱动, mmap */
sp<ProcessState> proc(ProcessState::self()); /* 获得BpServiceManager */
sp<IServiceManager> sm = defaultServiceManager(); sm->addService(String16("hello"), new BnHelloService()); /* 循环体 */
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool(); return ;
}
test_client.cpp
#define LOG_TAG "HelloService"
#include <fcntl.h>
#include <sys/prctl.h>
#include <sys/wait.h>
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <cutils/properties.h>
#include <utils/Log.h>
#include "IHelloService.h" using namespace android; /* ./test_client hello
* ./test_client hello <name>
*/
int main(int argc, char **argv)
{
int cnt; if (argc < ){
printf("Usage:\n");
printf("%s hello\n", argv[]);
printf("%s hello <name>\n", argv[]);
return -;
} /* getService */
/* 打开驱动, mmap */
sp<ProcessState> proc(ProcessState::self()); /* 获得BpServiceManager */
sp<IServiceManager> sm = defaultServiceManager();
//获取hello服务
sp<IBinder> binder = sm->getService(String16("hello")); if (binder == )
{
printf("can't get hello service\n");
return -;
} /* service肯定是BpHelloServie指针 */
sp<IHelloService> service = interface_cast<IHelloService>(binder); /* 调用Service的函数 */
if (argc < ) {
service->sayhello();
printf("client call sayhello");
}
else {
cnt = service->sayhello_to(argv[]);
printf("client call sayhello_to, cnt = %d", cnt);
} return ;
}
Andriod.mk
LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_SRC_FILES:= \
BnHelloService.cpp \
BpHelloService.cpp \
test_server.cpp LOCAL_SHARED_LIBRARIES := \
libcutils \
libutils \
liblog \
libbinder LOCAL_MODULE:= test_server
LOCAL_32_BIT_ONLY := true include $(BUILD_EXECUTABLE) include $(CLEAR_VARS) LOCAL_SRC_FILES:= \
BpHelloService.cpp \
test_client.cpp LOCAL_SHARED_LIBRARIES := \
libcutils \
libutils \
liblog \
libbinder LOCAL_MODULE:= test_client
LOCAL_32_BIT_ONLY := true include $(BUILD_EXECUTABLE)

纯C++binder服务和客户端实例的更多相关文章
- WebService 服务端客户端 实例 HTTPRIO (一) SOAP WSDL
		Delphi中WebService包含的组件解释(有7个) (1) THTTPRIO-------:使用Http消息来调用远程使用SOAP的接口对象 (2) THTTPReqResp- ... 
- [精华][推荐]CAS SSO单点登录服务端客户端实例
		1.修改server.xml文件,如下: 注意: 这里使用的是https的认证方式,需要将这个配置放开,并做如下修改: <Connector port="8443" prot ... 
- 用C++开发Binder服务
		用C++来实现Binder服务比较麻烦,原因是没有AIDL的辅助,必须手工来写中间的代码. 首先写一个服务类ExampleServer的代码: class ExampleServer : public ... 
- CXF发布webService服务以及客户端调用
		这篇随笔内容是CXF发布webService服务以及客户端调用的方法 CXF是什么? 开发工作之前需要下载CXF和安装 下载地址:http://cxf.apache.org 安装过程: <1&g ... 
- DelphiXE7中创建WebService(服务端+客户端)
		相关资料: http://www.2ccc.com/news/Html/?1507.html http://www.dfwlt.com/forum.php?mod=viewthread&tid ... 
- react服务端/客户端,同构代码心得
		FKP-REST是一套全栈javascript框架 react服务端/客户端,同构代码心得 作者:webkixi react服务端/客户端,同构代码心得 服务端,客户端同构一套代码,大前端的梦想, ... 
- android binder机制之——(创建binder服务)
		Binder机制编程 前面的几篇文章具体介绍了android中binder机制的方方面面,相信你对binder机制已经有了较深刻的理解.俗话说得好"学以致用",以下我们就通过在 ... 
- DelphiXE7中创建WebService(服务端+客户端)  good
		相关资料:http://www.2ccc.com/news/Html/?1507.html DelphiXE7新建WebService具体操作:1.打开“DelphiXE7”->“File”-& ... 
- ipv6禁用导致rpcbind服务启动失败实例
		ipv6禁用导致rpcbind服务启动失败实例 昨天在做服务器磁盘分区扩容的时候出现过一个服务启动的问题,在此记录.情景再现:前天晚上申请做磁盘扩容,得到批准后,昨天早上5点开始做停机调整维护 ... 
随机推荐
- 20181009-6 选题 Scrum立会报告+燃尽图 05
			Scrum立会报告+燃尽图(05)选题 此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2195 一.小组介绍 组长:刘莹莹 ... 
- New Concept English Two 33 91
			$课文89 口误 981. People will do anything to see a free show -- even if it is a bad one. 人们总要想尽办法看不花钱的演出 ... 
- fiddler手机端抓包配置
			首先,你得安装fiddler,这是前提条件,手机抓包有必须条件: 需要保持电脑和手机在同一个局域网中 (这一点,我一般会在电脑上启动一个wifi,然后手机连接即可) 下面说一下如何配置: 手机连接电脑 ... 
- [置顶]
        HashTable vs HashMap
			转载请注明出处:http://blog.csdn.net/crazy1235/article/details/76686891 HashTable vs HashMap 构造函数 hash函数 syn ... 
- Python  高性能并行计算之   mpi4py
			MPI 和 MPI4PY 的搭建上一篇文章已经介绍,这里面介绍一些基本用法. mpi4py 的 helloworld from mpi4py import MPI print(&quo ... 
- 【转】 python 删除非空文件夹
			转自:https://blog.csdn.net/xiaodongxiexie/article/details/77155864 一般删除文件时使用os库,然后利用os.remove(path)即可完 ... 
- 华为OJ:2199 推断输入字符串中的括号匹配
			依据不同的括号有个计数器.在遍历时.当计数器小于0则返回false或者当遍历完后,计数器仍旧不为零,也返回false. import java.util.Scanner; public class b ... 
- spring与hibernate注解及XML方式集成
			spring与hibernate注解及XML方式集成 Hibernate Xml方式 该种方式需要在sessionFactory中引入对应的hbm.xml文件,样例如下: <!-- spring ... 
- [C++ Primer] 第7章: 类
			定义抽象数据类型 定义在类内部的函数是隐式的inline函数. const成员函数 又叫做常量成员函数, 成员函数参数列表之后紧跟const关键字, const修饰的是类this指针. 默认情况下th ... 
- 【linux】less &&more
			命令 : less [文件名] ... 
