Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b63454ad authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Return message wrapped in Result from receiveMessage" into main

parents a2227341 10bae11c
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -275,7 +275,7 @@ public:
     * Return DEAD_OBJECT if the channel's peer has been closed.
     * Return DEAD_OBJECT if the channel's peer has been closed.
     * Other errors probably indicate that the channel is broken.
     * Other errors probably indicate that the channel is broken.
     */
     */
    status_t receiveMessage(InputMessage* msg);
    android::base::Result<InputMessage> receiveMessage();


    /* Tells whether there is a message in the channel available to be received.
    /* Tells whether there is a message in the channel available to be received.
     *
     *
+8 −7
Original line number Original line Diff line number Diff line
@@ -235,8 +235,9 @@ status_t InputConsumer::consume(InputEventFactoryInterface* factory, bool consum
            mMsgDeferred = false;
            mMsgDeferred = false;
        } else {
        } else {
            // Receive a fresh message.
            // Receive a fresh message.
            status_t result = mChannel->receiveMessage(&mMsg);
            android::base::Result<InputMessage> result = mChannel->receiveMessage();
            if (result == OK) {
            if (result.ok()) {
                mMsg = std::move(result.value());
                const auto [_, inserted] =
                const auto [_, inserted] =
                        mConsumeTimes.emplace(mMsg.header.seq, systemTime(SYSTEM_TIME_MONOTONIC));
                        mConsumeTimes.emplace(mMsg.header.seq, systemTime(SYSTEM_TIME_MONOTONIC));
                LOG_ALWAYS_FATAL_IF(!inserted, "Already have a consume time for seq=%" PRIu32,
                LOG_ALWAYS_FATAL_IF(!inserted, "Already have a consume time for seq=%" PRIu32,
@@ -244,11 +245,11 @@ status_t InputConsumer::consume(InputEventFactoryInterface* factory, bool consum


                // Trace the event processing timeline - event was just read from the socket
                // Trace the event processing timeline - event was just read from the socket
                ATRACE_ASYNC_BEGIN(mProcessingTraceTag.c_str(), /*cookie=*/mMsg.header.seq);
                ATRACE_ASYNC_BEGIN(mProcessingTraceTag.c_str(), /*cookie=*/mMsg.header.seq);
            }
            } else {
            if (result) {
                // Consume the next batched event unless batches are being held for later.
                // Consume the next batched event unless batches are being held for later.
                if (consumeBatches || result != WOULD_BLOCK) {
                if (consumeBatches || result.error().code() != WOULD_BLOCK) {
                    result = consumeBatch(factory, frameTime, outSeq, outEvent);
                    result = android::base::Error(
                            consumeBatch(factory, frameTime, outSeq, outEvent));
                    if (*outEvent) {
                    if (*outEvent) {
                        ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
                        ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
                                 "channel '%s' consumer ~ consumed batch event, seq=%u",
                                 "channel '%s' consumer ~ consumed batch event, seq=%u",
@@ -256,7 +257,7 @@ status_t InputConsumer::consume(InputEventFactoryInterface* factory, bool consum
                        break;
                        break;
                    }
                    }
                }
                }
                return result;
                return result.error().code();
            }
            }
        }
        }


