Skip to content

Commit b63454a

Browse files
Treehugger RobotGerrit Code Review
authored andcommitted
Merge "Return message wrapped in Result from receiveMessage" into main
2 parents a222734 + 10bae11 commit b63454a

5 files changed

Lines changed: 78 additions & 70 deletions

File tree

include/input/InputTransport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ class InputChannel : private android::os::InputChannelCore {
275275
* Return DEAD_OBJECT if the channel's peer has been closed.
276276
* Other errors probably indicate that the channel is broken.
277277
*/
278-
status_t receiveMessage(InputMessage* msg);
278+
android::base::Result<InputMessage> receiveMessage();
279279

280280
/* Tells whether there is a message in the channel available to be received.
281281
*

libs/input/InputConsumer.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,28 +235,29 @@ status_t InputConsumer::consume(InputEventFactoryInterface* factory, bool consum
235235
mMsgDeferred = false;
236236
} else {
237237
// Receive a fresh message.
238-
status_t result = mChannel->receiveMessage(&mMsg);
239-
if (result == OK) {
238+
android::base::Result<InputMessage> result = mChannel->receiveMessage();
239+
if (result.ok()) {
240+
mMsg = std::move(result.value());
240241
const auto [_, inserted] =
241242
mConsumeTimes.emplace(mMsg.header.seq, systemTime(SYSTEM_TIME_MONOTONIC));
242243
LOG_ALWAYS_FATAL_IF(!inserted, "Already have a consume time for seq=%" PRIu32,
243244
mMsg.header.seq);
244245

245246
// Trace the event processing timeline - event was just read from the socket
246247
ATRACE_ASYNC_BEGIN(mProcessingTraceTag.c_str(), /*cookie=*/mMsg.header.seq);
247-
}
248-
if (result) {
248+
} else {
249249
// Consume the next batched event unless batches are being held for later.
250-
if (consumeBatches || result != WOULD_BLOCK) {
251-
result = consumeBatch(factory, frameTime, outSeq, outEvent);
250+
if (consumeBatches || result.error().code() != WOULD_BLOCK) {
251+
result = android::base::Error(
252+
consumeBatch(factory, frameTime, outSeq, outEvent));
252253
if (*outEvent) {
253254
ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
254255
"channel '%s' consumer ~ consumed batch event, seq=%u",
255256
mChannel->getName().c_str(), *outSeq);
256257
break;
257258
}
258259
}
259-
return result;
260+
return result.error().code();
260261
}
261262
}
262263

