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

Commit fba6f77a authored by Steven Moreland's avatar Steven Moreland
Browse files

libbinder: 'RpcSession::addIncomingConnection'

Factor out complex bit of logic to setup an incoming connection into a
new function, in preparation for being able to set up the connection in
a different way.

Bug: 193801719
Test: binderRpcTest
Change-Id: I09a4520a5f00bb658c864000f7861a687a47efb0
parent 798e0d1a
Loading
Loading
Loading
Loading
+30 −26
Original line number Diff line number Diff line
@@ -476,6 +476,17 @@ bool RpcSession::setupOneSocketConnection(const RpcSocketAddress& addr, const Rp
        LOG_RPC_DETAIL("Socket at %s client with fd %d", addr.toString().c_str(), serverFd.get());

        if (reverse) {
            return addIncomingConnection(std::move(serverFd));
        } else {
            return addOutgoingConnection(std::move(serverFd), true);
        }
    }

    ALOGE("Ran out of retries to connect to %s", addr.toString().c_str());
    return false;
}

bool RpcSession::addIncomingConnection(unique_fd fd) {
    std::mutex mutex;
    std::condition_variable joinCv;
    std::unique_lock<std::mutex> lock(mutex);
@@ -484,13 +495,13 @@ bool RpcSession::setupOneSocketConnection(const RpcSocketAddress& addr, const Rp
    bool ownershipTransferred = false;
    thread = std::thread([&]() {
        std::unique_lock<std::mutex> threadLock(mutex);
                unique_fd fd = std::move(serverFd);
        unique_fd movedFd = std::move(fd);
        // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
        sp<RpcSession> session = thiz;
        session->preJoinThreadOwnership(std::move(thread));

        // only continue once we have a response or the connection fails
                auto setupResult = session->preJoinSetup(std::move(fd));
        auto setupResult = session->preJoinSetup(std::move(movedFd));

        ownershipTransferred = true;
        threadLock.unlock();
@@ -502,13 +513,6 @@ bool RpcSession::setupOneSocketConnection(const RpcSocketAddress& addr, const Rp
    joinCv.wait(lock, [&] { return ownershipTransferred; });
    LOG_ALWAYS_FATAL_IF(!ownershipTransferred);
    return true;
        } else {
            return addOutgoingConnection(std::move(serverFd), true);
        }
    }

    ALOGE("Ran out of retries to connect to %s", addr.toString().c_str());
    return false;
}

bool RpcSession::addOutgoingConnection(unique_fd fd, bool init) {
+1 −0
Original line number Diff line number Diff line
@@ -226,6 +226,7 @@ private:
    [[nodiscard]] bool setupSocketClient(const RpcSocketAddress& address);
    [[nodiscard]] bool setupOneSocketConnection(const RpcSocketAddress& address,
                                                const RpcAddress& sessionId, bool server);
    [[nodiscard]] bool addIncomingConnection(base::unique_fd fd);
    [[nodiscard]] bool addOutgoingConnection(base::unique_fd fd, bool init);
    [[nodiscard]] bool setForServer(const wp<RpcServer>& server,
                                    const wp<RpcSession::EventListener>& eventListener,