+30 −30
Original line number Original line Diff line number Diff line
@@ -362,10 +362,9 @@ void InputConsumerNoResampling::handleMessages(std::vector<InputMessage>&& messa
std::vector<InputMessage> InputConsumerNoResampling::readAllMessages() {
std::vector<InputMessage> InputConsumerNoResampling::readAllMessages() {
    std::vector<InputMessage> messages;
    std::vector<InputMessage> messages;
    while (true) {
    while (true) {
        InputMessage msg;
        android::base::Result<InputMessage> result = mChannel->receiveMessage();
        status_t result = mChannel->receiveMessage(&msg);
        if (result.ok()) {
        switch (result) {
            const InputMessage& msg = *result;
            case OK: {
            const auto [_, inserted] =
            const auto [_, inserted] =
                    mConsumeTimes.emplace(msg.header.seq, systemTime(SYSTEM_TIME_MONOTONIC));
                    mConsumeTimes.emplace(msg.header.seq, systemTime(SYSTEM_TIME_MONOTONIC));
            LOG_ALWAYS_FATAL_IF(!inserted, "Already have a consume time for seq=%" PRIu32,
            LOG_ALWAYS_FATAL_IF(!inserted, "Already have a consume time for seq=%" PRIu32,
@@ -376,8 +375,8 @@ std::vector<InputMessage> InputConsumerNoResampling::readAllMessages() {
            // in the same process.
            // in the same process.
            ATRACE_ASYNC_BEGIN("InputConsumer processing", /*cookie=*/msg.header.seq);
            ATRACE_ASYNC_BEGIN("InputConsumer processing", /*cookie=*/msg.header.seq);
            messages.push_back(msg);
            messages.push_back(msg);
                break;
        } else { // !result.ok()
            }
            switch (result.error().code()) {
                case WOULD_BLOCK: {
                case WOULD_BLOCK: {
                    return messages;
                    return messages;
                }
                }
@@ -390,12 +389,13 @@ std::vector<InputMessage> InputConsumerNoResampling::readAllMessages() {
                    break;
                    break;
                }
                }
                default: {
                default: {
                LOG(FATAL) << "Unexpected error: " << result;
                    LOG(FATAL) << "Unexpected error: " << result.error().message();
                    break;
                    break;
                }
                }
            }
            }
        }
        }
    }
    }
}


void InputConsumerNoResampling::handleMessage(const InputMessage& msg) const {
void InputConsumerNoResampling::handleMessage(const InputMessage& msg) const {
    switch (msg.header.type) {
    switch (msg.header.type) {
+20 −18
Original line number Original line Diff line number Diff line
@@ -424,10 +424,11 @@ status_t InputChannel::sendMessage(const InputMessage* msg) {
    return OK;
    return OK;
}
}


status_t InputChannel::receiveMessage(InputMessage* msg) {
android::base::Result<InputMessage> InputChannel::receiveMessage() {
    ssize_t nRead;
    ssize_t nRead;
    InputMessage msg;
    do {
    do {
        nRead = ::recv(getFd(), msg, sizeof(InputMessage), MSG_DONTWAIT);
        nRead = ::recv(getFd(), &msg, sizeof(InputMessage), MSG_DONTWAIT);
    } while (nRead == -1 && errno == EINTR);
    } while (nRead == -1 && errno == EINTR);


    if (nRead < 0) {
    if (nRead < 0) {
@@ -435,36 +436,36 @@ status_t InputChannel::receiveMessage(InputMessage* msg) {
        ALOGD_IF(DEBUG_CHANNEL_MESSAGES, "channel '%s' ~ receive message failed, errno=%d",
        ALOGD_IF(DEBUG_CHANNEL_MESSAGES, "channel '%s' ~ receive message failed, errno=%d",
                 name.c_str(), errno);
                 name.c_str(), errno);
        if (error == EAGAIN || error == EWOULDBLOCK) {
        if (error == EAGAIN || error == EWOULDBLOCK) {
            return WOULD_BLOCK;
            return android::base::Error(WOULD_BLOCK);
        }
        }
        if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED) {
        if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED) {
            return DEAD_OBJECT;
            return android::base::Error(DEAD_OBJECT);
        }
        }
        return -error;
        return android::base::Error(-error);
    }
    }


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


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


    ALOGD_IF(DEBUG_CHANNEL_MESSAGES, "channel '%s' ~ received message of type %s", name.c_str(),
    ALOGD_IF(DEBUG_CHANNEL_MESSAGES, "channel '%s' ~ received message of type %s", name.c_str(),
             ftl::enum_string(msg->header.type).c_str());
             ftl::enum_string(msg.header.type).c_str());
    if (ATRACE_ENABLED()) {
    if (ATRACE_ENABLED()) {
        // Add an additional trace point to include data about the received message.
        // Add an additional trace point to include data about the received message.
        std::string message =
        std::string message =
                StringPrintf("receiveMessage(inputChannel=%s, seq=0x%" PRIx32 ", type=%s)",
                StringPrintf("receiveMessage(inputChannel=%s, seq=0x%" PRIx32 ", type=%s)",
                             name.c_str(), msg->header.seq,
                             name.c_str(), msg.header.seq,
                             ftl::enum_string(msg->header.type).c_str());
                             ftl::enum_string(msg.header.type).c_str());
        ATRACE_NAME(message.c_str());
        ATRACE_NAME(message.c_str());
    }
    }
    return OK;
    return msg;
}
}


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


android::base::Result<InputPublisher::ConsumerResponse> InputPublisher::receiveConsumerResponse() {
android::base::Result<InputPublisher::ConsumerResponse> InputPublisher::receiveConsumerResponse() {
    InputMessage msg;
    android::base::Result<InputMessage> result = mChannel->receiveMessage();
    status_t result = mChannel->receiveMessage(&msg);
    if (!result.ok()) {
    if (result) {
        if (debugTransportPublisher() && result.error().code() != WOULD_BLOCK) {
        if (debugTransportPublisher() && result != WOULD_BLOCK) {
            LOG(INFO) << "channel '" << mChannel->getName() << "' publisher ~ " << __func__ << ": "
            LOG(INFO) << "channel '" << mChannel->getName() << "' publisher ~ " << __func__ << ": "
                      << strerror(result);
                      << result.error().message();
        }
        }
        return android::base::Error(result);
        return result.error();
    }
    }

    const InputMessage& msg = *result;
    if (msg.header.type == InputMessage::Type::FINISHED) {
    if (msg.header.type == InputMessage::Type::FINISHED) {
        ALOGD_IF(debugTransportPublisher(),
        ALOGD_IF(debugTransportPublisher(),
                 "channel '%s' publisher ~ %s: finished: seq=%u, handled=%s",
                 "channel '%s' publisher ~ %s: finished: seq=%u, handled=%s",
+19 −14
Original line number Original line Diff line number Diff line
@@ -78,9 +78,10 @@ TEST_F(InputChannelTest, OpenInputChannelPair_ReturnsAPairOfConnectedChannels) {
    EXPECT_EQ(OK, serverChannel->sendMessage(&serverMsg))
    EXPECT_EQ(OK, serverChannel->sendMessage(&serverMsg))
            << "server channel should be able to send message to client channel";
            << "server channel should be able to send message to client channel";


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


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


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


    InputMessage msg;
    android::base::Result<InputMessage> msgResult = clientChannel->receiveMessage();
    EXPECT_EQ(WOULD_BLOCK, clientChannel->receiveMessage(&msg))
    EXPECT_EQ(WOULD_BLOCK, msgResult.error().code())
            << "receiveMessage should have returned WOULD_BLOCK";
            << "receiveMessage should have returned WOULD_BLOCK";
}
}


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


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


    InputMessage msg;
    android::base::Result<InputMessage> msgResult = clientChannel->receiveMessage();
    EXPECT_EQ(DEAD_OBJECT, clientChannel->receiveMessage(&msg))
    EXPECT_EQ(DEAD_OBJECT, msgResult.error().code())
            << "receiveMessage should have returned DEAD_OBJECT";
            << "receiveMessage should have returned DEAD_OBJECT";
}
}


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


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


        EXPECT_EQ(OK, clientChannel->receiveMessage(&clientMsg))
        android::base::Result<InputMessage> clientMsgResult = clientChannel->receiveMessage();
        ASSERT_TRUE(clientMsgResult.ok())
                << "client channel should be able to receive message from server channel";
                << "client channel should be able to receive message from server channel";
        const InputMessage& clientMsg = *clientMsgResult;
        EXPECT_EQ(serverMsg.header.type, clientMsg.header.type);
        EXPECT_EQ(serverMsg.header.type, clientMsg.header.type);
        EXPECT_EQ(classification, clientMsg.body.motion.classification) <<
        EXPECT_EQ(classification, clientMsg.body.motion.classification)
                "Expected to receive " << motionClassificationToString(classification);
                << "Expected to receive " << motionClassificationToString(classification);
    }
    }
}
}