2 * Copyright (C) 2010 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <sys/types.h>
19 #include <sys/socket.h>
24 #include <utils/Errors.h>
26 #include <binder/Parcel.h>
28 #include <gui/BitTube.h>
31 // ----------------------------------------------------------------------------
33 // Socket buffer size. The default is typically about 128KB, which is much larger than
34 // we really need. So we make it smaller.
35 static const size_t DEFAULT_SOCKET_BUFFER_SIZE = 4 * 1024;
39 : mSendFd(-1), mReceiveFd(-1)
41 init(DEFAULT_SOCKET_BUFFER_SIZE, DEFAULT_SOCKET_BUFFER_SIZE);
44 BitTube::BitTube(size_t bufsize)
45 : mSendFd(-1), mReceiveFd(-1)
47 init(bufsize, bufsize);
50 BitTube::BitTube(const Parcel& data)
51 : mSendFd(-1), mReceiveFd(-1)
53 mReceiveFd = dup(data.readFileDescriptor());
56 ALOGE("BitTube(Parcel): can't dup filedescriptor (%s)",
57 strerror(-mReceiveFd));
70 void BitTube::init(size_t rcvbuf, size_t sndbuf) {
72 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets) == 0) {
73 size_t size = DEFAULT_SOCKET_BUFFER_SIZE;
74 setsockopt(sockets[0], SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf));
75 setsockopt(sockets[1], SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf));
76 // sine we don't use the "return channel", we keep it small...
77 setsockopt(sockets[0], SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
78 setsockopt(sockets[1], SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
79 fcntl(sockets[0], F_SETFL, O_NONBLOCK);
80 fcntl(sockets[1], F_SETFL, O_NONBLOCK);
81 mReceiveFd = sockets[0];
85 ALOGE("BitTube: pipe creation failed (%s)", strerror(-mReceiveFd));
89 status_t BitTube::initCheck() const
92 return status_t(mReceiveFd);
97 int BitTube::getFd() const
102 int BitTube::getSendFd() const
107 ssize_t BitTube::write(void const* vaddr, size_t size)
111 len = ::send(mSendFd, vaddr, size, MSG_DONTWAIT | MSG_NOSIGNAL);
112 // cannot return less than size, since we're using SOCK_SEQPACKET
113 err = len < 0 ? errno : 0;
114 } while (err == EINTR);
115 return err == 0 ? len : -err;
118 ssize_t BitTube::read(void* vaddr, size_t size)
122 len = ::recv(mReceiveFd, vaddr, size, MSG_DONTWAIT);
123 err = len < 0 ? errno : 0;
124 } while (err == EINTR);
125 if (err == EAGAIN || err == EWOULDBLOCK) {
126 // EAGAIN means that we have non-blocking I/O but there was
127 // no data to be read. Nothing the client should care about.
130 return err == 0 ? len : -err;
133 status_t BitTube::writeToParcel(Parcel* reply) const
138 status_t result = reply->writeDupFileDescriptor(mReceiveFd);
145 ssize_t BitTube::sendObjects(const sp<BitTube>& tube,
146 void const* events, size_t count, size_t objSize)
148 const char* vaddr = reinterpret_cast<const char*>(events);
149 ssize_t size = tube->write(vaddr, count*objSize);
151 // should never happen because of SOCK_SEQPACKET
152 LOG_ALWAYS_FATAL_IF((size >= 0) && (size % static_cast<ssize_t>(objSize)),
153 "BitTube::sendObjects(count=%zu, size=%zu), res=%zd (partial events were sent!)",
154 count, objSize, size);
156 //ALOGE_IF(size<0, "error %d sending %d events", size, count);
157 return size < 0 ? size : size / static_cast<ssize_t>(objSize);
160 ssize_t BitTube::recvObjects(const sp<BitTube>& tube,
161 void* events, size_t count, size_t objSize)
163 char* vaddr = reinterpret_cast<char*>(events);
164 ssize_t size = tube->read(vaddr, count*objSize);
166 // should never happen because of SOCK_SEQPACKET
167 LOG_ALWAYS_FATAL_IF((size >= 0) && (size % static_cast<ssize_t>(objSize)),
168 "BitTube::recvObjects(count=%zu, size=%zu), res=%zd (partial events were received!)",
169 count, objSize, size);
171 //ALOGE_IF(size<0, "error %d receiving %d events", size, count);
172 return size < 0 ? size : size / static_cast<ssize_t>(objSize);
175 // ----------------------------------------------------------------------------
176 }; // namespace android