AOSP中使用BitTube 的地方有显示(Display)子系统和Sensor子系统,在这里一探究竟
1. 概述
BitTube是用来处理进程间通讯的机制,和管道类似,主要是socketpair的封装,
2 socketpair
socketpair用来创建一堆未命名的,互相连接的套接字,套接字一端可以进行读写操作,用来实现全双工的通讯。
函数原型 int socketpair(int domain, int type, int protocol, int sv[2]);
- domain 在linux平台上只能使用AF_UNIX
- type表示socket的类型,目前支持如下类型
- protocol 目前必须为0
- sv[2] 代表两个互相连接的套接字
3 socketpair用法
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
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;
}
if(fork()){
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{
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;
}
4 BitTube
BitTube代码量很少,在(frameworks\native\libs\gui\BitTube.cpp)中,我们直接看它的几个重要的接口.
namespace android {
class Parcel;
class BitTube : public RefBase
{
public:
BitTube();
explicit BitTube(size_t bufsize);
explicit BitTube(const Parcel& data);
virtual ~BitTube();
status_t initCheck() const;
int getFd() const;
int getSendFd() const;
template <typename T>
static ssize_t sendObjects(const sp<BitTube>& tube,
T const* events, size_t count) {
return sendObjects(tube, events, count, sizeof(T));
}
template <typename T>
static ssize_t recvObjects(const sp<BitTube>& tube,
T* events, size_t count) {
return recvObjects(tube, events, count, sizeof(T));
}
status_t writeToParcel(Parcel* reply) const;
private:
void init(size_t rcvbuf, size_t sndbuf);
ssize_t write(void const* vaddr, size_t size);
ssize_t read(void* vaddr, size_t size);
int mSendFd;
mutable int mReceiveFd;
static ssize_t sendObjects(const sp<BitTube>& tube,
void const* events, size_t count, size_t objSize);
static ssize_t recvObjects(const sp<BitTube>& tube,
void* events, size_t count, size_t objSize);
};
};
namespace android {
static const size_t DEFAULT_SOCKET_BUFFER_SIZE = 4 * 1024;
BitTube::BitTube()
: mSendFd(-1), mReceiveFd(-1)
{
init(DEFAULT_SOCKET_BUFFER_SIZE, DEFAULT_SOCKET_BUFFER_SIZE);
}
BitTube::BitTube(size_t bufsize)
: mSendFd(-1), mReceiveFd(-1)
{
init(bufsize, bufsize);
}
BitTube::BitTube(const Parcel& data)
: mSendFd(-1), mReceiveFd(-1)
{
mReceiveFd = dup(data.readFileDescriptor());
if (mReceiveFd < 0) {
mReceiveFd = -errno;
ALOGE("BitTube(Parcel): can't dup filedescriptor (%s)",
strerror(-mReceiveFd));
}
}
BitTube::~BitTube()
{
if (mSendFd >= 0)
close(mSendFd);
if (mReceiveFd >= 0)
close(mReceiveFd);
}
void BitTube::init(size_t rcvbuf, size_t sndbuf) {
int sockets[2];
if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets) == 0) {
size_t size = DEFAULT_SOCKET_BUFFER_SIZE;
setsockopt(sockets[0], SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf));
setsockopt(sockets[1], SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf));
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));
}
}
status_t BitTube::initCheck() const
{
if (mReceiveFd < 0) {
return status_t(mReceiveFd);
}
return NO_ERROR;
}
int BitTube::getFd() const
{
return mReceiveFd;
}
int BitTube::getSendFd() const
{
return mSendFd;
}
ssize_t BitTube::write(void const* vaddr, size_t size)
{
ssize_t err, len;
do {
len = ::send(mSendFd, vaddr, size, MSG_DONTWAIT | MSG_NOSIGNAL);
err = len < 0 ? errno : 0;
} while (err == EINTR);
return err == 0 ? len : -err;
}
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) {
return 0;
}
return err == 0 ? len : -err;
}
status_t BitTube::writeToParcel(Parcel* reply) const
{
if (mReceiveFd < 0)
return -EINVAL;
status_t result = reply->writeDupFileDescriptor(mReceiveFd);
close(mReceiveFd);
mReceiveFd = -1;
return result;
}
ssize_t BitTube::sendObjects(const sp<BitTube>& tube,
void const* events, size_t count, size_t objSize)
{
const char* vaddr = reinterpret_cast<const char*>(events);
ssize_t size = tube->write(vaddr, count*objSize);
LOG_ALWAYS_FATAL_IF((size >= 0) && (size % static_cast<ssize_t>(objSize)),
"BitTube::sendObjects(count=%zu, size=%zu), res=%zd (partial events were sent!)",
count, objSize, size);
return size < 0 ? size : size / static_cast<ssize_t>(objSize);
}
ssize_t BitTube::recvObjects(const sp<BitTube>& tube,
void* events, size_t count, size_t objSize)
{
char* vaddr = reinterpret_cast<char*>(events);
ssize_t size = tube->read(vaddr, count*objSize);
LOG_ALWAYS_FATAL_IF((size >= 0) && (size % static_cast<ssize_t>(objSize)),
"BitTube::recvObjects(count=%zu, size=%zu), res=%zd (partial events were received!)",
count, objSize, size);
return size < 0 ? size : size / static_cast<ssize_t>(objSize);
}
};
5 andorod 中 BitTube应用-sensor
@service/SensorEventConnection.cpp
status_t SensorService::SensorEventConnection::sendEvents(
sensors_event_t const* buffer, size_t numEvents,
sensors_event_t* scratch,
wp<const SensorEventConnection> const * mapFlushEventsToConnections) {
...
ssize_t size = SensorEventQueue::write(mChannel,
reinterpret_cast<ASensorEvent const*>(scratch), count);
}
@frameworks/native/libs/sensor/SensorEventQueue.cpp
ssize_t SensorEventQueue::write(const sp<BitTube>& tube,
ASensorEvent const* events, size_t numEvents) {
return BitTube::sendObjects(tube, events, numEvents);
}
|