diff options
author | 2017-03-31 17:10:06 -0700 | |
---|---|---|
committer | 2017-04-06 11:42:58 -0700 | |
commit | 7d290174b08a56ae6bc6719bec58805ca38b348b (patch) | |
tree | 4a3df003f4d0df97560501713c3cb86d65713e33 /libs/gui/BitTube.cpp | |
parent | 27c8115510cebda13cbe24fd4caa946ea9c5003c (diff) |
libgui: Make BitTube Parcelable and use unique_fd
This change completes the Parcelable interface for BitTube (it was
semi-Parcelable before as it implemented writeToParcel, but this adds
the complementary readFromParcel).
It also changes the send and receive file descriptors from ints to
android::base::unique_fds, which simplifies some of their lifecycle
management.
Finally, it changes the default constructor to leave the class
uninitialized, adding a BitTube(BitTube::DefaultSize) constructor to
replace that functionality (and paving the way for the ability to
default-construct a BitTube prior to readFromParcel'ing into it).
Test: m -j + manual testing
Change-Id: Ib0cba4c7c443b449a9a1837f07f7334395d4f10d
Diffstat (limited to 'libs/gui/BitTube.cpp')
-rw-r--r-- | libs/gui/BitTube.cpp | 42 |
1 files changed, 20 insertions, 22 deletions
diff --git a/libs/gui/BitTube.cpp b/libs/gui/BitTube.cpp index 63f594bf8b..c0237116ab 100644 --- a/libs/gui/BitTube.cpp +++ b/libs/gui/BitTube.cpp @@ -34,26 +34,14 @@ namespace gui { // need. So we make it smaller. 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) { +BitTube::BitTube(size_t bufsize) { 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); +BitTube::BitTube(DefaultSizeType) : BitTube(DEFAULT_SOCKET_BUFFER_SIZE) {} - if (mReceiveFd >= 0) close(mReceiveFd); +BitTube::BitTube(const Parcel& data) { + readFromParcel(&data); } void BitTube::init(size_t rcvbuf, size_t sndbuf) { @@ -67,11 +55,11 @@ void BitTube::init(size_t rcvbuf, size_t sndbuf) { 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]; + mReceiveFd.reset(sockets[0]); + mSendFd.reset(sockets[1]); } else { - mReceiveFd = -errno; - ALOGE("BitTube: pipe creation failed (%s)", strerror(-mReceiveFd)); + mReceiveFd.reset(); + ALOGE("BitTube: pipe creation failed (%s)", strerror(errno)); } } @@ -118,11 +106,21 @@ status_t BitTube::writeToParcel(Parcel* reply) const { if (mReceiveFd < 0) return -EINVAL; status_t result = reply->writeDupFileDescriptor(mReceiveFd); - close(mReceiveFd); - mReceiveFd = -1; + mReceiveFd.reset(); return result; } +status_t BitTube::readFromParcel(const Parcel* parcel) { + mReceiveFd.reset(dup(parcel->readFileDescriptor())); + if (mReceiveFd < 0) { + mReceiveFd.reset(); + int error = errno; + ALOGE("BitTube::readFromParcel: can't dup file descriptor (%s)", strerror(error)); + return -error; + } + return NO_ERROR; +} + 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); |