2010-07-14 05:21:56 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/types.h>
|
2012-04-03 00:02:19 +00:00
|
|
|
#include <sys/socket.h>
|
2010-07-14 05:21:56 +00:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
2012-02-07 03:10:04 +00:00
|
|
|
#include <unistd.h>
|
2010-07-14 05:21:56 +00:00
|
|
|
|
|
|
|
#include <utils/Errors.h>
|
|
|
|
|
|
|
|
#include <binder/Parcel.h>
|
|
|
|
|
2011-10-21 01:42:02 +00:00
|
|
|
#include <gui/BitTube.h>
|
2010-07-14 05:21:56 +00:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2012-04-03 00:02:19 +00:00
|
|
|
// Socket buffer size. The default is typically about 128KB, which is much larger than
|
|
|
|
// we really need. So we make it smaller.
|
2013-09-10 06:36:25 +00:00
|
|
|
static const size_t DEFAULT_SOCKET_BUFFER_SIZE = 4 * 1024;
|
2012-04-03 00:02:19 +00:00
|
|
|
|
|
|
|
|
2011-10-21 01:42:02 +00:00
|
|
|
BitTube::BitTube()
|
2010-07-14 05:21:56 +00:00
|
|
|
: mSendFd(-1), mReceiveFd(-1)
|
|
|
|
{
|
2013-09-10 06:36:25 +00:00
|
|
|
init(DEFAULT_SOCKET_BUFFER_SIZE, DEFAULT_SOCKET_BUFFER_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
BitTube::BitTube(size_t bufsize)
|
|
|
|
: mSendFd(-1), mReceiveFd(-1)
|
|
|
|
{
|
|
|
|
init(bufsize, bufsize);
|
2010-07-14 05:21:56 +00:00
|
|
|
}
|
|
|
|
|
2011-10-21 01:42:02 +00:00
|
|
|
BitTube::BitTube(const Parcel& data)
|
2010-07-14 05:21:56 +00:00
|
|
|
: mSendFd(-1), mReceiveFd(-1)
|
|
|
|
{
|
|
|
|
mReceiveFd = dup(data.readFileDescriptor());
|
2013-09-10 06:36:25 +00:00
|
|
|
if (mReceiveFd < 0) {
|
2011-10-21 01:42:02 +00:00
|
|
|
mReceiveFd = -errno;
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("BitTube(Parcel): can't dup filedescriptor (%s)",
|
2011-10-21 01:42:02 +00:00
|
|
|
strerror(-mReceiveFd));
|
|
|
|
}
|
2010-07-14 05:21:56 +00:00
|
|
|
}
|
|
|
|
|
2011-10-21 01:42:02 +00:00
|
|
|
BitTube::~BitTube()
|
2010-07-14 05:21:56 +00:00
|
|
|
{
|
|
|
|
if (mSendFd >= 0)
|
|
|
|
close(mSendFd);
|
|
|
|
|
|
|
|
if (mReceiveFd >= 0)
|
|
|
|
close(mReceiveFd);
|
|
|
|
}
|
|
|
|
|
2013-09-10 06:36:25 +00:00
|
|
|
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));
|
|
|
|
// 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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-21 01:42:02 +00:00
|
|
|
status_t BitTube::initCheck() const
|
|
|
|
{
|
|
|
|
if (mReceiveFd < 0) {
|
|
|
|
return status_t(mReceiveFd);
|
|
|
|
}
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BitTube::getFd() const
|
2010-07-14 05:21:56 +00:00
|
|
|
{
|
|
|
|
return mReceiveFd;
|
|
|
|
}
|
|
|
|
|
2014-07-10 23:01:10 +00:00
|
|
|
int BitTube::getSendFd() const
|
|
|
|
{
|
|
|
|
return mSendFd;
|
|
|
|
}
|
|
|
|
|
2011-10-21 01:42:02 +00:00
|
|
|
ssize_t BitTube::write(void const* vaddr, size_t size)
|
2010-07-14 05:21:56 +00:00
|
|
|
{
|
2011-10-21 01:42:02 +00:00
|
|
|
ssize_t err, len;
|
|
|
|
do {
|
2012-04-03 00:02:19 +00:00
|
|
|
len = ::send(mSendFd, vaddr, size, MSG_DONTWAIT | MSG_NOSIGNAL);
|
2013-09-10 06:36:25 +00:00
|
|
|
// cannot return less than size, since we're using SOCK_SEQPACKET
|
2011-10-21 01:42:02 +00:00
|
|
|
err = len < 0 ? errno : 0;
|
|
|
|
} while (err == EINTR);
|
|
|
|
return err == 0 ? len : -err;
|
2010-07-14 05:21:56 +00:00
|
|
|
}
|
|
|
|
|
2011-10-21 01:42:02 +00:00
|
|
|
ssize_t BitTube::read(void* vaddr, size_t size)
|
2010-07-14 05:21:56 +00:00
|
|
|
{
|
2011-10-21 01:42:02 +00:00
|
|
|
ssize_t err, len;
|
|
|
|
do {
|
2012-04-03 00:02:19 +00:00
|
|
|
len = ::recv(mReceiveFd, vaddr, size, MSG_DONTWAIT);
|
2011-10-21 01:42:02 +00:00
|
|
|
err = len < 0 ? errno : 0;
|
|
|
|
} while (err == EINTR);
|
2011-11-29 19:42:32 +00:00
|
|
|
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;
|
|
|
|
}
|
2011-10-21 01:42:02 +00:00
|
|
|
return err == 0 ? len : -err;
|
2010-07-14 05:21:56 +00:00
|
|
|
}
|
|
|
|
|
2011-10-21 01:42:02 +00:00
|
|
|
status_t BitTube::writeToParcel(Parcel* reply) const
|
2010-07-14 05:21:56 +00:00
|
|
|
{
|
|
|
|
if (mReceiveFd < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
status_t result = reply->writeDupFileDescriptor(mReceiveFd);
|
|
|
|
close(mReceiveFd);
|
|
|
|
mReceiveFd = -1;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-04-03 00:02:19 +00:00
|
|
|
|
|
|
|
ssize_t BitTube::sendObjects(const sp<BitTube>& tube,
|
|
|
|
void const* events, size_t count, size_t objSize)
|
|
|
|
{
|
2013-09-10 06:36:25 +00:00
|
|
|
const char* vaddr = reinterpret_cast<const char*>(events);
|
|
|
|
ssize_t size = tube->write(vaddr, count*objSize);
|
|
|
|
|
|
|
|
// should never happen because of SOCK_SEQPACKET
|
2014-11-18 18:24:03 +00:00
|
|
|
LOG_ALWAYS_FATAL_IF((size >= 0) && (size % static_cast<ssize_t>(objSize)),
|
2014-06-09 21:27:45 +00:00
|
|
|
"BitTube::sendObjects(count=%zu, size=%zu), res=%zd (partial events were sent!)",
|
2013-09-10 06:36:25 +00:00
|
|
|
count, objSize, size);
|
|
|
|
|
|
|
|
//ALOGE_IF(size<0, "error %d sending %d events", size, count);
|
2014-11-18 18:24:03 +00:00
|
|
|
return size < 0 ? size : size / static_cast<ssize_t>(objSize);
|
2012-04-03 00:02:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t BitTube::recvObjects(const sp<BitTube>& tube,
|
|
|
|
void* events, size_t count, size_t objSize)
|
|
|
|
{
|
2013-09-10 06:36:25 +00:00
|
|
|
char* vaddr = reinterpret_cast<char*>(events);
|
|
|
|
ssize_t size = tube->read(vaddr, count*objSize);
|
|
|
|
|
|
|
|
// should never happen because of SOCK_SEQPACKET
|
2014-11-18 18:24:03 +00:00
|
|
|
LOG_ALWAYS_FATAL_IF((size >= 0) && (size % static_cast<ssize_t>(objSize)),
|
2014-06-09 21:27:45 +00:00
|
|
|
"BitTube::recvObjects(count=%zu, size=%zu), res=%zd (partial events were received!)",
|
2013-09-10 06:36:25 +00:00
|
|
|
count, objSize, size);
|
|
|
|
|
|
|
|
//ALOGE_IF(size<0, "error %d receiving %d events", size, count);
|
2014-11-18 18:24:03 +00:00
|
|
|
return size < 0 ? size : size / static_cast<ssize_t>(objSize);
|
2012-04-03 00:02:19 +00:00
|
|
|
}
|
|
|
|
|
2010-07-14 05:21:56 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
}; // namespace android
|