libs/input/InputConsumerNoResampling.cpp

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -362,36 +362,36 @@ void InputConsumerNoResampling::handleMessages(std::vector<InputMessage>&& messa
362362
std::vector<InputMessage> InputConsumerNoResampling::readAllMessages() {
363363
std::vector<InputMessage> messages;
364364
while (true) {
365-
InputMessage msg;
366-
status_t result = mChannel->receiveMessage(&msg);
367-
switch (result) {
368-
case OK: {
369-
const auto [_, inserted] =
370-
mConsumeTimes.emplace(msg.header.seq, systemTime(SYSTEM_TIME_MONOTONIC));
371-
LOG_ALWAYS_FATAL_IF(!inserted, "Already have a consume time for seq=%" PRIu32,
372-
msg.header.seq);
373-
374-
// Trace the event processing timeline - event was just read from the socket
375-
// TODO(b/329777420): distinguish between multiple instances of InputConsumer
376-
// in the same process.
377-
ATRACE_ASYNC_BEGIN("InputConsumer processing", /*cookie=*/msg.header.seq);
378-
messages.push_back(msg);
379-
break;
380-
}
381-
case WOULD_BLOCK: {
382-
return messages;
383-
}
384-
case DEAD_OBJECT: {
385-
LOG(FATAL) << "Got a dead object for " << mChannel->getName();
386-
break;
387-
}
388-
case BAD_VALUE: {
389-
LOG(FATAL) << "Got a bad value for " << mChannel->getName();
390-
break;
391-
}
392-
default: {
393-
LOG(FATAL) << "Unexpected error: " << result;
394-
break;
365+
android::base::Result<InputMessage> result = mChannel->receiveMessage();
366+
if (result.ok()) {
367+
const InputMessage& msg = *result;
368+
const auto [_, inserted] =
369+
mConsumeTimes.emplace(msg.header.seq, systemTime(SYSTEM_TIME_MONOTONIC));
370+
LOG_ALWAYS_FATAL_IF(!inserted, "Already have a consume time for seq=%" PRIu32,
371+
msg.header.seq);
372+
373+
// Trace the event processing timeline - event was just read from the socket
374+
// TODO(b/329777420): distinguish between multiple instances of InputConsumer
375+
// in the same process.
376+
ATRACE_ASYNC_BEGIN("InputConsumer processing", /*cookie=*/msg.header.seq);
377+
messages.push_back(msg);
378+
} else { // !result.ok()
379+
switch (result.error().code()) {
380+
case WOULD_BLOCK: {
381+
return messages;
382+
}
383+
case DEAD_OBJECT: {
384+
LOG(FATAL) << "Got a dead object for " << mChannel->getName();
385+
break;
386+
}
387+
case BAD_VALUE: {
388+
LOG(FATAL) << "Got a bad value for " << mChannel->getName();
389+
break;
390+
}
391+
default: {
392+
LOG(FATAL) << "Unexpected error: " << result.error().message();
393+
break;
394+
}
395395
}
396396
}
397397
}

libs/input/InputTransport.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -424,47 +424,48 @@ status_t InputChannel::sendMessage(const InputMessage* msg) {
424424
return OK;
425425
}
426426

427-
status_t InputChannel::receiveMessage(InputMessage* msg) {
427+
android::base::Result<InputMessage> InputChannel::receiveMessage() {
428428
ssize_t nRead;
429+
InputMessage msg;
429430
do {
430-
nRead = ::recv(getFd(), msg, sizeof(InputMessage), MSG_DONTWAIT);
431+
nRead = ::recv(getFd(), &msg, sizeof(InputMessage), MSG_DONTWAIT);
431432
} while (nRead == -1 && errno == EINTR);
432433

433434
if (nRead < 0) {
434435
int error = errno;
435436
ALOGD_IF(DEBUG_CHANNEL_MESSAGES, "channel '%s' ~ receive message failed, errno=%d",
436437
name.c_str(), errno);
437438
if (error == EAGAIN || error == EWOULDBLOCK) {
438-
return WOULD_BLOCK;
439+
return android::base::Error(WOULD_BLOCK);
439440
}
440441
if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED) {
441-
return DEAD_OBJECT;
442+
return android::base::Error(DEAD_OBJECT);
442443
}
443-
return -error;
444+
return android::base::Error(-error);
444445
}
445446

446447
if (nRead == 0) { // check for EOF
447448
ALOGD_IF(DEBUG_CHANNEL_MESSAGES,
448449
"channel '%s' ~ receive message failed because peer was closed", name.c_str());
449-
return DEAD_OBJECT;
450+
return android::base::Error(DEAD_OBJECT);
450451
}
451452

452-
if (!msg->isValid(nRead)) {
453+
if (!msg.isValid(nRead)) {
453454
ALOGE("channel '%s' ~ received invalid message of size %zd", name.c_str(), nRead);
454-
return BAD_VALUE;
455+
return android::base::Error(BAD_VALUE);
455456
}
456457

457458
ALOGD_IF(DEBUG_CHANNEL_MESSAGES, "channel '%s' ~ received message of type %s", name.c_str(),
458-
ftl::enum_string(msg->header.type).c_str());
459+
ftl::enum_string(msg.header.type).c_str());
459460
if (ATRACE_ENABLED()) {
460461
// Add an additional trace point to include data about the received message.
461462
std::string message =
462463
StringPrintf("receiveMessage(inputChannel=%s, seq=0x%" PRIx32 ", type=%s)",
463-
name.c_str(), msg->header.seq,
464-
ftl::enum_string(msg->header.type).c_str());
464+
name.c_str(), msg.header.seq,
465+
ftl::enum_string(msg.header.type).c_str());
465466
ATRACE_NAME(message.c_str());
466467
}
467-
return OK;
468+
return msg;
468469
}
469470

470471
bool InputChannel::probablyHasInput() const {
@@ -729,15 +730,16 @@ status_t InputPublisher::publishTouchModeEvent(uint32_t seq, int32_t eventId, bo
729730
}
730731

731732
android::base::Result<InputPublisher::ConsumerResponse> InputPublisher::receiveConsumerResponse() {
732-
InputMessage msg;
733-
status_t result = mChannel->receiveMessage(&msg);
734-
if (result) {
735-
if (debugTransportPublisher() && result != WOULD_BLOCK) {
733+
android::base::Result<InputMessage> result = mChannel->receiveMessage();
734+
if (!result.ok()) {
735+
if (debugTransportPublisher() && result.error().code() != WOULD_BLOCK) {
736736
LOG(INFO) << "channel '" << mChannel->getName() << "' publisher ~ " << __func__ << ": "
737-
<< strerror(result);
737+
<< result.error().message();
738738
}
739-
return android::base::Error(result);
739+
return result.error();
740740
}
741+
742+
const InputMessage& msg = *result;
741743
if (msg.header.type == InputMessage::Type::FINISHED) {
742744
ALOGD_IF(debugTransportPublisher(),
743745
"channel '%s' publisher ~ %s: finished: seq=%u, handled=%s",

libs/input/tests/InputChannel_test.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ TEST_F(InputChannelTest, OpenInputChannelPair_ReturnsAPairOfConnectedChannels) {
7878
EXPECT_EQ(OK, serverChannel->sendMessage(&serverMsg))
7979
<< "server channel should be able to send message to client channel";
8080

81-
InputMessage clientMsg;
82-
EXPECT_EQ(OK, clientChannel->receiveMessage(&clientMsg))
81+
android::base::Result<InputMessage> clientMsgResult = clientChannel->receiveMessage();
82+
ASSERT_TRUE(clientMsgResult.ok())
8383
<< "client channel should be able to receive message from server channel";
84+
const InputMessage& clientMsg = *clientMsgResult;
8485
EXPECT_EQ(serverMsg.header.type, clientMsg.header.type)
8586
<< "client channel should receive the correct message from server channel";
8687
EXPECT_EQ(serverMsg.body.key.action, clientMsg.body.key.action)
@@ -94,9 +95,10 @@ TEST_F(InputChannelTest, OpenInputChannelPair_ReturnsAPairOfConnectedChannels) {
9495
EXPECT_EQ(OK, clientChannel->sendMessage(&clientReply))
9596
<< "client channel should be able to send message to server channel";
9697

97-
InputMessage serverReply;
98-
EXPECT_EQ(OK, serverChannel->receiveMessage(&serverReply))
98+
android::base::Result<InputMessage> serverReplyResult = serverChannel->receiveMessage();
99+
ASSERT_TRUE(serverReplyResult.ok())
99100
<< "server channel should be able to receive message from client channel";
101+
const InputMessage& serverReply = *serverReplyResult;
100102
EXPECT_EQ(clientReply.header.type, serverReply.header.type)
101103
<< "server channel should receive the correct message from client channel";
102104
EXPECT_EQ(clientReply.header.seq, serverReply.header.seq)
@@ -134,9 +136,10 @@ TEST_F(InputChannelTest, ProbablyHasInput) {
134136
<< "client channel should observe that message is available before receiving it";
135137

136138
// Receive (consume) the message.
137-
InputMessage clientMsg;
138-
EXPECT_EQ(OK, receiverChannel->receiveMessage(&clientMsg))
139+
android::base::Result<InputMessage> clientMsgResult = receiverChannel->receiveMessage();
140+
ASSERT_TRUE(clientMsgResult.ok())
139141
<< "client channel should be able to receive message from server channel";
142+
const InputMessage& clientMsg = *clientMsgResult;
140143
EXPECT_EQ(serverMsg.header.type, clientMsg.header.type)
141144
<< "client channel should receive the correct message from server channel";
142145
EXPECT_EQ(serverMsg.body.key.action, clientMsg.body.key.action)
@@ -156,8 +159,8 @@ TEST_F(InputChannelTest, ReceiveSignal_WhenNoSignalPresent_ReturnsAnError) {
156159
ASSERT_EQ(OK, result)
157160
<< "should have successfully opened a channel pair";
158161

159-
InputMessage msg;
160-
EXPECT_EQ(WOULD_BLOCK, clientChannel->receiveMessage(&msg))
162+
android::base::Result<InputMessage> msgResult = clientChannel->receiveMessage();
163+
EXPECT_EQ(WOULD_BLOCK, msgResult.error().code())
161164
<< "receiveMessage should have returned WOULD_BLOCK";
162165
}
163166

@@ -172,8 +175,8 @@ TEST_F(InputChannelTest, ReceiveSignal_WhenPeerClosed_ReturnsAnError) {
172175

173176
serverChannel.reset(); // close server channel
174177

175-
InputMessage msg;
176-
EXPECT_EQ(DEAD_OBJECT, clientChannel->receiveMessage(&msg))
178+
android::base::Result<InputMessage> msgResult = clientChannel->receiveMessage();
179+
EXPECT_EQ(DEAD_OBJECT, msgResult.error().code())
177180
<< "receiveMessage should have returned DEAD_OBJECT";
178181
}
179182

@@ -207,7 +210,7 @@ TEST_F(InputChannelTest, SendAndReceive_MotionClassification) {
207210
MotionClassification::DEEP_PRESS,
208211
};
209212

210-
InputMessage serverMsg = {}, clientMsg;
213+
InputMessage serverMsg = {};
211214
serverMsg.header.type = InputMessage::Type::MOTION;
212215
serverMsg.header.seq = 1;
213216
serverMsg.body.motion.pointerCount = 1;
@@ -218,11 +221,13 @@ TEST_F(InputChannelTest, SendAndReceive_MotionClassification) {
218221
EXPECT_EQ(OK, serverChannel->sendMessage(&serverMsg))
219222
<< "server channel should be able to send message to client channel";
220223

221-
EXPECT_EQ(OK, clientChannel->receiveMessage(&clientMsg))
224+
android::base::Result<InputMessage> clientMsgResult = clientChannel->receiveMessage();
225+
ASSERT_TRUE(clientMsgResult.ok())
222226
<< "client channel should be able to receive message from server channel";
227+
const InputMessage& clientMsg = *clientMsgResult;
223228
EXPECT_EQ(serverMsg.header.type, clientMsg.header.type);
224-
EXPECT_EQ(classification, clientMsg.body.motion.classification) <<
225-
"Expected to receive " << motionClassificationToString(classification);
229+
EXPECT_EQ(classification, clientMsg.body.motion.classification)
230+
<< "Expected to receive " << motionClassificationToString(classification);
226231
}
227232
}
228233

0 commit comments

Comments
 (0)