blob: 8f2805fce44284662d4ec3496a6d6f98c0882c30 [file] [log] [blame]
Steven Moreland5553ac42020-11-11 02:14:45 +00001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#define LOG_TAG "RpcServer"
18
19#include <sys/socket.h>
20#include <sys/un.h>
21
Steven Morelandf137de92021-04-24 01:54:26 +000022#include <thread>
Steven Moreland5553ac42020-11-11 02:14:45 +000023#include <vector>
24
25#include <binder/Parcel.h>
26#include <binder/RpcServer.h>
27#include <log/log.h>
28#include "RpcState.h"
29
30#include "RpcWireFormat.h"
31
32namespace android {
33
Steven Moreland5553ac42020-11-11 02:14:45 +000034RpcServer::RpcServer() {}
35RpcServer::~RpcServer() {}
36
37sp<RpcServer> RpcServer::make() {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +000038 return sp<RpcServer>::make();
Steven Moreland5553ac42020-11-11 02:14:45 +000039}
40
41void RpcServer::iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction() {
42 mAgreedExperimental = true;
43}
44
Steven Morelandf137de92021-04-24 01:54:26 +000045void RpcServer::setMaxThreads(size_t threads) {
46 LOG_ALWAYS_FATAL_IF(threads <= 0, "RpcServer is useless without threads");
Steven Morelandebafe332021-04-24 00:24:35 +000047 {
Steven Morelandf137de92021-04-24 01:54:26 +000048 // this lock should only ever be needed in the error case
Steven Morelandebafe332021-04-24 00:24:35 +000049 std::lock_guard<std::mutex> _l(mLock);
Steven Morelandf137de92021-04-24 01:54:26 +000050 LOG_ALWAYS_FATAL_IF(mConnections.size() > 0,
51 "Must specify max threads before creating a connection");
Steven Morelandebafe332021-04-24 00:24:35 +000052 }
Steven Morelandf137de92021-04-24 01:54:26 +000053 mMaxThreads = threads;
54}
55
56size_t RpcServer::getMaxThreads() {
57 return mMaxThreads;
Steven Moreland5553ac42020-11-11 02:14:45 +000058}
59
60void RpcServer::setRootObject(const sp<IBinder>& binder) {
Steven Morelandebafe332021-04-24 00:24:35 +000061 std::lock_guard<std::mutex> _l(mLock);
Steven Moreland5553ac42020-11-11 02:14:45 +000062 mRootObject = binder;
63}
64
65sp<IBinder> RpcServer::getRootObject() {
Steven Morelandebafe332021-04-24 00:24:35 +000066 std::lock_guard<std::mutex> _l(mLock);
Steven Moreland5553ac42020-11-11 02:14:45 +000067 return mRootObject;
68}
69
Steven Morelandf137de92021-04-24 01:54:26 +000070sp<RpcConnection> RpcServer::addClientConnection() {
71 LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!");
72
73 auto connection = RpcConnection::make();
74 connection->setForServer(sp<RpcServer>::fromExisting(this));
75 {
76 std::lock_guard<std::mutex> _l(mLock);
77 LOG_ALWAYS_FATAL_IF(mStarted,
78 "currently only supports adding client connections at creation time");
79 mConnections.push_back(connection);
80 }
81 return connection;
82}
83
84void RpcServer::join() {
85 std::vector<std::thread> pool;
86 {
87 std::lock_guard<std::mutex> _l(mLock);
88 mStarted = true;
89 for (const sp<RpcConnection>& connection : mConnections) {
90 for (size_t i = 0; i < mMaxThreads; i++) {
91 pool.push_back(std::thread([=] { connection->join(); }));
92 }
93 }
94 }
95
96 // TODO(b/185167543): don't waste extra thread for join, and combine threads
97 // between clients
98 for (auto& t : pool) t.join();
99}
100
Steven Moreland5553ac42020-11-11 02:14:45 +0000101} // namespace android