在Android的Sensor框架代码里看到BitTube,这里对其进行学习整理.
BitTube是用来处理进程间通讯的机制,和管道类似,主要是对socketpair的封装.先了解一下socketpair
socketpair用来创建一对未命名、互相连接的套接字,套接字的一端可以进行读和写的操作,用来实现全双工的通讯.
函数原型:int socketpair(int domain, int type, int protocol, int sv[2]);
参数说明
1.domain在linux平台上只能使用AF_UNIX
2.type表示socket的类型,目前支持如下类型
/**
* enum sock_type - Socket types
* @SOCK_STREAM: stream (connection) socket
* @SOCK_DGRAM: datagram (conn.less) socket
* @SOCK_RAW: raw socket
* @SOCK_RDM: reliably-delivered message
* @SOCK_SEQPACKET: sequential packet socket
* @SOCK_DCCP: Datagram Congestion Control Protocol socket
* @SOCK_PACKET: linux specific way of getting packets at the dev level.
* For writing rarp and other similar things on the user level.
*
* When adding some new socket type please
* grep ARCH_HAS_SOCKET_TYPE include/asm-* /socket.h, at least MIPS
* overrides this enum for binary compat reasons.
*/
3.protocol 目前必须为0
4.sv[2] 代表两个互相连接的套接字.
通过socketpair实现不同进程之间的通讯极其简单,代码如下
//test in ubuntu 14.04 by helenxr
//step1:gcc socketpair.c
//step2:./a.out
/*test result like below:
create socketpair[3,4]
child process rcv msg:parent send to child.
parent rcv msg:parent send to child.
child process rcv msg:parent send to child.
parent rcv msg:parent send to child.
child process rcv msg:parent send to child.
parent rcv msg:parent send to child.
child process rcv msg:parent send to child.
parent rcv msg:parent send to child.
child process rcv msg:parent send to child.
parent rcv msg:parent send to child.
*/
#include
#include
#include
#include
#include
int main(){
int sockets[2];
unsigned char loop_cnt_p = 5,loop_cnt_c = 5;
if(socketpair(AF_UNIX,SOCK_SEQPACKET,0,sockets) == 0) {
printf("create socketpair[%d,%d]\n",sockets[0],sockets[1]);
}else {
printf("create socketpair error.\n");
return -1;
}
//create process
if(fork()){ //parent
while(loop_cnt_p--){
char parent_rcv_buf[100] = "";
char parent_send_buf[] = "parent send to child.";
if(write(sockets[1],parent_send_buf,sizeof(parent_send_buf)) < 0){
printf("parent write buffer error!\n");
return -1;
}
if(read(sockets[1],parent_rcv_buf,sizeof(parent_rcv_buf)) < 0){
printf("parent read buffer error!\n");
return -1;
}else{
printf("parent rcv msg:%s\n",parent_rcv_buf);
}
sleep(2);
}
}else{//child
while(loop_cnt_c--){
char child_rcv_buf[100] = "";
char child_send_buf[] = "clild send to parent.";
if(read(sockets[0],child_rcv_buf,sizeof(child_rcv_buf)) < 0){
printf("clild read buffer error!\n");
return -1;
}else{
printf("child process rcv msg:%s\n",child_rcv_buf);
}
if(write(sockets[0],child_rcv_buf,sizeof(child_rcv_buf)) < 0){
printf("child write buffer error!\n");
return -1;
}
sleep(2);
}
}
close(sockets[0]);
close(sockets[1]);
return 0;
}
父进程可以通过sockets[1]向子进程发送数据,同时也能通过sockets[1]接收子进程的数据.
BitTube代码量很少,在(frameworks\native\libs\gui\BitTube.cpp)中,我们直接看它的几个重要的接口.
BitTube的构造函数
BitTube::BitTube(size_t bufsize)
: mSendFd(-1), mReceiveFd(-1)
{
init(bufsize, bufsize);
}
其中init代码创建/配置socketpair.
void BitTube::init(size_t rcvbuf, size_t sndbuf) {
int sockets[2];
//创建socketpair
if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets) == 0) {
size_t size = DEFAULT_SOCKET_BUFFER_SIZE;
//对socketfd进行配置
setsockopt(sockets[0], SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf));
setsockopt(sockets[1], SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf));
// sine we don't use the "return channel", we keep it small...
setsockopt(sockets[0], SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
setsockopt(sockets[1], SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
//设置为非阻塞模式
fcntl(sockets[0], F_SETFL, O_NONBLOCK);
fcntl(sockets[1], F_SETFL, O_NONBLOCK);
mReceiveFd = sockets[0];
mSendFd = sockets[1];
} else {
mReceiveFd = -errno;
ALOGE("BitTube: pipe creation failed (%s)", strerror(-mReceiveFd));
}
}
成员变量mReceiveFd,看起来是一个接收端,实际上这个fd也可以用来发送,上面linux上测试的情况说明了这一点.同样mSendFd也可以用来接收.
sendObjects里调用的是write成员函数
ssize_t BitTube::write(void const* vaddr, size_t size)
{
ssize_t err, len;
do {
len = ::send(mSendFd, vaddr, size, MSG_DONTWAIT | MSG_NOSIGNAL);
// cannot return less than size, since we're using SOCK_SEQPACKET
err = len < 0 ? errno : 0;
} while (err == EINTR);
return err == 0 ? len : -err;
}
write中调用send接口将数据写入mSendFd中(send与write类似,与write相比多了第四个参数,用来做标志控制)
recvObjects里调用read成员函数
ssize_t BitTube::read(void* vaddr, size_t size)
{
ssize_t err, len;
do {
len = ::recv(mReceiveFd, vaddr, size, MSG_DONTWAIT);
err = len < 0 ? errno : 0;
} while (err == EINTR);
if (err == EAGAIN || err == EWOULDBLOCK) {
// EAGAIN means that we have non-blocking I/O but there was
// no data to be read. Nothing the client should care about.
return 0;
}
return err == 0 ? len : -err;
}
read中调用recv接口将数据从mReceiveFd中读出(recv与read类似,与read相比,多了第四个参数,用来做标志控制)
linux:socketpair
use socketpair